diff --git a/hub/route/server.go b/hub/route/server.go index 98dc3588..e01696be 100644 --- a/hub/route/server.go +++ b/hub/route/server.go @@ -3,6 +3,7 @@ package route import ( "bytes" "encoding/json" + "net" "net/http" "strings" "time" @@ -81,10 +82,15 @@ func Start(addr string, secret string) { }) } - log.Infoln("RESTful API listening at: %s", addr) - err := http.ListenAndServe(addr, r) + l, err := net.Listen("tcp", addr) if err != nil { - log.Errorln("External controller error: %s", err.Error()) + log.Errorln("External controller listen error: %s", err) + return + } + serverAddr = l.Addr().String() + log.Infoln("RESTful API listening at: %s", serverAddr) + if err = http.Serve(l, r); err != nil { + log.Errorln("External controller serve error: %s", err) } } diff --git a/listener/http/server.go b/listener/http/server.go index ed838371..6c5a7f6a 100644 --- a/listener/http/server.go +++ b/listener/http/server.go @@ -10,7 +10,6 @@ import ( type Listener struct { listener net.Listener - address string closed bool } @@ -31,7 +30,6 @@ func NewWithAuthenticate(addr string, in chan<- C.ConnContext, authenticate bool hl := &Listener{ listener: l, - address: addr, } go func() { for { @@ -55,5 +53,5 @@ func (l *Listener) Close() { } func (l *Listener) Address() string { - return l.address + return l.listener.Addr().String() } diff --git a/listener/mixed/mixed.go b/listener/mixed/mixed.go index 7d89e3c8..97be69f0 100644 --- a/listener/mixed/mixed.go +++ b/listener/mixed/mixed.go @@ -15,7 +15,6 @@ import ( type Listener struct { listener net.Listener - address string closed bool cache *cache.Cache } @@ -26,7 +25,10 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) { return nil, err } - ml := &Listener{l, addr, false, cache.New(30 * time.Second)} + ml := &Listener{ + listener: l, + cache: cache.New(30 * time.Second), + } go func() { for { c, err := ml.listener.Accept() @@ -49,7 +51,7 @@ func (l *Listener) Close() { } func (l *Listener) Address() string { - return l.address + return l.listener.Addr().String() } func handleConn(conn net.Conn, in chan<- C.ConnContext, cache *cache.Cache) { diff --git a/listener/redir/tcp.go b/listener/redir/tcp.go index 37268840..f7160eb0 100644 --- a/listener/redir/tcp.go +++ b/listener/redir/tcp.go @@ -9,7 +9,6 @@ import ( type Listener struct { listener net.Listener - address string closed bool } @@ -18,7 +17,9 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) { if err != nil { return nil, err } - rl := &Listener{l, addr, false} + rl := &Listener{ + listener: l, + } go func() { for { @@ -42,7 +43,7 @@ func (l *Listener) Close() { } func (l *Listener) Address() string { - return l.address + return l.listener.Addr().String() } func handleRedir(conn net.Conn, in chan<- C.ConnContext) { diff --git a/listener/socks/tcp.go b/listener/socks/tcp.go index 60da0e26..587af221 100644 --- a/listener/socks/tcp.go +++ b/listener/socks/tcp.go @@ -15,7 +15,6 @@ import ( type Listener struct { listener net.Listener - address string closed bool } @@ -25,7 +24,9 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) { return nil, err } - sl := &Listener{l, addr, false} + sl := &Listener{ + listener: l, + } go func() { for { c, err := l.Accept() @@ -48,7 +49,7 @@ func (l *Listener) Close() { } func (l *Listener) Address() string { - return l.address + return l.listener.Addr().String() } func handleSocks(conn net.Conn, in chan<- C.ConnContext) { diff --git a/listener/socks/udp.go b/listener/socks/udp.go index b98ae897..5de0a0bf 100644 --- a/listener/socks/udp.go +++ b/listener/socks/udp.go @@ -13,7 +13,6 @@ import ( type UDPListener struct { packetConn net.PacketConn - address string closed bool } @@ -27,7 +26,9 @@ func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error) log.Warnln("Failed to Reuse UDP Address: %s", err) } - sl := &UDPListener{l, addr, false} + sl := &UDPListener{ + packetConn: l, + } go func() { for { buf := pool.Get(pool.RelayBufferSize) @@ -52,7 +53,7 @@ func (l *UDPListener) Close() error { } func (l *UDPListener) Address() string { - return l.address + return l.packetConn.LocalAddr().String() } func handleSocksUDP(pc net.PacketConn, in chan<- *inbound.PacketAdapter, buf []byte, addr net.Addr) { diff --git a/listener/tproxy/tproxy.go b/listener/tproxy/tproxy.go index 142336fe..87f2d7d6 100644 --- a/listener/tproxy/tproxy.go +++ b/listener/tproxy/tproxy.go @@ -10,7 +10,6 @@ import ( type Listener struct { listener net.Listener - address string closed bool } @@ -33,7 +32,6 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) { rl := &Listener{ listener: l, - address: addr, } go func() { @@ -58,7 +56,7 @@ func (l *Listener) Close() { } func (l *Listener) Address() string { - return l.address + return l.listener.Addr().String() } func (l *Listener) handleTProxy(conn net.Conn, in chan<- C.ConnContext) { diff --git a/listener/tproxy/udp.go b/listener/tproxy/udp.go index 20c2e083..6f9de6f5 100644 --- a/listener/tproxy/udp.go +++ b/listener/tproxy/udp.go @@ -11,7 +11,6 @@ import ( type UDPListener struct { packetConn net.PacketConn - address string closed bool } @@ -21,7 +20,9 @@ func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error) return nil, err } - rl := &UDPListener{l, addr, false} + rl := &UDPListener{ + packetConn: l, + } c := l.(*net.UDPConn) @@ -65,7 +66,7 @@ func (l *UDPListener) Close() error { } func (l *UDPListener) Address() string { - return l.address + return l.packetConn.LocalAddr().String() } func handlePacketConn(pc net.PacketConn, in chan<- *inbound.PacketAdapter, buf []byte, lAddr *net.UDPAddr, rAddr *net.UDPAddr) {