Skip to content

Commit 8891a29

Browse files
committed
Fix socks5 udp associate address
1 parent 2fa0399 commit 8891a29

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

protocol/socks/handshake.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99

1010
"github.com/sagernet/sing/common"
1111
"github.com/sagernet/sing/common/auth"
12+
"github.com/sagernet/sing/common/buf"
13+
"github.com/sagernet/sing/common/bufio"
1214
E "github.com/sagernet/sing/common/exceptions"
1315
M "github.com/sagernet/sing/common/metadata"
1416
N "github.com/sagernet/sing/common/network"
@@ -76,6 +78,9 @@ func ClientHandshake5(conn io.ReadWriter, command byte, destination M.Socksaddr,
7678
} else if authResponse.Method != socks5.AuthTypeNotRequired {
7779
return socks5.Response{}, E.New("socks5: unsupported auth method: ", authResponse.Method)
7880
}
81+
if command == socks5.CommandUDPAssociate {
82+
destination = M.SocksaddrFrom(netip.IPv4Unspecified(), 0)
83+
}
7984
err = socks5.WriteRequest(conn, socks5.Request{
8085
Command: command,
8186
Destination: destination,
@@ -215,13 +220,20 @@ func HandleConnection0(ctx context.Context, conn net.Conn, version byte, authent
215220
if err != nil {
216221
return err
217222
}
218-
metadata.Protocol = "socks5"
219-
metadata.Destination = request.Destination
220223
var innerError error
221224
done := make(chan struct{})
222225
associatePacketConn := NewAssociatePacketConn(udpConn, request.Destination, conn)
226+
buffer := buf.NewPacket()
227+
defer buffer.Release()
228+
destination, err := associatePacketConn.ReadPacket(buffer)
229+
if buffer.IsEmpty() {
230+
return err
231+
}
232+
request.Destination = destination
233+
metadata.Protocol = "socks5"
234+
metadata.Destination = request.Destination
223235
go func() {
224-
innerError = handler.NewPacketConnection(ctx, associatePacketConn, metadata)
236+
innerError = handler.NewPacketConnection(ctx, bufio.NewCachedPacketConn(associatePacketConn, buffer, destination), metadata)
225237
close(done)
226238
}()
227239
err = common.Error(io.Copy(io.Discard, conn))

0 commit comments

Comments
 (0)