github.com/lazyboychen7/engine@v17.12.1-ce-rc2+incompatible/opts/hosts.go (about)

     1  package opts
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"net/url"
     7  	"strconv"
     8  	"strings"
     9  )
    10  
    11  var (
    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 = fmt.Sprintf("tcp://%s:%d", DefaultHTTPHost, DefaultHTTPPort)
    23  	// DefaultTLSHost constant defines the default host string used by docker for TLS sockets
    24  	DefaultTLSHost = fmt.Sprintf("tcp://%s:%d", DefaultHTTPHost, DefaultTLSHTTPPort)
    25  	// DefaultNamedPipe defines the default named pipe used by docker on Windows
    26  	DefaultNamedPipe = `//./pipe/docker_engine`
    27  )
    28  
    29  // ValidateHost validates that the specified string is a valid host and returns it.
    30  func ValidateHost(val string) (string, error) {
    31  	host := strings.TrimSpace(val)
    32  	// The empty string means default and is not handled by parseDaemonHost
    33  	if host != "" {
    34  		_, err := parseDaemonHost(host)
    35  		if err != nil {
    36  			return val, err
    37  		}
    38  	}
    39  	// Note: unlike most flag validators, we don't return the mutated value here
    40  	//       we need to know what the user entered later (using ParseHost) to adjust for TLS
    41  	return val, nil
    42  }
    43  
    44  // ParseHost and set defaults for a Daemon host string
    45  func ParseHost(defaultToTLS bool, val string) (string, error) {
    46  	host := strings.TrimSpace(val)
    47  	if host == "" {
    48  		if defaultToTLS {
    49  			host = DefaultTLSHost
    50  		} else {
    51  			host = DefaultHost
    52  		}
    53  	} else {
    54  		var err error
    55  		host, err = parseDaemonHost(host)
    56  		if err != nil {
    57  			return val, err
    58  		}
    59  	}
    60  	return host, nil
    61  }
    62  
    63  // parseDaemonHost parses the specified address and returns an address that will be used as the host.
    64  // Depending of the address specified, this may return one of the global Default* strings defined in hosts.go.
    65  func parseDaemonHost(addr string) (string, error) {
    66  	addrParts := strings.SplitN(addr, "://", 2)
    67  	if len(addrParts) == 1 && addrParts[0] != "" {
    68  		addrParts = []string{"tcp", addrParts[0]}
    69  	}
    70  
    71  	switch addrParts[0] {
    72  	case "tcp":
    73  		return ParseTCPAddr(addrParts[1], DefaultTCPHost)
    74  	case "unix":
    75  		return parseSimpleProtoAddr("unix", addrParts[1], DefaultUnixSocket)
    76  	case "npipe":
    77  		return parseSimpleProtoAddr("npipe", addrParts[1], DefaultNamedPipe)
    78  	case "fd":
    79  		return addr, nil
    80  	default:
    81  		return "", fmt.Errorf("Invalid bind address format: %s", addr)
    82  	}
    83  }
    84  
    85  // parseSimpleProtoAddr parses and validates that the specified address is a valid
    86  // socket address for simple protocols like unix and npipe. It returns a formatted
    87  // socket address, either using the address parsed from addr, or the contents of
    88  // defaultAddr if addr is a blank string.
    89  func parseSimpleProtoAddr(proto, addr, defaultAddr string) (string, error) {
    90  	addr = strings.TrimPrefix(addr, proto+"://")
    91  	if strings.Contains(addr, "://") {
    92  		return "", fmt.Errorf("Invalid proto, expected %s: %s", proto, addr)
    93  	}
    94  	if addr == "" {
    95  		addr = defaultAddr
    96  	}
    97  	return fmt.Sprintf("%s://%s", proto, addr), nil
    98  }
    99  
   100  // ParseTCPAddr parses and validates that the specified address is a valid TCP
   101  // address. It returns a formatted TCP address, either using the address parsed
   102  // from tryAddr, or the contents of defaultAddr if tryAddr is a blank string.
   103  // tryAddr is expected to have already been Trim()'d
   104  // defaultAddr must be in the full `tcp://host:port` form
   105  func ParseTCPAddr(tryAddr string, defaultAddr string) (string, error) {
   106  	if tryAddr == "" || tryAddr == "tcp://" {
   107  		return defaultAddr, nil
   108  	}
   109  	addr := strings.TrimPrefix(tryAddr, "tcp://")
   110  	if strings.Contains(addr, "://") || addr == "" {
   111  		return "", fmt.Errorf("Invalid proto, expected tcp: %s", tryAddr)
   112  	}
   113  
   114  	defaultAddr = strings.TrimPrefix(defaultAddr, "tcp://")
   115  	defaultHost, defaultPort, err := net.SplitHostPort(defaultAddr)
   116  	if err != nil {
   117  		return "", err
   118  	}
   119  	// url.Parse fails for trailing colon on IPv6 brackets on Go 1.5, but
   120  	// not 1.4. See https://github.com/golang/go/issues/12200 and
   121  	// https://github.com/golang/go/issues/6530.
   122  	if strings.HasSuffix(addr, "]:") {
   123  		addr += defaultPort
   124  	}
   125  
   126  	u, err := url.Parse("tcp://" + addr)
   127  	if err != nil {
   128  		return "", err
   129  	}
   130  	host, port, err := net.SplitHostPort(u.Host)
   131  	if err != nil {
   132  		// try port addition once
   133  		host, port, err = net.SplitHostPort(net.JoinHostPort(u.Host, defaultPort))
   134  	}
   135  	if err != nil {
   136  		return "", fmt.Errorf("Invalid bind address format: %s", tryAddr)
   137  	}
   138  
   139  	if host == "" {
   140  		host = defaultHost
   141  	}
   142  	if port == "" {
   143  		port = defaultPort
   144  	}
   145  	p, err := strconv.Atoi(port)
   146  	if err != nil && p == 0 {
   147  		return "", fmt.Errorf("Invalid bind address format: %s", tryAddr)
   148  	}
   149  
   150  	return fmt.Sprintf("tcp://%s%s", net.JoinHostPort(host, port), u.Path), nil
   151  }
   152  
   153  // ValidateExtraHost validates that the specified string is a valid extrahost and returns it.
   154  // ExtraHost is in the form of name:ip where the ip has to be a valid ip (IPv4 or IPv6).
   155  func ValidateExtraHost(val string) (string, error) {
   156  	// allow for IPv6 addresses in extra hosts by only splitting on first ":"
   157  	arr := strings.SplitN(val, ":", 2)
   158  	if len(arr) != 2 || len(arr[0]) == 0 {
   159  		return "", fmt.Errorf("bad format for add-host: %q", val)
   160  	}
   161  	if _, err := ValidateIPAddress(arr[1]); err != nil {
   162  		return "", fmt.Errorf("invalid IP address in add-host: %q", arr[1])
   163  	}
   164  	return val, nil
   165  }