From ecac8eb8e5f42721e6253b154d533e2e74eb4d74 Mon Sep 17 00:00:00 2001 From: Dreamacro <305009791@qq.com> Date: Sun, 7 Jun 2020 17:56:21 +0800 Subject: [PATCH] Fix: add lock for inbound proxy recreate --- proxy/listener.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/proxy/listener.go b/proxy/listener.go index 5eda5426..88d0f522 100644 --- a/proxy/listener.go +++ b/proxy/listener.go @@ -4,6 +4,7 @@ import ( "fmt" "net" "strconv" + "sync" "github.com/Dreamacro/clash/log" "github.com/Dreamacro/clash/proxy/http" @@ -23,6 +24,13 @@ var ( redirUDPListener *redir.RedirUDPListener mixedListener *mixed.MixedListener mixedUDPLister *socks.SockUDPListener + + // lock for recreate function + socksMux sync.Mutex + httpMux sync.Mutex + redirMux sync.Mutex + mixedMux sync.Mutex + tunMux sync.Mutex ) type listener interface { @@ -54,6 +62,9 @@ func SetBindAddress(host string) { } func ReCreateHTTP(port int) error { + httpMux.Lock() + defer httpMux.Unlock() + addr := genAddr(bindAddress, port, allowLan) if httpListener != nil { @@ -78,6 +89,9 @@ func ReCreateHTTP(port int) error { } func ReCreateSocks(port int) error { + socksMux.Lock() + defer socksMux.Unlock() + addr := genAddr(bindAddress, port, allowLan) shouldTCPIgnore := false @@ -127,6 +141,9 @@ func ReCreateSocks(port int) error { } func ReCreateRedir(port int) error { + redirMux.Lock() + defer redirMux.Unlock() + addr := genAddr(bindAddress, port, allowLan) if redirListener != nil { @@ -164,6 +181,9 @@ func ReCreateRedir(port int) error { } func ReCreateMixed(port int) error { + mixedMux.Lock() + defer mixedMux.Unlock() + addr := genAddr(bindAddress, port, allowLan) shouldTCPIgnore := false