-
Notifications
You must be signed in to change notification settings - Fork 42
/
parse-host.go
162 lines (138 loc) · 3.39 KB
/
parse-host.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package utils
import (
"fmt"
"net"
"sort"
"strings"
"github.com/pkg/errors"
)
func ConcatPorts(ports []int) string {
sort.Ints(ports)
if len(ports) <= 0 {
return ""
}
currentRangeStart := ports[0]
currentRangeEnd := ports[0]
blocks := []string{}
isInRange := false
for _, p := range ports[1:] {
isInRange = (p - currentRangeEnd) == 1
if !isInRange {
// current port interrupt the isInRange state
if currentRangeEnd != currentRangeStart {
// add range
blocks = append(blocks, fmt.Sprintf("%d-%d", currentRangeStart, currentRangeEnd))
} else {
// add port
blocks = append(blocks, fmt.Sprintf("%d", currentRangeStart))
}
currentRangeStart = p
}
currentRangeEnd = p
}
if currentRangeStart == ports[len(ports)-1] {
blocks = append(blocks, fmt.Sprintf("%d", currentRangeStart))
} else {
blocks = append(blocks, fmt.Sprintf("%d-%d", currentRangeStart, currentRangeEnd))
}
return strings.Join(blocks, ",")
}
type PortScanTarget struct {
Targets []string
TCPPort string
UDPPort string
}
func (t *PortScanTarget) String() string {
return fmt.Sprintf("Host:%v %v/tcp %v/udp", t.Targets, t.TCPPort, t.UDPPort)
}
func SplitHostsAndPorts(hosts, ports string, portGroupSize int, proto string) []PortScanTarget {
hostList := ParseStringToHosts(hosts)
portList := ParseStringToPorts(ports)
rawTarget := []PortScanTarget{}
for _, host := range hostList {
if strings.TrimSpace(host) == "" {
continue
}
ports := []int{}
for _, port := range portList {
if len(ports) < portGroupSize {
ports = append(ports, port)
} else {
switch proto {
case "udp":
rawTarget = append(rawTarget, PortScanTarget{
Targets: []string{host},
UDPPort: ConcatPorts(ports),
})
break
default:
rawTarget = append(rawTarget, PortScanTarget{
Targets: []string{host},
TCPPort: ConcatPorts(ports),
})
}
ports = []int{}
}
}
if len(ports) > 0 {
switch proto {
case "udp":
rawTarget = append(rawTarget, PortScanTarget{
Targets: []string{host},
UDPPort: ConcatPorts(ports),
})
break
default:
rawTarget = append(rawTarget, PortScanTarget{
Targets: []string{host},
TCPPort: ConcatPorts(ports),
})
}
}
}
return rawTarget
}
// IPv4ToCClassNetwork 尝试从一个 IPv4 地址中获取 C 类网络地址,并返回错误
// Example:
// ```
// network, err = str.IPv4ToCClassNetwork("192.168.0.1") // network = "192.168.0.0/24", err = nil
// ```
func GetCClassByIPv4(s string) (network string, err error) {
ip := net.ParseIP(FixForParseIP(s))
if ip != nil && ip.To4() != nil {
_, network, err := net.ParseCIDR(fmt.Sprintf("%v/24", s))
if err != nil {
return "", err
}
return network.String(), nil
}
return "", Errorf("invalid ipv4: %v", s)
}
func ParseIPNetToRange(n *net.IPNet) (int64, int64, error) {
startIp := n.IP.To4()
var (
start, end uint32
)
start, err := IPv4ToUint32(startIp)
if err != nil {
return 0, 0, errors.Errorf("cannot convert ip: %s to int: %s", startIp, err)
}
current := start
for {
ip := Uint32ToIPv4(uint32(current))
if ip == nil {
return 0, 0, errors.Errorf("cannot convert: %d to ip: %s", current, ip)
}
if n.Contains(ip) {
current += 1
continue
} else {
end = current
break
}
}
if start > end {
return 0, 0, errors.Errorf("unknown reason for parsing range")
}
return int64(start), int64(end), nil
}