Fix: goroutine leak while closing proxy (#43)

* Fix: goroutine leak while closing proxy

* Chore: improve proxy architecture

* Fix: stack overflow
This commit is contained in:
gVisor bot 2018-11-22 11:54:01 +08:00
parent 35b40e77a3
commit 913111dc2e
4 changed files with 79 additions and 74 deletions

View file

@ -14,21 +14,25 @@ var (
tun = tunnel.Instance() tun = tunnel.Instance()
) )
func NewHttpProxy(addr string) (chan<- struct{}, <-chan struct{}, error) { type httpListener struct {
net.Listener
address string
closed bool
}
func NewHttpProxy(addr string) (*httpListener, error) {
l, err := net.Listen("tcp", addr) l, err := net.Listen("tcp", addr)
if err != nil { if err != nil {
return nil, nil, err return nil, err
} }
hl := &httpListener{l, addr, false}
done := make(chan struct{})
closed := make(chan struct{})
go func() { go func() {
log.Infoln("HTTP proxy listening at: %s", addr) log.Infoln("HTTP proxy listening at: %s", addr)
for { for {
c, err := l.Accept() c, err := hl.Accept()
if err != nil { if err != nil {
if _, open := <-done; !open { if hl.closed {
break break
} }
continue continue
@ -37,13 +41,16 @@ func NewHttpProxy(addr string) (chan<- struct{}, <-chan struct{}, error) {
} }
}() }()
go func() { return hl, nil
<-done }
l.Close()
closed <- struct{}{}
}()
return done, closed, nil func (l *httpListener) Close() {
l.closed = true
l.Listener.Close()
}
func (l *httpListener) Address() string {
return l.address
} }
func handleConn(conn net.Conn) { func handleConn(conn net.Conn) {

View file

@ -13,15 +13,14 @@ import (
var ( var (
allowLan = false allowLan = false
socksListener *listener socksListener listener
httpListener *listener httpListener listener
redirListener *listener redirListener listener
) )
type listener struct { type listener interface {
Address string Close()
Done chan<- struct{} Address() string
Closed <-chan struct{}
} }
type Ports struct { type Ports struct {
@ -42,11 +41,10 @@ func ReCreateHTTP(port int) error {
addr := genAddr(port, allowLan) addr := genAddr(port, allowLan)
if httpListener != nil { if httpListener != nil {
if httpListener.Address == addr { if httpListener.Address() == addr {
return nil return nil
} }
httpListener.Done <- struct{}{} httpListener.Close()
<-httpListener.Closed
httpListener = nil httpListener = nil
} }
@ -54,16 +52,12 @@ func ReCreateHTTP(port int) error {
return nil return nil
} }
done, closed, err := http.NewHttpProxy(addr) var err error
httpListener, err = http.NewHttpProxy(addr)
if err != nil { if err != nil {
return err return err
} }
httpListener = &listener{
Address: addr,
Done: done,
Closed: closed,
}
return nil return nil
} }
@ -71,11 +65,10 @@ func ReCreateSocks(port int) error {
addr := genAddr(port, allowLan) addr := genAddr(port, allowLan)
if socksListener != nil { if socksListener != nil {
if socksListener.Address == addr { if socksListener.Address() == addr {
return nil return nil
} }
socksListener.Done <- struct{}{} socksListener.Close()
<-socksListener.Closed
socksListener = nil socksListener = nil
} }
@ -83,16 +76,12 @@ func ReCreateSocks(port int) error {
return nil return nil
} }
done, closed, err := socks.NewSocksProxy(addr) var err error
socksListener, err = socks.NewSocksProxy(addr)
if err != nil { if err != nil {
return err return err
} }
socksListener = &listener{
Address: addr,
Done: done,
Closed: closed,
}
return nil return nil
} }
@ -100,11 +89,10 @@ func ReCreateRedir(port int) error {
addr := genAddr(port, allowLan) addr := genAddr(port, allowLan)
if redirListener != nil { if redirListener != nil {
if redirListener.Address == addr { if redirListener.Address() == addr {
return nil return nil
} }
redirListener.Done <- struct{}{} redirListener.Close()
<-redirListener.Closed
redirListener = nil redirListener = nil
} }
@ -112,16 +100,12 @@ func ReCreateRedir(port int) error {
return nil return nil
} }
done, closed, err := redir.NewRedirProxy(addr) var err error
redirListener, err = redir.NewRedirProxy(addr)
if err != nil { if err != nil {
return err return err
} }
redirListener = &listener{
Address: addr,
Done: done,
Closed: closed,
}
return nil return nil
} }
@ -130,19 +114,19 @@ func GetPorts() *Ports {
ports := &Ports{} ports := &Ports{}
if httpListener != nil { if httpListener != nil {
_, portStr, _ := net.SplitHostPort(httpListener.Address) _, portStr, _ := net.SplitHostPort(httpListener.Address())
port, _ := strconv.Atoi(portStr) port, _ := strconv.Atoi(portStr)
ports.Port = port ports.Port = port
} }
if socksListener != nil { if socksListener != nil {
_, portStr, _ := net.SplitHostPort(socksListener.Address) _, portStr, _ := net.SplitHostPort(socksListener.Address())
port, _ := strconv.Atoi(portStr) port, _ := strconv.Atoi(portStr)
ports.SocksPort = port ports.SocksPort = port
} }
if redirListener != nil { if redirListener != nil {
_, portStr, _ := net.SplitHostPort(redirListener.Address) _, portStr, _ := net.SplitHostPort(redirListener.Address())
port, _ := strconv.Atoi(portStr) port, _ := strconv.Atoi(portStr)
ports.RedirPort = port ports.RedirPort = port
} }

View file

@ -13,21 +13,25 @@ var (
tun = tunnel.Instance() tun = tunnel.Instance()
) )
func NewRedirProxy(addr string) (chan<- struct{}, <-chan struct{}, error) { type redirListener struct {
net.Listener
address string
closed bool
}
func NewRedirProxy(addr string) (*redirListener, error) {
l, err := net.Listen("tcp", addr) l, err := net.Listen("tcp", addr)
if err != nil { if err != nil {
return nil, nil, err return nil, err
} }
rl := &redirListener{l, addr, false}
done := make(chan struct{})
closed := make(chan struct{})
go func() { go func() {
log.Infof("Redir proxy listening at: %s", addr) log.Infof("Redir proxy listening at: %s", addr)
for { for {
c, err := l.Accept() c, err := l.Accept()
if err != nil { if err != nil {
if _, open := <-done; !open { if rl.closed {
break break
} }
continue continue
@ -36,13 +40,16 @@ func NewRedirProxy(addr string) (chan<- struct{}, <-chan struct{}, error) {
} }
}() }()
go func() { return rl, nil
<-done }
l.Close()
closed <- struct{}{}
}()
return done, closed, nil func (l *redirListener) Close() {
l.closed = true
l.Listener.Close()
}
func (l *redirListener) Address() string {
return l.address
} }
func handleRedir(conn net.Conn) { func handleRedir(conn net.Conn) {

View file

@ -14,21 +14,25 @@ var (
tun = tunnel.Instance() tun = tunnel.Instance()
) )
func NewSocksProxy(addr string) (chan<- struct{}, <-chan struct{}, error) { type sockListener struct {
net.Listener
address string
closed bool
}
func NewSocksProxy(addr string) (*sockListener, error) {
l, err := net.Listen("tcp", addr) l, err := net.Listen("tcp", addr)
if err != nil { if err != nil {
return nil, nil, err return nil, err
} }
done := make(chan struct{}) sl := &sockListener{l, addr, false}
closed := make(chan struct{})
go func() { go func() {
log.Infof("SOCKS proxy listening at: %s", addr) log.Infof("SOCKS proxy listening at: %s", addr)
for { for {
c, err := l.Accept() c, err := l.Accept()
if err != nil { if err != nil {
if _, open := <-done; !open { if sl.closed {
break break
} }
continue continue
@ -37,13 +41,16 @@ func NewSocksProxy(addr string) (chan<- struct{}, <-chan struct{}, error) {
} }
}() }()
go func() { return sl, nil
<-done }
l.Close()
closed <- struct{}{}
}()
return done, closed, nil func (l *sockListener) Close() {
l.closed = true
l.Listener.Close()
}
func (l *sockListener) Address() string {
return l.address
} }
func handleSocks(conn net.Conn) { func handleSocks(conn net.Conn) {