package config import ( "fmt" "net" "strconv" "strings" ) // EgressHost parses an egress host entry, "host" or "host:port", into its // host and port; the port is 443 when none is given. The host is a DNS name, // optionally with a whole first-label wildcard ("*.example.com") over at // least three labels: sandbox policy names hosts, never addresses. func EgressHost(entry string) (string, int, error) { entry = strings.TrimSpace(entry) host, port := entry, 443 if h, p, err := net.SplitHostPort(entry); err == nil { n, err := strconv.Atoi(p) if err != nil || n < 1 || n > 65535 { return "", 0, fmt.Errorf("egress host %q: port must be 1 to 65535", entry) } host, port = h, n } host = strings.ToLower(strings.TrimSuffix(host, ".")) if host == "" || net.ParseIP(host) != nil { return "", 0, fmt.Errorf("egress host %q: want a DNS name", entry) } labels := strings.Split(host, ".") for i, label := range labels { if label == "" || (strings.Contains(label, "*") && (i != 0 || label != "*")) { return "", 0, fmt.Errorf("egress host %q: a wildcard must be the whole first label", entry) } } if labels[0] == "*" && len(labels) < 3 { return "", 0, fmt.Errorf("egress host %q: a wildcard host needs at least three labels", entry) } return host, port, nil }