Skip to content

Commit 851a7fc

Browse files
authored
Merge pull request #78 from ndeloof/square_angles
support both square brackets and raw syntax for IPV6
2 parents eb34a0b + 6e64bca commit 851a7fc

File tree

2 files changed

+82
-33
lines changed

2 files changed

+82
-33
lines changed

nat/nat.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,16 @@ func splitParts(rawport string) (string, string, string) {
175175
// ParsePortSpec parses a port specification string into a slice of PortMappings
176176
func ParsePortSpec(rawPort string) ([]PortMapping, error) {
177177
var proto string
178-
rawIP, hostPort, containerPort := splitParts(rawPort)
178+
ip, hostPort, containerPort := splitParts(rawPort)
179179
proto, containerPort = SplitProtoPort(containerPort)
180180

181-
// Strip [] from IPV6 addresses
182-
ip, _, err := net.SplitHostPort(rawIP + ":")
183-
if err != nil {
184-
return nil, fmt.Errorf("Invalid ip address %v: %s", rawIP, err)
181+
if ip != "" && ip[0] == '[' {
182+
// Strip [] from IPV6 addresses
183+
rawIP, _, err := net.SplitHostPort(ip + ":")
184+
if err != nil {
185+
return nil, fmt.Errorf("Invalid ip address %v: %s", ip, err)
186+
}
187+
ip = rawIP
185188
}
186189
if ip != "" && net.ParseIP(ip) == nil {
187190
return nil, fmt.Errorf("Invalid ip address: %s", ip)

nat/nat_test.go

Lines changed: 74 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -201,42 +201,88 @@ func TestParsePortSpecFull(t *testing.T) {
201201
}
202202

203203
func TestPartPortSpecIPV6(t *testing.T) {
204-
portMappings, err := ParsePortSpec("[2001:4860:0:2001::68]::333")
205-
if err != nil {
206-
t.Fatalf("expected nil error, got: %v", err)
204+
type test struct {
205+
name string
206+
spec string
207+
expected []PortMapping
207208
}
208-
209-
expected := []PortMapping{
209+
cases := []test{
210210
{
211-
Port: "333/tcp",
212-
Binding: PortBinding{
213-
HostIP: "2001:4860:0:2001::68",
214-
HostPort: "",
211+
name: "square angled IPV6 without host port",
212+
spec: "[2001:4860:0:2001::68]::333",
213+
expected: []PortMapping{
214+
{
215+
Port: "333/tcp",
216+
Binding: PortBinding{
217+
HostIP: "2001:4860:0:2001::68",
218+
HostPort: "",
219+
},
220+
},
215221
},
216222
},
217-
}
218-
if !reflect.DeepEqual(expected, portMappings) {
219-
t.Fatalf("wrong port mappings: got=%v, want=%v", portMappings, expected)
220-
}
221-
}
222-
223-
func TestPartPortSpecIPV6WithHostPort(t *testing.T) {
224-
portMappings, err := ParsePortSpec("[::1]:80:80")
225-
if err != nil {
226-
t.Fatalf("expected nil error, got: %v", err)
227-
}
228-
229-
expected := []PortMapping{
230223
{
231-
Port: "80/tcp",
232-
Binding: PortBinding{
233-
HostIP: "::1",
234-
HostPort: "80",
224+
name: "square angled IPV6 with host port",
225+
spec: "[::1]:80:80",
226+
expected: []PortMapping{
227+
{
228+
Port: "80/tcp",
229+
Binding: PortBinding{
230+
HostIP: "::1",
231+
HostPort: "80",
232+
},
233+
},
234+
},
235+
},
236+
{
237+
name: "IPV6 without host port",
238+
spec: "2001:4860:0:2001::68::333",
239+
expected: []PortMapping{
240+
{
241+
Port: "333/tcp",
242+
Binding: PortBinding{
243+
HostIP: "2001:4860:0:2001::68",
244+
HostPort: "",
245+
},
246+
},
247+
},
248+
},
249+
{
250+
name: "IPV6 with host port",
251+
spec: "::1:80:80",
252+
expected: []PortMapping{
253+
{
254+
Port: "80/tcp",
255+
Binding: PortBinding{
256+
HostIP: "::1",
257+
HostPort: "80",
258+
},
259+
},
260+
},
261+
},
262+
{
263+
name: ":: IPV6, without host port",
264+
spec: "::::80",
265+
expected: []PortMapping{
266+
{
267+
Port: "80/tcp",
268+
Binding: PortBinding{
269+
HostIP: "::",
270+
HostPort: "",
271+
},
272+
},
235273
},
236274
},
237275
}
238-
if !reflect.DeepEqual(expected, portMappings) {
239-
t.Fatalf("wrong port mappings: got=%v, want=%v", portMappings, expected)
276+
for _, c := range cases {
277+
t.Run(c.name, func(t *testing.T) {
278+
portMappings, err := ParsePortSpec(c.spec)
279+
if err != nil {
280+
t.Fatalf("expected nil error, got: %v", err)
281+
}
282+
if !reflect.DeepEqual(c.expected, portMappings) {
283+
t.Fatalf("wrong port mappings: got=%v, want=%v", portMappings, c.expected)
284+
}
285+
})
240286
}
241287
}
242288

0 commit comments

Comments
 (0)