mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-02 07:26:24 -05:00
refine dispatcher
This commit is contained in:
parent
5dc05d6352
commit
9471b5b066
@ -12,5 +12,5 @@ const (
|
|||||||
|
|
||||||
// PacketDispatcher dispatch a packet and possibly further network payload to its destination.
|
// PacketDispatcher dispatch a packet and possibly further network payload to its destination.
|
||||||
type PacketDispatcher interface {
|
type PacketDispatcher interface {
|
||||||
DispatchToOutbound(meta *proxy.InboundHandlerMeta, session *proxy.SessionInfo) ray.InboundRay
|
DispatchToOutbound(session *proxy.SessionInfo) ray.InboundRay
|
||||||
}
|
}
|
||||||
|
@ -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()
|
direct := ray.NewRay()
|
||||||
dispatcher := this.ohm.GetDefaultHandler()
|
dispatcher := this.ohm.GetDefaultHandler()
|
||||||
destination := session.Destination
|
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)
|
go dispatcher.Dispatch(destination, alloc.NewLocalBuffer(32).Clear(), direct)
|
||||||
} else {
|
} else {
|
||||||
go this.FilterPacketAndDispatch(destination, direct, dispatcher)
|
go this.FilterPacketAndDispatch(destination, direct, dispatcher)
|
||||||
|
@ -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()
|
traffic := ray.NewRay()
|
||||||
this.Destination <- session.Destination
|
this.Destination <- session.Destination
|
||||||
go this.Handler(session.Destination, traffic)
|
go this.Handler(session.Destination, traffic)
|
||||||
|
@ -50,11 +50,9 @@ type UDPNameServer struct {
|
|||||||
|
|
||||||
func NewUDPNameServer(address v2net.Destination, dispatcher dispatcher.PacketDispatcher) *UDPNameServer {
|
func NewUDPNameServer(address v2net.Destination, dispatcher dispatcher.PacketDispatcher) *UDPNameServer {
|
||||||
s := &UDPNameServer{
|
s := &UDPNameServer{
|
||||||
address: address,
|
address: address,
|
||||||
requests: make(map[uint16]*PendingRequest),
|
requests: make(map[uint16]*PendingRequest),
|
||||||
udpServer: udp.NewUDPServer(&proxy.InboundHandlerMeta{
|
udpServer: udp.NewUDPServer(dispatcher),
|
||||||
AllowPassiveConnection: false,
|
|
||||||
}, dispatcher),
|
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
@ -90,7 +90,7 @@ func (this *DokodemoDoor) Start() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *DokodemoDoor) ListenUDP() error {
|
func (this *DokodemoDoor) ListenUDP() error {
|
||||||
this.udpServer = udp.NewUDPServer(this.meta, this.packetDispatcher)
|
this.udpServer = udp.NewUDPServer(this.packetDispatcher)
|
||||||
udpHub, err := udp.ListenUDP(
|
udpHub, err := udp.ListenUDP(
|
||||||
this.meta.Address, this.meta.Port, udp.ListenOption{
|
this.meta.Address, this.meta.Port, udp.ListenOption{
|
||||||
Callback: this.handleUDPPackets,
|
Callback: this.handleUDPPackets,
|
||||||
@ -114,6 +114,7 @@ func (this *DokodemoDoor) handleUDPPackets(payload *alloc.Buffer, session *proxy
|
|||||||
log.Info("Dokodemo: Unknown destination, stop forwarding...")
|
log.Info("Dokodemo: Unknown destination, stop forwarding...")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
session.Inbound = this.meta
|
||||||
this.udpServer.Dispatch(session, payload, this.handleUDPResponse)
|
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)
|
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()),
|
Source: v2net.DestinationFromAddr(conn.RemoteAddr()),
|
||||||
Destination: dest,
|
Destination: dest,
|
||||||
|
Inbound: this.meta,
|
||||||
})
|
})
|
||||||
defer ray.InboundOutput().Release()
|
defer ray.InboundOutput().Release()
|
||||||
|
|
||||||
|
@ -124,6 +124,7 @@ func (this *Server) handleConnection(conn internet.Connection) {
|
|||||||
session := &proxy.SessionInfo{
|
session := &proxy.SessionInfo{
|
||||||
Source: v2net.DestinationFromAddr(conn.RemoteAddr()),
|
Source: v2net.DestinationFromAddr(conn.RemoteAddr()),
|
||||||
Destination: dest,
|
Destination: dest,
|
||||||
|
Inbound: this.meta,
|
||||||
}
|
}
|
||||||
if strings.ToUpper(request.Method) == "CONNECT" {
|
if strings.ToUpper(request.Method) == "CONNECT" {
|
||||||
this.handleConnect(request, session, reader, conn)
|
this.handleConnect(request, session, reader, conn)
|
||||||
@ -146,7 +147,7 @@ func (this *Server) handleConnect(request *http.Request, session *proxy.SessionI
|
|||||||
}
|
}
|
||||||
response.Write(writer)
|
response.Write(writer)
|
||||||
|
|
||||||
ray := this.packetDispatcher.DispatchToOutbound(this.meta, session)
|
ray := this.packetDispatcher.DispatchToOutbound(session)
|
||||||
this.transport(reader, writer, ray)
|
this.transport(reader, writer, ray)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -226,7 +227,7 @@ func (this *Server) handlePlainHTTP(request *http.Request, session *proxy.Sessio
|
|||||||
request.Host = request.URL.Host
|
request.Host = request.URL.Host
|
||||||
StripHopByHopHeaders(request)
|
StripHopByHopHeaders(request)
|
||||||
|
|
||||||
ray := this.packetDispatcher.DispatchToOutbound(this.meta, session)
|
ray := this.packetDispatcher.DispatchToOutbound(session)
|
||||||
defer ray.InboundInput().Close()
|
defer ray.InboundInput().Close()
|
||||||
defer ray.InboundOutput().Release()
|
defer ray.InboundOutput().Release()
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@ type SessionInfo struct {
|
|||||||
Source v2net.Destination
|
Source v2net.Destination
|
||||||
Destination v2net.Destination
|
Destination v2net.Destination
|
||||||
User *protocol.User
|
User *protocol.User
|
||||||
|
Inbound *InboundHandlerMeta
|
||||||
}
|
}
|
||||||
|
|
||||||
type InboundHandlerMeta struct {
|
type InboundHandlerMeta struct {
|
||||||
|
@ -90,7 +90,7 @@ func (this *Server) Start() error {
|
|||||||
this.tcpHub = tcpHub
|
this.tcpHub = tcpHub
|
||||||
|
|
||||||
if this.config.UdpEnabled {
|
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})
|
udpHub, err := udp.ListenUDP(this.meta.Address, this.meta.Port, udp.ListenOption{Callback: this.handlerUDPPayload})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Shadowsocks: Failed to listen UDP on ", this.meta.Address, ":", this.meta.Port, ": ", err)
|
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.Access(source, dest, log.AccessAccepted, "")
|
||||||
log.Info("Shadowsocks|Server: Tunnelling request to ", dest)
|
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()
|
defer payload.Release()
|
||||||
|
|
||||||
data, err := EncodeUDPPacket(request, payload)
|
data, err := EncodeUDPPacket(request, payload)
|
||||||
@ -171,10 +171,11 @@ func (this *Server) handleConnection(conn internet.Connection) {
|
|||||||
log.Access(conn.RemoteAddr(), dest, log.AccessAccepted, "")
|
log.Access(conn.RemoteAddr(), dest, log.AccessAccepted, "")
|
||||||
log.Info("Shadowsocks|Server: Tunnelling request to ", dest)
|
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()),
|
Source: v2net.DestinationFromAddr(conn.RemoteAddr()),
|
||||||
Destination: dest,
|
Destination: dest,
|
||||||
User: request.User,
|
User: request.User,
|
||||||
|
Inbound: this.meta,
|
||||||
})
|
})
|
||||||
defer ray.InboundOutput().Release()
|
defer ray.InboundOutput().Release()
|
||||||
|
|
||||||
|
@ -285,6 +285,7 @@ func (this *Server) handleSocks4(clientAddr v2net.Destination, reader *v2io.Buff
|
|||||||
session := &proxy.SessionInfo{
|
session := &proxy.SessionInfo{
|
||||||
Source: clientAddr,
|
Source: clientAddr,
|
||||||
Destination: dest,
|
Destination: dest,
|
||||||
|
Inbound: this.meta,
|
||||||
}
|
}
|
||||||
log.Access(clientAddr, dest, log.AccessAccepted, "")
|
log.Access(clientAddr, dest, log.AccessAccepted, "")
|
||||||
this.transport(reader, writer, session)
|
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) {
|
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()
|
input := ray.InboundInput()
|
||||||
output := ray.InboundOutput()
|
output := ray.InboundOutput()
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (this *Server) listenUDP() error {
|
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})
|
udpHub, err := udp.ListenUDP(this.meta.Address, this.meta.Port, udp.ListenOption{Callback: this.handleUDPPayload})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Socks: Failed to listen on udp ", this.meta.Address, ":", this.meta.Port)
|
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.Info("Socks: Send packet to ", request.Destination(), " with ", request.Data.Len(), " bytes")
|
||||||
log.Access(source, request.Destination, log.AccessAccepted, "")
|
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{
|
response := &protocol.Socks5UDPRequest{
|
||||||
Fragment: 0,
|
Fragment: 0,
|
||||||
Address: request.Destination().Address,
|
Address: request.Destination().Address,
|
||||||
|
@ -31,11 +31,12 @@ func (this *InboundConnectionHandler) Close() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *InboundConnectionHandler) Communicate(destination v2net.Destination) error {
|
func (this *InboundConnectionHandler) Communicate(destination v2net.Destination) error {
|
||||||
ray := this.PacketDispatcher.DispatchToOutbound(&proxy.InboundHandlerMeta{
|
ray := this.PacketDispatcher.DispatchToOutbound(&proxy.SessionInfo{
|
||||||
AllowPassiveConnection: false,
|
|
||||||
}, &proxy.SessionInfo{
|
|
||||||
Source: v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(0)),
|
Source: v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(0)),
|
||||||
Destination: destination,
|
Destination: destination,
|
||||||
|
Inbound: &proxy.InboundHandlerMeta{
|
||||||
|
AllowPassiveConnection: false,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
input := ray.InboundInput()
|
input := ray.InboundInput()
|
||||||
|
@ -166,10 +166,11 @@ func (this *VMessInboundHandler) HandleConnection(connection internet.Connection
|
|||||||
|
|
||||||
connection.SetReusable(request.Option.Has(protocol.RequestOptionConnectionReuse))
|
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()),
|
Source: v2net.DestinationFromAddr(connection.RemoteAddr()),
|
||||||
Destination: request.Destination(),
|
Destination: request.Destination(),
|
||||||
User: request.User,
|
User: request.User,
|
||||||
|
Inbound: this.meta,
|
||||||
})
|
})
|
||||||
input := ray.InboundInput()
|
input := ray.InboundInput()
|
||||||
output := ray.InboundOutput()
|
output := ray.InboundOutput()
|
||||||
|
@ -96,14 +96,12 @@ type UDPServer struct {
|
|||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
conns map[string]*TimedInboundRay
|
conns map[string]*TimedInboundRay
|
||||||
packetDispatcher dispatcher.PacketDispatcher
|
packetDispatcher dispatcher.PacketDispatcher
|
||||||
meta *proxy.InboundHandlerMeta
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUDPServer(meta *proxy.InboundHandlerMeta, packetDispatcher dispatcher.PacketDispatcher) *UDPServer {
|
func NewUDPServer(packetDispatcher dispatcher.PacketDispatcher) *UDPServer {
|
||||||
return &UDPServer{
|
return &UDPServer{
|
||||||
conns: make(map[string]*TimedInboundRay),
|
conns: make(map[string]*TimedInboundRay),
|
||||||
packetDispatcher: packetDispatcher,
|
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)
|
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)
|
timedInboundRay := NewTimedInboundRay(destString, inboundRay, this)
|
||||||
outputStream := timedInboundRay.InboundInput()
|
outputStream := timedInboundRay.InboundInput()
|
||||||
if outputStream != nil {
|
if outputStream != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user