2015-12-04 21:53:31 +00:00
|
|
|
// Package point is a shell of V2Ray to run on various of systems.
|
|
|
|
// Point server is a full functionality proxying system. It consists of an inbound and an outbound
|
|
|
|
// connection, as well as any number of inbound and outbound detours. It provides a way internally
|
|
|
|
// to route network packets.
|
2015-10-14 12:51:19 +00:00
|
|
|
package point
|
2015-09-05 15:48:38 +00:00
|
|
|
|
|
|
|
import (
|
2015-12-05 21:55:45 +00:00
|
|
|
"github.com/v2ray/v2ray-core/app"
|
2015-12-11 11:01:20 +00:00
|
|
|
"github.com/v2ray/v2ray-core/app/controller"
|
2015-11-22 16:41:52 +00:00
|
|
|
"github.com/v2ray/v2ray-core/app/router"
|
2015-09-19 22:50:21 +00:00
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
2015-09-19 21:54:36 +00:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2015-10-13 10:27:50 +00:00
|
|
|
"github.com/v2ray/v2ray-core/common/retry"
|
2015-10-29 23:11:29 +00:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/common/connhandler"
|
2015-10-14 12:51:19 +00:00
|
|
|
"github.com/v2ray/v2ray-core/transport/ray"
|
2015-09-05 15:48:38 +00:00
|
|
|
)
|
|
|
|
|
2015-12-04 21:53:31 +00:00
|
|
|
// Point shell of V2Ray.
|
2015-09-12 20:11:54 +00:00
|
|
|
type Point struct {
|
2015-12-02 20:44:01 +00:00
|
|
|
port v2net.Port
|
2015-11-22 16:41:52 +00:00
|
|
|
ich connhandler.InboundConnectionHandler
|
|
|
|
och connhandler.OutboundConnectionHandler
|
|
|
|
idh []*InboundDetourHandler
|
2015-11-30 15:14:38 +00:00
|
|
|
odh map[string]connhandler.OutboundConnectionHandler
|
2015-11-22 16:41:52 +00:00
|
|
|
router router.Router
|
2015-12-11 11:01:20 +00:00
|
|
|
space *controller.SpaceController
|
2015-09-05 15:48:38 +00:00
|
|
|
}
|
|
|
|
|
2015-09-12 20:11:54 +00:00
|
|
|
// NewPoint returns a new Point server based on given configuration.
|
2015-09-07 10:00:46 +00:00
|
|
|
// The server is not started at this point.
|
2015-12-06 15:41:41 +00:00
|
|
|
func NewPoint(pConfig PointConfig) (*Point, error) {
|
2015-09-12 20:11:54 +00:00
|
|
|
var vpoint = new(Point)
|
2015-10-06 21:11:08 +00:00
|
|
|
vpoint.port = pConfig.Port()
|
2015-09-12 09:51:42 +00:00
|
|
|
|
2015-12-05 20:10:14 +00:00
|
|
|
if pConfig.LogConfig() != nil {
|
|
|
|
logConfig := pConfig.LogConfig()
|
|
|
|
if len(logConfig.AccessLog()) > 0 {
|
|
|
|
err := log.InitAccessLogger(logConfig.AccessLog())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(logConfig.ErrorLog()) > 0 {
|
|
|
|
err := log.InitErrorLogger(logConfig.ErrorLog())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.SetLogLevel(logConfig.LogLevel())
|
|
|
|
}
|
|
|
|
|
2015-12-11 11:01:20 +00:00
|
|
|
vpoint.space = controller.New()
|
2015-12-05 21:55:45 +00:00
|
|
|
vpoint.space.Bind(vpoint)
|
|
|
|
|
2015-10-29 23:11:29 +00:00
|
|
|
ichFactory := connhandler.GetInboundConnectionHandlerFactory(pConfig.InboundConfig().Protocol())
|
2015-10-14 12:51:19 +00:00
|
|
|
if ichFactory == nil {
|
2015-10-07 08:05:11 +00:00
|
|
|
log.Error("Unknown inbound connection handler factory %s", pConfig.InboundConfig().Protocol())
|
2015-12-06 15:41:41 +00:00
|
|
|
return nil, BadConfiguration
|
2015-09-12 09:51:42 +00:00
|
|
|
}
|
2015-10-29 22:35:54 +00:00
|
|
|
ichConfig := pConfig.InboundConfig().Settings()
|
2015-12-10 22:55:39 +00:00
|
|
|
ich, err := ichFactory.Create(vpoint.space.ForContext("vpoint-default-inbound"), ichConfig)
|
2015-10-06 22:30:44 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to create inbound connection handler: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
vpoint.ich = ich
|
2015-09-12 09:51:42 +00:00
|
|
|
|
2015-10-29 23:11:29 +00:00
|
|
|
ochFactory := connhandler.GetOutboundConnectionHandlerFactory(pConfig.OutboundConfig().Protocol())
|
2015-10-14 12:51:19 +00:00
|
|
|
if ochFactory == nil {
|
2015-10-07 08:05:11 +00:00
|
|
|
log.Error("Unknown outbound connection handler factory %s", pConfig.OutboundConfig().Protocol())
|
2015-12-06 15:41:41 +00:00
|
|
|
return nil, BadConfiguration
|
2015-09-12 09:51:42 +00:00
|
|
|
}
|
2015-10-29 22:35:54 +00:00
|
|
|
ochConfig := pConfig.OutboundConfig().Settings()
|
2015-12-10 22:55:39 +00:00
|
|
|
och, err := ochFactory.Create(vpoint.space.ForContext("vpoint-default-outbound"), ochConfig)
|
2015-10-06 22:30:44 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to create outbound connection handler: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
vpoint.och = och
|
2015-09-10 22:24:18 +00:00
|
|
|
|
2015-10-31 23:11:41 +00:00
|
|
|
detours := pConfig.InboundDetours()
|
|
|
|
if len(detours) > 0 {
|
|
|
|
vpoint.idh = make([]*InboundDetourHandler, len(detours))
|
|
|
|
for idx, detourConfig := range detours {
|
|
|
|
detourHandler := &InboundDetourHandler{
|
2015-12-10 22:55:39 +00:00
|
|
|
space: vpoint.space.ForContext(detourConfig.Tag()),
|
2015-10-31 23:11:41 +00:00
|
|
|
config: detourConfig,
|
|
|
|
}
|
|
|
|
err := detourHandler.Initialize()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
vpoint.idh[idx] = detourHandler
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-22 16:41:52 +00:00
|
|
|
outboundDetours := pConfig.OutboundDetours()
|
|
|
|
if len(outboundDetours) > 0 {
|
2015-11-30 15:14:38 +00:00
|
|
|
vpoint.odh = make(map[string]connhandler.OutboundConnectionHandler)
|
2015-11-22 16:41:52 +00:00
|
|
|
for _, detourConfig := range outboundDetours {
|
|
|
|
detourFactory := connhandler.GetOutboundConnectionHandlerFactory(detourConfig.Protocol())
|
|
|
|
if detourFactory == nil {
|
|
|
|
log.Error("Unknown detour outbound connection handler factory %s", detourConfig.Protocol())
|
2015-12-06 15:41:41 +00:00
|
|
|
return nil, BadConfiguration
|
2015-11-22 16:41:52 +00:00
|
|
|
}
|
2015-12-10 22:55:39 +00:00
|
|
|
detourHandler, err := detourFactory.Create(vpoint.space.ForContext(detourConfig.Tag()), detourConfig.Settings())
|
2015-11-22 16:41:52 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to create detour outbound connection handler: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
vpoint.odh[detourConfig.Tag()] = detourHandler
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
routerConfig := pConfig.RouterConfig()
|
|
|
|
if routerConfig != nil {
|
|
|
|
r, err := router.CreateRouter(routerConfig.Strategy(), routerConfig.Settings())
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to create router: %v", err)
|
2015-12-06 15:41:41 +00:00
|
|
|
return nil, BadConfiguration
|
2015-11-22 16:41:52 +00:00
|
|
|
}
|
|
|
|
vpoint.router = r
|
|
|
|
}
|
|
|
|
|
2015-09-06 20:10:42 +00:00
|
|
|
return vpoint, nil
|
2015-09-05 15:48:38 +00:00
|
|
|
}
|
|
|
|
|
2015-09-12 20:11:54 +00:00
|
|
|
// Start starts the Point server, and return any error during the process.
|
2015-09-07 10:00:46 +00:00
|
|
|
// In the case of any errors, the state of the server is unpredicatable.
|
2015-11-27 11:29:20 +00:00
|
|
|
func (this *Point) Start() error {
|
|
|
|
if this.port <= 0 {
|
|
|
|
log.Error("Invalid port %d", this.port)
|
2015-12-06 15:41:41 +00:00
|
|
|
return BadConfiguration
|
2015-09-06 20:10:42 +00:00
|
|
|
}
|
2015-09-22 16:11:55 +00:00
|
|
|
|
2015-10-31 23:11:41 +00:00
|
|
|
err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
|
2015-11-27 11:29:20 +00:00
|
|
|
err := this.ich.Listen(this.port)
|
2015-10-31 23:11:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-10-13 23:15:29 +00:00
|
|
|
}
|
2015-11-27 11:29:20 +00:00
|
|
|
log.Warning("Point server started on port %d", this.port)
|
2015-10-31 23:11:41 +00:00
|
|
|
return nil
|
2015-10-13 10:27:50 +00:00
|
|
|
})
|
2015-10-31 23:11:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-27 11:29:20 +00:00
|
|
|
for _, detourHandler := range this.idh {
|
2015-10-31 23:11:41 +00:00
|
|
|
err := detourHandler.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-09-06 20:10:42 +00:00
|
|
|
}
|
2015-09-10 22:24:18 +00:00
|
|
|
|
2015-12-03 14:12:47 +00:00
|
|
|
// Dispatches a Packet to an OutboundConnection.
|
|
|
|
// The packet will be passed through the router (if configured), and then sent to an outbound
|
|
|
|
// connection with matching tag.
|
2015-12-10 22:55:39 +00:00
|
|
|
func (this *Point) DispatchToOutbound(context app.Context, packet v2net.Packet) ray.InboundRay {
|
2015-10-14 12:51:19 +00:00
|
|
|
direct := ray.NewRay()
|
2015-11-22 16:41:52 +00:00
|
|
|
dest := packet.Destination()
|
|
|
|
|
2015-11-27 11:29:20 +00:00
|
|
|
if this.router != nil {
|
|
|
|
tag, err := this.router.TakeDetour(dest)
|
2015-11-22 16:41:52 +00:00
|
|
|
if err == nil {
|
2015-11-27 11:29:20 +00:00
|
|
|
handler, found := this.odh[tag]
|
2015-11-22 16:41:52 +00:00
|
|
|
if found {
|
|
|
|
go handler.Dispatch(packet, direct)
|
|
|
|
return direct
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-27 11:29:20 +00:00
|
|
|
go this.och.Dispatch(packet, direct)
|
2015-10-14 12:51:19 +00:00
|
|
|
return direct
|
2015-09-10 22:24:18 +00:00
|
|
|
}
|