This commit is contained in:
blizard863 2021-12-28 20:23:05 +08:00
parent d7831973ec
commit 9fb8e6fc26
5 changed files with 12 additions and 32 deletions

View File

@ -234,15 +234,12 @@ func (ctl *Control) connectServer() (conn net.Conn, err error) {
} }
} }
opts := []frpNet.DialOption{ conn, err = frpNet.DialWithOptions(net.JoinHostPort(ctl.clientCfg.ServerAddr, strconv.Itoa(ctl.clientCfg.ServerPort)),
frpNet.WithProxy(ctl.clientCfg.HTTPProxy), frpNet.WithProxyURL(ctl.clientCfg.HTTPProxy),
frpNet.WithProtocol(ctl.clientCfg.Protocol), frpNet.WithProtocol(ctl.clientCfg.Protocol),
frpNet.WithRemoteAddress(net.JoinHostPort(ctl.clientCfg.ServerAddr, strconv.Itoa(ctl.clientCfg.ServerPort))),
frpNet.WithTLSConfig(tlsConfig), frpNet.WithTLSConfig(tlsConfig),
frpNet.WithDisableCustomTLSHeadByte(ctl.clientCfg.DisableCustomTLSFirstByte), frpNet.WithDisableCustomTLSHeadByte(ctl.clientCfg.DisableCustomTLSFirstByte))
}
conn, err = frpNet.DialWithOptions(opts...)
if err != nil { if err != nil {
xl.Warn("start new connection to server error: %v", err) xl.Warn("start new connection to server error: %v", err)
return return

View File

@ -790,7 +790,7 @@ func HandleTCPWorkConnection(ctx context.Context, localInfo *config.LocalSvrConf
return return
} }
localConn, err := frpNet.ConnectServer("tcp", fmt.Sprintf("%s:%d", localInfo.LocalIP, localInfo.LocalPort)) localConn, err := frpNet.DialWithOptions(net.JoinHostPort(localInfo.LocalIP, strconv.Itoa(localInfo.LocalPort)))
if err != nil { if err != nil {
workConn.Close() workConn.Close()
xl.Error("connect to local service [%s:%d] error: %v", localInfo.LocalIP, localInfo.LocalPort, err) xl.Error("connect to local service [%s:%d] error: %v", localInfo.LocalIP, localInfo.LocalPort, err)

View File

@ -228,15 +228,12 @@ func (svr *Service) login() (conn net.Conn, session *fmux.Session, err error) {
} }
} }
opts := []frpNet.DialOption{ conn, err = frpNet.DialWithOptions(net.JoinHostPort(svr.cfg.ServerAddr, strconv.Itoa(svr.cfg.ServerPort)),
frpNet.WithProxy(svr.cfg.HTTPProxy), frpNet.WithProxyURL(svr.cfg.HTTPProxy),
frpNet.WithProtocol(svr.cfg.Protocol), frpNet.WithProtocol(svr.cfg.Protocol),
frpNet.WithRemoteAddress(net.JoinHostPort(svr.cfg.ServerAddr, strconv.Itoa(svr.cfg.ServerPort))),
frpNet.WithTLSConfig(tlsConfig), frpNet.WithTLSConfig(tlsConfig),
frpNet.WithDisableCustomTLSHeadByte(svr.cfg.DisableCustomTLSFirstByte), frpNet.WithDisableCustomTLSHeadByte(svr.cfg.DisableCustomTLSFirstByte))
}
conn, err = frpNet.DialWithOptions(opts...)
if err != nil { if err != nil {
return return
} }

View File

@ -226,7 +226,7 @@ func ConnectServerByProxy(proxyURL string, protocol string, addr string) (c net.
case "tcp": case "tcp":
return gnet.DialTcpByProxy(proxyURL, addr) return gnet.DialTcpByProxy(proxyURL, addr)
default: default:
return nil, fmt.Errorf("unsupport protocol: %s", protocol) return nil, fmt.Errorf("unsupport protocol: %s when connecting by proxy", protocol)
} }
} }

View File

@ -8,8 +8,6 @@ import (
type dialOptions struct { type dialOptions struct {
proxyURL string proxyURL string
protocol string protocol string
laddr string
addr string
tlsConfig *tls.Config tlsConfig *tls.Config
disableCustomTLSHeadByte bool disableCustomTLSHeadByte bool
} }
@ -42,30 +40,18 @@ func DefaultDialOptions() dialOptions {
} }
} }
func WithProxy(proxyURL string) DialOption { func WithProxyURL(proxyURL string) DialOption {
return newFuncDialOption(func(do *dialOptions) { return newFuncDialOption(func(do *dialOptions) {
do.proxyURL = proxyURL do.proxyURL = proxyURL
}) })
} }
func WithBindAddress(laddr string) DialOption {
return newFuncDialOption(func(do *dialOptions) {
do.laddr = laddr
})
}
func WithTLSConfig(tlsConfig *tls.Config) DialOption { func WithTLSConfig(tlsConfig *tls.Config) DialOption {
return newFuncDialOption(func(do *dialOptions) { return newFuncDialOption(func(do *dialOptions) {
do.tlsConfig = tlsConfig do.tlsConfig = tlsConfig
}) })
} }
func WithRemoteAddress(addr string) DialOption {
return newFuncDialOption(func(do *dialOptions) {
do.addr = addr
})
}
func WithDisableCustomTLSHeadByte(disableCustomTLSHeadByte bool) DialOption { func WithDisableCustomTLSHeadByte(disableCustomTLSHeadByte bool) DialOption {
return newFuncDialOption(func(do *dialOptions) { return newFuncDialOption(func(do *dialOptions) {
do.disableCustomTLSHeadByte = disableCustomTLSHeadByte do.disableCustomTLSHeadByte = disableCustomTLSHeadByte
@ -78,7 +64,7 @@ func WithProtocol(protocol string) DialOption {
}) })
} }
func DialWithOptions(opts ...DialOption) (c net.Conn, err error) { func DialWithOptions(addr string, opts ...DialOption) (c net.Conn, err error) {
op := DefaultDialOptions() op := DefaultDialOptions()
for _, opt := range opts { for _, opt := range opts {
@ -86,9 +72,9 @@ func DialWithOptions(opts ...DialOption) (c net.Conn, err error) {
} }
if op.proxyURL == "" { if op.proxyURL == "" {
c, err = ConnectServer(op.protocol, op.addr) c, err = ConnectServer(op.protocol, addr)
} else { } else {
c, err = ConnectServerByProxy(op.proxyURL, op.protocol, op.addr) c, err = ConnectServerByProxy(op.proxyURL, op.protocol, addr)
} }
if err != nil { if err != nil {
return nil, err return nil, err