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 (
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/app"
|
|
|
|
"v2ray.com/core/app/dispatcher"
|
|
|
|
dispatchers "v2ray.com/core/app/dispatcher/impl"
|
|
|
|
"v2ray.com/core/app/dns"
|
|
|
|
"v2ray.com/core/app/proxyman"
|
|
|
|
"v2ray.com/core/app/router"
|
|
|
|
"v2ray.com/core/common"
|
|
|
|
"v2ray.com/core/common/log"
|
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/common/retry"
|
|
|
|
"v2ray.com/core/proxy"
|
|
|
|
proxyregistry "v2ray.com/core/proxy/registry"
|
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 {
|
2016-01-21 00:40:52 +00:00
|
|
|
port v2net.Port
|
2016-05-29 14:37:52 +00:00
|
|
|
listen v2net.Address
|
2016-01-25 16:18:24 +00:00
|
|
|
ich proxy.InboundHandler
|
|
|
|
och proxy.OutboundHandler
|
2016-01-21 00:40:52 +00:00
|
|
|
idh []InboundDetourHandler
|
|
|
|
taggedIdh map[string]InboundDetourHandler
|
2016-01-25 16:18:24 +00:00
|
|
|
odh map[string]proxy.OutboundHandler
|
2016-01-21 00:40:52 +00:00
|
|
|
router router.Router
|
2016-05-18 06:05:52 +00:00
|
|
|
space app.Space
|
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.
|
2016-01-17 20:43:10 +00:00
|
|
|
func NewPoint(pConfig *Config) (*Point, error) {
|
2015-09-12 20:11:54 +00:00
|
|
|
var vpoint = new(Point)
|
2016-06-03 22:38:22 +00:00
|
|
|
vpoint.port = pConfig.InboundConfig.Port
|
|
|
|
if vpoint.port == 0 {
|
|
|
|
vpoint.port = pConfig.Port // Backward compatibility
|
|
|
|
}
|
|
|
|
|
|
|
|
vpoint.listen = pConfig.InboundConfig.ListenOn
|
2015-09-12 09:51:42 +00:00
|
|
|
|
2016-06-01 23:49:25 +00:00
|
|
|
if pConfig.TransportConfig != nil {
|
2016-06-02 18:52:52 +00:00
|
|
|
pConfig.TransportConfig.Apply()
|
2016-06-01 23:49:25 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 20:43:10 +00:00
|
|
|
if pConfig.LogConfig != nil {
|
|
|
|
logConfig := pConfig.LogConfig
|
|
|
|
if len(logConfig.AccessLog) > 0 {
|
|
|
|
err := log.InitAccessLogger(logConfig.AccessLog)
|
2015-12-05 20:10:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 20:43:10 +00:00
|
|
|
if len(logConfig.ErrorLog) > 0 {
|
|
|
|
err := log.InitErrorLogger(logConfig.ErrorLog)
|
2015-12-05 20:10:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 20:43:10 +00:00
|
|
|
log.SetLogLevel(logConfig.LogLevel)
|
2015-12-05 20:10:14 +00:00
|
|
|
}
|
|
|
|
|
2016-05-18 06:05:52 +00:00
|
|
|
vpoint.space = app.NewSpace()
|
|
|
|
vpoint.space.BindApp(proxyman.APP_ID_INBOUND_MANAGER, vpoint)
|
|
|
|
|
2016-05-18 15:12:04 +00:00
|
|
|
outboundHandlerManager := proxyman.NewDefaultOutboundHandlerManager()
|
2016-05-18 06:05:52 +00:00
|
|
|
vpoint.space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outboundHandlerManager)
|
|
|
|
|
|
|
|
dnsConfig := pConfig.DNSConfig
|
|
|
|
if dnsConfig != nil {
|
|
|
|
dnsServer := dns.NewCacheServer(vpoint.space, dnsConfig)
|
|
|
|
vpoint.space.BindApp(dns.APP_ID, dnsServer)
|
|
|
|
}
|
|
|
|
|
|
|
|
routerConfig := pConfig.RouterConfig
|
|
|
|
if routerConfig != nil {
|
|
|
|
r, err := router.CreateRouter(routerConfig.Strategy, routerConfig.Settings, vpoint.space)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to create router: ", err)
|
2016-08-18 06:21:20 +00:00
|
|
|
return nil, common.ErrBadConfiguration
|
2016-05-18 06:05:52 +00:00
|
|
|
}
|
|
|
|
vpoint.space.BindApp(router.APP_ID, r)
|
|
|
|
vpoint.router = r
|
|
|
|
}
|
|
|
|
|
|
|
|
vpoint.space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(vpoint.space))
|
2015-12-05 21:55:45 +00:00
|
|
|
|
2016-01-17 20:43:10 +00:00
|
|
|
ichConfig := pConfig.InboundConfig.Settings
|
2016-08-17 21:30:15 +00:00
|
|
|
ich, err := proxyregistry.CreateInboundHandler(
|
2016-06-04 12:25:13 +00:00
|
|
|
pConfig.InboundConfig.Protocol, vpoint.space, ichConfig, &proxy.InboundHandlerMeta{
|
2016-08-12 21:37:21 +00:00
|
|
|
Tag: "system.inbound",
|
|
|
|
Address: pConfig.InboundConfig.ListenOn,
|
|
|
|
Port: vpoint.port,
|
|
|
|
StreamSettings: pConfig.InboundConfig.StreamSettings,
|
|
|
|
AllowPassiveConnection: pConfig.InboundConfig.AllowPassiveConnection,
|
|
|
|
})
|
2015-10-06 22:30:44 +00:00
|
|
|
if err != nil {
|
2016-01-18 11:24:33 +00:00
|
|
|
log.Error("Failed to create inbound connection handler: ", err)
|
2015-10-06 22:30:44 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
vpoint.ich = ich
|
2015-09-12 09:51:42 +00:00
|
|
|
|
2016-01-17 20:43:10 +00:00
|
|
|
ochConfig := pConfig.OutboundConfig.Settings
|
2016-08-17 21:30:15 +00:00
|
|
|
och, err := proxyregistry.CreateOutboundHandler(
|
2016-06-04 12:25:13 +00:00
|
|
|
pConfig.OutboundConfig.Protocol, vpoint.space, ochConfig, &proxy.OutboundHandlerMeta{
|
2016-06-14 20:54:08 +00:00
|
|
|
Tag: "system.outbound",
|
|
|
|
Address: pConfig.OutboundConfig.SendThrough,
|
|
|
|
StreamSettings: pConfig.OutboundConfig.StreamSettings,
|
|
|
|
})
|
2015-10-06 22:30:44 +00:00
|
|
|
if err != nil {
|
2016-01-18 11:24:33 +00:00
|
|
|
log.Error("Failed to create outbound connection handler: ", err)
|
2015-10-06 22:30:44 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
vpoint.och = och
|
2016-05-18 06:05:52 +00:00
|
|
|
outboundHandlerManager.SetDefaultHandler(och)
|
2015-09-10 22:24:18 +00:00
|
|
|
|
2016-01-21 00:40:52 +00:00
|
|
|
vpoint.taggedIdh = make(map[string]InboundDetourHandler)
|
2016-01-17 20:43:10 +00:00
|
|
|
detours := pConfig.InboundDetours
|
2015-10-31 23:11:41 +00:00
|
|
|
if len(detours) > 0 {
|
2016-01-21 00:40:52 +00:00
|
|
|
vpoint.idh = make([]InboundDetourHandler, len(detours))
|
2015-10-31 23:11:41 +00:00
|
|
|
for idx, detourConfig := range detours {
|
2016-01-21 00:40:52 +00:00
|
|
|
allocConfig := detourConfig.Allocation
|
|
|
|
var detourHandler InboundDetourHandler
|
|
|
|
switch allocConfig.Strategy {
|
|
|
|
case AllocationStrategyAlways:
|
2016-05-18 06:05:52 +00:00
|
|
|
dh, err := NewInboundDetourHandlerAlways(vpoint.space, detourConfig)
|
2016-01-21 00:40:52 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Point: Failed to create detour handler: ", err)
|
2016-08-18 06:21:20 +00:00
|
|
|
return nil, common.ErrBadConfiguration
|
2016-01-21 00:40:52 +00:00
|
|
|
}
|
|
|
|
detourHandler = dh
|
2016-01-22 15:25:01 +00:00
|
|
|
case AllocationStrategyRandom:
|
2016-05-18 06:05:52 +00:00
|
|
|
dh, err := NewInboundDetourHandlerDynamic(vpoint.space, detourConfig)
|
2016-01-22 15:25:01 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Point: Failed to create detour handler: ", err)
|
2016-08-18 06:21:20 +00:00
|
|
|
return nil, common.ErrBadConfiguration
|
2016-01-22 15:25:01 +00:00
|
|
|
}
|
|
|
|
detourHandler = dh
|
2016-01-21 00:40:52 +00:00
|
|
|
default:
|
|
|
|
log.Error("Point: Unknown allocation strategy: ", allocConfig.Strategy)
|
2016-08-18 06:21:20 +00:00
|
|
|
return nil, common.ErrBadConfiguration
|
2015-10-31 23:11:41 +00:00
|
|
|
}
|
|
|
|
vpoint.idh[idx] = detourHandler
|
2016-01-21 00:40:52 +00:00
|
|
|
if len(detourConfig.Tag) > 0 {
|
|
|
|
vpoint.taggedIdh[detourConfig.Tag] = detourHandler
|
|
|
|
}
|
2015-10-31 23:11:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 20:43:10 +00:00
|
|
|
outboundDetours := pConfig.OutboundDetours
|
2015-11-22 16:41:52 +00:00
|
|
|
if len(outboundDetours) > 0 {
|
2016-01-25 16:18:24 +00:00
|
|
|
vpoint.odh = make(map[string]proxy.OutboundHandler)
|
2015-11-22 16:41:52 +00:00
|
|
|
for _, detourConfig := range outboundDetours {
|
2016-08-17 21:30:15 +00:00
|
|
|
detourHandler, err := proxyregistry.CreateOutboundHandler(
|
2016-06-04 12:25:13 +00:00
|
|
|
detourConfig.Protocol, vpoint.space, detourConfig.Settings, &proxy.OutboundHandlerMeta{
|
2016-06-14 20:54:08 +00:00
|
|
|
Tag: detourConfig.Tag,
|
|
|
|
Address: detourConfig.SendThrough,
|
|
|
|
StreamSettings: detourConfig.StreamSettings,
|
|
|
|
})
|
2015-11-22 16:41:52 +00:00
|
|
|
if err != nil {
|
2016-06-01 20:45:12 +00:00
|
|
|
log.Error("Point: Failed to create detour outbound connection handler: ", err)
|
2015-11-22 16:41:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-17 20:43:10 +00:00
|
|
|
vpoint.odh[detourConfig.Tag] = detourHandler
|
2016-05-18 06:05:52 +00:00
|
|
|
outboundHandlerManager.SetHandler(detourConfig.Tag, detourHandler)
|
2015-11-22 16:41:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-18 15:12:04 +00:00
|
|
|
if err := vpoint.space.Initialize(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-09-06 20:10:42 +00:00
|
|
|
return vpoint, nil
|
2015-09-05 15:48:38 +00:00
|
|
|
}
|
|
|
|
|
2016-01-03 23:33:25 +00:00
|
|
|
func (this *Point) Close() {
|
|
|
|
this.ich.Close()
|
|
|
|
for _, idh := range this.idh {
|
|
|
|
idh.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-06-01 20:45:12 +00:00
|
|
|
log.Error("Point: Invalid port ", this.port)
|
2016-08-18 06:21:20 +00:00
|
|
|
return common.ErrBadConfiguration
|
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 {
|
2016-06-03 22:38:22 +00:00
|
|
|
err := this.ich.Start()
|
2015-10-31 23:11:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-10-13 23:15:29 +00:00
|
|
|
}
|
2016-06-01 20:45:12 +00:00
|
|
|
log.Warning("Point: started on port ", 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
|
|
|
|
2016-05-18 06:05:52 +00:00
|
|
|
func (this *Point) GetHandler(tag string) (proxy.InboundHandler, int) {
|
2016-01-21 16:22:56 +00:00
|
|
|
handler, found := this.taggedIdh[tag]
|
|
|
|
if !found {
|
2016-01-30 10:45:33 +00:00
|
|
|
log.Warning("Point: Unable to find an inbound handler with tag: ", tag)
|
2016-01-21 16:22:56 +00:00
|
|
|
return nil, 0
|
|
|
|
}
|
|
|
|
return handler.GetConnectionHandler()
|
2016-01-21 00:40:52 +00:00
|
|
|
}
|
2016-05-18 15:12:04 +00:00
|
|
|
|
|
|
|
func (this *Point) Release() {
|
|
|
|
|
|
|
|
}
|