|
|
@ -5,25 +5,24 @@ import ( |
|
|
|
"io" |
|
|
|
"net" |
|
|
|
"net/http" |
|
|
|
"os" |
|
|
|
"strconv" |
|
|
|
"sync" |
|
|
|
"time" |
|
|
|
|
|
|
|
"gitlab.com/wpetit/goweb/logger" |
|
|
|
|
|
|
|
"forge.cadoles.com/wpetit/go-tunnel/control" |
|
|
|
cmap "github.com/orcaman/concurrent-map" |
|
|
|
"github.com/pkg/errors" |
|
|
|
"github.com/xtaci/kcp-go/v5" |
|
|
|
"github.com/xtaci/smux" |
|
|
|
) |
|
|
|
|
|
|
|
type Client struct { |
|
|
|
conf *ClientConfig |
|
|
|
conn *kcp.UDPSession |
|
|
|
sess *smux.Session |
|
|
|
control *control.Control |
|
|
|
http *http.Client |
|
|
|
proxies cmap.ConcurrentMap |
|
|
|
conf *ClientConfig |
|
|
|
conn *kcp.UDPSession |
|
|
|
sess *smux.Session |
|
|
|
control *control.Control |
|
|
|
http *http.Client |
|
|
|
openStreamMutex sync.Mutex |
|
|
|
} |
|
|
|
|
|
|
|
func (c *Client) Connect(ctx context.Context) error { |
|
|
@ -43,24 +42,22 @@ func (c *Client) Connect(ctx context.Context) error { |
|
|
|
|
|
|
|
config := smux.DefaultConfig() |
|
|
|
config.Version = 2 |
|
|
|
config.KeepAliveInterval = 10 * time.Second |
|
|
|
config.KeepAliveTimeout = 2 * config.KeepAliveInterval |
|
|
|
|
|
|
|
sess, err := smux.Client(conn, config) |
|
|
|
if err != nil { |
|
|
|
return errors.WithStack(err) |
|
|
|
} |
|
|
|
|
|
|
|
controlStream, err := sess.OpenStream() |
|
|
|
if err != nil { |
|
|
|
control := control.New() |
|
|
|
if err := control.Init(ctx, sess, false); err != nil { |
|
|
|
return errors.WithStack(err) |
|
|
|
} |
|
|
|
|
|
|
|
c.conn = conn |
|
|
|
c.sess = sess |
|
|
|
c.control = control.New(sess, controlStream) |
|
|
|
|
|
|
|
logger.Debug(ctx, "sending auth request") |
|
|
|
|
|
|
|
success, err := c.control.AuthRequest(c.conf.Credentials) |
|
|
|
success, err := control.AuthRequest(c.conf.Credentials) |
|
|
|
if err != nil { |
|
|
|
return errors.WithStack(err) |
|
|
|
} |
|
|
@ -70,15 +67,21 @@ func (c *Client) Connect(ctx context.Context) error { |
|
|
|
return errors.WithStack(ErrAuthFailed) |
|
|
|
} |
|
|
|
|
|
|
|
c.control = control |
|
|
|
c.conn = conn |
|
|
|
c.sess = sess |
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
func (c *Client) Listen(ctx context.Context) error { |
|
|
|
logger.Debug(ctx, "listening for messages") |
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctx) |
|
|
|
defer cancel() |
|
|
|
|
|
|
|
err := c.control.Listen(ctx, control.Handlers{ |
|
|
|
control.TypeProxyRequest: c.handleProxyRequest, |
|
|
|
control.TypeCloseProxy: c.handleCloseProxy, |
|
|
|
}) |
|
|
|
|
|
|
|
if errors.Is(err, io.ErrClosedPipe) { |
|
|
@ -99,105 +102,60 @@ func (c *Client) Close() error { |
|
|
|
return errors.WithStack(err) |
|
|
|
} |
|
|
|
|
|
|
|
if c.sess != nil && !c.sess.IsClosed() { |
|
|
|
if err := c.sess.Close(); err != nil { |
|
|
|
return errors.WithStack(err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
func (c *Client) handleCloseProxy(ctx context.Context, m *control.Message) (*control.Message, error) { |
|
|
|
closeProxyPayload, ok := m.Payload.(*control.CloseProxyPayload) |
|
|
|
if !ok { |
|
|
|
return nil, errors.WithStack(ErrUnexpectedMessage) |
|
|
|
} |
|
|
|
|
|
|
|
requestID := strconv.FormatInt(closeProxyPayload.RequestID, 10) |
|
|
|
|
|
|
|
rawCloseChan, exists := c.proxies.Get(requestID) |
|
|
|
if !exists { |
|
|
|
return nil, nil |
|
|
|
} |
|
|
|
|
|
|
|
closeChan, ok := rawCloseChan.(chan struct{}) |
|
|
|
if !ok { |
|
|
|
return nil, nil |
|
|
|
} |
|
|
|
|
|
|
|
closeChan <- struct{}{} |
|
|
|
|
|
|
|
return nil, nil |
|
|
|
} |
|
|
|
|
|
|
|
func (c *Client) handleProxyRequest(ctx context.Context, m *control.Message) (*control.Message, error) { |
|
|
|
proxyReqPayload, ok := m.Payload.(*control.ProxyRequestPayload) |
|
|
|
if !ok { |
|
|
|
return nil, errors.WithStack(ErrUnexpectedMessage) |
|
|
|
} |
|
|
|
|
|
|
|
requestID := strconv.FormatInt(proxyReqPayload.RequestID, 10) |
|
|
|
|
|
|
|
ctx = logger.With(ctx, logger.F("requestID", requestID)) |
|
|
|
|
|
|
|
logger.Debug( |
|
|
|
ctx, "handling proxy request", |
|
|
|
ctx = logger.With(ctx, |
|
|
|
logger.F("network", proxyReqPayload.Network), |
|
|
|
logger.F("address", proxyReqPayload.Address), |
|
|
|
) |
|
|
|
|
|
|
|
stream, err := c.sess.OpenStream() |
|
|
|
logger.Debug(ctx, "handling proxy request") |
|
|
|
|
|
|
|
out, err := net.Dial(proxyReqPayload.Network, proxyReqPayload.Address) |
|
|
|
if err != nil { |
|
|
|
return nil, errors.WithStack(err) |
|
|
|
} |
|
|
|
|
|
|
|
closeChan := make(chan struct{}) |
|
|
|
|
|
|
|
go func() { |
|
|
|
defer func() { |
|
|
|
stream.Close() |
|
|
|
logger.Debug(ctx, "proxy stream closed") |
|
|
|
}() |
|
|
|
|
|
|
|
proxy := func() error { |
|
|
|
net, err := net.Dial(proxyReqPayload.Network, proxyReqPayload.Address) |
|
|
|
if err != nil { |
|
|
|
return errors.WithStack(err) |
|
|
|
} |
|
|
|
defer net.Close() |
|
|
|
go c.handleProxyStream(ctx, out) |
|
|
|
|
|
|
|
err = pipe(ctx, stream, net) |
|
|
|
if errors.Is(err, os.ErrClosed) { |
|
|
|
return nil |
|
|
|
} |
|
|
|
return nil, nil |
|
|
|
} |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
return errors.WithStack(err) |
|
|
|
} |
|
|
|
func (c *Client) handleProxyStream(ctx context.Context, out net.Conn) { |
|
|
|
c.openStreamMutex.Lock() |
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
in, err := c.sess.OpenStream() |
|
|
|
if err != nil { |
|
|
|
c.openStreamMutex.Unlock() |
|
|
|
logger.Error(ctx, "error while accepting proxy stream", logger.E(err)) |
|
|
|
|
|
|
|
for { |
|
|
|
select { |
|
|
|
case <-closeChan: |
|
|
|
return |
|
|
|
default: |
|
|
|
if err := proxy(); err != nil { |
|
|
|
logger.Error(ctx, "error while proxying", logger.E(err)) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
continue |
|
|
|
} |
|
|
|
c.openStreamMutex.Unlock() |
|
|
|
|
|
|
|
return |
|
|
|
streamCopy := func(dst io.Writer, src io.ReadCloser) { |
|
|
|
if _, err := Copy(dst, src); err != nil { |
|
|
|
if errors.Is(err, smux.ErrInvalidProtocol) { |
|
|
|
logger.Error(ctx, "error while proxying", logger.E(errors.WithStack(err))) |
|
|
|
} |
|
|
|
} |
|
|
|
}() |
|
|
|
|
|
|
|
c.proxies.Set(requestID, closeChan) |
|
|
|
logger.Debug(ctx, "closing proxy stream") |
|
|
|
|
|
|
|
return nil, nil |
|
|
|
in.Close() |
|
|
|
out.Close() |
|
|
|
} |
|
|
|
|
|
|
|
go streamCopy(in, out) |
|
|
|
streamCopy(out, in) |
|
|
|
} |
|
|
|
|
|
|
|
func NewClient(funcs ...ClientConfigFunc) *Client { |
|
|
@ -208,8 +166,7 @@ func NewClient(funcs ...ClientConfigFunc) *Client { |
|
|
|
} |
|
|
|
|
|
|
|
return &Client{ |
|
|
|
conf: conf, |
|
|
|
http: &http.Client{}, |
|
|
|
proxies: cmap.New(), |
|
|
|
conf: conf, |
|
|
|
http: &http.Client{}, |
|
|
|
} |
|
|
|
} |