-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprocess.go
More file actions
261 lines (257 loc) · 6.88 KB
/
Copy pathprocess.go
File metadata and controls
261 lines (257 loc) · 6.88 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package main
import (
"log"
"marcopolo/asn"
"marcopolo/ssl"
"marcopolo/utils"
"sync"
"time"
)
func (input *Input) SearchIPsWithKeywords(stopOnAsnIPFound bool, ipsRanges asn.IPRangeWrapper, threadsNumber int, tcpTimeout, progressEach time.Duration, chanProgress chan Progress, chanIPsFound chan IPFoundMeta) []IPFoundMeta {
input.setInputs()
bufferSize := input.BufferSize
if bufferSize == 0 {
bufferSize = 1024
}
var finishedEverything bool
var progress float64
var countFirst, countSecond uint32
var IPsFound []IPFoundMeta
var mutex sync.Mutex
var wg sync.WaitGroup
asnIDIPFound := make(map[string]bool)
chanIP := make(chan IPFoundMeta, threadsNumber)
if chanProgress != nil {
chanProgress <- Progress{
CountFirst: countFirst,
CountSecond: countSecond,
TotalIPs: ipsRanges.Quantity,
}
seconds := int(progressEach.Seconds())
if seconds == 0 {
progressEach = time.Minute
}
go func() {
for {
time.Sleep(progressEach)
mutex.Lock()
chanProgress <- Progress{
CountFirst: countFirst,
CountSecond: countSecond,
Progress: progress,
TotalIPs: ipsRanges.Quantity,
IpsFoundCount: len(IPsFound),
}
mutex.Unlock()
if finishedEverything {
break
}
}
}()
}
currentIps := make([]uint32, len(ipsRanges.IPs))
for range threadsNumber {
go func() {
for ipMetadata := range chanIP {
polo, whichStep, buffer, err := input.Marco(ipMetadata.IP.IP, tcpTimeout)
mutex.Lock()
progress++
if whichStep == 1 {
countFirst++
} else {
countSecond++
}
mutex.Unlock()
if err != nil {
//log.Println("err: ", ipMetadata.IP.IP, err)
wg.Done()
continue
}
if polo {
mutex.Lock()
if stopOnAsnIPFound {
asnIDIPFound[ipMetadata.AsnID] = true
//cleaning the progress for the ASN found
for asnIndex, ip := range ipsRanges.IPs {
if ipMetadata.AsnID != ip.AsnID {
continue
}
currentIP := currentIps[asnIndex]
if currentIP > ip.End {
continue
}
remainingIPsNumber := ip.End - currentIP
progress += float64(remainingIPsNumber)
currentIps[asnIndex] = ip.End + 1
}
}
IPsFound = append(IPsFound, ipMetadata)
mutex.Unlock()
if chanIPsFound != nil {
ipMetadata.Body = buffer
chanIPsFound <- ipMetadata
}
}
wg.Done()
}
}()
}
// I know it looks weird but with this I make sure the workers won't focus on one specific IP range from an row from the asn file but rather distrubute
//the work among all of the rows, this will look better if I make detail explanation in future
//these will make faster searches, so lets say theare are 100 rows to check, and the Ip is row 80, if were to do it normally
//one row per row it will take a lof of time, but if I instead distribute the work on all the rows, like this:
// first I check row[0]: ipstart
for asnIndex, ip := range ipsRanges.IPs {
currentIps[asnIndex] = ip.Start
}
for {
var atLeastOneIPSent bool
for asnIndex, ip := range ipsRanges.IPs {
if stopOnAsnIPFound {
mutex.Lock()
if asnIDIPFound[ip.AsnID] {
mutex.Unlock()
continue
}
mutex.Unlock()
}
currentIP := currentIps[asnIndex]
if currentIP > ip.End {
continue
}
wg.Add(1)
ipString := utils.Uint32ToIP(currentIP)
currentIP++
currentIps[asnIndex] = currentIP
chanIP <- IPFoundMeta{IP: IP{IP: ipString}, AsnIndex: asnIndex, AsnName: ip.AsnName, AsnID: ip.AsnID}
atLeastOneIPSent = true
}
if !atLeastOneIPSent {
break
}
}
//
wg.Wait()
log.Println("finished all, ips found: ", len(IPsFound))
finishedEverything = true
close(chanIP)
return IPsFound
}
func (input *Input) SearchIPsWithSSLMatches(stopOnSSlFound bool, ipsRanges asn.IPRangeWrapper, threadsNumber int, sslTimeout, progressEach time.Duration, chanProgress chan Progress, chanIPsFound chan IPFoundMeta) []IPFoundMeta {
var finishedEverything bool
var progress float64
var countFirst, countSecond uint32
var IPsFound []IPFoundMeta
var mutex sync.Mutex
var wg sync.WaitGroup
chanIP := make(chan IPFoundMeta, threadsNumber)
if chanProgress != nil {
chanProgress <- Progress{
CountFirst: countFirst,
CountSecond: countSecond,
TotalIPs: ipsRanges.Quantity,
}
seconds := int(progressEach.Seconds())
if seconds == 0 {
progressEach = time.Minute
}
go func() {
for {
time.Sleep(progressEach)
mutex.Lock()
chanProgress <- Progress{
CountFirst: countFirst,
CountSecond: countSecond,
Progress: progress,
TotalIPs: ipsRanges.Quantity,
IpsFoundCount: len(IPsFound),
}
mutex.Unlock()
if finishedEverything {
break
}
}
}()
}
for range threadsNumber {
go func() {
for ipMetadata := range chanIP {
if finishedEverything {
wg.Done()
continue
}
err := ssl.VerifyHost(ipMetadata.IP.IP, input.URL.Host, sslTimeout)
mutex.Lock()
progress++
mutex.Unlock()
if err != nil {
//log.Println("err: ", ip.IP, err)
wg.Done()
continue
}
mutex.Lock()
if stopOnSSlFound {
finishedEverything = true
}
IPsFound = append(IPsFound, ipMetadata)
mutex.Unlock()
if chanIPsFound != nil {
chanIPsFound <- ipMetadata
}
wg.Done()
}
}()
}
// I know it looks weird but with this I make sure the workers won't focus on one specific IP range from an row from the asn file but rather distrubute
//the work among all of the rows, this will look better if I make detail explanation in future
//these will make faster searches, so lets say theare are 100 rows to check, and the Ip is row 80, if were to do it normally
//one row per row it will take a lof of time, but if I instead distribute the work on all the rows, like this:
// first I check row[0]: ipstart
currentIps := make([]uint32, len(ipsRanges.IPs))
for asnIndex, ip := range ipsRanges.IPs {
currentIps[asnIndex] = ip.Start
}
for {
var atLeastOneIPSent bool
if finishedEverything {
break
}
for ipRangeIndex, ip := range ipsRanges.IPs {
if finishedEverything {
break
}
currentIP := currentIps[ipRangeIndex]
if currentIP > ip.End {
continue
}
wg.Add(1)
ipString := utils.Uint32ToIP(currentIP)
chanIP <- IPFoundMeta{IP: IP{IP: ipString}, AsnIndex: ipRangeIndex, AsnName: ip.AsnName, AsnID: ip.AsnID}
atLeastOneIPSent = true
currentIP++
currentIps[ipRangeIndex] = currentIP
}
if !atLeastOneIPSent {
break
}
}
//
wg.Wait()
log.Println("finished all, ips found: ", len(IPsFound))
finishedEverything = true
close(chanIP)
return IPsFound
}
/*
for asnIndex, ip := range ipsRanges.IPs {
wg.Add(1)
go func(asnIndex int, ip IpRange) {
for ipUint := ip.Start; ipUint <= ip.End; ipUint++ {
wg.Add(1)
ipString := utils.Uint32ToIP(ipUint)
chanIP <- IPFound{Index: asnIndex, IP: ipString, AsnName: ip.AsnName, AsnID: ip.AsnID}
}
wg.Done()
}(asnIndex, ip)
}
*/