Compare commits

...

2 Commits

Author SHA1 Message Date
wpetit 30564efd85 feat: dispatch proxy error 2020-10-23 13:42:44 +02:00
wpetit 7aa977d2fe feat: cleanup remote client on connection closed 2020-10-23 13:42:18 +02:00
4 changed files with 23 additions and 7 deletions

View File

@ -117,7 +117,9 @@ func (c *Client) handleProxyRequest(ctx context.Context, m *control.Message) (*c
return nil, errors.WithStack(err) return nil, errors.WithStack(err)
} }
pipe(stream, net) if err := pipe(stream, net); err != nil {
return nil, errors.WithStack(err)
}
return nil, nil return nil, nil
} }

View File

@ -5,22 +5,29 @@ import (
"net" "net"
) )
func pipe(client net.Conn, server net.Conn) { func pipe(client net.Conn, server net.Conn) (err error) {
stop := make(chan bool) stop := make(chan bool)
go relay(client, server, stop) go func() {
go relay(server, client, stop) err = relay(client, server, stop)
}()
go func() {
err = relay(server, client, stop)
}()
select { select {
case <-stop: case <-stop:
return return err
} }
} }
func relay(src net.Conn, dst net.Conn, stop chan bool) { func relay(src io.ReadCloser, dst io.WriteCloser, stop chan bool) (err error) {
io.Copy(dst, src) _, err = io.Copy(dst, src)
dst.Close() dst.Close()
src.Close() src.Close()
stop <- true stop <- true
return return
} }

View File

@ -125,6 +125,12 @@ func (c *RemoteClient) Proxy(ctx context.Context, network, address string) (net.
return c.control.Proxy(ctx, network, address) return c.control.Proxy(ctx, network, address)
} }
func (c *RemoteClient) Close() {
if c.sess != nil && !c.sess.IsClosed() {
c.sess.Close()
}
}
func NewRemoteClient() *RemoteClient { func NewRemoteClient() *RemoteClient {
return &RemoteClient{} return &RemoteClient{}
} }

View File

@ -37,6 +37,7 @@ func (s *Server) handleNewConn(ctx context.Context, conn *kcp.UDPSession) {
ctx = logger.With(ctx, logger.F("remoteAddr", conn.RemoteAddr().String())) ctx = logger.With(ctx, logger.F("remoteAddr", conn.RemoteAddr().String()))
remoteClient := NewRemoteClient() remoteClient := NewRemoteClient()
defer remoteClient.Close()
remoteClient.ConfigureHooks(s.conf.Hooks) remoteClient.ConfigureHooks(s.conf.Hooks)