mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #16950 from cpuguy83/support_ipv6_in_host_parser
Allow API to bind to ipv6 addresses
This commit is contained in:
commit
4fc97e40c9
2 changed files with 47 additions and 23 deletions
|
@ -5,6 +5,7 @@ package parsers
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"path"
|
||||
"runtime"
|
||||
|
@ -74,26 +75,33 @@ func ParseTCPAddr(tryAddr string, defaultAddr string) (string, error) {
|
|||
if err != nil {
|
||||
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)
|
||||
}
|
||||
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 == "" {
|
||||
host = strings.TrimPrefix(defaults[1], "//")
|
||||
host = defaultHost
|
||||
}
|
||||
if hostParts[1] == "" {
|
||||
hostParts[1] = defaults[2]
|
||||
if port == "" {
|
||||
port = defaultPort
|
||||
}
|
||||
p, err := strconv.Atoi(hostParts[1])
|
||||
p, err := strconv.Atoi(port)
|
||||
if err != nil && p == 0 {
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -31,6 +31,10 @@ func TestParseDockerDaemonHost(t *testing.T) {
|
|||
"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/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/path": "tcp://127.0.0.1:6666/path",
|
||||
"": defaultHOST,
|
||||
|
@ -44,6 +48,9 @@ func TestParseDockerDaemonHost(t *testing.T) {
|
|||
"unix://": "unix:///var/run/docker.sock",
|
||||
"fd://": "fd://",
|
||||
"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 {
|
||||
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",
|
||||
"tcp://:7777": "tcp://127.0.0.1:7777",
|
||||
"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 {
|
||||
if addr, err := ParseTCPAddr(invalidAddr, defaultHTTPHost); err == nil || err.Error() != expectedError {
|
||||
|
|
Loading…
Reference in a new issue