hosts.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package opts // import "github.com/docker/docker/opts"
  2. import (
  3. "net"
  4. "net/url"
  5. "path/filepath"
  6. "strconv"
  7. "strings"
  8. "github.com/docker/docker/pkg/homedir"
  9. "github.com/pkg/errors"
  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 + ":2375"
  23. // DefaultTLSHost constant defines the default host string used by docker for TLS sockets
  24. DefaultTLSHost = "tcp://" + DefaultHTTPHost + ":2376"
  25. // DefaultNamedPipe defines the default named pipe used by docker on Windows
  26. DefaultNamedPipe = `//./pipe/docker_engine`
  27. // HostGatewayName is the string value that can be passed
  28. // to the IPAddr section in --add-host that is replaced by
  29. // the value of HostGatewayIP daemon config value
  30. HostGatewayName = "host-gateway"
  31. )
  32. // ValidateHost validates that the specified string is a valid host and returns it.
  33. func ValidateHost(val string) (string, error) {
  34. host := strings.TrimSpace(val)
  35. // The empty string means default and is not handled by parseDaemonHost
  36. if host != "" {
  37. _, err := parseDaemonHost(host)
  38. if err != nil {
  39. return val, err
  40. }
  41. }
  42. // Note: unlike most flag validators, we don't return the mutated value here
  43. // we need to know what the user entered later (using ParseHost) to adjust for TLS
  44. return val, nil
  45. }
  46. // ParseHost and set defaults for a Daemon host string.
  47. // defaultToTLS is preferred over defaultToUnixXDG.
  48. func ParseHost(defaultToTLS, defaultToUnixXDG bool, val string) (string, error) {
  49. host := strings.TrimSpace(val)
  50. if host == "" {
  51. if defaultToTLS {
  52. host = DefaultTLSHost
  53. } else if defaultToUnixXDG {
  54. runtimeDir, err := homedir.GetRuntimeDir()
  55. if err != nil {
  56. return "", err
  57. }
  58. host = "unix://" + filepath.Join(runtimeDir, "docker.sock")
  59. } else {
  60. host = DefaultHost
  61. }
  62. } else {
  63. var err error
  64. host, err = parseDaemonHost(host)
  65. if err != nil {
  66. return val, err
  67. }
  68. }
  69. return host, nil
  70. }
  71. // parseDaemonHost parses the specified address and returns an address that will be used as the host.
  72. // Depending on the address specified, this may return one of the global Default* strings defined in hosts.go.
  73. func parseDaemonHost(address string) (string, error) {
  74. proto, addr, ok := strings.Cut(address, "://")
  75. if !ok && proto != "" {
  76. addr = proto
  77. proto = "tcp"
  78. }
  79. switch proto {
  80. case "tcp":
  81. return ParseTCPAddr(address, DefaultTCPHost)
  82. case "unix":
  83. a, err := parseSimpleProtoAddr(proto, addr, DefaultUnixSocket)
  84. if err != nil {
  85. return "", errors.Wrapf(err, "invalid bind address (%s)", address)
  86. }
  87. return a, nil
  88. case "npipe":
  89. a, err := parseSimpleProtoAddr(proto, addr, DefaultNamedPipe)
  90. if err != nil {
  91. return "", errors.Wrapf(err, "invalid bind address (%s)", address)
  92. }
  93. return a, nil
  94. case "fd":
  95. return address, nil
  96. default:
  97. return "", errors.Errorf("invalid bind address (%s): unsupported proto '%s'", address, proto)
  98. }
  99. }
  100. // parseSimpleProtoAddr parses and validates that the specified address is a valid
  101. // socket address for simple protocols like unix and npipe. It returns a formatted
  102. // socket address, either using the address parsed from addr, or the contents of
  103. // defaultAddr if addr is a blank string.
  104. func parseSimpleProtoAddr(proto, addr, defaultAddr string) (string, error) {
  105. if strings.Contains(addr, "://") {
  106. return "", errors.Errorf("invalid %s address: %s", proto, addr)
  107. }
  108. if addr == "" {
  109. addr = defaultAddr
  110. }
  111. return proto + "://" + addr, nil
  112. }
  113. // ParseTCPAddr parses and validates that the specified address is a valid TCP
  114. // address. It returns a formatted TCP address, either using the address parsed
  115. // from tryAddr, or the contents of defaultAddr if tryAddr is a blank string.
  116. // tryAddr is expected to have already been Trim()'d
  117. // defaultAddr must be in the full `tcp://host:port` form
  118. func ParseTCPAddr(tryAddr string, defaultAddr string) (string, error) {
  119. def, err := parseTCPAddr(defaultAddr, true)
  120. if err != nil {
  121. return "", errors.Wrapf(err, "invalid default address (%s)", defaultAddr)
  122. }
  123. addr, err := parseTCPAddr(tryAddr, false)
  124. if err != nil {
  125. return "", errors.Wrapf(err, "invalid bind address (%s)", tryAddr)
  126. }
  127. host := addr.Hostname()
  128. if host == "" {
  129. host = def.Hostname()
  130. }
  131. port := addr.Port()
  132. if port == "" {
  133. port = def.Port()
  134. }
  135. return "tcp://" + net.JoinHostPort(host, port), nil
  136. }
  137. // parseTCPAddr parses the given addr and validates if it is in the expected
  138. // format. If strict is enabled, the address must contain a scheme (tcp://),
  139. // a host (or IP-address) and a port number.
  140. func parseTCPAddr(address string, strict bool) (*url.URL, error) {
  141. if !strict && !strings.Contains(address, "://") {
  142. address = "tcp://" + address
  143. }
  144. parsedURL, err := url.Parse(address)
  145. if err != nil {
  146. return nil, err
  147. }
  148. if parsedURL.Scheme != "tcp" {
  149. return nil, errors.Errorf("unsupported proto '%s'", parsedURL.Scheme)
  150. }
  151. if parsedURL.Path != "" {
  152. return nil, errors.New("should not contain a path element")
  153. }
  154. if strict && parsedURL.Host == "" {
  155. return nil, errors.New("no host or IP address")
  156. }
  157. if parsedURL.Port() != "" || strict {
  158. if p, err := strconv.Atoi(parsedURL.Port()); err != nil || p == 0 {
  159. return nil, errors.Errorf("invalid port: %s", parsedURL.Port())
  160. }
  161. }
  162. return parsedURL, nil
  163. }
  164. // ValidateExtraHost validates that the specified string is a valid extrahost and returns it.
  165. // ExtraHost is in the form of name:ip where the ip has to be a valid ip (IPv4 or IPv6).
  166. func ValidateExtraHost(val string) (string, error) {
  167. // allow for IPv6 addresses in extra hosts by only splitting on first ":"
  168. name, ip, ok := strings.Cut(val, ":")
  169. if !ok || name == "" {
  170. return "", errors.Errorf("bad format for add-host: %q", val)
  171. }
  172. // Skip IPaddr validation for special "host-gateway" string
  173. if ip != HostGatewayName {
  174. if _, err := ValidateIPAddress(ip); err != nil {
  175. return "", errors.Errorf("invalid IP address in add-host: %q", ip)
  176. }
  177. }
  178. return val, nil
  179. }