diff --git a/app/dns/server_test.go b/app/dns/server_test.go index 83e40cc64..d2c1bb8dc 100644 --- a/app/dns/server_test.go +++ b/app/dns/server_test.go @@ -10,6 +10,7 @@ import ( . "github.com/v2ray/v2ray-core/app/dns" "github.com/v2ray/v2ray-core/app/proxyman" v2net "github.com/v2ray/v2ray-core/common/net" + "github.com/v2ray/v2ray-core/proxy" "github.com/v2ray/v2ray-core/proxy/freedom" "github.com/v2ray/v2ray-core/testing/assert" ) @@ -20,7 +21,7 @@ func TestDnsAdd(t *testing.T) { space := app.NewSpace() outboundHandlerManager := proxyman.NewDefaultOutboundHandlerManager() - outboundHandlerManager.SetDefaultHandler(freedom.NewFreedomConnection(&freedom.Config{}, space, v2net.AnyIP)) + outboundHandlerManager.SetDefaultHandler(freedom.NewFreedomConnection(&freedom.Config{}, space, &proxy.OutboundHandlerMeta{Address: v2net.AnyIP})) space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outboundHandlerManager) space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space)) diff --git a/proxy/blackhole/blackhole.go b/proxy/blackhole/blackhole.go index 7d57a7a63..1aa013775 100644 --- a/proxy/blackhole/blackhole.go +++ b/proxy/blackhole/blackhole.go @@ -11,10 +11,13 @@ import ( // BlackHole is an outbound connection that sliently swallow the entire payload. type BlackHole struct { + meta *proxy.OutboundHandlerMeta } -func NewBlackHole() *BlackHole { - return &BlackHole{} +func NewBlackHole(space app.Space, config *Config, meta *proxy.OutboundHandlerMeta) *BlackHole { + return &BlackHole{ + meta: meta, + } } func (this *BlackHole) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error { @@ -31,7 +34,7 @@ func (this *BlackHole) Dispatch(destination v2net.Destination, payload *alloc.Bu func init() { internal.MustRegisterOutboundHandlerCreator("blackhole", - func(space app.Space, config interface{}, sendThrough v2net.Address) (proxy.OutboundHandler, error) { - return NewBlackHole(), nil + func(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) { + return NewBlackHole(space, config.(*Config), meta), nil }) } diff --git a/proxy/dokodemo/dokodemo.go b/proxy/dokodemo/dokodemo.go index 81376855f..84b352076 100644 --- a/proxy/dokodemo/dokodemo.go +++ b/proxy/dokodemo/dokodemo.go @@ -25,17 +25,15 @@ type DokodemoDoor struct { tcpListener *hub.TCPHub udpHub *hub.UDPHub udpServer *hub.UDPServer - listeningPort v2net.Port - listeningAddress v2net.Address + meta *proxy.InboundHandlerMeta } -func NewDokodemoDoor(config *Config, space app.Space, listen v2net.Address, port v2net.Port) *DokodemoDoor { +func NewDokodemoDoor(config *Config, space app.Space, meta *proxy.InboundHandlerMeta) *DokodemoDoor { d := &DokodemoDoor{ - config: config, - address: config.Address, - port: config.Port, - listeningAddress: listen, - listeningPort: port, + config: config, + address: config.Address, + port: config.Port, + meta: meta, } space.InitializeApplication(func() error { if !space.HasApp(dispatcher.APP_ID) { @@ -49,7 +47,7 @@ func NewDokodemoDoor(config *Config, space app.Space, listen v2net.Address, port } func (this *DokodemoDoor) Port() v2net.Port { - return this.listeningPort + return this.meta.Port } func (this *DokodemoDoor) Close() { @@ -75,13 +73,13 @@ func (this *DokodemoDoor) Start() error { this.accepting = true if this.config.Network.HasNetwork(v2net.TCPNetwork) { - err := this.ListenTCP(this.listeningAddress, this.listeningPort) + err := this.ListenTCP() if err != nil { return err } } if this.config.Network.HasNetwork(v2net.UDPNetwork) { - err := this.ListenUDP(this.listeningAddress, this.listeningPort) + err := this.ListenUDP() if err != nil { return err } @@ -89,11 +87,11 @@ func (this *DokodemoDoor) Start() error { return nil } -func (this *DokodemoDoor) ListenUDP(address v2net.Address, port v2net.Port) error { +func (this *DokodemoDoor) ListenUDP() error { this.udpServer = hub.NewUDPServer(this.packetDispatcher) - udpHub, err := hub.ListenUDP(address, port, this.handleUDPPackets) + udpHub, err := hub.ListenUDP(this.meta.Address, this.meta.Port, this.handleUDPPackets) if err != nil { - log.Error("Dokodemo failed to listen on port ", port, ": ", err) + log.Error("Dokodemo failed to listen on ", this.meta.Address, ":", this.meta.Port, ": ", err) return err } this.udpMutex.Lock() @@ -116,10 +114,10 @@ func (this *DokodemoDoor) handleUDPResponse(dest v2net.Destination, payload *all this.udpHub.WriteTo(payload.Value, dest) } -func (this *DokodemoDoor) ListenTCP(address v2net.Address, port v2net.Port) error { - tcpListener, err := hub.ListenTCP(address, port, this.HandleTCPConnection, nil) +func (this *DokodemoDoor) ListenTCP() error { + tcpListener, err := hub.ListenTCP(this.meta.Address, this.meta.Port, this.HandleTCPConnection, nil) if err != nil { - log.Error("Dokodemo: Failed to listen on port ", port, ": ", err) + log.Error("Dokodemo: Failed to listen on ", this.meta.Address, ":", this.meta.Port, ": ", err) return err } this.tcpMutex.Lock() @@ -164,7 +162,7 @@ func (this *DokodemoDoor) HandleTCPConnection(conn *hub.Connection) { func init() { internal.MustRegisterInboundHandlerCreator("dokodemo-door", - func(space app.Space, rawConfig interface{}, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) { - return NewDokodemoDoor(rawConfig.(*Config), space, listen, port), nil + func(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) { + return NewDokodemoDoor(rawConfig.(*Config), space, meta), nil }) } diff --git a/proxy/dokodemo/dokodemo_test.go b/proxy/dokodemo/dokodemo_test.go index 45f4f04b8..5787a60c5 100644 --- a/proxy/dokodemo/dokodemo_test.go +++ b/proxy/dokodemo/dokodemo_test.go @@ -10,6 +10,7 @@ import ( "github.com/v2ray/v2ray-core/app/proxyman" v2net "github.com/v2ray/v2ray-core/common/net" v2nettesting "github.com/v2ray/v2ray-core/common/net/testing" + "github.com/v2ray/v2ray-core/proxy" . "github.com/v2ray/v2ray-core/proxy/dokodemo" "github.com/v2ray/v2ray-core/proxy/freedom" "github.com/v2ray/v2ray-core/testing/assert" @@ -37,7 +38,9 @@ func TestDokodemoTCP(t *testing.T) { space := app.NewSpace() space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space)) ohm := proxyman.NewDefaultOutboundHandlerManager() - ohm.SetDefaultHandler(freedom.NewFreedomConnection(&freedom.Config{}, space, v2net.LocalHostIP)) + ohm.SetDefaultHandler( + freedom.NewFreedomConnection( + &freedom.Config{}, space, &proxy.OutboundHandlerMeta{Address: v2net.LocalHostIP})) space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, ohm) data2Send := "Data to be sent to remote." @@ -48,7 +51,7 @@ func TestDokodemoTCP(t *testing.T) { Port: tcpServer.Port, Network: v2net.TCPNetwork.AsList(), Timeout: 600, - }, space, v2net.LocalHostIP, port) + }, space, &proxy.InboundHandlerMeta{Address: v2net.LocalHostIP, Port: port}) defer dokodemo.Close() assert.Error(space.Initialize()).IsNil() @@ -95,7 +98,9 @@ func TestDokodemoUDP(t *testing.T) { space := app.NewSpace() space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space)) ohm := proxyman.NewDefaultOutboundHandlerManager() - ohm.SetDefaultHandler(freedom.NewFreedomConnection(&freedom.Config{}, space, v2net.AnyIP)) + ohm.SetDefaultHandler( + freedom.NewFreedomConnection( + &freedom.Config{}, space, &proxy.OutboundHandlerMeta{Address: v2net.AnyIP})) space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, ohm) data2Send := "Data to be sent to remote." @@ -106,7 +111,7 @@ func TestDokodemoUDP(t *testing.T) { Port: udpServer.Port, Network: v2net.UDPNetwork.AsList(), Timeout: 600, - }, space, v2net.LocalHostIP, port) + }, space, &proxy.InboundHandlerMeta{Address: v2net.LocalHostIP, Port: port}) defer dokodemo.Close() assert.Error(space.Initialize()).IsNil() diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index f451de99f..b1a54515a 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -23,14 +23,14 @@ type FreedomConnection struct { domainStrategy DomainStrategy timeout uint32 dns dns.Server - sendThrough v2net.Address + meta *proxy.OutboundHandlerMeta } -func NewFreedomConnection(config *Config, space app.Space, sendThrough v2net.Address) *FreedomConnection { +func NewFreedomConnection(config *Config, space app.Space, meta *proxy.OutboundHandlerMeta) *FreedomConnection { f := &FreedomConnection{ domainStrategy: config.DomainStrategy, timeout: config.Timeout, - sendThrough: sendThrough, + meta: meta, } space.InitializeApplication(func() error { if config.DomainStrategy == DomainStrategyUseIP { @@ -80,7 +80,7 @@ func (this *FreedomConnection) Dispatch(destination v2net.Destination, payload * destination = this.ResolveIP(destination) } err := retry.Timed(5, 100).On(func() error { - rawConn, err := hub.DialWithoutCache(this.sendThrough, destination) + rawConn, err := hub.DialWithoutCache(this.meta.Address, destination) if err != nil { return err } @@ -140,7 +140,7 @@ func (this *FreedomConnection) Dispatch(destination v2net.Destination, payload * func init() { internal.MustRegisterOutboundHandlerCreator("freedom", - func(space app.Space, config interface{}, sendThrough v2net.Address) (proxy.OutboundHandler, error) { - return NewFreedomConnection(config.(*Config), space, sendThrough), nil + func(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) { + return NewFreedomConnection(config.(*Config), space, meta), nil }) } diff --git a/proxy/freedom/freedom_test.go b/proxy/freedom/freedom_test.go index 8bda6a835..c9f5ec8e3 100644 --- a/proxy/freedom/freedom_test.go +++ b/proxy/freedom/freedom_test.go @@ -14,6 +14,7 @@ import ( "github.com/v2ray/v2ray-core/common/alloc" v2net "github.com/v2ray/v2ray-core/common/net" v2nettesting "github.com/v2ray/v2ray-core/common/net/testing" + "github.com/v2ray/v2ray-core/proxy" . "github.com/v2ray/v2ray-core/proxy/freedom" "github.com/v2ray/v2ray-core/testing/assert" "github.com/v2ray/v2ray-core/testing/servers/tcp" @@ -37,7 +38,7 @@ func TestSinglePacket(t *testing.T) { assert.Error(err).IsNil() space := app.NewSpace() - freedom := NewFreedomConnection(&Config{}, space, v2net.AnyIP) + freedom := NewFreedomConnection(&Config{}, space, &proxy.OutboundHandlerMeta{Address: v2net.AnyIP}) space.Initialize() traffic := ray.NewRay() @@ -57,7 +58,7 @@ func TestSinglePacket(t *testing.T) { func TestUnreachableDestination(t *testing.T) { assert := assert.On(t) - freedom := NewFreedomConnection(&Config{}, app.NewSpace(), v2net.AnyIP) + freedom := NewFreedomConnection(&Config{}, app.NewSpace(), &proxy.OutboundHandlerMeta{Address: v2net.AnyIP}) traffic := ray.NewRay() data2Send := "Data to be sent to remote" payload := alloc.NewSmallBuffer().Clear().Append([]byte(data2Send)) @@ -81,7 +82,10 @@ func TestIPResolution(t *testing.T) { }) space.BindApp(dns.APP_ID, dnsServer) - freedom := NewFreedomConnection(&Config{DomainStrategy: DomainStrategyUseIP}, space, v2net.AnyIP) + freedom := NewFreedomConnection( + &Config{DomainStrategy: DomainStrategyUseIP}, + space, + &proxy.OutboundHandlerMeta{Address: v2net.AnyIP}) space.Initialize() diff --git a/proxy/http/server.go b/proxy/http/server.go index 18c2ac632..f8763c2e0 100644 --- a/proxy/http/server.go +++ b/proxy/http/server.go @@ -29,21 +29,19 @@ type Server struct { packetDispatcher dispatcher.PacketDispatcher config *Config tcpListener *hub.TCPHub - listeningPort v2net.Port - listeningAddress v2net.Address + meta *proxy.InboundHandlerMeta } -func NewServer(config *Config, packetDispatcher dispatcher.PacketDispatcher, listen v2net.Address, port v2net.Port) *Server { +func NewServer(config *Config, packetDispatcher dispatcher.PacketDispatcher, meta *proxy.InboundHandlerMeta) *Server { return &Server{ packetDispatcher: packetDispatcher, config: config, - listeningAddress: listen, - listeningPort: port, + meta: meta, } } func (this *Server) Port() v2net.Port { - return this.listeningPort + return this.meta.Port } func (this *Server) Close() { @@ -65,9 +63,9 @@ func (this *Server) Start() error { if this.config.TLSConfig != nil { tlsConfig = this.config.TLSConfig.GetConfig() } - tcpListener, err := hub.ListenTCP(this.listeningAddress, this.listeningPort, this.handleConnection, tlsConfig) + tcpListener, err := hub.ListenTCP(this.meta.Address, this.meta.Port, this.handleConnection, tlsConfig) if err != nil { - log.Error("HTTP: Failed listen on port ", this.listeningPort, ": ", err) + log.Error("HTTP: Failed listen on ", this.meta.Address, ":", this.meta.Port, ": ", err) return err } this.Lock() @@ -272,14 +270,13 @@ func (this *Server) handlePlainHTTP(request *http.Request, dest v2net.Destinatio func init() { internal.MustRegisterInboundHandlerCreator("http", - func(space app.Space, rawConfig interface{}, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) { + func(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) { if !space.HasApp(dispatcher.APP_ID) { return nil, internal.ErrorBadConfiguration } return NewServer( rawConfig.(*Config), space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher), - listen, - port), nil + meta), nil }) } diff --git a/proxy/http/server_test.go b/proxy/http/server_test.go index ef5eec7f8..bbd5fe374 100644 --- a/proxy/http/server_test.go +++ b/proxy/http/server_test.go @@ -9,6 +9,7 @@ import ( testdispatcher "github.com/v2ray/v2ray-core/app/dispatcher/testing" v2net "github.com/v2ray/v2ray-core/common/net" v2nettesting "github.com/v2ray/v2ray-core/common/net/testing" + "github.com/v2ray/v2ray-core/proxy" . "github.com/v2ray/v2ray-core/proxy/http" "github.com/v2ray/v2ray-core/testing/assert" ) @@ -53,7 +54,7 @@ func TestNormalGetRequest(t *testing.T) { testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil) port := v2nettesting.PickPort() - httpProxy := NewServer(&Config{}, testPacketDispatcher, v2net.LocalHostIP, port) + httpProxy := NewServer(&Config{}, testPacketDispatcher, &proxy.InboundHandlerMeta{Address: v2net.LocalHostIP, Port: port}) defer httpProxy.Close() err := httpProxy.Start() diff --git a/proxy/internal/creator.go b/proxy/internal/creator.go index 5a20562fe..8a2fa5e8c 100644 --- a/proxy/internal/creator.go +++ b/proxy/internal/creator.go @@ -2,9 +2,8 @@ package internal import ( "github.com/v2ray/v2ray-core/app" - v2net "github.com/v2ray/v2ray-core/common/net" "github.com/v2ray/v2ray-core/proxy" ) -type InboundHandlerCreator func(space app.Space, config interface{}, listenOn v2net.Address, port v2net.Port) (proxy.InboundHandler, error) -type OutboundHandlerCreator func(space app.Space, config interface{}, sendThrough v2net.Address) (proxy.OutboundHandler, error) +type InboundHandlerCreator func(space app.Space, config interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) +type OutboundHandlerCreator func(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) diff --git a/proxy/internal/handler_cache.go b/proxy/internal/handler_cache.go index de8eded17..896ff6e0b 100644 --- a/proxy/internal/handler_cache.go +++ b/proxy/internal/handler_cache.go @@ -4,7 +4,6 @@ import ( "errors" "github.com/v2ray/v2ray-core/app" - v2net "github.com/v2ray/v2ray-core/common/net" "github.com/v2ray/v2ray-core/proxy" "github.com/v2ray/v2ray-core/proxy/internal/config" ) @@ -46,7 +45,7 @@ func MustRegisterOutboundHandlerCreator(name string, creator OutboundHandlerCrea } } -func CreateInboundHandler(name string, space app.Space, rawConfig []byte, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) { +func CreateInboundHandler(name string, space app.Space, rawConfig []byte, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) { creator, found := inboundFactories[name] if !found { return nil, ErrorProxyNotFound @@ -56,12 +55,12 @@ func CreateInboundHandler(name string, space app.Space, rawConfig []byte, listen if err != nil { return nil, err } - return creator(space, proxyConfig, listen, port) + return creator(space, proxyConfig, meta) } - return creator(space, nil, listen, port) + return creator(space, nil, meta) } -func CreateOutboundHandler(name string, space app.Space, rawConfig []byte, sendThrough v2net.Address) (proxy.OutboundHandler, error) { +func CreateOutboundHandler(name string, space app.Space, rawConfig []byte, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) { creator, found := outboundFactories[name] if !found { return nil, ErrorNameExists @@ -72,8 +71,8 @@ func CreateOutboundHandler(name string, space app.Space, rawConfig []byte, sendT if err != nil { return nil, err } - return creator(space, proxyConfig, sendThrough) + return creator(space, proxyConfig, meta) } - return creator(space, nil, sendThrough) + return creator(space, nil, meta) } diff --git a/proxy/proxy.go b/proxy/proxy.go index 947adb6af..bf7469ba7 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -14,6 +14,17 @@ const ( HandlerStateRunning = HandlerState(1) ) +type InboundHandlerMeta struct { + Tag string + Address v2net.Address + Port v2net.Port +} + +type OutboundHandlerMeta struct { + Tag string + Address v2net.Address +} + // An InboundHandler handles inbound network connections to V2Ray. type InboundHandler interface { // Listen starts a InboundHandler. diff --git a/proxy/repo/repo.go b/proxy/repo/repo.go index baf07a859..77e4d092d 100644 --- a/proxy/repo/repo.go +++ b/proxy/repo/repo.go @@ -2,15 +2,14 @@ package repo import ( "github.com/v2ray/v2ray-core/app" - v2net "github.com/v2ray/v2ray-core/common/net" "github.com/v2ray/v2ray-core/proxy" "github.com/v2ray/v2ray-core/proxy/internal" ) -func CreateInboundHandler(name string, space app.Space, rawConfig []byte, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) { - return internal.CreateInboundHandler(name, space, rawConfig, listen, port) +func CreateInboundHandler(name string, space app.Space, rawConfig []byte, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) { + return internal.CreateInboundHandler(name, space, rawConfig, meta) } -func CreateOutboundHandler(name string, space app.Space, rawConfig []byte, sendThrough v2net.Address) (proxy.OutboundHandler, error) { - return internal.CreateOutboundHandler(name, space, rawConfig, sendThrough) +func CreateOutboundHandler(name string, space app.Space, rawConfig []byte, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) { + return internal.CreateOutboundHandler(name, space, rawConfig, meta) } diff --git a/proxy/shadowsocks/server.go b/proxy/shadowsocks/server.go index 85caff7f4..7b5eee313 100644 --- a/proxy/shadowsocks/server.go +++ b/proxy/shadowsocks/server.go @@ -22,25 +22,23 @@ import ( type Server struct { packetDispatcher dispatcher.PacketDispatcher config *Config - port v2net.Port - address v2net.Address + meta *proxy.InboundHandlerMeta accepting bool tcpHub *hub.TCPHub udpHub *hub.UDPHub udpServer *hub.UDPServer } -func NewServer(config *Config, packetDispatcher dispatcher.PacketDispatcher, listen v2net.Address, port v2net.Port) *Server { +func NewServer(config *Config, packetDispatcher dispatcher.PacketDispatcher, meta *proxy.InboundHandlerMeta) *Server { return &Server{ config: config, packetDispatcher: packetDispatcher, - address: listen, - port: port, + meta: meta, } } func (this *Server) Port() v2net.Port { - return this.port + return this.meta.Port } func (this *Server) Close() { @@ -63,18 +61,18 @@ func (this *Server) Start() error { return nil } - tcpHub, err := hub.ListenTCP(this.address, this.port, this.handleConnection, nil) + tcpHub, err := hub.ListenTCP(this.meta.Address, this.meta.Port, this.handleConnection, nil) if err != nil { - log.Error("Shadowsocks: Failed to listen TCP on port ", this.port, ": ", err) + log.Error("Shadowsocks: Failed to listen TCP on ", this.meta.Address, ":", this.meta.Port, ": ", err) return err } this.tcpHub = tcpHub if this.config.UDP { this.udpServer = hub.NewUDPServer(this.packetDispatcher) - udpHub, err := hub.ListenUDP(this.address, this.port, this.handlerUDPPayload) + udpHub, err := hub.ListenUDP(this.meta.Address, this.meta.Port, this.handlerUDPPayload) if err != nil { - log.Error("Shadowsocks: Failed to listen UDP on port ", this.port, ": ", err) + log.Error("Shadowsocks: Failed to listen UDP on ", this.meta.Address, ":", this.meta.Port, ": ", err) return err } this.udpHub = udpHub @@ -252,14 +250,13 @@ func (this *Server) handleConnection(conn *hub.Connection) { func init() { internal.MustRegisterInboundHandlerCreator("shadowsocks", - func(space app.Space, rawConfig interface{}, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) { + func(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) { if !space.HasApp(dispatcher.APP_ID) { return nil, internal.ErrorBadConfiguration } return NewServer( rawConfig.(*Config), space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher), - listen, - port), nil + meta), nil }) } diff --git a/proxy/socks/server.go b/proxy/socks/server.go index 09242c7c0..2484b4fdf 100644 --- a/proxy/socks/server.go +++ b/proxy/socks/server.go @@ -33,23 +33,21 @@ type Server struct { udpHub *hub.UDPHub udpAddress v2net.Destination udpServer *hub.UDPServer - listeningPort v2net.Port - listeningAddress v2net.Address + meta *proxy.InboundHandlerMeta } // NewServer creates a new Server object. -func NewServer(config *Config, packetDispatcher dispatcher.PacketDispatcher, listen v2net.Address, port v2net.Port) *Server { +func NewServer(config *Config, packetDispatcher dispatcher.PacketDispatcher, meta *proxy.InboundHandlerMeta) *Server { return &Server{ config: config, packetDispatcher: packetDispatcher, - listeningAddress: listen, - listeningPort: port, + meta: meta, } } // Port implements InboundHandler.Port(). func (this *Server) Port() v2net.Port { - return this.listeningPort + return this.meta.Port } // Close implements InboundHandler.Close(). @@ -76,12 +74,12 @@ func (this *Server) Start() error { } listener, err := hub.ListenTCP( - this.listeningAddress, - this.listeningPort, + this.meta.Address, + this.meta.Port, this.handleConnection, nil) if err != nil { - log.Error("Socks: failed to listen on port ", this.listeningPort, ": ", err) + log.Error("Socks: failed to listen on ", this.meta.Address, ":", this.meta.Port, ": ", err) return err } this.accepting = true @@ -89,7 +87,7 @@ func (this *Server) Start() error { this.tcpListener = listener this.tcpMutex.Unlock() if this.config.UDPEnabled { - this.listenUDP(this.listeningAddress, this.listeningPort) + this.listenUDP() } return nil } @@ -301,14 +299,13 @@ func (this *Server) transport(reader io.Reader, writer io.Writer, destination v2 func init() { internal.MustRegisterInboundHandlerCreator("socks", - func(space app.Space, rawConfig interface{}, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) { + func(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) { if !space.HasApp(dispatcher.APP_ID) { return nil, internal.ErrorBadConfiguration } return NewServer( rawConfig.(*Config), space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher), - listen, - port), nil + meta), nil }) } diff --git a/proxy/socks/server_test.go b/proxy/socks/server_test.go index cc37b27ed..021813a85 100644 --- a/proxy/socks/server_test.go +++ b/proxy/socks/server_test.go @@ -32,7 +32,7 @@ func TestSocksTcpConnect(t *testing.T) { } protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och", - func(space app.Space, config interface{}, sendThrough v2net.Address) (v2proxy.OutboundHandler, error) { + func(space app.Space, config interface{}, meta *v2proxy.OutboundHandlerMeta) (v2proxy.OutboundHandler, error) { return och, nil }) assert.Error(err).IsNil() @@ -98,7 +98,7 @@ func TestSocksTcpConnectWithUserPass(t *testing.T) { } protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och", - func(space app.Space, config interface{}, sendThrough v2net.Address) (v2proxy.OutboundHandler, error) { + func(space app.Space, config interface{}, meta *v2proxy.OutboundHandlerMeta) (v2proxy.OutboundHandler, error) { return och, nil }) assert.Error(err).IsNil() @@ -167,7 +167,7 @@ func TestSocksTcpConnectWithWrongUserPass(t *testing.T) { } protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och", - func(space app.Space, config interface{}, sendThrough v2net.Address) (v2proxy.OutboundHandler, error) { + func(space app.Space, config interface{}, meta *v2proxy.OutboundHandlerMeta) (v2proxy.OutboundHandler, error) { return och, nil }) assert.Error(err).IsNil() @@ -222,7 +222,7 @@ func TestSocksTcpConnectWithWrongAuthMethod(t *testing.T) { } protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och", - func(space app.Space, config interface{}, sendThrough v2net.Address) (v2proxy.OutboundHandler, error) { + func(space app.Space, config interface{}, meta *v2proxy.OutboundHandlerMeta) (v2proxy.OutboundHandler, error) { return och, nil }) assert.Error(err).IsNil() diff --git a/proxy/socks/server_udp.go b/proxy/socks/server_udp.go index bd748ff6b..57b692194 100644 --- a/proxy/socks/server_udp.go +++ b/proxy/socks/server_udp.go @@ -8,15 +8,15 @@ import ( "github.com/v2ray/v2ray-core/transport/hub" ) -func (this *Server) listenUDP(address v2net.Address, port v2net.Port) error { +func (this *Server) listenUDP() error { this.udpServer = hub.NewUDPServer(this.packetDispatcher) - udpHub, err := hub.ListenUDP(address, port, this.handleUDPPayload) + udpHub, err := hub.ListenUDP(this.meta.Address, this.meta.Port, this.handleUDPPayload) if err != nil { - log.Error("Socks: Failed to listen on udp port ", port) + log.Error("Socks: Failed to listen on udp ", this.meta.Address, ":", this.meta.Port) return err } this.udpMutex.Lock() - this.udpAddress = v2net.UDPDestination(this.config.Address, port) + this.udpAddress = v2net.UDPDestination(this.config.Address, this.meta.Port) this.udpHub = udpHub this.udpMutex.Unlock() return nil diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index 002159fac..62db0e59e 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -74,12 +74,11 @@ type VMessInboundHandler struct { accepting bool listener *hub.TCPHub detours *DetourConfig - listeningPort v2net.Port - listeningAddress v2net.Address + meta *proxy.InboundHandlerMeta } func (this *VMessInboundHandler) Port() v2net.Port { - return this.listeningPort + return this.meta.Port } func (this *VMessInboundHandler) Close() { @@ -107,9 +106,9 @@ func (this *VMessInboundHandler) Start() error { return nil } - tcpListener, err := hub.ListenTCP(this.listeningAddress, this.listeningPort, this.HandleConnection, nil) + tcpListener, err := hub.ListenTCP(this.meta.Address, this.meta.Port, this.HandleConnection, nil) if err != nil { - log.Error("Unable to listen tcp port ", this.listeningPort, ": ", err) + log.Error("Unable to listen tcp ", this.meta.Address, ":", this.meta.Port, ": ", err) return err } this.accepting = true @@ -220,7 +219,7 @@ func (this *VMessInboundHandler) HandleConnection(connection *hub.Connection) { func init() { internal.MustRegisterInboundHandlerCreator("vmess", - func(space app.Space, rawConfig interface{}, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) { + func(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) { if !space.HasApp(dispatcher.APP_ID) { return nil, internal.ErrorBadConfiguration } @@ -236,8 +235,7 @@ func init() { clients: allowedClients, detours: config.DetourConfig, usersByEmail: NewUserByEmail(config.AllowedUsers, config.Defaults), - listeningAddress: listen, - listeningPort: port, + meta: meta, } if space.HasApp(proxyman.APP_ID_INBOUND_MANAGER) { diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index 038385686..1917b7f84 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -21,7 +21,7 @@ import ( type VMessOutboundHandler struct { receiverManager *ReceiverManager - sendThrough v2net.Address + meta *proxy.OutboundHandlerMeta } func (this *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error { @@ -43,7 +43,7 @@ func (this *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *al Option: protocol.RequestOptionChunkStream, } - conn, err := hub.Dial(this.sendThrough, destination) + conn, err := hub.Dial(this.meta.Address, destination) if err != nil { log.Error("Failed to open ", destination, ": ", err) return err @@ -143,11 +143,11 @@ func (this *VMessOutboundHandler) handleResponse(session *raw.ClientSession, con func init() { internal.MustRegisterOutboundHandlerCreator("vmess", - func(space app.Space, rawConfig interface{}, sendThrough v2net.Address) (proxy.OutboundHandler, error) { + func(space app.Space, rawConfig interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) { vOutConfig := rawConfig.(*Config) return &VMessOutboundHandler{ receiverManager: NewReceiverManager(vOutConfig.Receivers), - sendThrough: sendThrough, + meta: meta, }, nil }) } diff --git a/proxy/vmess/vmess_test.go b/proxy/vmess/vmess_test.go index ea50e8890..477844815 100644 --- a/proxy/vmess/vmess_test.go +++ b/proxy/vmess/vmess_test.go @@ -39,9 +39,9 @@ func TestVMessInAndOut(t *testing.T) { } protocol, err := proxytesting.RegisterInboundConnectionHandlerCreator("mock_ich", - func(space app.Space, config interface{}, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) { - ich.ListeningAddress = listen - ich.ListeningPort = port + func(space app.Space, config interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) { + ich.ListeningAddress = meta.Address + ich.ListeningPort = meta.Port ich.PacketDispatcher = space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher) return ich, nil }) @@ -89,7 +89,7 @@ func TestVMessInAndOut(t *testing.T) { } protocol, err = proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och", - func(space app.Space, config interface{}, sendThrough v2net.Address) (proxy.OutboundHandler, error) { + func(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) { return och, nil }) assert.Error(err).IsNil() diff --git a/shell/point/inbound_detour_always.go b/shell/point/inbound_detour_always.go index 3de7c89c1..dd420ae34 100644 --- a/shell/point/inbound_detour_always.go +++ b/shell/point/inbound_detour_always.go @@ -4,23 +4,16 @@ import ( "github.com/v2ray/v2ray-core/app" "github.com/v2ray/v2ray-core/common/dice" "github.com/v2ray/v2ray-core/common/log" - v2net "github.com/v2ray/v2ray-core/common/net" "github.com/v2ray/v2ray-core/common/retry" "github.com/v2ray/v2ray-core/proxy" proxyrepo "github.com/v2ray/v2ray-core/proxy/repo" ) -type InboundConnectionHandlerWithPort struct { - port v2net.Port - listen v2net.Address - handler proxy.InboundHandler -} - // Handler for inbound detour connections. type InboundDetourHandlerAlways struct { space app.Space config *InboundDetourConfig - ich []*InboundConnectionHandlerWithPort + ich []proxy.InboundHandler } func NewInboundDetourHandlerAlways(space app.Space, config *InboundDetourConfig) (*InboundDetourHandlerAlways, error) { @@ -29,31 +22,30 @@ func NewInboundDetourHandlerAlways(space app.Space, config *InboundDetourConfig) config: config, } ports := config.PortRange - handler.ich = make([]*InboundConnectionHandlerWithPort, 0, ports.To-ports.From+1) + handler.ich = make([]proxy.InboundHandler, 0, ports.To-ports.From+1) for i := ports.From; i <= ports.To; i++ { ichConfig := config.Settings - ich, err := proxyrepo.CreateInboundHandler(config.Protocol, space, ichConfig, config.ListenOn, i) + ich, err := proxyrepo.CreateInboundHandler(config.Protocol, space, ichConfig, &proxy.InboundHandlerMeta{ + Address: config.ListenOn, + Port: i, + Tag: config.Tag}) if err != nil { log.Error("Failed to create inbound connection handler: ", err) return nil, err } - handler.ich = append(handler.ich, &InboundConnectionHandlerWithPort{ - port: i, - handler: ich, - listen: config.ListenOn, - }) + handler.ich = append(handler.ich, ich) } return handler, nil } func (this *InboundDetourHandlerAlways) GetConnectionHandler() (proxy.InboundHandler, int) { ich := this.ich[dice.Roll(len(this.ich))] - return ich.handler, this.config.Allocation.Refresh + return ich, this.config.Allocation.Refresh } func (this *InboundDetourHandlerAlways) Close() { for _, ich := range this.ich { - ich.handler.Close() + ich.Close() } } @@ -61,9 +53,9 @@ func (this *InboundDetourHandlerAlways) Close() { func (this *InboundDetourHandlerAlways) Start() error { for _, ich := range this.ich { err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error { - err := ich.handler.Start() + err := ich.Start() if err != nil { - log.Error("Failed to start inbound detour on port ", ich.port, ": ", err) + log.Error("Failed to start inbound detour:", err) return err } return nil diff --git a/shell/point/inbound_detour_dynamic.go b/shell/point/inbound_detour_dynamic.go index 622fbf6bc..e32500c50 100644 --- a/shell/point/inbound_detour_dynamic.go +++ b/shell/point/inbound_detour_dynamic.go @@ -30,7 +30,10 @@ func NewInboundDetourHandlerDynamic(space app.Space, config *InboundDetourConfig handler.ichs = make([]proxy.InboundHandler, config.Allocation.Concurrency) // To test configuration - ich, err := proxyrepo.CreateInboundHandler(config.Protocol, space, config.Settings, config.ListenOn, 0) + ich, err := proxyrepo.CreateInboundHandler(config.Protocol, space, config.Settings, &proxy.InboundHandlerMeta{ + Address: config.ListenOn, + Port: 0, + Tag: config.Tag}) if err != nil { log.Error("Point: Failed to create inbound connection handler: ", err) return nil, err @@ -80,7 +83,8 @@ func (this *InboundDetourHandlerDynamic) refresh() error { for idx, _ := range newIchs { port := this.pickUnusedPort() - ich, err := proxyrepo.CreateInboundHandler(config.Protocol, this.space, config.Settings, config.ListenOn, port) + ich, err := proxyrepo.CreateInboundHandler(config.Protocol, this.space, config.Settings, &proxy.InboundHandlerMeta{ + Address: config.ListenOn, Port: port, Tag: config.Tag}) if err != nil { log.Error("Point: Failed to create inbound connection handler: ", err) return err diff --git a/shell/point/point.go b/shell/point/point.go index 1b4226a42..752eee5e1 100644 --- a/shell/point/point.go +++ b/shell/point/point.go @@ -91,7 +91,11 @@ func NewPoint(pConfig *Config) (*Point, error) { vpoint.space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(vpoint.space)) ichConfig := pConfig.InboundConfig.Settings - ich, err := proxyrepo.CreateInboundHandler(pConfig.InboundConfig.Protocol, vpoint.space, ichConfig, pConfig.InboundConfig.ListenOn, vpoint.port) + ich, err := proxyrepo.CreateInboundHandler( + pConfig.InboundConfig.Protocol, vpoint.space, ichConfig, &proxy.InboundHandlerMeta{ + Tag: "system.inbound", + Address: pConfig.InboundConfig.ListenOn, + Port: vpoint.port}) if err != nil { log.Error("Failed to create inbound connection handler: ", err) return nil, err @@ -99,7 +103,10 @@ func NewPoint(pConfig *Config) (*Point, error) { vpoint.ich = ich ochConfig := pConfig.OutboundConfig.Settings - och, err := proxyrepo.CreateOutboundHandler(pConfig.OutboundConfig.Protocol, vpoint.space, ochConfig, pConfig.OutboundConfig.SendThrough) + och, err := proxyrepo.CreateOutboundHandler( + pConfig.OutboundConfig.Protocol, vpoint.space, ochConfig, &proxy.OutboundHandlerMeta{ + Tag: "system.outbound", + Address: pConfig.OutboundConfig.SendThrough}) if err != nil { log.Error("Failed to create outbound connection handler: ", err) return nil, err @@ -144,7 +151,10 @@ func NewPoint(pConfig *Config) (*Point, error) { if len(outboundDetours) > 0 { vpoint.odh = make(map[string]proxy.OutboundHandler) for _, detourConfig := range outboundDetours { - detourHandler, err := proxyrepo.CreateOutboundHandler(detourConfig.Protocol, vpoint.space, detourConfig.Settings, detourConfig.SendThrough) + detourHandler, err := proxyrepo.CreateOutboundHandler( + detourConfig.Protocol, vpoint.space, detourConfig.Settings, &proxy.OutboundHandlerMeta{ + Tag: detourConfig.Tag, + Address: detourConfig.SendThrough}) if err != nil { log.Error("Point: Failed to create detour outbound connection handler: ", err) return nil, err