-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.go
51 lines (45 loc) · 1.21 KB
/
utils.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
package ip4scout
import (
"fmt"
"github.com/google/gopacket/layers"
"strconv"
"strings"
)
// CREDIT MENTION : to https://github.com/projectdiscovery/naabu/blob/master/v2/pkg/runner/ports.go#L173
func ParsePortsList(data string) (portList []layers.TCPPort, err error) {
ports := make(map[int]struct{})
ranges := strings.Split(data, ",")
for _, r := range ranges {
r = strings.TrimSpace(r)
if strings.Contains(r, "-") {
parts := strings.Split(r, "-")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid port selection segment: '%s'", r)
}
p1, err := strconv.Atoi(parts[0])
if err != nil {
return nil, fmt.Errorf("invalid port number: '%s'", parts[0])
}
p2, err := strconv.Atoi(parts[1])
if err != nil {
return nil, fmt.Errorf("invalid port number: '%s'", parts[1])
}
if p1 > p2 {
return nil, fmt.Errorf("invalid port range: %d-%d", p1, p2)
}
for i := p1; i <= p2; i++ {
ports[i] = struct{}{}
}
} else {
port, err := strconv.Atoi(r)
if err != nil {
return nil, fmt.Errorf("invalid port number: '%s'", r)
}
ports[port] = struct{}{}
}
}
for port, _ := range ports {
portList = append(portList, layers.TCPPort(port))
}
return portList, nil
}