Skip to content

Commit b077e55

Browse files
committed
Only resolve the net.Addr if it's not already a UDPAddr.
1 parent 07c1c41 commit b077e55

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

service/udp.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func (h *associationHandler) HandleAssociation(ctx context.Context, clientConn n
165165

166166
connError := func() *onet.ConnectionError {
167167
var payload []byte
168-
var tgtAddr net.Addr
168+
var tgtAddr *net.UDPAddr
169169
if targetConn == nil {
170170
ip := clientConn.RemoteAddr().(*net.UDPAddr).AddrPort().Addr()
171171
var textData []byte
@@ -211,7 +211,7 @@ func (h *associationHandler) HandleAssociation(ctx context.Context, clientConn n
211211
}
212212

213213
debugUDP(l, "Proxy exit.")
214-
proxyTargetBytes, err = targetConn.WriteTo(payload, tgtAddr)
214+
proxyTargetBytes, err = targetConn.WriteTo(payload, tgtAddr) // accept only `net.UDPAddr` despite the signature
215215
if err != nil {
216216
return ensureConnectionError(err, "ERR_WRITE", "Failed to write to target")
217217
}
@@ -229,7 +229,7 @@ func (h *associationHandler) HandleAssociation(ctx context.Context, clientConn n
229229

230230
// extractPayloadAndDestination processes a decrypted Shadowsocks UDP packet and
231231
// extracts the payload data and destination address.
232-
func (h *associationHandler) extractPayloadAndDestination(textData []byte) ([]byte, net.Addr, *onet.ConnectionError) {
232+
func (h *associationHandler) extractPayloadAndDestination(textData []byte) ([]byte, *net.UDPAddr, *onet.ConnectionError) {
233233
tgtAddr := socks.SplitAddr(textData)
234234
if tgtAddr == nil {
235235
return nil, nil, onet.NewConnectionError("ERR_READ_ADDRESS", "Failed to get target address", nil)
@@ -391,14 +391,22 @@ type validatingPacketConn struct {
391391
}
392392

393393
func (vpc *validatingPacketConn) WriteTo(p []byte, addr net.Addr) (int, error) {
394-
udpAddr, err := net.ResolveUDPAddr("udp", addr.String())
395-
if err != nil {
396-
return 0, fmt.Errorf("failed to resolve target address %v", udpAddr)
394+
var (
395+
udpAddr *net.UDPAddr
396+
ok bool
397+
)
398+
if udpAddr, ok = addr.(*net.UDPAddr); !ok {
399+
var err error
400+
udpAddr, err = net.ResolveUDPAddr("udp", addr.String())
401+
if err != nil {
402+
return 0, fmt.Errorf("failed to resolve target address %v", addr)
403+
}
397404
}
398405
if err := vpc.targetIPValidator(udpAddr.IP); err != nil {
399406
return 0, ensureConnectionError(err, "ERR_ADDRESS_INVALID", "invalid address")
400407
}
401-
return vpc.PacketConn.WriteTo(p, addr)
408+
409+
return vpc.PacketConn.WriteTo(p, udpAddr) // accept only `net.UDPAddr` despite the signature
402410
}
403411

404412
type timedPacketConn struct {

0 commit comments

Comments
 (0)