-
Notifications
You must be signed in to change notification settings - Fork 78
/
parse.go
54 lines (44 loc) · 1.68 KB
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package net
import (
"errors"
"fmt"
"net"
"k8s.io/apimachinery/pkg/util/validation"
netutils "k8s.io/utils/net"
)
// ParseHostPort parses a network address of the form "host:port", "ipv4:port", "[ipv6]:port" into host and port;
// ":port" can be eventually omitted.
// If the string is not a valid representation of network address, ParseHostPort returns an error.
func ParseHostPort(hostport string) (string, string, error) {
var host, port string
var err error
// try to split host and port
if host, port, err = net.SplitHostPort(hostport); err != nil {
// if SplitHostPort returns an error, the entire hostport is considered as host
host = hostport
}
// if port is defined, parse and validate it
if port != "" {
if _, err := ParsePort(port); err != nil {
return "", "", fmt.Errorf("hostport %s: port %s must be a valid number between 1 and 65535, inclusive", hostport, port)
}
}
// if host is a valid IP, returns it
if ip := netutils.ParseIPSloppy(host); ip != nil {
return host, port, nil
}
// if host is a validated RFC-1123 subdomain, returns it
if errs := validation.IsDNS1123Subdomain(host); len(errs) == 0 {
return host, port, nil
}
return "", "", fmt.Errorf("hostport %s: host '%s' must be a valid IP address or a valid RFC-1123 DNS subdomain", hostport, host)
}
// ParsePort parses a string representing a TCP port.
// If the string is not a valid representation of a TCP port, ParsePort returns an error.
func ParsePort(port string) (int, error) {
portInt, err := netutils.ParsePort(port, true)
if err == nil && (1 <= portInt && portInt <= 65535) {
return portInt, nil
}
return 0, errors.New("port must be a valid number between 1 and 65535, inclusive")
}