hosts.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package opts // import "github.com/docker/docker/opts"
  2. import (
  3. "fmt"
  4. "net"
  5. "net/url"
  6. "path/filepath"
  7. "strconv"
  8. "strings"
  9. "github.com/docker/docker/pkg/homedir"
  10. "github.com/pkg/errors"
  11. )
  12. const (
  13. // DefaultHTTPPort Default HTTP Port used if only the protocol is provided to -H flag e.g. dockerd -H tcp://
  14. // These are the IANA registered port numbers for use with Docker
  15. // see http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml?search=docker
  16. DefaultHTTPPort = 2375 // Default HTTP Port
  17. // DefaultTLSHTTPPort Default HTTP Port used when TLS enabled
  18. DefaultTLSHTTPPort = 2376 // Default TLS encrypted HTTP Port
  19. // DefaultUnixSocket Path for the unix socket.
  20. // Docker daemon by default always listens on the default unix socket
  21. DefaultUnixSocket = "/var/run/docker.sock"
  22. // DefaultTCPHost constant defines the default host string used by docker on Windows
  23. DefaultTCPHost = "tcp://" + DefaultHTTPHost + ":2375"
  24. // DefaultTLSHost constant defines the default host string used by docker for TLS sockets
  25. DefaultTLSHost = "tcp://" + DefaultHTTPHost + ":2376"
  26. // DefaultNamedPipe defines the default named pipe used by docker on Windows
  27. DefaultNamedPipe = `//./pipe/docker_engine`
  28. // HostGatewayName is the string value that can be passed
  29. // to the IPAddr section in --add-host that is replaced by
  30. // the value of HostGatewayIP daemon config value
  31. HostGatewayName = "host-gateway"
  32. )
  33. // ValidateHost validates that the specified string is a valid host and returns it.
  34. func ValidateHost(val string) (string, error) {
  35. host := strings.TrimSpace(val)
  36. // The empty string means default and is not handled by parseDaemonHost
  37. if host != "" {
  38. _, err := parseDaemonHost(host)
  39. if err != nil {
  40. return val, err
  41. }
  42. }
  43. // Note: unlike most flag validators, we don't return the mutated value here
  44. // we need to know what the user entered later (using ParseHost) to adjust for TLS
  45. return val, nil
  46. }
  47. // ParseHost and set defaults for a Daemon host string.
  48. // defaultToTLS is preferred over defaultToUnixXDG.
  49. func ParseHost(defaultToTLS, defaultToUnixXDG bool, val string) (string, error) {
  50. host := strings.TrimSpace(val)
  51. if host == "" {
  52. if defaultToTLS {
  53. host = DefaultTLSHost
  54. } else if defaultToUnixXDG {
  55. runtimeDir, err := homedir.GetRuntimeDir()
  56. if err != nil {
  57. return "", err
  58. }
  59. socket := filepath.Join(runtimeDir, "docker.sock")
  60. host = "unix://" + socket
  61. } else {
  62. host = DefaultHost
  63. }
  64. } else {
  65. var err error
  66. host, err = parseDaemonHost(host)
  67. if err != nil {
  68. return val, err
  69. }
  70. }
  71. return host, nil
  72. }
  73. // parseDaemonHost parses the specified address and returns an address that will be used as the host.
  74. // Depending of the address specified, this may return one of the global Default* strings defined in hosts.go.
  75. func parseDaemonHost(addr string) (string, error) {
  76. addrParts := strings.SplitN(addr, "://", 2)
  77. if len(addrParts) == 1 && addrParts[0] != "" {
  78. addrParts = []string{"tcp", addrParts[0]}
  79. }
  80. switch addrParts[0] {
  81. case "tcp":
  82. return ParseTCPAddr(addrParts[1], DefaultTCPHost)
  83. case "unix":
  84. return parseSimpleProtoAddr("unix", addrParts[1], DefaultUnixSocket)
  85. case "npipe":
  86. return parseSimpleProtoAddr("npipe", addrParts[1], DefaultNamedPipe)
  87. case "fd":
  88. return addr, nil
  89. default:
  90. return "", fmt.Errorf("Invalid bind address format: %s", addr)
  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. // ParseTCPAddr parses and validates that the specified address is a valid TCP
  108. // address. It returns a formatted TCP address, either using the address parsed
  109. // from tryAddr, or the contents of defaultAddr if tryAddr is a blank string.
  110. // tryAddr is expected to have already been Trim()'d
  111. // defaultAddr must be in the full `tcp://host:port` form
  112. func ParseTCPAddr(tryAddr string, defaultAddr string) (string, error) {
  113. if tryAddr == "" || tryAddr == "tcp://" {
  114. return defaultAddr, nil
  115. }
  116. addr := strings.TrimPrefix(tryAddr, "tcp://")
  117. if strings.Contains(addr, "://") || addr == "" {
  118. return "", fmt.Errorf("Invalid proto, expected tcp: %s", tryAddr)
  119. }
  120. defaultAddr = strings.TrimPrefix(defaultAddr, "tcp://")
  121. defaultHost, defaultPort, err := net.SplitHostPort(defaultAddr)
  122. if err != nil {
  123. return "", err
  124. }
  125. u, err := url.Parse("tcp://" + addr)
  126. if err != nil {
  127. return "", err
  128. }
  129. host, port, err := net.SplitHostPort(u.Host)
  130. if err != nil {
  131. // try port addition once
  132. host, port, err = net.SplitHostPort(net.JoinHostPort(u.Host, defaultPort))
  133. }
  134. if err != nil {
  135. return "", fmt.Errorf("Invalid bind address format: %s", tryAddr)
  136. }
  137. if host == "" {
  138. host = defaultHost
  139. }
  140. if port == "" {
  141. port = defaultPort
  142. }
  143. p, err := strconv.Atoi(port)
  144. if err != nil && p == 0 {
  145. return "", fmt.Errorf("Invalid bind address format: %s", tryAddr)
  146. }
  147. if u.Path != "" {
  148. return "", errors.Errorf("invalid bind address (%s): should not contain a path element", tryAddr)
  149. }
  150. return "tcp://" + net.JoinHostPort(host, port), nil
  151. }
  152. // ValidateExtraHost validates that the specified string is a valid extrahost and returns it.
  153. // ExtraHost is in the form of name:ip where the ip has to be a valid ip (IPv4 or IPv6).
  154. func ValidateExtraHost(val string) (string, error) {
  155. // allow for IPv6 addresses in extra hosts by only splitting on first ":"
  156. arr := strings.SplitN(val, ":", 2)
  157. if len(arr) != 2 || len(arr[0]) == 0 {
  158. return "", fmt.Errorf("bad format for add-host: %q", val)
  159. }
  160. // Skip IPaddr validation for special "host-gateway" string
  161. if arr[1] != HostGatewayName {
  162. if _, err := ValidateIPAddress(arr[1]); err != nil {
  163. return "", fmt.Errorf("invalid IP address in add-host: %q", arr[1])
  164. }
  165. }
  166. return val, nil
  167. }