github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/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. docker daemon -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 parseDockerDaemonHost
    33  	if host != "" {
    34  		_, err := parseDockerDaemonHost(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 = parseDockerDaemonHost(host)
    56  		if err != nil {
    57  			return val, err
    58  		}
    59  	}
    60  	return host, nil
    61  }
    62  
    63  // parseDockerDaemonHost 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 parseDockerDaemonHost(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  }