-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpand.go
More file actions
47 lines (44 loc) · 1.34 KB
/
Copy pathexpand.go
File metadata and controls
47 lines (44 loc) · 1.34 KB
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
package main
import (
"strconv"
"strings"
"unicode"
)
func expandAddress(input string) []string {
// Example string input: "176.9.0.0/16,116.202.0.0/16", output: [176.9.0.0/16,116.202.0.0/16]
for _, a := range input {
if !(unicode.IsNumber(a) || a == ',' || a == '.' || a == '/') {
handleError("Invalid characters in address list. Valid characters include: \"123456789,./\"")
}
}
return strings.Split(input, ",")
}
func expandPort(input string) []uint16 {
// Example input: "123,456-458,11111", output: [123,456,567,458,11111]
for _, a := range input {
if !(unicode.IsNumber(a) || a == ',' || a == '-') {
handleError("Invalid characters in port list. Valid characters include: \"123456789,-\"")
}
}
var output []uint16
for _, o := range strings.Split(input, ",") {
if strings.Contains(o, "-") {
test := strings.Split(o, "-")
startPort, err1 := strconv.ParseInt(test[0], 10, 16)
endPort, err2 := strconv.ParseInt(test[1], 10, 16)
if err1 != nil || err2 != nil {
handleError("Port could not be parsed to integer")
}
for port := uint16(startPort); port < uint16(endPort+1); port++ {
output = append(output, port)
}
} else {
port, err := strconv.ParseUint(o, 10, 16)
if err != nil {
handleError("Port could not be parsed to integer")
}
output = append(output, uint16(port))
}
}
return output
}