Skip to content

Commit cd6468c

Browse files
author
Shawn Hwei
authored
IPv6 remotes support (closes #197, thanks @shawnhwei)
* Support IPv6 remotes * Adjust validation for IPv6 remotes * IPv6 refactoring * Further loosen remote part matching regex
1 parent 6fce8e1 commit cd6468c

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

share/settings/remote.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func DecodeRemote(s string) (*Remote, error) {
4646
s = strings.TrimPrefix(s, revPrefix)
4747
reverse = true
4848
}
49-
parts := strings.Split(s, ":")
49+
parts := regexp.MustCompile(`(\[[^\[\]]+\]|[^\[\]:]+):?`).FindAllStringSubmatch(s, -1)
5050
if len(parts) <= 0 || len(parts) >= 5 {
5151
return nil, errors.New("Invalid remote")
5252
}
@@ -55,7 +55,7 @@ func DecodeRemote(s string) (*Remote, error) {
5555
//then to set 'local' fields second (allows the 'remote' side
5656
//to provide the defaults)
5757
for i := len(parts) - 1; i >= 0; i-- {
58-
p := parts[i]
58+
p := parts[i][1]
5959
//remote portion is socks?
6060
if i == len(parts)-1 && p == "socks" {
6161
r.Socks = true
@@ -144,7 +144,7 @@ func isPort(s string) bool {
144144
}
145145

146146
func isHost(s string) bool {
147-
_, err := url.Parse(s)
147+
_, err := url.Parse("//" + s)
148148
if err != nil {
149149
return false
150150
}

share/settings/remote_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ func TestRemoteDecode(t *testing.T) {
4040
},
4141
"R:0.0.0.0:80:google.com:80",
4242
},
43+
{
44+
"示例網站.com:80",
45+
Remote{
46+
LocalPort: "80",
47+
RemoteHost: "示例網站.com",
48+
RemotePort: "80",
49+
},
50+
"0.0.0.0:80:示例網站.com:80",
51+
},
4352
{
4453
"socks",
4554
Remote{
@@ -81,6 +90,27 @@ func TestRemoteDecode(t *testing.T) {
8190
},
8291
"localhost:5353:1.1.1.1:53/udp",
8392
},
93+
{
94+
"[::1]:8080:google.com:80",
95+
Remote{
96+
LocalHost: "[::1]",
97+
LocalPort: "8080",
98+
RemoteHost: "google.com",
99+
RemotePort: "80",
100+
},
101+
"[::1]:8080:google.com:80",
102+
},
103+
{
104+
"R:[::]:3000:[::1]:3000",
105+
Remote{
106+
LocalHost: "[::]",
107+
LocalPort: "3000",
108+
RemoteHost: "[::1]",
109+
RemotePort: "3000",
110+
Reverse: true,
111+
},
112+
"R:[::]:3000:[::1]:3000",
113+
},
84114
} {
85115
//expected defaults
86116
expected := test.Output

0 commit comments

Comments
 (0)