2018-02-05 21:05:59 +00:00
|
|
|
package opts // import "github.com/docker/docker/opts"
|
2015-12-16 01:53:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
2018-10-15 07:52:53 +00:00
|
|
|
"path/filepath"
|
2015-12-16 01:53:17 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2018-10-15 07:52:53 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/homedir"
|
2015-12-16 01:53:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-12-26 14:27:56 +00:00
|
|
|
// DefaultHTTPPort Default HTTP Port used if only the protocol is provided to -H flag e.g. dockerd -H tcp://
|
2015-12-16 01:53:17 +00:00
|
|
|
// These are the IANA registered port numbers for use with Docker
|
|
|
|
// see http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml?search=docker
|
|
|
|
DefaultHTTPPort = 2375 // Default HTTP Port
|
|
|
|
// DefaultTLSHTTPPort Default HTTP Port used when TLS enabled
|
|
|
|
DefaultTLSHTTPPort = 2376 // Default TLS encrypted HTTP Port
|
|
|
|
// DefaultUnixSocket Path for the unix socket.
|
|
|
|
// Docker daemon by default always listens on the default unix socket
|
|
|
|
DefaultUnixSocket = "/var/run/docker.sock"
|
|
|
|
// DefaultTCPHost constant defines the default host string used by docker on Windows
|
|
|
|
DefaultTCPHost = fmt.Sprintf("tcp://%s:%d", DefaultHTTPHost, DefaultHTTPPort)
|
|
|
|
// DefaultTLSHost constant defines the default host string used by docker for TLS sockets
|
|
|
|
DefaultTLSHost = fmt.Sprintf("tcp://%s:%d", DefaultHTTPHost, DefaultTLSHTTPPort)
|
2016-01-31 02:45:49 +00:00
|
|
|
// DefaultNamedPipe defines the default named pipe used by docker on Windows
|
|
|
|
DefaultNamedPipe = `//./pipe/docker_engine`
|
2015-12-16 01:53:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ValidateHost validates that the specified string is a valid host and returns it.
|
|
|
|
func ValidateHost(val string) (string, error) {
|
2016-01-31 02:45:49 +00:00
|
|
|
host := strings.TrimSpace(val)
|
2017-10-25 12:39:51 +00:00
|
|
|
// The empty string means default and is not handled by parseDaemonHost
|
2016-01-31 02:45:49 +00:00
|
|
|
if host != "" {
|
2017-10-25 12:39:51 +00:00
|
|
|
_, err := parseDaemonHost(host)
|
2016-01-31 02:45:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return val, err
|
|
|
|
}
|
2015-12-16 01:53:17 +00:00
|
|
|
}
|
|
|
|
// Note: unlike most flag validators, we don't return the mutated value here
|
2016-12-20 11:14:41 +00:00
|
|
|
// we need to know what the user entered later (using ParseHost) to adjust for TLS
|
2015-12-16 01:53:17 +00:00
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
2018-10-15 07:52:53 +00:00
|
|
|
// ParseHost and set defaults for a Daemon host string.
|
|
|
|
// defaultToTLS is preferred over defaultToUnixRootless.
|
|
|
|
func ParseHost(defaultToTLS, defaultToUnixRootless bool, val string) (string, error) {
|
2016-01-31 02:45:49 +00:00
|
|
|
host := strings.TrimSpace(val)
|
|
|
|
if host == "" {
|
|
|
|
if defaultToTLS {
|
|
|
|
host = DefaultTLSHost
|
2018-10-15 07:52:53 +00:00
|
|
|
} else if defaultToUnixRootless {
|
|
|
|
runtimeDir, err := homedir.GetRuntimeDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
socket := filepath.Join(runtimeDir, "docker.sock")
|
|
|
|
host = "unix://" + socket
|
2016-01-31 02:45:49 +00:00
|
|
|
} else {
|
|
|
|
host = DefaultHost
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var err error
|
2017-10-25 12:39:51 +00:00
|
|
|
host, err = parseDaemonHost(host)
|
2016-01-31 02:45:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return val, err
|
|
|
|
}
|
2015-12-16 01:53:17 +00:00
|
|
|
}
|
|
|
|
return host, nil
|
|
|
|
}
|
|
|
|
|
2017-10-25 12:39:51 +00:00
|
|
|
// parseDaemonHost parses the specified address and returns an address that will be used as the host.
|
2016-01-31 02:45:49 +00:00
|
|
|
// Depending of the address specified, this may return one of the global Default* strings defined in hosts.go.
|
2017-10-25 12:39:51 +00:00
|
|
|
func parseDaemonHost(addr string) (string, error) {
|
2016-06-22 00:14:55 +00:00
|
|
|
addrParts := strings.SplitN(addr, "://", 2)
|
2016-01-31 02:45:49 +00:00
|
|
|
if len(addrParts) == 1 && addrParts[0] != "" {
|
2015-12-16 01:53:17 +00:00
|
|
|
addrParts = []string{"tcp", addrParts[0]}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch addrParts[0] {
|
|
|
|
case "tcp":
|
2016-06-21 21:27:04 +00:00
|
|
|
return ParseTCPAddr(addrParts[1], DefaultTCPHost)
|
2015-12-16 01:53:17 +00:00
|
|
|
case "unix":
|
2016-01-31 02:45:49 +00:00
|
|
|
return parseSimpleProtoAddr("unix", addrParts[1], DefaultUnixSocket)
|
|
|
|
case "npipe":
|
|
|
|
return parseSimpleProtoAddr("npipe", addrParts[1], DefaultNamedPipe)
|
2015-12-16 01:53:17 +00:00
|
|
|
case "fd":
|
|
|
|
return addr, nil
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("Invalid bind address format: %s", addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-31 02:45:49 +00:00
|
|
|
// parseSimpleProtoAddr parses and validates that the specified address is a valid
|
|
|
|
// socket address for simple protocols like unix and npipe. It returns a formatted
|
|
|
|
// socket address, either using the address parsed from addr, or the contents of
|
|
|
|
// defaultAddr if addr is a blank string.
|
|
|
|
func parseSimpleProtoAddr(proto, addr, defaultAddr string) (string, error) {
|
|
|
|
addr = strings.TrimPrefix(addr, proto+"://")
|
2015-12-16 01:53:17 +00:00
|
|
|
if strings.Contains(addr, "://") {
|
2016-01-31 02:45:49 +00:00
|
|
|
return "", fmt.Errorf("Invalid proto, expected %s: %s", proto, addr)
|
2015-12-16 01:53:17 +00:00
|
|
|
}
|
|
|
|
if addr == "" {
|
|
|
|
addr = defaultAddr
|
|
|
|
}
|
2016-01-31 02:45:49 +00:00
|
|
|
return fmt.Sprintf("%s://%s", proto, addr), nil
|
2015-12-16 01:53:17 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 21:27:04 +00:00
|
|
|
// ParseTCPAddr parses and validates that the specified address is a valid TCP
|
2015-12-16 01:53:17 +00:00
|
|
|
// address. It returns a formatted TCP address, either using the address parsed
|
|
|
|
// from tryAddr, or the contents of defaultAddr if tryAddr is a blank string.
|
|
|
|
// tryAddr is expected to have already been Trim()'d
|
|
|
|
// defaultAddr must be in the full `tcp://host:port` form
|
2016-06-21 21:27:04 +00:00
|
|
|
func ParseTCPAddr(tryAddr string, defaultAddr string) (string, error) {
|
2015-12-16 01:53:17 +00:00
|
|
|
if tryAddr == "" || tryAddr == "tcp://" {
|
|
|
|
return defaultAddr, nil
|
|
|
|
}
|
|
|
|
addr := strings.TrimPrefix(tryAddr, "tcp://")
|
|
|
|
if strings.Contains(addr, "://") || addr == "" {
|
|
|
|
return "", fmt.Errorf("Invalid proto, expected tcp: %s", tryAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultAddr = strings.TrimPrefix(defaultAddr, "tcp://")
|
|
|
|
defaultHost, defaultPort, err := net.SplitHostPort(defaultAddr)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
// url.Parse fails for trailing colon on IPv6 brackets on Go 1.5, but
|
|
|
|
// not 1.4. See https://github.com/golang/go/issues/12200 and
|
|
|
|
// https://github.com/golang/go/issues/6530.
|
|
|
|
if strings.HasSuffix(addr, "]:") {
|
|
|
|
addr += defaultPort
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse("tcp://" + addr)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
host, port, err := net.SplitHostPort(u.Host)
|
2016-06-21 21:27:04 +00:00
|
|
|
if err != nil {
|
|
|
|
// try port addition once
|
|
|
|
host, port, err = net.SplitHostPort(net.JoinHostPort(u.Host, defaultPort))
|
|
|
|
}
|
2015-12-16 01:53:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Invalid bind address format: %s", tryAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if host == "" {
|
|
|
|
host = defaultHost
|
|
|
|
}
|
|
|
|
if port == "" {
|
|
|
|
port = defaultPort
|
|
|
|
}
|
|
|
|
p, err := strconv.Atoi(port)
|
|
|
|
if err != nil && p == 0 {
|
|
|
|
return "", fmt.Errorf("Invalid bind address format: %s", tryAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("tcp://%s%s", net.JoinHostPort(host, port), u.Path), nil
|
|
|
|
}
|
2016-12-23 19:09:12 +00:00
|
|
|
|
|
|
|
// ValidateExtraHost validates that the specified string is a valid extrahost and returns it.
|
|
|
|
// ExtraHost is in the form of name:ip where the ip has to be a valid ip (IPv4 or IPv6).
|
|
|
|
func ValidateExtraHost(val string) (string, error) {
|
|
|
|
// allow for IPv6 addresses in extra hosts by only splitting on first ":"
|
|
|
|
arr := strings.SplitN(val, ":", 2)
|
|
|
|
if len(arr) != 2 || len(arr[0]) == 0 {
|
|
|
|
return "", fmt.Errorf("bad format for add-host: %q", val)
|
|
|
|
}
|
|
|
|
if _, err := ValidateIPAddress(arr[1]); err != nil {
|
|
|
|
return "", fmt.Errorf("invalid IP address in add-host: %q", arr[1])
|
|
|
|
}
|
|
|
|
return val, nil
|
|
|
|
}
|