|
@@ -16,15 +16,16 @@ package net
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
- "crypto/tls"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"io"
|
|
|
"net"
|
|
|
+ "net/url"
|
|
|
"sync/atomic"
|
|
|
"time"
|
|
|
|
|
|
"github.com/fatedier/frp/pkg/util/xlog"
|
|
|
+ "golang.org/x/net/websocket"
|
|
|
|
|
|
gnet "github.com/fatedier/golib/net"
|
|
|
kcp "github.com/fatedier/kcp-go"
|
|
@@ -194,50 +195,61 @@ func ConnectServer(protocol string, addr string) (c net.Conn, err error) {
|
|
|
case "tcp":
|
|
|
return net.Dial("tcp", addr)
|
|
|
case "kcp":
|
|
|
- kcpConn, errRet := kcp.DialWithOptions(addr, nil, 10, 3)
|
|
|
- if errRet != nil {
|
|
|
- err = errRet
|
|
|
- return
|
|
|
- }
|
|
|
- kcpConn.SetStreamMode(true)
|
|
|
- kcpConn.SetWriteDelay(true)
|
|
|
- kcpConn.SetNoDelay(1, 20, 2, 1)
|
|
|
- kcpConn.SetWindowSize(128, 512)
|
|
|
- kcpConn.SetMtu(1350)
|
|
|
- kcpConn.SetACKNoDelay(false)
|
|
|
- kcpConn.SetReadBuffer(4194304)
|
|
|
- kcpConn.SetWriteBuffer(4194304)
|
|
|
- c = kcpConn
|
|
|
- return
|
|
|
+ return DialKCPServer(addr)
|
|
|
+ case "websocket":
|
|
|
+ return DialWebsocketServer(addr)
|
|
|
default:
|
|
|
return nil, fmt.Errorf("unsupport protocol: %s", protocol)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func DialKCPServer(addr string) (c net.Conn, err error) {
|
|
|
+ kcpConn, errRet := kcp.DialWithOptions(addr, nil, 10, 3)
|
|
|
+ if errRet != nil {
|
|
|
+ err = errRet
|
|
|
+ return
|
|
|
+ }
|
|
|
+ kcpConn.SetStreamMode(true)
|
|
|
+ kcpConn.SetWriteDelay(true)
|
|
|
+ kcpConn.SetNoDelay(1, 20, 2, 1)
|
|
|
+ kcpConn.SetWindowSize(128, 512)
|
|
|
+ kcpConn.SetMtu(1350)
|
|
|
+ kcpConn.SetACKNoDelay(false)
|
|
|
+ kcpConn.SetReadBuffer(4194304)
|
|
|
+ kcpConn.SetWriteBuffer(4194304)
|
|
|
+ c = kcpConn
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
func ConnectServerByProxy(proxyURL string, protocol string, addr string) (c net.Conn, err error) {
|
|
|
switch protocol {
|
|
|
case "tcp":
|
|
|
return gnet.DialTcpByProxy(proxyURL, addr)
|
|
|
- case "kcp":
|
|
|
- // http proxy is not supported for kcp
|
|
|
- return ConnectServer(protocol, addr)
|
|
|
- case "websocket":
|
|
|
- return ConnectWebsocketServer(addr)
|
|
|
default:
|
|
|
- return nil, fmt.Errorf("unsupport protocol: %s", protocol)
|
|
|
+ return nil, fmt.Errorf("unsupport protocol: %s when connecting by proxy", protocol)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func ConnectServerByProxyWithTLS(proxyURL string, protocol string, addr string, tlsConfig *tls.Config, disableCustomTLSHeadByte bool) (c net.Conn, err error) {
|
|
|
- c, err = ConnectServerByProxy(proxyURL, protocol, addr)
|
|
|
+// addr: domain:port
|
|
|
+func DialWebsocketServer(addr string) (net.Conn, error) {
|
|
|
+ addr = "ws://" + addr + FrpWebsocketPath
|
|
|
+ uri, err := url.Parse(addr)
|
|
|
if err != nil {
|
|
|
- return
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
- if tlsConfig == nil {
|
|
|
- return
|
|
|
+ origin := "http://" + uri.Host
|
|
|
+ cfg, err := websocket.NewConfig(addr, origin)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ cfg.Dialer = &net.Dialer{
|
|
|
+ Timeout: 10 * time.Second,
|
|
|
}
|
|
|
|
|
|
- c = WrapTLSClientConn(c, tlsConfig, disableCustomTLSHeadByte)
|
|
|
- return
|
|
|
+ conn, err := websocket.DialConfig(cfg)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ return conn, nil
|
|
|
}
|