Skip to content

Commit

Permalink
Fix socks5 udp associate address
Browse files Browse the repository at this point in the history
  • Loading branch information
dyhkwong committed Apr 10, 2024
1 parent 2fa0399 commit 475d0e0
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions protocol/socks/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/auth"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
Expand Down Expand Up @@ -76,6 +78,9 @@ func ClientHandshake5(conn io.ReadWriter, command byte, destination M.Socksaddr,
} else if authResponse.Method != socks5.AuthTypeNotRequired {
return socks5.Response{}, E.New("socks5: unsupported auth method: ", authResponse.Method)
}
if command == socks5.CommandUDPAssociate {
destination = M.SocksaddrFrom(netip.IPv4Unspecified(), 0)
}
err = socks5.WriteRequest(conn, socks5.Request{
Command: command,
Destination: destination,
Expand Down Expand Up @@ -215,13 +220,23 @@ func HandleConnection0(ctx context.Context, conn net.Conn, version byte, authent
if err != nil {
return err
}
metadata.Protocol = "socks5"
metadata.Destination = request.Destination
var innerError error
done := make(chan struct{})
associatePacketConn := NewAssociatePacketConn(udpConn, request.Destination, conn)
var cachedConn N.PacketConn
buffer := buf.NewPacket()
defer buffer.Release()
destination, err := associatePacketConn.ReadPacket(buffer)
request.Destination = destination
if !buffer.IsEmpty() {
cachedConn = bufio.NewCachedPacketConn(associatePacketConn, buffer, destination)
} else {
return err
}
metadata.Protocol = "socks5"
metadata.Destination = request.Destination
go func() {
innerError = handler.NewPacketConnection(ctx, associatePacketConn, metadata)
innerError = handler.NewPacketConnection(ctx, cachedConn, metadata)
close(done)
}()
err = common.Error(io.Copy(io.Discard, conn))
Expand Down

0 comments on commit 475d0e0

Please sign in to comment.