Compare commits

..

1 Commits

Author SHA1 Message Date
wpetit 9186173322 fix: enhance proxy stability 2020-10-24 18:43:17 +02:00
1 changed files with 15 additions and 27 deletions

View File

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