-
Notifications
You must be signed in to change notification settings - Fork 17
/
argparser.go
85 lines (72 loc) · 2.39 KB
/
argparser.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
package main
import (
"errors"
"fmt"
"strconv"
)
// parse --portscan-range
func parseConfigPortScannerPortrange() (errorMessage error) {
var err error
var firstPort int64
var lastPort int64
if len(Config.Collectors.Portscan.Scanner.Ports) > 0 {
portscanPortRange = []Portrange{}
for _, portrange := range Config.Collectors.Portscan.Scanner.Ports {
// parse via regexp
portscanRangeSubMatch := portrangeRegexp.FindStringSubmatch(portrange)
if len(portscanRangeSubMatch) == 0 {
// portrange is invalid
errorMessage = fmt.Errorf("unable to parse collectors.portscan.scanner.ports, has to be format \"nnn-mmm\"")
return
}
// get named submatches
portscanRangeSubMatchResult := make(map[string]string)
for i, name := range portrangeRegexp.SubexpNames() {
if i != 0 && name != "" {
portscanRangeSubMatchResult[name] = portscanRangeSubMatch[i]
}
}
// parse first port
firstPort, err = strconv.ParseInt(portscanRangeSubMatchResult["first"], 10, 32)
if err != nil {
errorMessage = fmt.Errorf("failed to parse collectors.portscan.scanner.ports: %w", err)
return
}
// parse last port (optional)
if portscanRangeSubMatchResult["last"] != "" {
lastPort, err = strconv.ParseInt(portscanRangeSubMatchResult["last"], 10, 32)
if err != nil {
errorMessage = fmt.Errorf("failed to parse collectors.portscan.scanner.ports: %w", err)
return
}
} else {
// single port only
lastPort = firstPort
}
// check min port
if firstPort < 1 {
errorMessage = fmt.Errorf("failed to parse collectors.portscan.scanner.ports: first port cannot be smaller then 0 (%v -> %v)", firstPort, lastPort)
return
}
// check max port
if lastPort > 65535 {
errorMessage = fmt.Errorf("failed to parse collectors.portscan.scanner.ports: last port cannot be bigger then 65535 (%v -> %v)", firstPort, lastPort)
return
}
// check if range is ok
if firstPort > lastPort {
errorMessage = fmt.Errorf("failed to parse collectors.portscan.scanner.ports: first port cannot be beyond last port (%v -> %v)", firstPort, lastPort)
return
}
// add to portlist
portscanPortRange = append(
portscanPortRange,
Portrange{FirstPort: int(firstPort), LastPort: int(lastPort)},
)
}
} else {
errorMessage = errors.New("no port range available, set via collectors.portscan.scanner.ports")
return
}
return
}