diff --git a/constant/listener.go b/constant/listener.go index 4edae67c4..c5e327ece 100644 --- a/constant/listener.go +++ b/constant/listener.go @@ -16,7 +16,7 @@ type AdvanceListener interface { type NewListener interface { Name() string - ReCreate(tcpIn chan<- ConnContext, udpIn chan<- PacketAdapter) error + Listen(tcpIn chan<- ConnContext, udpIn chan<- PacketAdapter) error Close() error Address() string RawAddress() string diff --git a/hub/executor/executor.go b/hub/executor/executor.go index e1ac7f6e1..9a1003efa 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -138,11 +138,16 @@ func GetGeneral() *config.General { func updateListeners(listeners map[string]C.NewListener) { tcpIn := tunnel.TCPIn() udpIn := tunnel.UDPIn() + for _, listener := range tunnel.Listeners() { + _ = listener.Close() + } + for _, listener := range listeners { - if err := listener.ReCreate(tcpIn, udpIn); err != nil { + if err := listener.Listen(tcpIn, udpIn); err != nil { log.Errorln("Listener %s listen err: %s", listener.Name(), err.Error()) } } + tunnel.UpdateListeners(listeners) } func updateExperimental(c *config.Config) { diff --git a/listener/inbound/base.go b/listener/inbound/base.go index a00ec5e66..100088435 100644 --- a/listener/inbound/base.go +++ b/listener/inbound/base.go @@ -52,7 +52,7 @@ func (b *Base) RawAddress() string { } // ReCreate implements constant.NewListener -func (*Base) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { +func (*Base) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { return nil } diff --git a/listener/inbound/http.go b/listener/inbound/http.go index 3b3de1937..1092d2f92 100644 --- a/listener/inbound/http.go +++ b/listener/inbound/http.go @@ -30,9 +30,8 @@ func (h *HTTP) Address() string { } // ReCreate implements constant.NewListener -func (h *HTTP) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { +func (h *HTTP) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { var err error - _ = h.Close() h.l, err = http.NewWithInfos(h.RawAddress(), h.name, h.preferRulesName, tcpIn) if err != nil { return err diff --git a/listener/inbound/mixed.go b/listener/inbound/mixed.go index 087c69fe8..949064fe5 100644 --- a/listener/inbound/mixed.go +++ b/listener/inbound/mixed.go @@ -39,9 +39,8 @@ func (m *Mixed) Address() string { } // ReCreate implements constant.NewListener -func (m *Mixed) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { +func (m *Mixed) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { var err error - _ = m.Close() m.l, err = mixed.NewWithInfos(m.RawAddress(), m.name, m.preferRulesName, tcpIn) if err != nil { return err diff --git a/listener/inbound/redir.go b/listener/inbound/redir.go index a58644646..029e103ab 100644 --- a/listener/inbound/redir.go +++ b/listener/inbound/redir.go @@ -31,9 +31,8 @@ func (r *Redir) Address() string { } // ReCreate implements constant.NewListener -func (r *Redir) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { +func (r *Redir) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { var err error - _ = r.Close() r.l, err = redir.NewWithInfos(r.Address(), r.name, r.preferRulesName, tcpIn) if err != nil { return err diff --git a/listener/inbound/socks.go b/listener/inbound/socks.go index a1b44a243..1aa3c23ff 100644 --- a/listener/inbound/socks.go +++ b/listener/inbound/socks.go @@ -2,8 +2,6 @@ package inbound import ( "fmt" - "sync" - C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/listener/socks" "github.com/Dreamacro/clash/log" @@ -16,7 +14,6 @@ type SocksOption struct { type Socks struct { *Base - mux sync.Mutex udp bool stl *socks.Listener sul *socks.UDPListener @@ -60,11 +57,8 @@ func (s *Socks) Address() string { } // ReCreate implements constant.NewListener -func (s *Socks) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { - s.mux.Lock() - defer s.mux.Unlock() +func (s *Socks) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { var err error - _ = s.Close() if s.stl, err = socks.NewWithInfos(s.RawAddress(), s.name, s.preferRulesName, tcpIn); err != nil { return err } diff --git a/listener/inbound/tproxy.go b/listener/inbound/tproxy.go index 396117d22..00d9c555c 100644 --- a/listener/inbound/tproxy.go +++ b/listener/inbound/tproxy.go @@ -38,9 +38,8 @@ func (t *TProxy) Address() string { } // ReCreate implements constant.NewListener -func (t *TProxy) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { +func (t *TProxy) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { var err error - _ = t.Close() t.lTCP, err = tproxy.NewWithInfos(t.RawAddress(), t.name, t.preferRulesName, tcpIn) if err != nil { return err diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index 99fcb33b8..2c8d37e0e 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -29,6 +29,7 @@ var ( udpQueue = make(chan C.PacketAdapter, 200) natTable = nat.New() rules []C.Rule + listeners = make(map[string]C.NewListener) subRules map[string][]C.Rule proxies = make(map[string]C.Proxy) providers map[string]provider.ProxyProvider @@ -86,6 +87,9 @@ func Rules() []C.Rule { return rules } +func Listeners()map[string]C.NewListener{ + return listeners +} // UpdateRules handle update rules func UpdateRules(newRules []C.Rule, newSubRule map[string][]C.Rule, rp map[string]provider.RuleProvider) { configMux.Lock() @@ -118,6 +122,12 @@ func UpdateProxies(newProxies map[string]C.Proxy, newProviders map[string]provid configMux.Unlock() } +func UpdateListeners(newListeners map[string]C.NewListener) { + configMux.Lock() + defer configMux.Unlock() + listeners=newListeners +} + func UpdateSniffer(dispatcher *sniffer.SnifferDispatcher) { configMux.Lock() sniffer.Dispatcher = dispatcher