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 := proxy.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 }
func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig *proxyman.ReceiverConfig, proxyConfig interface{}) (*AlwaysOnInboundHandler, error) { p, err := proxy.CreateInboundHandler(ctx, proxyConfig) if err != nil { return nil, err } h := &AlwaysOnInboundHandler{ proxy: p, } nl := p.Network() pr := receiverConfig.PortRange address := receiverConfig.Listen.AsAddress() if address == nil { address = net.AnyIP } for port := pr.From; port <= pr.To; port++ { if nl.HasNetwork(net.Network_TCP) { log.Debug("Proxyman|DefaultInboundHandler: creating tcp worker on ", address, ":", port) worker := &tcpWorker{ address: address, port: net.Port(port), proxy: p, stream: receiverConfig.StreamSettings, recvOrigDest: receiverConfig.ReceiveOriginalDestination, tag: tag, allowPassiveConn: receiverConfig.AllowPassiveConnection, } h.workers = append(h.workers, worker) } if nl.HasNetwork(net.Network_UDP) { worker := &udpWorker{ tag: tag, proxy: p, address: address, port: net.Port(port), recvOrigDest: receiverConfig.ReceiveOriginalDestination, } h.workers = append(h.workers, worker) } } return h, nil }
func (v *InboundDetourHandlerDynamic) refresh() error { v.lastRefresh = time.Now() config := v.config v.ich2Recyle = v.ichs newIchs := make([]proxy.InboundHandler, config.GetAllocationStrategyValue().Concurrency.GetValue()) for idx := range newIchs { err := retry.Timed(5, 100).On(func() error { port := v.pickUnusedPort() ichConfig, _ := config.GetTypedSettings() ich, err := proxy.CreateInboundHandler(config.Settings.Type, v.space, ichConfig, &proxy.InboundHandlerMeta{ Address: config.GetListenOnValue(), Port: port, Tag: config.Tag, StreamSettings: config.StreamSettings}) if err != nil { delete(v.portsInUse, port) return err } err = ich.Start() if err != nil { delete(v.portsInUse, port) return err } v.portsInUse[port] = true newIchs[idx] = ich return nil }) if err != nil { log.Error("Point: Failed to create inbound connection handler: ", err) return err } } v.Lock() v.ichs = newIchs v.Unlock() return nil }
func (h *DynamicInboundHandler) refresh() error { h.lastRefresh = time.Now() ports2Del := make([]v2net.Port, 0, 16) for _, worker := range h.worker2Recycle { worker.Close() ports2Del = append(ports2Del, worker.Port()) } h.Lock() for _, port := range ports2Del { delete(h.portsInUse, port) } h.Unlock() h.worker2Recycle, h.worker = h.worker, h.worker2Recycle[:0] address := h.receiverConfig.Listen.AsAddress() if address == nil { address = v2net.AnyIP } for i := uint32(0); i < h.receiverConfig.AllocationStrategy.GetConcurrencyValue(); i++ { port := h.allocatePort() p, err := proxy.CreateInboundHandler(h.ctx, h.proxyConfig) if err != nil { log.Warning("Proxyman|DefaultInboundHandler: Failed to create proxy instance: ", err) continue } nl := p.Network() if nl.HasNetwork(v2net.Network_TCP) { worker := &tcpWorker{ tag: h.tag, address: address, port: port, proxy: p, stream: h.receiverConfig.StreamSettings, recvOrigDest: h.receiverConfig.ReceiveOriginalDestination, allowPassiveConn: h.receiverConfig.AllowPassiveConnection, } if err := worker.Start(); err != nil { return err } h.worker = append(h.worker, worker) } if nl.HasNetwork(v2net.Network_UDP) { worker := &udpWorker{ tag: h.tag, proxy: p, address: address, port: port, recvOrigDest: h.receiverConfig.ReceiveOriginalDestination, } if err := worker.Start(); err != nil { return err } h.worker = append(h.worker, worker) } } return nil }