Skip to content

Commit

Permalink
fix: socks5 udp listen addr when omitting address & udp domain
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyxdd committed Jun 11, 2023
1 parent ca82106 commit da63981
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions app/socks5/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,11 @@ func (s *Server) handleUDP(c *net.TCPConn, r *socks5.Request) error {
s.UDPErrorFunc(c.RemoteAddr(), closeErr)
}()
// Start local UDP server
// Bind to the same address that the incoming TCP connection is sending to
udpConn, err := net.ListenUDP("udp", &net.UDPAddr{
IP: s.TCPAddr.IP,
Zone: s.TCPAddr.Zone,
IP: c.LocalAddr().(*net.TCPAddr).IP,
Zone: c.LocalAddr().(*net.TCPAddr).Zone,
Port: 0, // Random port
})
if err != nil {
_ = sendReply(c, socks5.RepServerFailure)
Expand Down Expand Up @@ -348,6 +350,9 @@ func (s *Server) udpServer(clientConn, localRelayConn *net.UDPConn, hyUDP cs.HyU
if err != nil {
continue
}
if atyp == socks5.ATYPDomain {
addr = addr[1:] // Remove the leading length byte
}
d := socks5.NewDatagram(atyp, addr, port, bs)
_, _ = clientConn.WriteToUDP(d.Bytes(), clientAddr)
}
Expand All @@ -362,6 +367,9 @@ func (s *Server) udpServer(clientConn, localRelayConn *net.UDPConn, hyUDP cs.HyU
if err != nil {
continue
}
if atyp == socks5.ATYPDomain {
addr = addr[1:] // Remove the leading length byte
}
d := socks5.NewDatagram(atyp, addr, port, buf[:n])
_, _ = clientConn.WriteToUDP(d.Bytes(), clientAddr)
}
Expand Down

0 comments on commit da63981

Please sign in to comment.