github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/cli/opts/hosts.go (about) 1 package opts 2 3 import ( 4 "fmt" 5 "net" 6 "net/url" 7 "strconv" 8 "strings" 9 ) 10 11 const ( 12 // defaultHTTPPort Default HTTP Port used if only the protocol is provided to -H flag e.g. dockerd -H tcp:// 13 // These are the IANA registered port numbers for use with Docker 14 // see http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml?search=docker 15 defaultHTTPPort = "2375" // Default HTTP Port 16 // defaultTLSHTTPPort Default HTTP Port used when TLS enabled 17 defaultTLSHTTPPort = "2376" // Default TLS encrypted HTTP Port 18 // defaultUnixSocket Path for the unix socket. 19 // Docker daemon by default always listens on the default unix socket 20 defaultUnixSocket = "/var/run/docker.sock" 21 // defaultTCPHost constant defines the default host string used by docker on Windows 22 defaultTCPHost = "tcp://" + defaultHTTPHost + ":" + defaultHTTPPort 23 // DefaultTLSHost constant defines the default host string used by docker for TLS sockets 24 defaultTLSHost = "tcp://" + defaultHTTPHost + ":" + defaultTLSHTTPPort 25 // DefaultNamedPipe defines the default named pipe used by docker on Windows 26 defaultNamedPipe = `//./pipe/docker_engine` 27 // hostGatewayName defines a special string which users can append to --add-host 28 // to add an extra entry in /etc/hosts that maps host.docker.internal to the host IP 29 // TODO Consider moving the hostGatewayName constant defined in docker at 30 // github.com/docker/docker/daemon/network/constants.go outside of the "daemon" 31 // package, so that the CLI can consume it. 32 hostGatewayName = "host-gateway" 33 ) 34 35 // ValidateHost validates that the specified string is a valid host and returns it. 36 func ValidateHost(val string) (string, error) { 37 host := strings.TrimSpace(val) 38 // The empty string means default and is not handled by parseDockerDaemonHost 39 if host != "" { 40 _, err := parseDockerDaemonHost(host) 41 if err != nil { 42 return val, err 43 } 44 } 45 // Note: unlike most flag validators, we don't return the mutated value here 46 // we need to know what the user entered later (using ParseHost) to adjust for TLS 47 return val, nil 48 } 49 50 // ParseHost and set defaults for a Daemon host string 51 func ParseHost(defaultToTLS bool, val string) (string, error) { 52 host := strings.TrimSpace(val) 53 if host == "" { 54 if defaultToTLS { 55 host = defaultTLSHost 56 } else { 57 host = defaultHost 58 } 59 } else { 60 var err error 61 host, err = parseDockerDaemonHost(host) 62 if err != nil { 63 return val, err 64 } 65 } 66 return host, nil 67 } 68 69 // parseDockerDaemonHost parses the specified address and returns an address that will be used as the host. 70 // Depending of the address specified, this may return one of the global Default* strings defined in hosts.go. 71 func parseDockerDaemonHost(addr string) (string, error) { 72 addrParts := strings.SplitN(addr, "://", 2) 73 if len(addrParts) == 1 && addrParts[0] != "" { 74 addrParts = []string{"tcp", addrParts[0]} 75 } 76 77 switch addrParts[0] { 78 case "tcp": 79 return ParseTCPAddr(addrParts[1], defaultTCPHost) 80 case "unix": 81 return parseSimpleProtoAddr("unix", addrParts[1], defaultUnixSocket) 82 case "npipe": 83 return parseSimpleProtoAddr("npipe", addrParts[1], defaultNamedPipe) 84 case "fd": 85 return addr, nil 86 case "ssh": 87 return addr, nil 88 default: 89 return "", fmt.Errorf("Invalid bind address format: %s", addr) 90 } 91 } 92 93 // parseSimpleProtoAddr parses and validates that the specified address is a valid 94 // socket address for simple protocols like unix and npipe. It returns a formatted 95 // socket address, either using the address parsed from addr, or the contents of 96 // defaultAddr if addr is a blank string. 97 func parseSimpleProtoAddr(proto, addr, defaultAddr string) (string, error) { 98 addr = strings.TrimPrefix(addr, proto+"://") 99 if strings.Contains(addr, "://") { 100 return "", fmt.Errorf("Invalid proto, expected %s: %s", proto, addr) 101 } 102 if addr == "" { 103 addr = defaultAddr 104 } 105 return fmt.Sprintf("%s://%s", proto, addr), nil 106 } 107 108 // ParseTCPAddr parses and validates that the specified address is a valid TCP 109 // address. It returns a formatted TCP address, either using the address parsed 110 // from tryAddr, or the contents of defaultAddr if tryAddr is a blank string. 111 // tryAddr is expected to have already been Trim()'d 112 // defaultAddr must be in the full `tcp://host:port` form 113 func ParseTCPAddr(tryAddr string, defaultAddr string) (string, error) { 114 if tryAddr == "" || tryAddr == "tcp://" { 115 return defaultAddr, nil 116 } 117 addr := strings.TrimPrefix(tryAddr, "tcp://") 118 if strings.Contains(addr, "://") || addr == "" { 119 return "", fmt.Errorf("Invalid proto, expected tcp: %s", tryAddr) 120 } 121 122 defaultAddr = strings.TrimPrefix(defaultAddr, "tcp://") 123 defaultHost, defaultPort, err := net.SplitHostPort(defaultAddr) 124 if err != nil { 125 return "", err 126 } 127 // url.Parse fails for trailing colon on IPv6 brackets on Go 1.5, but 128 // not 1.4. See https://github.com/golang/go/issues/12200 and 129 // https://github.com/golang/go/issues/6530. 130 if strings.HasSuffix(addr, "]:") { 131 addr += defaultPort 132 } 133 134 u, err := url.Parse("tcp://" + addr) 135 if err != nil { 136 return "", err 137 } 138 host, port, err := net.SplitHostPort(u.Host) 139 if err != nil { 140 // try port addition once 141 host, port, err = net.SplitHostPort(net.JoinHostPort(u.Host, defaultPort)) 142 } 143 if err != nil { 144 return "", fmt.Errorf("Invalid bind address format: %s", tryAddr) 145 } 146 147 if host == "" { 148 host = defaultHost 149 } 150 if port == "" { 151 port = defaultPort 152 } 153 p, err := strconv.Atoi(port) 154 if err != nil && p == 0 { 155 return "", fmt.Errorf("Invalid bind address format: %s", tryAddr) 156 } 157 158 return fmt.Sprintf("tcp://%s%s", net.JoinHostPort(host, port), u.Path), nil 159 } 160 161 // ValidateExtraHost validates that the specified string is a valid extrahost and returns it. 162 // ExtraHost is in the form of name:ip where the ip has to be a valid ip (IPv4 or IPv6). 163 func ValidateExtraHost(val string) (string, error) { 164 // allow for IPv6 addresses in extra hosts by only splitting on first ":" 165 arr := strings.SplitN(val, ":", 2) 166 if len(arr) != 2 || len(arr[0]) == 0 { 167 return "", fmt.Errorf("bad format for add-host: %q", val) 168 } 169 // Skip IPaddr validation for "host-gateway" string 170 if arr[1] != hostGatewayName { 171 if _, err := ValidateIPAddress(arr[1]); err != nil { 172 return "", fmt.Errorf("invalid IP address in add-host: %q", arr[1]) 173 } 174 } 175 return val, nil 176 }