@@ -25,17 +25,19 @@ import (
2525)
2626
2727type UDPForwarder struct {
28- ctx context.Context
29- stack * stack.Stack
30- handler Handler
31- udpNat * udpnat.Service
28+ ctx context.Context
29+ stack * stack.Stack
30+ ethernet bool
31+ handler Handler
32+ udpNat * udpnat.Service
3233}
3334
34- func NewUDPForwarder (ctx context.Context , stack * stack.Stack , handler Handler , timeout time.Duration ) * UDPForwarder {
35+ func NewUDPForwarder (ctx context.Context , stack * stack.Stack , ethernet bool , handler Handler , timeout time.Duration ) * UDPForwarder {
3536 forwarder := & UDPForwarder {
36- ctx : ctx ,
37- stack : stack ,
38- handler : handler ,
37+ ctx : ctx ,
38+ stack : stack ,
39+ ethernet : ethernet ,
40+ handler : handler ,
3941 }
4042 forwarder .udpNat = udpnat .New (handler , forwarder .PreparePacketConnection , timeout , true )
4143 return forwarder
@@ -77,16 +79,21 @@ func (f *UDPForwarder) PreparePacketConnection(source M.Socksaddr, destination M
7779 sourcePort : source .Port ,
7880 sourceNetwork : sourceNetwork ,
7981 }
82+ if f .ethernet {
83+ ethHdr := header .Ethernet (userData .(* stack.PacketBuffer ).LinkHeader ().Slice ())
84+ writer .linkDestination = ethHdr .DestinationAddress ()
85+ }
8086 return true , f .ctx , writer , nil
8187}
8288
8389type UDPBackWriter struct {
84- access sync.Mutex
85- stack * stack.Stack
86- packet * stack.PacketBuffer
87- source tcpip.Address
88- sourcePort uint16
89- sourceNetwork tcpip.NetworkProtocolNumber
90+ access sync.Mutex
91+ stack * stack.Stack
92+ packet * stack.PacketBuffer
93+ source tcpip.Address
94+ sourcePort uint16
95+ sourceNetwork tcpip.NetworkProtocolNumber
96+ linkDestination tcpip.LinkAddress
9097}
9198
9299func (w * UDPBackWriter ) HandshakeSuccess () error {
@@ -139,7 +146,9 @@ func (w *UDPBackWriter) WritePacket(packetBuffer *buf.Buffer, destination M.Sock
139146 Payload : buffer .MakeWithData (packetBuffer .Bytes ()),
140147 })
141148 defer packet .DecRef ()
142-
149+ if w .linkDestination != "" {
150+ route .ResolveWith (w .linkDestination )
151+ }
143152 packet .TransportProtocolNumber = header .UDPProtocolNumber
144153 udpHdr := header .UDP (packet .TransportHeader ().Push (header .UDPMinimumSize ))
145154 pLen := uint16 (packet .Size ())
0 commit comments