Skip to content

Commit ed61049

Browse files
authored
Bugfix: add ipv6 parsing with address of frps (fatedier#2163)
1 parent abe6f58 commit ed61049

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

client/control.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ package client
1717
import (
1818
"context"
1919
"crypto/tls"
20-
"fmt"
2120
"io"
2221
"net"
2322
"runtime/debug"
23+
"strconv"
2424
"sync"
2525
"time"
2626

@@ -222,8 +222,10 @@ func (ctl *Control) connectServer() (conn net.Conn, err error) {
222222
return
223223
}
224224
}
225-
conn, err = frpNet.ConnectServerByProxyWithTLS(ctl.clientCfg.HTTPProxy, ctl.clientCfg.Protocol,
226-
fmt.Sprintf("%s:%d", ctl.clientCfg.ServerAddr, ctl.clientCfg.ServerPort), tlsConfig)
225+
226+
address := net.JoinHostPort(ctl.clientCfg.ServerAddr, strconv.Itoa(ctl.clientCfg.ServerPort))
227+
conn, err = frpNet.ConnectServerByProxyWithTLS(ctl.clientCfg.HTTPProxy, ctl.clientCfg.Protocol, address, tlsConfig)
228+
227229
if err != nil {
228230
xl.Warn("start new connection to server error: %v", err)
229231
return

client/service.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io/ioutil"
2222
"net"
2323
"runtime"
24+
"strconv"
2425
"sync"
2526
"sync/atomic"
2627
"time"
@@ -215,8 +216,9 @@ func (svr *Service) login() (conn net.Conn, session *fmux.Session, err error) {
215216
return
216217
}
217218
}
218-
conn, err = frpNet.ConnectServerByProxyWithTLS(svr.cfg.HTTPProxy, svr.cfg.Protocol,
219-
fmt.Sprintf("%s:%d", svr.cfg.ServerAddr, svr.cfg.ServerPort), tlsConfig)
219+
220+
address := net.JoinHostPort(svr.cfg.ServerAddr, strconv.Itoa(svr.cfg.ServerPort))
221+
conn, err = frpNet.ConnectServerByProxyWithTLS(svr.cfg.HTTPProxy, svr.cfg.Protocol, address, tlsConfig)
220222
if err != nil {
221223
return
222224
}

cmd/frpc/sub/root.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,16 @@ func parseClientCommonCfgFromIni(content string) (config.ClientCommonConf, error
157157
func parseClientCommonCfgFromCmd() (cfg config.ClientCommonConf, err error) {
158158
cfg = config.GetDefaultClientConf()
159159

160-
strs := strings.Split(serverAddr, ":")
161-
if len(strs) < 2 {
162-
err = fmt.Errorf("invalid server_addr")
160+
ipStr, portStr, err := net.SplitHostPort(serverAddr)
161+
if err != nil {
162+
err = fmt.Errorf("invalid server_addr: %v", err)
163163
return
164164
}
165-
if strs[0] != "" {
166-
cfg.ServerAddr = strs[0]
167-
}
168-
cfg.ServerPort, err = strconv.Atoi(strs[1])
165+
166+
cfg.ServerAddr = ipStr
167+
cfg.ServerPort, err = strconv.Atoi(portStr)
169168
if err != nil {
170-
err = fmt.Errorf("invalid server_addr")
169+
err = fmt.Errorf("invalid server_addr: %v", err)
171170
return
172171
}
173172

pkg/util/util/http.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package util
1616

1717
import (
18+
"net"
1819
"net/http"
1920
"strings"
2021
)
@@ -33,6 +34,7 @@ func OkResponse() *http.Response {
3334
return res
3435
}
3536

37+
// TODO: use "CanonicalHost" func to replace all "GetHostFromAddr" func.
3638
func GetHostFromAddr(addr string) (host string) {
3739
strs := strings.Split(addr, ":")
3840
if len(strs) > 1 {
@@ -42,3 +44,34 @@ func GetHostFromAddr(addr string) (host string) {
4244
}
4345
return
4446
}
47+
48+
// canonicalHost strips port from host if present and returns the canonicalized
49+
// host name.
50+
func CanonicalHost(host string) (string, error) {
51+
var err error
52+
host = strings.ToLower(host)
53+
if hasPort(host) {
54+
host, _, err = net.SplitHostPort(host)
55+
if err != nil {
56+
return "", err
57+
}
58+
}
59+
if strings.HasSuffix(host, ".") {
60+
// Strip trailing dot from fully qualified domain names.
61+
host = host[:len(host)-1]
62+
}
63+
return host, nil
64+
}
65+
66+
// hasPort reports whether host contains a port number. host may be a host
67+
// name, an IPv4 or an IPv6 address.
68+
func hasPort(host string) bool {
69+
colons := strings.Count(host, ":")
70+
if colons == 0 {
71+
return false
72+
}
73+
if colons == 1 {
74+
return true
75+
}
76+
return host[0] == '[' && strings.Contains(host, "]:")
77+
}

0 commit comments

Comments
 (0)