diff --git a/dns/server.go b/dns/server.go index 7abdd4d0..49d4541a 100644 --- a/dns/server.go +++ b/dns/server.go @@ -47,11 +47,11 @@ func (s *Server) setHandler(handler handler) { s.handler = handler } -func ReCreateServer(addr string, resolver *Resolver, mapper *ResolverEnhancer) error { +func ReCreateServer(addr string, resolver *Resolver, mapper *ResolverEnhancer) { if addr == address && resolver != nil { handler := newHandler(resolver, mapper) server.setHandler(handler) - return nil + return } if server.Server != nil { @@ -60,24 +60,33 @@ func ReCreateServer(addr string, resolver *Resolver, mapper *ResolverEnhancer) e address = "" } + var err error + defer func() { + if err != nil { + log.Errorln("Start DNS server error: %s", err.Error()) + } + }() + _, port, err := net.SplitHostPort(addr) if port == "0" || port == "" || err != nil { - return nil + return } udpAddr, err := net.ResolveUDPAddr("udp", addr) if err != nil { - return err + return } p, err := net.ListenUDP("udp", udpAddr) if err != nil { - return err + return } err = sockopt.UDPReuseaddr(p) if err != nil { log.Warnln("Failed to Reuse UDP Address: %s", err) + + err = nil } address = addr @@ -88,5 +97,6 @@ func ReCreateServer(addr string, resolver *Resolver, mapper *ResolverEnhancer) e go func() { server.ActivateAndServe() }() - return nil + + log.Infoln("DNS server listening at: %s", p.LocalAddr().String()) } diff --git a/hub/executor/executor.go b/hub/executor/executor.go index 59f94881..1405e81b 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -141,14 +141,7 @@ func updateDNS(c *config.DNS) { resolver.DefaultResolver = r resolver.DefaultHostMapper = m - if err := dns.ReCreateServer(c.Listen, r, m); err != nil { - log.Errorln("Start DNS server error: %s", err.Error()) - return - } - - if c.Listen != "" { - log.Infoln("DNS server listening at: %s", c.Listen) - } + dns.ReCreateServer(c.Listen, r, m) } func updateHosts(tree *trie.DomainTrie) { @@ -185,25 +178,11 @@ func updateGeneral(general *config.General, force bool) { tcpIn := tunnel.TCPIn() udpIn := tunnel.UDPIn() - if err := P.ReCreateHTTP(general.Port, tcpIn); err != nil { - log.Errorln("Start HTTP server error: %s", err.Error()) - } - - if err := P.ReCreateSocks(general.SocksPort, tcpIn, udpIn); err != nil { - log.Errorln("Start SOCKS server error: %s", err.Error()) - } - - if err := P.ReCreateRedir(general.RedirPort, tcpIn, udpIn); err != nil { - log.Errorln("Start Redir server error: %s", err.Error()) - } - - if err := P.ReCreateTProxy(general.TProxyPort, tcpIn, udpIn); err != nil { - log.Errorln("Start TProxy server error: %s", err.Error()) - } - - if err := P.ReCreateMixed(general.MixedPort, tcpIn, udpIn); err != nil { - log.Errorln("Start Mixed(http and socks) server error: %s", err.Error()) - } + P.ReCreateHTTP(general.Port, tcpIn) + P.ReCreateSocks(general.SocksPort, tcpIn, udpIn) + P.ReCreateRedir(general.RedirPort, tcpIn, udpIn) + P.ReCreateTProxy(general.TProxyPort, tcpIn, udpIn) + P.ReCreateMixed(general.MixedPort, tcpIn, udpIn) } func updateUsers(users []auth.AuthUser) { diff --git a/listener/listener.go b/listener/listener.go index d20af99d..b8e54b95 100644 --- a/listener/listener.go +++ b/listener/listener.go @@ -62,38 +62,50 @@ func SetBindAddress(host string) { bindAddress = host } -func ReCreateHTTP(port int, tcpIn chan<- C.ConnContext) error { +func ReCreateHTTP(port int, tcpIn chan<- C.ConnContext) { httpMux.Lock() defer httpMux.Unlock() + var err error + defer func() { + if err != nil { + log.Errorln("Start HTTP server error: %s", err.Error()) + } + }() + addr := genAddr(bindAddress, port, allowLan) if httpListener != nil { if httpListener.RawAddress() == addr { - return nil + return } httpListener.Close() httpListener = nil } if portIsZero(addr) { - return nil + return } - var err error httpListener, err = http.New(addr, tcpIn) if err != nil { - return err + return } log.Infoln("HTTP proxy listening at: %s", httpListener.Address()) - return nil } -func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) error { +func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) { socksMux.Lock() defer socksMux.Unlock() + var err error + defer func() { + if err != nil { + log.Errorln("Start SOCKS server error: %s", err.Error()) + } + }() + addr := genAddr(bindAddress, port, allowLan) shouldTCPIgnore := false @@ -118,40 +130,46 @@ func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P } if shouldTCPIgnore && shouldUDPIgnore { - return nil + return } if portIsZero(addr) { - return nil + return } tcpListener, err := socks.New(addr, tcpIn) if err != nil { - return err + return } udpListener, err := socks.NewUDP(addr, udpIn) if err != nil { tcpListener.Close() - return err + return } socksListener = tcpListener socksUDPListener = udpListener log.Infoln("SOCKS proxy listening at: %s", socksListener.Address()) - return nil } -func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) error { +func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) { redirMux.Lock() defer redirMux.Unlock() + var err error + defer func() { + if err != nil { + log.Errorln("Start Redir server error: %s", err.Error()) + } + }() + addr := genAddr(bindAddress, port, allowLan) if redirListener != nil { if redirListener.RawAddress() == addr { - return nil + return } redirListener.Close() redirListener = nil @@ -159,20 +177,19 @@ func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P if redirUDPListener != nil { if redirUDPListener.RawAddress() == addr { - return nil + return } redirUDPListener.Close() redirUDPListener = nil } if portIsZero(addr) { - return nil + return } - var err error redirListener, err = redir.New(addr, tcpIn) if err != nil { - return err + return } redirUDPListener, err = tproxy.NewUDP(addr, udpIn) @@ -181,18 +198,24 @@ func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P } log.Infoln("Redirect proxy listening at: %s", redirListener.Address()) - return nil } -func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) error { +func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) { tproxyMux.Lock() defer tproxyMux.Unlock() + var err error + defer func() { + if err != nil { + log.Errorln("Start TProxy server error: %s", err.Error()) + } + }() + addr := genAddr(bindAddress, port, allowLan) if tproxyListener != nil { if tproxyListener.RawAddress() == addr { - return nil + return } tproxyListener.Close() tproxyListener = nil @@ -200,20 +223,19 @@ func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound. if tproxyUDPListener != nil { if tproxyUDPListener.RawAddress() == addr { - return nil + return } tproxyUDPListener.Close() tproxyUDPListener = nil } if portIsZero(addr) { - return nil + return } - var err error tproxyListener, err = tproxy.New(addr, tcpIn) if err != nil { - return err + return } tproxyUDPListener, err = tproxy.NewUDP(addr, udpIn) @@ -222,13 +244,19 @@ func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound. } log.Infoln("TProxy server listening at: %s", tproxyListener.Address()) - return nil } -func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) error { +func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) { mixedMux.Lock() defer mixedMux.Unlock() + var err error + defer func() { + if err != nil { + log.Errorln("Start Mixed(http+socks) server error: %s", err.Error()) + } + }() + addr := genAddr(bindAddress, port, allowLan) shouldTCPIgnore := false @@ -252,27 +280,25 @@ func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P } if shouldTCPIgnore && shouldUDPIgnore { - return nil + return } if portIsZero(addr) { - return nil + return } - var err error mixedListener, err = mixed.New(addr, tcpIn) if err != nil { - return err + return } mixedUDPLister, err = socks.NewUDP(addr, udpIn) if err != nil { mixedListener.Close() - return err + return } log.Infoln("Mixed(http+socks) proxy listening at: %s", mixedListener.Address()) - return nil } // GetPorts return the ports of proxy servers