Browse Source

xtcp: wrap yamux on kcp connections

fatedier 6 years ago
parent
commit
3df27b9c04
2 changed files with 36 additions and 2 deletions
  1. 18 1
      client/proxy/proxy.go
  2. 18 1
      client/visitor.go

+ 18 - 1
client/proxy/proxy.go

@@ -18,6 +18,7 @@ import (
 	"bytes"
 	"bytes"
 	"fmt"
 	"fmt"
 	"io"
 	"io"
+	"io/ioutil"
 	"net"
 	"net"
 	"sync"
 	"sync"
 	"time"
 	"time"
@@ -33,6 +34,7 @@ import (
 	"github.com/fatedier/golib/errors"
 	"github.com/fatedier/golib/errors"
 	frpIo "github.com/fatedier/golib/io"
 	frpIo "github.com/fatedier/golib/io"
 	"github.com/fatedier/golib/pool"
 	"github.com/fatedier/golib/pool"
+	fmux "github.com/hashicorp/yamux"
 )
 )
 
 
 // Proxy defines how to handle work connections for different proxy type.
 // Proxy defines how to handle work connections for different proxy type.
@@ -302,8 +304,23 @@ func (pxy *XtcpProxy) InWorkConn(conn frpNet.Conn) {
 		return
 		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,
 	HandleTcpWorkConnection(&pxy.cfg.LocalSvrConf, pxy.proxyPlugin, &pxy.cfg.BaseProxyConf,
-		frpNet.WrapConn(kcpConn), []byte(pxy.cfg.Sk))
+		frpNet.WrapConn(muxConn), []byte(pxy.cfg.Sk))
 }
 }
 
 
 // UDP
 // UDP

+ 18 - 1
client/visitor.go

@@ -18,6 +18,7 @@ import (
 	"bytes"
 	"bytes"
 	"fmt"
 	"fmt"
 	"io"
 	"io"
+	"io/ioutil"
 	"net"
 	"net"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
@@ -35,6 +36,7 @@ import (
 
 
 	frpIo "github.com/fatedier/golib/io"
 	frpIo "github.com/fatedier/golib/io"
 	"github.com/fatedier/golib/pool"
 	"github.com/fatedier/golib/pool"
+	fmux "github.com/hashicorp/yamux"
 )
 )
 
 
 // Visitor is used for forward traffics from local port tot remote service.
 // 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)
 		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")
 	sv.Debug("join connections closed")
 }
 }