From 461e0a68735ab1cf79f6f2160aa68eb2ff8402a5 Mon Sep 17 00:00:00 2001 From: oasiscifr <55550764+oasiscifr@users.noreply.github.com> Date: Sun, 13 Oct 2019 05:19:46 +0200 Subject: [PATCH] Fix: UDP socks recreate behavior (#355) --- proxy/listener.go | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/proxy/listener.go b/proxy/listener.go index 1966b0bf..9e7d0311 100644 --- a/proxy/listener.go +++ b/proxy/listener.go @@ -75,41 +75,37 @@ func ReCreateSocks(port int) error { addr := genAddr(bindAddress, port, allowLan) if socksListener != nil { - if socksListener.Address() == addr { - return nil + if socksListener.Address() != addr { + socksListener.Close() + socksListener = nil + } + } + + if socksUDPListener != nil { + if socksUDPListener.Address() != addr { + socksUDPListener.Close() + socksUDPListener = nil } - socksListener.Close() - socksListener = nil } if portIsZero(addr) { return nil } - var err error - socksListener, err = socks.NewSocksProxy(addr) + tcpListener, err := socks.NewSocksProxy(addr) if err != nil { return err } - return reCreateSocksUDP(addr) -} - -func reCreateSocksUDP(addr string) error { - if socksUDPListener != nil { - if socksUDPListener.Address() == addr { - return nil - } - socksUDPListener.Close() - socksUDPListener = nil - } - - var err error - socksUDPListener, err = socks.NewSocksUDPProxy(addr) + udpListener, err := socks.NewSocksUDPProxy(addr) if err != nil { + tcpListener.Close() return err } + socksListener = tcpListener + socksUDPListener = udpListener + return nil }