|
|
|
@ -5,16 +5,10 @@ import (
|
|
|
|
|
|
|
|
|
|
"v2ray.com/core/common/buf" |
|
|
|
|
"v2ray.com/core/common/net" |
|
|
|
|
"v2ray.com/core/common/protocol/udp" |
|
|
|
|
"v2ray.com/core/transport/internet" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// Payload represents a single UDP payload.
|
|
|
|
|
type Payload struct { |
|
|
|
|
Content *buf.Buffer |
|
|
|
|
Source net.Destination |
|
|
|
|
OriginalDestination net.Destination |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type HubOption func(h *Hub) |
|
|
|
|
|
|
|
|
|
func HubCapacity(capacity int) HubOption { |
|
|
|
@ -31,7 +25,7 @@ func HubReceiveOriginalDestination(r bool) HubOption {
|
|
|
|
|
|
|
|
|
|
type Hub struct { |
|
|
|
|
conn *net.UDPConn |
|
|
|
|
cache chan *Payload |
|
|
|
|
cache chan *udp.Packet |
|
|
|
|
capacity int |
|
|
|
|
recvOrigDest bool |
|
|
|
|
} |
|
|
|
@ -62,7 +56,7 @@ func ListenUDP(ctx context.Context, address net.Address, port net.Port, streamSe
|
|
|
|
|
} |
|
|
|
|
newError("listening UDP on ", address, ":", port).WriteToLog() |
|
|
|
|
hub.conn = udpConn.(*net.UDPConn) |
|
|
|
|
hub.cache = make(chan *Payload, hub.capacity) |
|
|
|
|
hub.cache = make(chan *udp.Packet, hub.capacity) |
|
|
|
|
|
|
|
|
|
go hub.start() |
|
|
|
|
return hub, nil |
|
|
|
@ -106,14 +100,14 @@ func (h *Hub) start() {
|
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
payload := &Payload{ |
|
|
|
|
Content: buffer, |
|
|
|
|
payload := &udp.Packet{ |
|
|
|
|
Payload: buffer, |
|
|
|
|
Source: net.UDPDestination(net.IPAddress(addr.IP), net.Port(addr.Port)), |
|
|
|
|
} |
|
|
|
|
if h.recvOrigDest && noob > 0 { |
|
|
|
|
payload.OriginalDestination = RetrieveOriginalDest(oobBytes[:noob]) |
|
|
|
|
if payload.OriginalDestination.IsValid() { |
|
|
|
|
newError("UDP original destination: ", payload.OriginalDestination).AtDebug().WriteToLog() |
|
|
|
|
payload.Target = RetrieveOriginalDest(oobBytes[:noob]) |
|
|
|
|
if payload.Target.IsValid() { |
|
|
|
|
newError("UDP original destination: ", payload.Target).AtDebug().WriteToLog() |
|
|
|
|
} else { |
|
|
|
|
newError("failed to read UDP original destination").WriteToLog() |
|
|
|
|
} |
|
|
|
@ -123,7 +117,7 @@ func (h *Hub) start() {
|
|
|
|
|
case c <- payload: |
|
|
|
|
default: |
|
|
|
|
buffer.Release() |
|
|
|
|
payload.Content = nil |
|
|
|
|
payload.Payload = nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
@ -134,6 +128,6 @@ func (h *Hub) Addr() net.Addr {
|
|
|
|
|
return h.conn.LocalAddr() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (h *Hub) Receive() <-chan *Payload { |
|
|
|
|
func (h *Hub) Receive() <-chan *udp.Packet { |
|
|
|
|
return h.cache |
|
|
|
|
} |
|
|
|
|