コード例 #1
0
func NewInboundDetourHandlerDynamic(space app.Space, config *InboundConnectionConfig) (*InboundDetourHandlerDynamic, error) {
	handler := &InboundDetourHandlerDynamic{
		space:      space,
		config:     config,
		portsInUse: make(map[v2net.Port]bool),
	}
	handler.ichs = make([]proxy.InboundHandler, config.GetAllocationStrategyValue().Concurrency.GetValue())

	// To test configuration
	ichConfig, err := config.GetTypedSettings()
	if err != nil {
		return nil, err
	}
	ich, err := proxyregistry.CreateInboundHandler(config.Settings.Type, space, ichConfig, &proxy.InboundHandlerMeta{
		Address:                config.GetListenOnValue(),
		Port:                   0,
		Tag:                    config.Tag,
		StreamSettings:         config.StreamSettings,
		AllowPassiveConnection: config.AllowPassiveConnection,
	})
	if err != nil {
		log.Error("Point: Failed to create inbound connection handler: ", err)
		return nil, err
	}
	ich.Close()

	return handler, nil
}
コード例 #2
0
func NewInboundDetourHandlerAlways(space app.Space, config *InboundConnectionConfig) (*InboundDetourHandlerAlways, error) {
	handler := &InboundDetourHandlerAlways{
		space:  space,
		config: config,
	}
	ports := config.PortRange
	handler.ich = make([]proxy.InboundHandler, 0, ports.To-ports.From+1)
	for i := ports.FromPort(); i <= ports.ToPort(); i++ {
		ichConfig, err := config.GetTypedSettings()
		if err != nil {
			return nil, err
		}
		ich, err := proxyregistry.CreateInboundHandler(config.Settings.Type, space, ichConfig, &proxy.InboundHandlerMeta{
			Address:                config.GetListenOnValue(),
			Port:                   i,
			Tag:                    config.Tag,
			StreamSettings:         config.StreamSettings,
			AllowPassiveConnection: config.AllowPassiveConnection,
		})
		if err != nil {
			log.Error("Failed to create inbound connection handler: ", err)
			return nil, err
		}
		handler.ich = append(handler.ich, ich)
	}
	return handler, nil
}
コード例 #3
0
func (this *InboundDetourHandlerDynamic) refresh() error {
	this.lastRefresh = time.Now()

	config := this.config
	this.ich2Recyle = this.ichs
	newIchs := make([]proxy.InboundHandler, config.GetAllocationStrategyValue().Concurrency.GetValue())

	for idx := range newIchs {
		err := retry.Timed(5, 100).On(func() error {
			port := this.pickUnusedPort()
			ichConfig, _ := config.GetTypedSettings()
			ich, err := proxyregistry.CreateInboundHandler(config.Settings.Type, this.space, ichConfig, &proxy.InboundHandlerMeta{
				Address: config.GetListenOnValue(), Port: port, Tag: config.Tag, StreamSettings: config.StreamSettings})
			if err != nil {
				delete(this.portsInUse, port)
				return err
			}
			err = ich.Start()
			if err != nil {
				delete(this.portsInUse, port)
				return err
			}
			this.portsInUse[port] = true
			newIchs[idx] = ich
			return nil
		})
		if err != nil {
			log.Error("Point: Failed to create inbound connection handler: ", err)
			return err
		}
	}

	this.Lock()
	this.ichs = newIchs
	this.Unlock()

	return nil
}
コード例 #4
0
ファイル: point.go プロジェクト: DZLZHCODE/v2ray-core
// NewPoint returns a new Point server based on given configuration.
// The server is not started at this point.
func NewPoint(pConfig *Config) (*Point, error) {
	var vpoint = new(Point)
	vpoint.port = pConfig.InboundConfig.Port
	if vpoint.port == 0 {
		vpoint.port = pConfig.Port // Backward compatibility
	}

	vpoint.listen = pConfig.InboundConfig.ListenOn

	if pConfig.TransportConfig != nil {
		pConfig.TransportConfig.Apply()
	}

	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)
	}

	vpoint.space = app.NewSpace()
	vpoint.space.BindApp(proxyman.APP_ID_INBOUND_MANAGER, vpoint)

	outboundHandlerManager := proxyman.NewDefaultOutboundHandlerManager()
	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)
			return nil, common.ErrBadConfiguration
		}
		vpoint.space.BindApp(router.APP_ID, r)
		vpoint.router = r
	}

	vpoint.space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(vpoint.space))

	ichConfig := pConfig.InboundConfig.Settings
	ich, err := proxyregistry.CreateInboundHandler(
		pConfig.InboundConfig.Protocol, vpoint.space, ichConfig, &proxy.InboundHandlerMeta{
			Tag:                    "system.inbound",
			Address:                pConfig.InboundConfig.ListenOn,
			Port:                   vpoint.port,
			StreamSettings:         pConfig.InboundConfig.StreamSettings,
			AllowPassiveConnection: pConfig.InboundConfig.AllowPassiveConnection,
		})
	if err != nil {
		log.Error("Failed to create inbound connection handler: ", err)
		return nil, err
	}
	vpoint.ich = ich

	ochConfig := pConfig.OutboundConfig.Settings
	och, err := proxyregistry.CreateOutboundHandler(
		pConfig.OutboundConfig.Protocol, vpoint.space, ochConfig, &proxy.OutboundHandlerMeta{
			Tag:            "system.outbound",
			Address:        pConfig.OutboundConfig.SendThrough,
			StreamSettings: pConfig.OutboundConfig.StreamSettings,
		})
	if err != nil {
		log.Error("Failed to create outbound connection handler: ", err)
		return nil, err
	}
	vpoint.och = och
	outboundHandlerManager.SetDefaultHandler(och)

	vpoint.taggedIdh = make(map[string]InboundDetourHandler)
	detours := pConfig.InboundDetours
	if len(detours) > 0 {
		vpoint.idh = make([]InboundDetourHandler, len(detours))
		for idx, detourConfig := range detours {
			allocConfig := detourConfig.Allocation
			var detourHandler InboundDetourHandler
			switch allocConfig.Strategy {
			case AllocationStrategyAlways:
				dh, err := NewInboundDetourHandlerAlways(vpoint.space, detourConfig)
				if err != nil {
					log.Error("Point: Failed to create detour handler: ", err)
					return nil, common.ErrBadConfiguration
				}
				detourHandler = dh
			case AllocationStrategyRandom:
				dh, err := NewInboundDetourHandlerDynamic(vpoint.space, detourConfig)
				if err != nil {
					log.Error("Point: Failed to create detour handler: ", err)
					return nil, common.ErrBadConfiguration
				}
				detourHandler = dh
			default:
				log.Error("Point: Unknown allocation strategy: ", allocConfig.Strategy)
				return nil, common.ErrBadConfiguration
			}
			vpoint.idh[idx] = detourHandler
			if len(detourConfig.Tag) > 0 {
				vpoint.taggedIdh[detourConfig.Tag] = detourHandler
			}
		}
	}

	outboundDetours := pConfig.OutboundDetours
	if len(outboundDetours) > 0 {
		vpoint.odh = make(map[string]proxy.OutboundHandler)
		for _, detourConfig := range outboundDetours {
			detourHandler, err := proxyregistry.CreateOutboundHandler(
				detourConfig.Protocol, vpoint.space, detourConfig.Settings, &proxy.OutboundHandlerMeta{
					Tag:            detourConfig.Tag,
					Address:        detourConfig.SendThrough,
					StreamSettings: detourConfig.StreamSettings,
				})
			if err != nil {
				log.Error("Point: Failed to create detour outbound connection handler: ", err)
				return nil, err
			}
			vpoint.odh[detourConfig.Tag] = detourHandler
			outboundHandlerManager.SetHandler(detourConfig.Tag, detourHandler)
		}
	}

	if err := vpoint.space.Initialize(); err != nil {
		return nil, err
	}

	return vpoint, nil
}