Bugfix: add ipv6 parsing with address of frps (#2163)

This commit is contained in:
yuyulei 2020-12-24 21:48:26 +08:00 committed by GitHub
parent abe6f580c0
commit ed61049041
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 13 deletions

View File

@ -17,10 +17,10 @@ package client
import ( import (
"context" "context"
"crypto/tls" "crypto/tls"
"fmt"
"io" "io"
"net" "net"
"runtime/debug" "runtime/debug"
"strconv"
"sync" "sync"
"time" "time"
@ -222,8 +222,10 @@ func (ctl *Control) connectServer() (conn net.Conn, err error) {
return return
} }
} }
conn, err = frpNet.ConnectServerByProxyWithTLS(ctl.clientCfg.HTTPProxy, ctl.clientCfg.Protocol,
fmt.Sprintf("%s:%d", ctl.clientCfg.ServerAddr, ctl.clientCfg.ServerPort), tlsConfig) address := net.JoinHostPort(ctl.clientCfg.ServerAddr, strconv.Itoa(ctl.clientCfg.ServerPort))
conn, err = frpNet.ConnectServerByProxyWithTLS(ctl.clientCfg.HTTPProxy, ctl.clientCfg.Protocol, address, tlsConfig)
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

@ -21,6 +21,7 @@ import (
"io/ioutil" "io/ioutil"
"net" "net"
"runtime" "runtime"
"strconv"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
@ -215,8 +216,9 @@ func (svr *Service) login() (conn net.Conn, session *fmux.Session, err error) {
return return
} }
} }
conn, err = frpNet.ConnectServerByProxyWithTLS(svr.cfg.HTTPProxy, svr.cfg.Protocol,
fmt.Sprintf("%s:%d", svr.cfg.ServerAddr, svr.cfg.ServerPort), tlsConfig) address := net.JoinHostPort(svr.cfg.ServerAddr, strconv.Itoa(svr.cfg.ServerPort))
conn, err = frpNet.ConnectServerByProxyWithTLS(svr.cfg.HTTPProxy, svr.cfg.Protocol, address, tlsConfig)
if err != nil { if err != nil {
return return
} }

View File

@ -157,17 +157,16 @@ func parseClientCommonCfgFromIni(content string) (config.ClientCommonConf, error
func parseClientCommonCfgFromCmd() (cfg config.ClientCommonConf, err error) { func parseClientCommonCfgFromCmd() (cfg config.ClientCommonConf, err error) {
cfg = config.GetDefaultClientConf() cfg = config.GetDefaultClientConf()
strs := strings.Split(serverAddr, ":") ipStr, portStr, err := net.SplitHostPort(serverAddr)
if len(strs) < 2 { if err != nil {
err = fmt.Errorf("invalid server_addr") err = fmt.Errorf("invalid server_addr: %v", err)
return return
} }
if strs[0] != "" {
cfg.ServerAddr = strs[0] cfg.ServerAddr = ipStr
} cfg.ServerPort, err = strconv.Atoi(portStr)
cfg.ServerPort, err = strconv.Atoi(strs[1])
if err != nil { if err != nil {
err = fmt.Errorf("invalid server_addr") err = fmt.Errorf("invalid server_addr: %v", err)
return return
} }

View File

@ -15,6 +15,7 @@
package util package util
import ( import (
"net"
"net/http" "net/http"
"strings" "strings"
) )
@ -33,6 +34,7 @@ func OkResponse() *http.Response {
return res return res
} }
// TODO: use "CanonicalHost" func to replace all "GetHostFromAddr" func.
func GetHostFromAddr(addr string) (host string) { func GetHostFromAddr(addr string) (host string) {
strs := strings.Split(addr, ":") strs := strings.Split(addr, ":")
if len(strs) > 1 { if len(strs) > 1 {
@ -42,3 +44,34 @@ func GetHostFromAddr(addr string) (host string) {
} }
return return
} }
// canonicalHost strips port from host if present and returns the canonicalized
// host name.
func CanonicalHost(host string) (string, error) {
var err error
host = strings.ToLower(host)
if hasPort(host) {
host, _, err = net.SplitHostPort(host)
if err != nil {
return "", err
}
}
if strings.HasSuffix(host, ".") {
// Strip trailing dot from fully qualified domain names.
host = host[:len(host)-1]
}
return host, nil
}
// hasPort reports whether host contains a port number. host may be a host
// name, an IPv4 or an IPv6 address.
func hasPort(host string) bool {
colons := strings.Count(host, ":")
if colons == 0 {
return false
}
if colons == 1 {
return true
}
return host[0] == '[' && strings.Contains(host, "]:")
}