mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Allow API to bind to ipv6 addresses
Use `net.SplitHostPort` which supports ipv6 rather than relying on splitting on `:` Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
parent
56ed0a6e8c
commit
a82e3bc704
2 changed files with 47 additions and 23 deletions
|
@ -5,6 +5,7 @@ package parsers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
@ -74,26 +75,33 @@ func ParseTCPAddr(tryAddr string, defaultAddr string) (string, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
hostParts := strings.Split(u.Host, ":")
|
|
||||||
if len(hostParts) != 2 {
|
host, port, err := net.SplitHostPort(u.Host)
|
||||||
|
if err != nil {
|
||||||
return "", fmt.Errorf("Invalid bind address format: %s", tryAddr)
|
return "", fmt.Errorf("Invalid bind address format: %s", tryAddr)
|
||||||
}
|
}
|
||||||
defaults := strings.Split(defaultAddr, ":")
|
|
||||||
if len(defaults) != 3 {
|
|
||||||
return "", fmt.Errorf("Invalid defaults address format: %s", defaultAddr)
|
|
||||||
}
|
|
||||||
|
|
||||||
host := hostParts[0]
|
defaultAddr = strings.TrimPrefix(defaultAddr, "tcp://")
|
||||||
|
defaultHost, defaultPort, err := net.SplitHostPort(defaultAddr)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
if host == "" {
|
if host == "" {
|
||||||
host = strings.TrimPrefix(defaults[1], "//")
|
host = defaultHost
|
||||||
}
|
}
|
||||||
if hostParts[1] == "" {
|
if port == "" {
|
||||||
hostParts[1] = defaults[2]
|
port = defaultPort
|
||||||
}
|
}
|
||||||
p, err := strconv.Atoi(hostParts[1])
|
p, err := strconv.Atoi(port)
|
||||||
if err != nil && p == 0 {
|
if err != nil && p == 0 {
|
||||||
return "", fmt.Errorf("Invalid bind address format: %s", tryAddr)
|
return "", fmt.Errorf("Invalid bind address format: %s", tryAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if net.ParseIP(host).To4() == nil && strings.Contains(host, ":") {
|
||||||
|
// This is either an ipv6 address
|
||||||
|
host = "[" + host + "]"
|
||||||
|
}
|
||||||
return fmt.Sprintf("tcp://%s:%d%s", host, p, u.Path), nil
|
return fmt.Sprintf("tcp://%s:%d%s", host, p, u.Path), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,10 @@ func TestParseDockerDaemonHost(t *testing.T) {
|
||||||
"0.0.0.1:": "tcp://0.0.0.1:2376",
|
"0.0.0.1:": "tcp://0.0.0.1:2376",
|
||||||
"0.0.0.1:5555": "tcp://0.0.0.1:5555",
|
"0.0.0.1:5555": "tcp://0.0.0.1:5555",
|
||||||
"0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path",
|
"0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path",
|
||||||
|
"[::1]:": "tcp://[::1]:2376",
|
||||||
|
"[::1]:5555/path": "tcp://[::1]:5555/path",
|
||||||
|
"[0:0:0:0:0:0:0:1]:": "tcp://[0:0:0:0:0:0:0:1]:2376",
|
||||||
|
"[0:0:0:0:0:0:0:1]:5555/path": "tcp://[0:0:0:0:0:0:0:1]:5555/path",
|
||||||
":6666": "tcp://127.0.0.1:6666",
|
":6666": "tcp://127.0.0.1:6666",
|
||||||
":6666/path": "tcp://127.0.0.1:6666/path",
|
":6666/path": "tcp://127.0.0.1:6666/path",
|
||||||
"": defaultHOST,
|
"": defaultHOST,
|
||||||
|
@ -44,6 +48,9 @@ func TestParseDockerDaemonHost(t *testing.T) {
|
||||||
"unix://": "unix:///var/run/docker.sock",
|
"unix://": "unix:///var/run/docker.sock",
|
||||||
"fd://": "fd://",
|
"fd://": "fd://",
|
||||||
"fd://something": "fd://something",
|
"fd://something": "fd://something",
|
||||||
|
"localhost:": "tcp://localhost:2376",
|
||||||
|
"localhost:5555": "tcp://localhost:5555",
|
||||||
|
"localhost:5555/path": "tcp://localhost:5555/path",
|
||||||
}
|
}
|
||||||
for invalidAddr, expectedError := range invalids {
|
for invalidAddr, expectedError := range invalids {
|
||||||
if addr, err := ParseDockerDaemonHost(defaultHTTPHost, defaultUnix, invalidAddr); err == nil || err.Error() != expectedError {
|
if addr, err := ParseDockerDaemonHost(defaultHTTPHost, defaultUnix, invalidAddr); err == nil || err.Error() != expectedError {
|
||||||
|
@ -78,6 +85,15 @@ func TestParseTCP(t *testing.T) {
|
||||||
":6666/path": "tcp://127.0.0.1:6666/path",
|
":6666/path": "tcp://127.0.0.1:6666/path",
|
||||||
"tcp://:7777": "tcp://127.0.0.1:7777",
|
"tcp://:7777": "tcp://127.0.0.1:7777",
|
||||||
"tcp://:7777/path": "tcp://127.0.0.1:7777/path",
|
"tcp://:7777/path": "tcp://127.0.0.1:7777/path",
|
||||||
|
"[::1]:": "tcp://[::1]:2376",
|
||||||
|
"[::1]:5555": "tcp://[::1]:5555",
|
||||||
|
"[::1]:5555/path": "tcp://[::1]:5555/path",
|
||||||
|
"[0:0:0:0:0:0:0:1]:": "tcp://[0:0:0:0:0:0:0:1]:2376",
|
||||||
|
"[0:0:0:0:0:0:0:1]:5555": "tcp://[0:0:0:0:0:0:0:1]:5555",
|
||||||
|
"[0:0:0:0:0:0:0:1]:5555/path": "tcp://[0:0:0:0:0:0:0:1]:5555/path",
|
||||||
|
"localhost:": "tcp://localhost:2376",
|
||||||
|
"localhost:5555": "tcp://localhost:5555",
|
||||||
|
"localhost:5555/path": "tcp://localhost:5555/path",
|
||||||
}
|
}
|
||||||
for invalidAddr, expectedError := range invalids {
|
for invalidAddr, expectedError := range invalids {
|
||||||
if addr, err := ParseTCPAddr(invalidAddr, defaultHTTPHost); err == nil || err.Error() != expectedError {
|
if addr, err := ParseTCPAddr(invalidAddr, defaultHTTPHost); err == nil || err.Error() != expectedError {
|
||||||
|
|
Loading…
Reference in a new issue