-
Notifications
You must be signed in to change notification settings - Fork 18
/
portuils.go
133 lines (101 loc) · 3.36 KB
/
portuils.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package portutils
import (
"fmt"
"strconv"
"strings"
"go.aporeto.io/gaia/protocols"
)
// ConvertToPortsRange converts a :-separated string to a min and max port.
func ConvertToPortsRange(ports string) (*PortsRange, error) {
sp := strings.Split(ports, ":")
if len(sp) != 2 {
return nil, fmt.Errorf("%s is not a valid range format. It should be of form fromPort:toPort", ports)
}
fromPort, err := ConvertToSinglePort(sp[0])
if err != nil {
return nil, err
}
toPort, err := ConvertToSinglePort(sp[1])
if err != nil {
return nil, err
}
if fromPort >= toPort {
return nil, fmt.Errorf("left bound is greater or equal to right bound")
}
return &PortsRange{fromPort, toPort}, nil
}
// ConvertToPortsList convert a , separated string to a list of port.
func ConvertToPortsList(ports string) (*PortsList, error) {
results := PortsList{}
p, err := ConvertToSinglePort(ports)
if err != nil {
return nil, err
}
results = append(results, p)
return &results, nil
}
// ConvertToSinglePort converts a string to port.
func ConvertToSinglePort(port string) (int64, error) {
p, err := strconv.ParseInt(port, 10, 64)
if err != nil {
return -1, fmt.Errorf("%s is not a valid port", port)
}
if p < 1 || p > 65535 {
return -1, fmt.Errorf("%s is not in between 1 and 65535", port)
}
return p, nil
}
// ExtractPortsAndProtocol extracts ports and protocol from the service
// NOTE: The protocol should be in uppercase to match our list of protocols here
// https://github.com/aporeto-inc/gaia/blob/master/protocols/protocols.go
func ExtractPortsAndProtocol(service string) (string, string, error) {
upperService := strings.ToUpper(service)
proto := ""
var portSubString string
if strings.HasPrefix(upperService, protocols.L4ProtocolUDP+"/") {
proto = protocols.L4ProtocolUDP
portSubString = upperService[4:]
} else if strings.HasPrefix(upperService, protocols.L4ProtocolTCP+"/") {
proto = protocols.L4ProtocolTCP
portSubString = upperService[4:]
} else {
return "", "", fmt.Errorf("invalid protocol/ports: %s", service)
}
return portSubString, proto, nil
}
// ExtractPortsAndProtocolFromHostService extracts the port range and the protocol from a host service like tcp/80:100.
// If the prefix is neither tcp or udp it will return an error.
// NOTE: The protocol should be in uppercase to match our list of protocols here
// https://github.com/aporeto-inc/gaia/blob/master/protocols/protocols.go
func ExtractPortsAndProtocolFromHostService(service string) (*PortsRange, string, error) {
proto := ""
var portSubString string
portSubString, proto, err := ExtractPortsAndProtocol(service)
if err != nil {
proto = protocols.L4ProtocolTCP
portSubString = strings.ToUpper(service)
}
if strings.Contains(portSubString, ":") {
parts := strings.SplitN(portSubString, ":", 2)
if len(parts) != 2 {
return nil, "", fmt.Errorf("%s does not have a valid port range", service)
}
min, err := ConvertToSinglePort(parts[0])
if err != nil {
return nil, "", err
}
max, err := ConvertToSinglePort(parts[1])
if err != nil {
return nil, "", err
}
if min > max {
return nil, "", fmt.Errorf("%s is not a valid port range", portSubString)
}
return &PortsRange{FromPort: min, ToPort: max}, proto, nil
}
min, err := ConvertToSinglePort(portSubString)
if err != nil {
return nil, "", err
}
return &PortsRange{FromPort: min, ToPort: min}, proto, nil
}