Skip to content

Commit 65cc5ef

Browse files
author
kix
committed
fix(control): add graceful fallback for UDP port conflicts
- Add graceful fallback when UDP connection creation fails - Use listening connection directly when address is already in use - Improve error handling and logging for better debugging - Maintain backward compatibility with existing functionality
1 parent cec1642 commit 65cc5ef

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

control/udp.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,21 @@ func ChooseNatTimeout(data []byte, sniffDns bool) (dmsg *dnsmessage.Msg, timeout
5555
func sendPkt(log *logrus.Logger, data []byte, from netip.AddrPort, realTo, to netip.AddrPort, lConn *net.UDPConn) (err error) {
5656
uConn, _, err := DefaultAnyfromPool.GetOrCreate(from.String(), AnyfromTimeout)
5757
if err != nil {
58-
return fmt.Errorf("failed to get UDP connection from %s to %s: %w", from, realTo, err)
58+
// If we can't create a new connection (e.g., address already in use),
59+
// fallback to using the listening connection directly
60+
if log.IsLevelEnabled(logrus.DebugLevel) {
61+
log.WithError(err).WithFields(logrus.Fields{
62+
"from": from.String(),
63+
"to": realTo.String(),
64+
}).Debug("failed to create UDP connection, using fallback")
65+
}
66+
67+
// Fallback: send directly through the listening connection
68+
_, err = lConn.WriteToUDPAddrPort(data, realTo)
69+
if err != nil {
70+
return fmt.Errorf("failed to write UDP packet via fallback to %s: %w", realTo, err)
71+
}
72+
return nil
5973
}
6074
_, err = uConn.WriteToUDPAddrPort(data, realTo)
6175
if err != nil {

0 commit comments

Comments
 (0)