diff --git a/app/space.go b/app/space.go new file mode 100644 index 00000000..e837d4e3 --- /dev/null +++ b/app/space.go @@ -0,0 +1,23 @@ +package app + +type Space struct { + packetDispatcher PacketDispatcher +} + +func NewSpace() *Space { + return new(Space) +} + +func (this *Space) HasPacketDispatcher() bool { + return this.packetDispatcher != nil +} + +func (this *Space) PacketDispatcher() PacketDispatcher { + return this.packetDispatcher +} + +func (this *Space) Bind(object interface{}) { + if packetDispatcher, ok := object.(PacketDispatcher); ok { + this.packetDispatcher = packetDispatcher + } +} diff --git a/proxy/common/connhandler/inbound_connection.go b/proxy/common/connhandler/inbound_connection.go index 7aaa997a..dc0037d6 100644 --- a/proxy/common/connhandler/inbound_connection.go +++ b/proxy/common/connhandler/inbound_connection.go @@ -8,7 +8,7 @@ import ( // A InboundConnectionHandlerFactory creates InboundConnectionHandler on demand. type InboundConnectionHandlerFactory interface { // Create creates a new InboundConnectionHandler with given configuration. - Create(dispatch app.PacketDispatcher, config interface{}) (InboundConnectionHandler, error) + Create(space *app.Space, config interface{}) (InboundConnectionHandler, error) } // A InboundConnectionHandler handles inbound network connections to V2Ray. diff --git a/proxy/dokodemo/dokodemo.go b/proxy/dokodemo/dokodemo.go index b96fa026..b06313b7 100644 --- a/proxy/dokodemo/dokodemo.go +++ b/proxy/dokodemo/dokodemo.go @@ -14,16 +14,16 @@ import ( ) type DokodemoDoor struct { - config *json.DokodemoConfig - accepting bool - address v2net.Address - dispatcher app.PacketDispatcher + config *json.DokodemoConfig + accepting bool + address v2net.Address + space *app.Space } -func NewDokodemoDoor(dispatcher app.PacketDispatcher, config *json.DokodemoConfig) *DokodemoDoor { +func NewDokodemoDoor(space *app.Space, config *json.DokodemoConfig) *DokodemoDoor { d := &DokodemoDoor{ - config: config, - dispatcher: dispatcher, + config: config, + space: space, } ip := net.ParseIP(config.Host) if ip != nil { @@ -79,7 +79,7 @@ func (this *DokodemoDoor) handleUDPPackets(udpConn *net.UDPConn) { } packet := v2net.NewPacket(v2net.NewUDPDestination(this.address), buffer, false) - ray := this.dispatcher.DispatchToOutbound(packet) + ray := this.space.PacketDispatcher().DispatchToOutbound(packet) close(ray.InboundInput()) for payload := range ray.InboundOutput() { @@ -120,7 +120,7 @@ func (this *DokodemoDoor) HandleTCPConnection(conn *net.TCPConn) { defer conn.Close() packet := v2net.NewPacket(v2net.NewTCPDestination(this.address), nil, true) - ray := this.dispatcher.DispatchToOutbound(packet) + ray := this.space.PacketDispatcher().DispatchToOutbound(packet) var inputFinish, outputFinish sync.Mutex inputFinish.Lock() diff --git a/proxy/dokodemo/dokodemo_factory.go b/proxy/dokodemo/dokodemo_factory.go index b8452153..561d272e 100644 --- a/proxy/dokodemo/dokodemo_factory.go +++ b/proxy/dokodemo/dokodemo_factory.go @@ -9,9 +9,9 @@ import ( type DokodemoDoorFactory struct { } -func (this DokodemoDoorFactory) Create(dispatcher app.PacketDispatcher, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) { +func (this DokodemoDoorFactory) Create(space *app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) { config := rawConfig.(*json.DokodemoConfig) - return NewDokodemoDoor(dispatcher, config), nil + return NewDokodemoDoor(space, config), nil } func init() { diff --git a/proxy/socks/socks.go b/proxy/socks/socks.go index b4c2fb4c..1b52fcd2 100644 --- a/proxy/socks/socks.go +++ b/proxy/socks/socks.go @@ -24,15 +24,15 @@ var ( // SocksServer is a SOCKS 5 proxy server type SocksServer struct { - accepting bool - dispatcher app.PacketDispatcher - config config.SocksConfig + accepting bool + space *app.Space + config config.SocksConfig } -func NewSocksServer(dispatcher app.PacketDispatcher, config config.SocksConfig) *SocksServer { +func NewSocksServer(space *app.Space, config config.SocksConfig) *SocksServer { return &SocksServer{ - dispatcher: dispatcher, - config: config, + space: space, + config: config, } } @@ -252,7 +252,7 @@ func (this *SocksServer) handleSocks4(reader io.Reader, writer io.Writer, auth p } func (this *SocksServer) transport(reader io.Reader, writer io.Writer, firstPacket v2net.Packet) { - ray := this.dispatcher.DispatchToOutbound(firstPacket) + ray := this.space.PacketDispatcher().DispatchToOutbound(firstPacket) input := ray.InboundInput() output := ray.InboundOutput() diff --git a/proxy/socks/socksfactory.go b/proxy/socks/socksfactory.go index a65c0d80..c3e047b9 100644 --- a/proxy/socks/socksfactory.go +++ b/proxy/socks/socksfactory.go @@ -9,8 +9,8 @@ import ( type SocksServerFactory struct { } -func (this SocksServerFactory) Create(dispatcher app.PacketDispatcher, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) { - return NewSocksServer(dispatcher, rawConfig.(config.SocksConfig)), nil +func (this SocksServerFactory) Create(space *app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) { + return NewSocksServer(space, rawConfig.(config.SocksConfig)), nil } func init() { diff --git a/proxy/socks/udp.go b/proxy/socks/udp.go index 0c215d68..7437f03f 100644 --- a/proxy/socks/udp.go +++ b/proxy/socks/udp.go @@ -65,7 +65,7 @@ func (this *SocksServer) AcceptPackets(conn *net.UDPConn) error { } func (this *SocksServer) handlePacket(conn *net.UDPConn, packet v2net.Packet, clientAddr *net.UDPAddr, targetAddr v2net.Address) { - ray := this.dispatcher.DispatchToOutbound(packet) + ray := this.space.PacketDispatcher().DispatchToOutbound(packet) close(ray.InboundInput()) for data := range ray.InboundOutput() { diff --git a/proxy/testing/mocks/inboundhandler.go b/proxy/testing/mocks/inboundhandler.go index 920659a0..6e8277f8 100644 --- a/proxy/testing/mocks/inboundhandler.go +++ b/proxy/testing/mocks/inboundhandler.go @@ -11,7 +11,7 @@ import ( type InboundConnectionHandler struct { Port v2net.Port - Dispatcher app.PacketDispatcher + space *app.Space ConnInput io.Reader ConnOutput io.Writer } @@ -22,7 +22,7 @@ func (this *InboundConnectionHandler) Listen(port v2net.Port) error { } func (this *InboundConnectionHandler) Communicate(packet v2net.Packet) error { - ray := this.Dispatcher.DispatchToOutbound(packet) + ray := this.space.PacketDispatcher().DispatchToOutbound(packet) input := ray.InboundInput() output := ray.InboundOutput() @@ -49,7 +49,7 @@ func (this *InboundConnectionHandler) Communicate(packet v2net.Packet) error { return nil } -func (this *InboundConnectionHandler) Create(dispatcher app.PacketDispatcher, config interface{}) (connhandler.InboundConnectionHandler, error) { - this.Dispatcher = dispatcher +func (this *InboundConnectionHandler) Create(space *app.Space, config interface{}) (connhandler.InboundConnectionHandler, error) { + this.space = space return this, nil } diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index 8a29ded1..b470b4c5 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -20,15 +20,15 @@ import ( // Inbound connection handler that handles messages in VMess format. type VMessInboundHandler struct { - dispatcher app.PacketDispatcher - clients user.UserSet - accepting bool + space *app.Space + clients user.UserSet + accepting bool } -func NewVMessInboundHandler(dispatcher app.PacketDispatcher, clients user.UserSet) *VMessInboundHandler { +func NewVMessInboundHandler(space *app.Space, clients user.UserSet) *VMessInboundHandler { return &VMessInboundHandler{ - dispatcher: dispatcher, - clients: clients, + space: space, + clients: clients, } } @@ -78,7 +78,7 @@ func (this *VMessInboundHandler) HandleConnection(connection *net.TCPConn) error log.Access(connection.RemoteAddr().String(), request.Address.String(), log.AccessAccepted, "") log.Debug("VMessIn: Received request for %s", request.Address.String()) - ray := this.dispatcher.DispatchToOutbound(v2net.NewPacket(request.Destination(), nil, true)) + ray := this.space.PacketDispatcher().DispatchToOutbound(v2net.NewPacket(request.Destination(), nil, true)) input := ray.InboundInput() output := ray.InboundOutput() var readFinish, writeFinish sync.Mutex @@ -142,7 +142,7 @@ func handleOutput(request *protocol.VMessRequest, writer io.Writer, output <-cha type VMessInboundHandlerFactory struct { } -func (this *VMessInboundHandlerFactory) Create(dispatcher app.PacketDispatcher, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) { +func (this *VMessInboundHandlerFactory) Create(space *app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) { config := rawConfig.(config.Inbound) allowedClients := user.NewTimedUserSet() @@ -150,7 +150,7 @@ func (this *VMessInboundHandlerFactory) Create(dispatcher app.PacketDispatcher, allowedClients.AddUser(user) } - return NewVMessInboundHandler(dispatcher, allowedClients), nil + return NewVMessInboundHandler(space, allowedClients), nil } func init() { diff --git a/shell/point/inbound_detour.go b/shell/point/inbound_detour.go index 27b752f7..f69239c6 100644 --- a/shell/point/inbound_detour.go +++ b/shell/point/inbound_detour.go @@ -1,6 +1,7 @@ package point import ( + "github.com/v2ray/v2ray-core/app" "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" "github.com/v2ray/v2ray-core/common/retry" @@ -15,7 +16,7 @@ type InboundConnectionHandlerWithPort struct { // Handler for inbound detour connections. type InboundDetourHandler struct { - point *Point + space *app.Space config config.InboundDetourConfig ich []*InboundConnectionHandlerWithPort } @@ -31,7 +32,7 @@ func (this *InboundDetourHandler) Initialize() error { this.ich = make([]*InboundConnectionHandlerWithPort, 0, ports.To()-ports.From()+1) for i := ports.From(); i <= ports.To(); i++ { ichConfig := this.config.Settings() - ich, err := ichFactory.Create(this.point, ichConfig) + ich, err := ichFactory.Create(this.space, ichConfig) if err != nil { log.Error("Failed to create inbound connection handler: %v", err) return err diff --git a/shell/point/point.go b/shell/point/point.go index 8900b736..dfc2b25d 100644 --- a/shell/point/point.go +++ b/shell/point/point.go @@ -5,6 +5,7 @@ package point import ( + "github.com/v2ray/v2ray-core/app" "github.com/v2ray/v2ray-core/app/router" "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" @@ -22,6 +23,7 @@ type Point struct { idh []*InboundDetourHandler odh map[string]connhandler.OutboundConnectionHandler router router.Router + space *app.Space } // NewPoint returns a new Point server based on given configuration. @@ -49,13 +51,16 @@ func NewPoint(pConfig config.PointConfig) (*Point, error) { log.SetLogLevel(logConfig.LogLevel()) } + vpoint.space = app.NewSpace() + vpoint.space.Bind(vpoint) + ichFactory := connhandler.GetInboundConnectionHandlerFactory(pConfig.InboundConfig().Protocol()) if ichFactory == nil { log.Error("Unknown inbound connection handler factory %s", pConfig.InboundConfig().Protocol()) return nil, config.BadConfiguration } ichConfig := pConfig.InboundConfig().Settings() - ich, err := ichFactory.Create(vpoint, ichConfig) + ich, err := ichFactory.Create(vpoint.space, ichConfig) if err != nil { log.Error("Failed to create inbound connection handler: %v", err) return nil, err @@ -80,7 +85,7 @@ func NewPoint(pConfig config.PointConfig) (*Point, error) { vpoint.idh = make([]*InboundDetourHandler, len(detours)) for idx, detourConfig := range detours { detourHandler := &InboundDetourHandler{ - point: vpoint, + space: vpoint.space, config: detourConfig, } err := detourHandler.Initialize()