From 0ea4a8abf30c3147443c625d8cad5b49c21e860a Mon Sep 17 00:00:00 2001 From: fregie Date: Fri, 17 May 2024 10:58:16 +0800 Subject: [PATCH] fix: bindIP not work on udp --- handle.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/handle.go b/handle.go index e00ca85..a38f612 100644 --- a/handle.go +++ b/handle.go @@ -175,7 +175,18 @@ func (sf *Server) handleAssociate(ctx context.Context, writer io.Writer, request return net.Dial(net_, addr) } } - bindLn, err := net.ListenUDP("udp", nil) + var udpAddr *net.UDPAddr + if sf.bindIP != nil { + var err error + udpAddr, err = net.ResolveUDPAddr("udp", sf.bindIP.String()+":0") + if err != nil { + if err := SendReply(writer, statute.RepServerFailure, nil); err != nil { + return fmt.Errorf("failed to send reply, %v", err) + } + return fmt.Errorf("resolve udp addr failed, %v", err) + } + } + bindLn, err := net.ListenUDP("udp", udpAddr) if err != nil { if err := SendReply(writer, statute.RepServerFailure, nil); err != nil { return fmt.Errorf("failed to send reply, %v", err)