Skip to content

Commit 4246d92

Browse files
committed
support ipv6
1 parent 5f854dd commit 4246d92

File tree

3 files changed

+14
-32
lines changed

3 files changed

+14
-32
lines changed

pkg/remoting/getty/session_manager.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"net"
2424
"reflect"
25+
"strconv"
2526
"sync"
2627
"sync/atomic"
2728
"time"
@@ -76,7 +77,7 @@ func (g *SessionManager) init() {
7677
}
7778
for _, address := range addressList {
7879
gettyClient := getty.NewTCPClient(
79-
getty.WithServerAddress(fmt.Sprintf("%s:%d", address.Addr, address.Port)),
80+
getty.WithServerAddress(net.JoinHostPort(address.Addr, strconv.Itoa(address.Port))),
8081
// todo if read c.gettyConf.ConnectionNum, will cause the connect to fail
8182
getty.WithConnectionNumber(1),
8283
getty.WithReconnectInterval(g.gettyConf.ReconnectInterval),

pkg/util/net/address_validator.go

+4-23
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,18 @@ package net
1919

2020
import (
2121
"fmt"
22-
"regexp"
22+
"net"
2323
"strconv"
24-
"strings"
2524
)
2625

27-
const (
28-
addressSplitChar = ":"
29-
)
30-
31-
// Analyze it yourself here, in order to support the diversity of configurations
3226
func SplitIPPortStr(addr string) (string, int, error) {
3327
if addr == "" {
3428
return "", 0, fmt.Errorf("split ip err: param addr must not empty")
3529
}
3630

37-
if addr[0] == '[' {
38-
reg := regexp.MustCompile("[\\[\\]]")
39-
addr = reg.ReplaceAllString(addr, "")
40-
}
41-
42-
i := strings.LastIndex(addr, addressSplitChar)
43-
if i < 0 {
44-
return "", 0, fmt.Errorf("address %s: missing port in address", addr)
45-
}
46-
47-
host := addr[:i]
48-
port := addr[i+1:]
49-
50-
if strings.Contains(host, "%") {
51-
reg := regexp.MustCompile("\\%[0-9]+")
52-
host = reg.ReplaceAllString(host, "")
31+
host, port, err := net.SplitHostPort(addr)
32+
if err != nil {
33+
return "", 0, err
5334
}
5435

5536
portInt, err := strconv.Atoi(port)

pkg/util/net/address_validator_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,15 @@ func TestAddressValidator(t *testing.T) {
7575
name: "endpoint is ipv6",
7676
address: "[2000:0000:0000:0000:0001:2345:6789:abcd%10]:8080",
7777
want: &IPAddr{
78-
"2000:0000:0000:0000:0001:2345:6789:abcd", 8080,
78+
"2000:0000:0000:0000:0001:2345:6789:abcd%10", 8080,
7979
},
8080
wantErr: false,
8181
},
8282
{
83-
name: "endpoint is ipv6",
84-
address: "2000:0000:0000:0000:0001:2345:6789:abcd:8080",
85-
want: &IPAddr{
86-
"2000:0000:0000:0000:0001:2345:6789:abcd", 8080,
87-
},
88-
wantErr: false,
83+
name: "endpoint is ipv6",
84+
address: "2000:0000:0000:0000:0001:2345:6789:abcd:8080",
85+
wantErr: true,
86+
wantErrMsg: "address 2000:0000:0000:0000:0001:2345:6789:abcd:8080: too many colons in address",
8987
},
9088
{
9189
name: "endpoint is ipv6",
@@ -97,7 +95,7 @@ func TestAddressValidator(t *testing.T) {
9795
},
9896
{
9997
name: "endpoint is ipv6",
100-
address: "::FFFF:192.168.1.2:8080",
98+
address: "[::FFFF:192.168.1.2]:8080",
10199
want: &IPAddr{
102100
"::FFFF:192.168.1.2", 8080,
103101
},
@@ -114,7 +112,9 @@ func TestAddressValidator(t *testing.T) {
114112
}
115113
for _, tt := range tests {
116114
t.Run(tt.name, func(t *testing.T) {
115+
//host, port, err := SplitIPPortStr(tt.address)
117116
host, port, err := SplitIPPortStr(tt.address)
117+
118118
if (err != nil) != tt.wantErr {
119119
t.Errorf("SplitIPPortStr() error = %v, wantErr = %v", err, tt.wantErr)
120120
}

0 commit comments

Comments
 (0)