From 3cf83f57a863a74194e3b767fb029efd8881b0a5 Mon Sep 17 00:00:00 2001 From: fatedier Date: Sun, 3 Mar 2019 22:29:08 +0800 Subject: [PATCH 1/3] update yamux version --- go.mod | 2 +- go.sum | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 3870c2f..fe82c1f 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/gorilla/context v1.1.1 // indirect github.com/gorilla/mux v1.6.2 github.com/gorilla/websocket v1.2.0 - github.com/hashicorp/yamux v0.0.0-20180314200745-2658be15c5f0 + github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/mattn/go-runewidth v0.0.4 // indirect github.com/pkg/errors v0.8.0 // indirect diff --git a/go.sum b/go.sum index 86992a1..e71b602 100644 --- a/go.sum +++ b/go.sum @@ -9,7 +9,8 @@ github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8 github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg= github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= -github.com/hashicorp/yamux v0.0.0-20180314200745-2658be15c5f0/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= +github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ= +github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y= From ba45d29b7c1eb2840646ac26baaa035a1a2e3664 Mon Sep 17 00:00:00 2001 From: fatedier Date: Sun, 3 Mar 2019 23:44:44 +0800 Subject: [PATCH 2/3] fix xtcp cmd --- client/visitor.go | 2 ++ cmd/frpc/sub/xtcp.go | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/client/visitor.go b/client/visitor.go index 1e8ffec..e81f798 100644 --- a/client/visitor.go +++ b/client/visitor.go @@ -280,6 +280,8 @@ func (sv *XtcpVisitor) handleConn(userConn frpNet.Conn) { sv.Error("listen on visitorConn's local adress error: %v", err) return } + defer lConn.Close() + lConn.SetReadDeadline(time.Now().Add(5 * time.Second)) sidBuf := pool.GetBuf(1024) n, _, err = lConn.ReadFromUDP(sidBuf) diff --git a/cmd/frpc/sub/xtcp.go b/cmd/frpc/sub/xtcp.go index 0a81e18..c15ac5a 100644 --- a/cmd/frpc/sub/xtcp.go +++ b/cmd/frpc/sub/xtcp.go @@ -68,7 +68,7 @@ var xtcpCmd = &cobra.Command{ if role == "server" { cfg := &config.XtcpProxyConf{} cfg.ProxyName = prefix + proxyName - cfg.ProxyType = consts.StcpProxy + cfg.ProxyType = consts.XtcpProxy cfg.UseEncryption = useEncryption cfg.UseCompression = useCompression cfg.Role = role @@ -84,7 +84,7 @@ var xtcpCmd = &cobra.Command{ } else if role == "visitor" { cfg := &config.XtcpVisitorConf{} cfg.ProxyName = prefix + proxyName - cfg.ProxyType = consts.StcpProxy + cfg.ProxyType = consts.XtcpProxy cfg.UseEncryption = useEncryption cfg.UseCompression = useCompression cfg.Role = role From 3df27b9c043d435fca41cd09b7c2f8c8081898af Mon Sep 17 00:00:00 2001 From: fatedier Date: Tue, 5 Mar 2019 11:18:17 +0800 Subject: [PATCH 3/3] xtcp: wrap yamux on kcp connections --- client/proxy/proxy.go | 19 ++++++++++++++++++- client/visitor.go | 19 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/client/proxy/proxy.go b/client/proxy/proxy.go index 4b721aa..f08d3ea 100644 --- a/client/proxy/proxy.go +++ b/client/proxy/proxy.go @@ -18,6 +18,7 @@ import ( "bytes" "fmt" "io" + "io/ioutil" "net" "sync" "time" @@ -33,6 +34,7 @@ import ( "github.com/fatedier/golib/errors" frpIo "github.com/fatedier/golib/io" "github.com/fatedier/golib/pool" + fmux "github.com/hashicorp/yamux" ) // Proxy defines how to handle work connections for different proxy type. @@ -302,8 +304,23 @@ func (pxy *XtcpProxy) InWorkConn(conn frpNet.Conn) { return } + fmuxCfg := fmux.DefaultConfig() + fmuxCfg.KeepAliveInterval = 5 * time.Second + fmuxCfg.LogOutput = ioutil.Discard + sess, err := fmux.Server(kcpConn, fmuxCfg) + if err != nil { + pxy.Error("create yamux server from kcp connection error: %v", err) + return + } + defer sess.Close() + muxConn, err := sess.Accept() + if err != nil { + pxy.Error("accept for yamux connection error: %v", err) + return + } + HandleTcpWorkConnection(&pxy.cfg.LocalSvrConf, pxy.proxyPlugin, &pxy.cfg.BaseProxyConf, - frpNet.WrapConn(kcpConn), []byte(pxy.cfg.Sk)) + frpNet.WrapConn(muxConn), []byte(pxy.cfg.Sk)) } // UDP diff --git a/client/visitor.go b/client/visitor.go index e81f798..35abcf5 100644 --- a/client/visitor.go +++ b/client/visitor.go @@ -18,6 +18,7 @@ import ( "bytes" "fmt" "io" + "io/ioutil" "net" "strconv" "strings" @@ -35,6 +36,7 @@ import ( frpIo "github.com/fatedier/golib/io" "github.com/fatedier/golib/pool" + fmux "github.com/hashicorp/yamux" ) // Visitor is used for forward traffics from local port tot remote service. @@ -316,7 +318,22 @@ func (sv *XtcpVisitor) handleConn(userConn frpNet.Conn) { remote = frpIo.WithCompression(remote) } - frpIo.Join(userConn, remote) + fmuxCfg := fmux.DefaultConfig() + fmuxCfg.KeepAliveInterval = 5 * time.Second + fmuxCfg.LogOutput = ioutil.Discard + sess, err := fmux.Client(remote, fmuxCfg) + if err != nil { + sv.Error("create yamux session error: %v", err) + return + } + defer sess.Close() + muxConn, err := sess.Open() + if err != nil { + sv.Error("open yamux stream error: %v", err) + return + } + + frpIo.Join(userConn, muxConn) sv.Debug("join connections closed") }