Compare commits

..

1 Commits

Author SHA1 Message Date
wpetit 501ba87c2d fix: enhance proxy stability 2020-10-24 19:00:55 +02:00
1 changed files with 27 additions and 15 deletions

View File

@ -2,6 +2,7 @@ package tunnel
import (
"context"
"io"
"net"
"sync"
"time"
@ -19,7 +20,6 @@ type RemoteClient struct {
onClientAuthHook OnClientAuthHook
onClientConnectHook OnClientConnectHook
onClientDisconnectHook OnClientDisconnectHook
conn net.Conn
sess *smux.Session
control *control.Control
remoteAddr net.Addr
@ -33,21 +33,38 @@ func (c *RemoteClient) Accept(ctx context.Context, conn *kcp.UDPSession) error {
config.KeepAliveInterval = 10 * time.Second
config.KeepAliveTimeout = 2 * config.KeepAliveInterval
sess, err := smux.Server(conn, config)
if err != nil {
return errors.WithStack(err)
}
var (
sess *smux.Session
err error
ctrl *control.Control
)
control := control.New()
for {
logger.Debug(ctx, "creating server session")
if err := control.Init(ctx, sess, true); err != nil {
return errors.WithStack(err)
sess, err = smux.Server(conn, config)
if err != nil {
return errors.WithStack(err)
}
ctrl = control.New()
err = ctrl.Init(ctx, sess, true)
if err != nil && errors.Is(err, io.ErrClosedPipe) {
continue
}
if err != nil {
return errors.WithStack(err)
}
break
}
c.sess = sess
c.remoteAddr = conn.RemoteAddr()
c.control = control
c.conn = conn
c.control = ctrl
if c.onClientConnectHook != nil {
if err := c.onClientConnectHook.OnClientConnect(ctx, c); err != nil {
@ -101,12 +118,7 @@ func (c *RemoteClient) Close() {
c.sess.Close()
}
if c.conn != nil {
c.conn.Close()
}
c.sess = nil
c.conn = nil
c.control = nil
}