hosts.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. socket := filepath.Join(runtimeDir, "docker.sock")
  59. host = "unix://" + socket
  60. } else {
  61. host = DefaultHost
  62. }
  63. } else {
  64. var err error
  65. host, err = parseDaemonHost(host)
  66. if err != nil {
  67. return val, err
  68. }
  69. }
  70. return host, nil
  71. }
  72. // parseDaemonHost parses the specified address and returns an address that will be used as the host.
  73. // Depending on the address specified, this may return one of the global Default* strings defined in hosts.go.
  74. func parseDaemonHost(addr string) (string, error) {
  75. addrParts := strings.SplitN(addr, "://", 2)
  76. if len(addrParts) == 1 && addrParts[0] != "" {
  77. addrParts = []string{"tcp", addrParts[0]}
  78. }
  79. switch addrParts[0] {
  80. case "tcp":
  81. return ParseTCPAddr(addr, DefaultTCPHost)
  82. case "unix":
  83. return parseSimpleProtoAddr("unix", addrParts[1], DefaultUnixSocket)
  84. case "npipe":
  85. return parseSimpleProtoAddr("npipe", addrParts[1], DefaultNamedPipe)
  86. case "fd":
  87. return addr, nil
  88. default:
  89. return "", errors.Errorf("invalid bind address (%s): unsupported proto '%s'", addr, addrParts[0])
  90. }
  91. }
  92. // parseSimpleProtoAddr parses and validates that the specified address is a valid
  93. // socket address for simple protocols like unix and npipe. It returns a formatted
  94. // socket address, either using the address parsed from addr, or the contents of
  95. // defaultAddr if addr is a blank string.
  96. func parseSimpleProtoAddr(proto, addr, defaultAddr string) (string, error) {
  97. addr = strings.TrimPrefix(addr, proto+"://")
  98. if strings.Contains(addr, "://") {
  99. return "", errors.Errorf("invalid proto, expected %s: %s", proto, addr)
  100. }
  101. if addr == "" {
  102. addr = defaultAddr
  103. }
  104. return proto + "://" + addr, nil
  105. }
  106. // ParseTCPAddr parses and validates that the specified address is a valid TCP
  107. // address. It returns a formatted TCP address, either using the address parsed
  108. // from tryAddr, or the contents of defaultAddr if tryAddr is a blank string.
  109. // tryAddr is expected to have already been Trim()'d
  110. // defaultAddr must be in the full `tcp://host:port` form
  111. func ParseTCPAddr(tryAddr string, defaultAddr string) (string, error) {
  112. def, err := parseTCPAddr(defaultAddr, true)
  113. if err != nil {
  114. return "", errors.Wrapf(err, "invalid default address (%s)", defaultAddr)
  115. }
  116. addr, err := parseTCPAddr(tryAddr, false)
  117. if err != nil {
  118. return "", errors.Wrapf(err, "invalid bind address (%s)", tryAddr)
  119. }
  120. host := addr.Hostname()
  121. if host == "" {
  122. host = def.Hostname()
  123. }
  124. port := addr.Port()
  125. if port == "" {
  126. port = def.Port()
  127. }
  128. return "tcp://" + net.JoinHostPort(host, port), nil
  129. }
  130. // parseTCPAddr parses the given addr and validates if it is in the expected
  131. // format. If strict is enabled, the address must contain a scheme (tcp://),
  132. // a host (or IP-address) and a port number.
  133. func parseTCPAddr(address string, strict bool) (*url.URL, error) {
  134. if !strict && !strings.Contains(address, "://") {
  135. address = "tcp://" + address
  136. }
  137. parsedURL, err := url.Parse(address)
  138. if err != nil {
  139. return nil, err
  140. }
  141. if parsedURL.Scheme != "tcp" {
  142. return nil, errors.Errorf("unsupported proto '%s'", parsedURL.Scheme)
  143. }
  144. if parsedURL.Path != "" {
  145. return nil, errors.New("should not contain a path element")
  146. }
  147. if strict && parsedURL.Host == "" {
  148. return nil, errors.New("no host or IP address")
  149. }
  150. if parsedURL.Port() != "" || strict {
  151. if p, err := strconv.Atoi(parsedURL.Port()); err != nil || p == 0 {
  152. return nil, errors.Errorf("invalid port: %s", parsedURL.Port())
  153. }
  154. }
  155. return parsedURL, nil
  156. }
  157. // ValidateExtraHost validates that the specified string is a valid extrahost and returns it.
  158. // ExtraHost is in the form of name:ip where the ip has to be a valid ip (IPv4 or IPv6).
  159. func ValidateExtraHost(val string) (string, error) {
  160. // allow for IPv6 addresses in extra hosts by only splitting on first ":"
  161. arr := strings.SplitN(val, ":", 2)
  162. if len(arr) != 2 || len(arr[0]) == 0 {
  163. return "", errors.Errorf("bad format for add-host: %q", val)
  164. }
  165. // Skip IPaddr validation for special "host-gateway" string
  166. if arr[1] != HostGatewayName {
  167. if _, err := ValidateIPAddress(arr[1]); err != nil {
  168. return "", errors.Errorf("invalid IP address in add-host: %q", arr[1])
  169. }
  170. }
  171. return val, nil
  172. }