diff --git a/app/dispatcher/dispatcher.go b/app/dispatcher/dispatcher.go index 4cea8b6a..eb73ee1d 100644 --- a/app/dispatcher/dispatcher.go +++ b/app/dispatcher/dispatcher.go @@ -12,5 +12,5 @@ const ( // PacketDispatcher dispatch a packet and possibly further network payload to its destination. type PacketDispatcher interface { - DispatchToOutbound(meta *proxy.InboundHandlerMeta, session *proxy.SessionInfo) ray.InboundRay + DispatchToOutbound(session *proxy.SessionInfo) ray.InboundRay } diff --git a/app/dispatcher/impl/default.go b/app/dispatcher/impl/default.go index f52d6232..bf2f69d0 100644 --- a/app/dispatcher/impl/default.go +++ b/app/dispatcher/impl/default.go @@ -43,7 +43,7 @@ func (this *DefaultDispatcher) Release() { } -func (this *DefaultDispatcher) DispatchToOutbound(meta *proxy.InboundHandlerMeta, session *proxy.SessionInfo) ray.InboundRay { +func (this *DefaultDispatcher) DispatchToOutbound(session *proxy.SessionInfo) ray.InboundRay { direct := ray.NewRay() dispatcher := this.ohm.GetDefaultHandler() destination := session.Destination @@ -61,7 +61,7 @@ func (this *DefaultDispatcher) DispatchToOutbound(meta *proxy.InboundHandlerMeta } } - if meta.AllowPassiveConnection { + if session.Inbound != nil && session.Inbound.AllowPassiveConnection { go dispatcher.Dispatch(destination, alloc.NewLocalBuffer(32).Clear(), direct) } else { go this.FilterPacketAndDispatch(destination, direct, dispatcher) diff --git a/app/dispatcher/testing/dispatcher.go b/app/dispatcher/testing/dispatcher.go index b4035bf5..d194eae7 100644 --- a/app/dispatcher/testing/dispatcher.go +++ b/app/dispatcher/testing/dispatcher.go @@ -30,7 +30,7 @@ func NewTestPacketDispatcher(handler func(destination v2net.Destination, traffic } } -func (this *TestPacketDispatcher) DispatchToOutbound(meta *proxy.InboundHandlerMeta, session *proxy.SessionInfo) ray.InboundRay { +func (this *TestPacketDispatcher) DispatchToOutbound(session *proxy.SessionInfo) ray.InboundRay { traffic := ray.NewRay() this.Destination <- session.Destination go this.Handler(session.Destination, traffic) diff --git a/app/dns/nameserver.go b/app/dns/nameserver.go index b86daf49..e9766fda 100644 --- a/app/dns/nameserver.go +++ b/app/dns/nameserver.go @@ -50,11 +50,9 @@ type UDPNameServer struct { func NewUDPNameServer(address v2net.Destination, dispatcher dispatcher.PacketDispatcher) *UDPNameServer { s := &UDPNameServer{ - address: address, - requests: make(map[uint16]*PendingRequest), - udpServer: udp.NewUDPServer(&proxy.InboundHandlerMeta{ - AllowPassiveConnection: false, - }, dispatcher), + address: address, + requests: make(map[uint16]*PendingRequest), + udpServer: udp.NewUDPServer(dispatcher), } return s } diff --git a/proxy/dokodemo/dokodemo.go b/proxy/dokodemo/dokodemo.go index a1c28f04..00550312 100644 --- a/proxy/dokodemo/dokodemo.go +++ b/proxy/dokodemo/dokodemo.go @@ -90,7 +90,7 @@ func (this *DokodemoDoor) Start() error { } func (this *DokodemoDoor) ListenUDP() error { - this.udpServer = udp.NewUDPServer(this.meta, this.packetDispatcher) + this.udpServer = udp.NewUDPServer(this.packetDispatcher) udpHub, err := udp.ListenUDP( this.meta.Address, this.meta.Port, udp.ListenOption{ Callback: this.handleUDPPackets, @@ -114,6 +114,7 @@ func (this *DokodemoDoor) handleUDPPackets(payload *alloc.Buffer, session *proxy log.Info("Dokodemo: Unknown destination, stop forwarding...") return } + session.Inbound = this.meta this.udpServer.Dispatch(session, payload, this.handleUDPResponse) } @@ -160,9 +161,10 @@ func (this *DokodemoDoor) HandleTCPConnection(conn internet.Connection) { } log.Info("Dokodemo: Handling request to ", dest) - ray := this.packetDispatcher.DispatchToOutbound(this.meta, &proxy.SessionInfo{ + ray := this.packetDispatcher.DispatchToOutbound(&proxy.SessionInfo{ Source: v2net.DestinationFromAddr(conn.RemoteAddr()), Destination: dest, + Inbound: this.meta, }) defer ray.InboundOutput().Release() diff --git a/proxy/http/server.go b/proxy/http/server.go index 7564bb8f..ce26153b 100644 --- a/proxy/http/server.go +++ b/proxy/http/server.go @@ -124,6 +124,7 @@ func (this *Server) handleConnection(conn internet.Connection) { session := &proxy.SessionInfo{ Source: v2net.DestinationFromAddr(conn.RemoteAddr()), Destination: dest, + Inbound: this.meta, } if strings.ToUpper(request.Method) == "CONNECT" { this.handleConnect(request, session, reader, conn) @@ -146,7 +147,7 @@ func (this *Server) handleConnect(request *http.Request, session *proxy.SessionI } response.Write(writer) - ray := this.packetDispatcher.DispatchToOutbound(this.meta, session) + ray := this.packetDispatcher.DispatchToOutbound(session) this.transport(reader, writer, ray) } @@ -226,7 +227,7 @@ func (this *Server) handlePlainHTTP(request *http.Request, session *proxy.Sessio request.Host = request.URL.Host StripHopByHopHeaders(request) - ray := this.packetDispatcher.DispatchToOutbound(this.meta, session) + ray := this.packetDispatcher.DispatchToOutbound(session) defer ray.InboundInput().Close() defer ray.InboundOutput().Release() diff --git a/proxy/proxy.go b/proxy/proxy.go index cb399fd2..af2a2fcc 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -20,6 +20,7 @@ type SessionInfo struct { Source v2net.Destination Destination v2net.Destination User *protocol.User + Inbound *InboundHandlerMeta } type InboundHandlerMeta struct { diff --git a/proxy/shadowsocks/server.go b/proxy/shadowsocks/server.go index 49d542af..1cf81325 100644 --- a/proxy/shadowsocks/server.go +++ b/proxy/shadowsocks/server.go @@ -90,7 +90,7 @@ func (this *Server) Start() error { this.tcpHub = tcpHub if this.config.UdpEnabled { - this.udpServer = udp.NewUDPServer(this.meta, this.packetDispatcher) + this.udpServer = udp.NewUDPServer(this.packetDispatcher) udpHub, err := udp.ListenUDP(this.meta.Address, this.meta.Port, udp.ListenOption{Callback: this.handlerUDPPayload}) if err != nil { log.Error("Shadowsocks: Failed to listen UDP on ", this.meta.Address, ":", this.meta.Port, ": ", err) @@ -130,7 +130,7 @@ func (this *Server) handlerUDPPayload(payload *alloc.Buffer, session *proxy.Sess log.Access(source, dest, log.AccessAccepted, "") log.Info("Shadowsocks|Server: Tunnelling request to ", dest) - this.udpServer.Dispatch(&proxy.SessionInfo{Source: source, Destination: dest, User: request.User}, data, func(destination v2net.Destination, payload *alloc.Buffer) { + this.udpServer.Dispatch(&proxy.SessionInfo{Source: source, Destination: dest, User: request.User, Inbound: this.meta}, data, func(destination v2net.Destination, payload *alloc.Buffer) { defer payload.Release() data, err := EncodeUDPPacket(request, payload) @@ -171,10 +171,11 @@ func (this *Server) handleConnection(conn internet.Connection) { log.Access(conn.RemoteAddr(), dest, log.AccessAccepted, "") log.Info("Shadowsocks|Server: Tunnelling request to ", dest) - ray := this.packetDispatcher.DispatchToOutbound(this.meta, &proxy.SessionInfo{ + ray := this.packetDispatcher.DispatchToOutbound(&proxy.SessionInfo{ Source: v2net.DestinationFromAddr(conn.RemoteAddr()), Destination: dest, User: request.User, + Inbound: this.meta, }) defer ray.InboundOutput().Release() diff --git a/proxy/socks/server.go b/proxy/socks/server.go index ff23a4a2..2f880831 100644 --- a/proxy/socks/server.go +++ b/proxy/socks/server.go @@ -285,6 +285,7 @@ func (this *Server) handleSocks4(clientAddr v2net.Destination, reader *v2io.Buff session := &proxy.SessionInfo{ Source: clientAddr, Destination: dest, + Inbound: this.meta, } log.Access(clientAddr, dest, log.AccessAccepted, "") this.transport(reader, writer, session) @@ -292,7 +293,7 @@ func (this *Server) handleSocks4(clientAddr v2net.Destination, reader *v2io.Buff } func (this *Server) transport(reader io.Reader, writer io.Writer, session *proxy.SessionInfo) { - ray := this.packetDispatcher.DispatchToOutbound(this.meta, session) + ray := this.packetDispatcher.DispatchToOutbound(session) input := ray.InboundInput() output := ray.InboundOutput() diff --git a/proxy/socks/server_udp.go b/proxy/socks/server_udp.go index c8eb0fd4..56f3de10 100644 --- a/proxy/socks/server_udp.go +++ b/proxy/socks/server_udp.go @@ -10,7 +10,7 @@ import ( ) func (this *Server) listenUDP() error { - this.udpServer = udp.NewUDPServer(this.meta, this.packetDispatcher) + this.udpServer = udp.NewUDPServer(this.packetDispatcher) udpHub, err := udp.ListenUDP(this.meta.Address, this.meta.Port, udp.ListenOption{Callback: this.handleUDPPayload}) if err != nil { log.Error("Socks: Failed to listen on udp ", this.meta.Address, ":", this.meta.Port) @@ -46,7 +46,7 @@ func (this *Server) handleUDPPayload(payload *alloc.Buffer, session *proxy.Sessi log.Info("Socks: Send packet to ", request.Destination(), " with ", request.Data.Len(), " bytes") log.Access(source, request.Destination, log.AccessAccepted, "") - this.udpServer.Dispatch(&proxy.SessionInfo{Source: source, Destination: request.Destination()}, request.Data, func(destination v2net.Destination, payload *alloc.Buffer) { + this.udpServer.Dispatch(&proxy.SessionInfo{Source: source, Destination: request.Destination(), Inbound: this.meta}, request.Data, func(destination v2net.Destination, payload *alloc.Buffer) { response := &protocol.Socks5UDPRequest{ Fragment: 0, Address: request.Destination().Address, diff --git a/proxy/testing/mocks/inboundhandler.go b/proxy/testing/mocks/inboundhandler.go index 68141676..cd92c150 100644 --- a/proxy/testing/mocks/inboundhandler.go +++ b/proxy/testing/mocks/inboundhandler.go @@ -31,11 +31,12 @@ func (this *InboundConnectionHandler) Close() { } func (this *InboundConnectionHandler) Communicate(destination v2net.Destination) error { - ray := this.PacketDispatcher.DispatchToOutbound(&proxy.InboundHandlerMeta{ - AllowPassiveConnection: false, - }, &proxy.SessionInfo{ + ray := this.PacketDispatcher.DispatchToOutbound(&proxy.SessionInfo{ Source: v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(0)), Destination: destination, + Inbound: &proxy.InboundHandlerMeta{ + AllowPassiveConnection: false, + }, }) input := ray.InboundInput() diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index 1833c0e8..011c7b26 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -166,10 +166,11 @@ func (this *VMessInboundHandler) HandleConnection(connection internet.Connection connection.SetReusable(request.Option.Has(protocol.RequestOptionConnectionReuse)) - ray := this.packetDispatcher.DispatchToOutbound(this.meta, &proxy.SessionInfo{ + ray := this.packetDispatcher.DispatchToOutbound(&proxy.SessionInfo{ Source: v2net.DestinationFromAddr(connection.RemoteAddr()), Destination: request.Destination(), User: request.User, + Inbound: this.meta, }) input := ray.InboundInput() output := ray.InboundOutput() diff --git a/transport/internet/udp/udp_server.go b/transport/internet/udp/udp_server.go index eb2fc37a..219900af 100644 --- a/transport/internet/udp/udp_server.go +++ b/transport/internet/udp/udp_server.go @@ -96,14 +96,12 @@ type UDPServer struct { sync.RWMutex conns map[string]*TimedInboundRay packetDispatcher dispatcher.PacketDispatcher - meta *proxy.InboundHandlerMeta } -func NewUDPServer(meta *proxy.InboundHandlerMeta, packetDispatcher dispatcher.PacketDispatcher) *UDPServer { +func NewUDPServer(packetDispatcher dispatcher.PacketDispatcher) *UDPServer { return &UDPServer{ conns: make(map[string]*TimedInboundRay), packetDispatcher: packetDispatcher, - meta: meta, } } @@ -144,7 +142,7 @@ func (this *UDPServer) Dispatch(session *proxy.SessionInfo, payload *alloc.Buffe } log.Info("UDP Server: establishing new connection for ", destString) - inboundRay := this.packetDispatcher.DispatchToOutbound(this.meta, session) + inboundRay := this.packetDispatcher.DispatchToOutbound(session) timedInboundRay := NewTimedInboundRay(destString, inboundRay, this) outputStream := timedInboundRay.InboundInput() if outputStream != nil {