Skip to content

Commit

Permalink
Merge pull request #78 from ndeloof/square_angles
Browse files Browse the repository at this point in the history
support both square brackets and raw syntax for IPV6
  • Loading branch information
thaJeztah authored Mar 15, 2021
2 parents eb34a0b + 6e64bca commit 851a7fc
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 33 deletions.
13 changes: 8 additions & 5 deletions nat/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,16 @@ func splitParts(rawport string) (string, string, string) {
// ParsePortSpec parses a port specification string into a slice of PortMappings
func ParsePortSpec(rawPort string) ([]PortMapping, error) {
var proto string
rawIP, hostPort, containerPort := splitParts(rawPort)
ip, hostPort, containerPort := splitParts(rawPort)
proto, containerPort = SplitProtoPort(containerPort)

// Strip [] from IPV6 addresses
ip, _, err := net.SplitHostPort(rawIP + ":")
if err != nil {
return nil, fmt.Errorf("Invalid ip address %v: %s", rawIP, err)
if ip != "" && ip[0] == '[' {
// Strip [] from IPV6 addresses
rawIP, _, err := net.SplitHostPort(ip + ":")
if err != nil {
return nil, fmt.Errorf("Invalid ip address %v: %s", ip, err)
}
ip = rawIP
}
if ip != "" && net.ParseIP(ip) == nil {
return nil, fmt.Errorf("Invalid ip address: %s", ip)
Expand Down
102 changes: 74 additions & 28 deletions nat/nat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,42 +201,88 @@ func TestParsePortSpecFull(t *testing.T) {
}

func TestPartPortSpecIPV6(t *testing.T) {
portMappings, err := ParsePortSpec("[2001:4860:0:2001::68]::333")
if err != nil {
t.Fatalf("expected nil error, got: %v", err)
type test struct {
name string
spec string
expected []PortMapping
}

expected := []PortMapping{
cases := []test{
{
Port: "333/tcp",
Binding: PortBinding{
HostIP: "2001:4860:0:2001::68",
HostPort: "",
name: "square angled IPV6 without host port",
spec: "[2001:4860:0:2001::68]::333",
expected: []PortMapping{
{
Port: "333/tcp",
Binding: PortBinding{
HostIP: "2001:4860:0:2001::68",
HostPort: "",
},
},
},
},
}
if !reflect.DeepEqual(expected, portMappings) {
t.Fatalf("wrong port mappings: got=%v, want=%v", portMappings, expected)
}
}

func TestPartPortSpecIPV6WithHostPort(t *testing.T) {
portMappings, err := ParsePortSpec("[::1]:80:80")
if err != nil {
t.Fatalf("expected nil error, got: %v", err)
}

expected := []PortMapping{
{
Port: "80/tcp",
Binding: PortBinding{
HostIP: "::1",
HostPort: "80",
name: "square angled IPV6 with host port",
spec: "[::1]:80:80",
expected: []PortMapping{
{
Port: "80/tcp",
Binding: PortBinding{
HostIP: "::1",
HostPort: "80",
},
},
},
},
{
name: "IPV6 without host port",
spec: "2001:4860:0:2001::68::333",
expected: []PortMapping{
{
Port: "333/tcp",
Binding: PortBinding{
HostIP: "2001:4860:0:2001::68",
HostPort: "",
},
},
},
},
{
name: "IPV6 with host port",
spec: "::1:80:80",
expected: []PortMapping{
{
Port: "80/tcp",
Binding: PortBinding{
HostIP: "::1",
HostPort: "80",
},
},
},
},
{
name: ":: IPV6, without host port",
spec: "::::80",
expected: []PortMapping{
{
Port: "80/tcp",
Binding: PortBinding{
HostIP: "::",
HostPort: "",
},
},
},
},
}
if !reflect.DeepEqual(expected, portMappings) {
t.Fatalf("wrong port mappings: got=%v, want=%v", portMappings, expected)
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
portMappings, err := ParsePortSpec(c.spec)
if err != nil {
t.Fatalf("expected nil error, got: %v", err)
}
if !reflect.DeepEqual(c.expected, portMappings) {
t.Fatalf("wrong port mappings: got=%v, want=%v", portMappings, c.expected)
}
})
}
}

Expand Down

0 comments on commit 851a7fc

Please sign in to comment.