-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscanner.go
287 lines (250 loc) · 6.63 KB
/
scanner.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package scanner
import (
"context"
"errors"
"math/rand"
"net"
"net/url"
"os"
"slices"
"strings"
"time"
"github.com/carlmjohnson/requests"
"github.com/gookit/color"
"github.com/k0kubun/go-ansi"
"github.com/schollz/progressbar/v3"
"github.com/sourcegraph/conc/pool"
json "github.com/json-iterator/go"
)
// New ...
func New(
poolSize int,
goal int,
timeout time.Duration,
urlsList []string,
port []string,
excludePorts []string,
ipv4 bool,
ipv6 bool,
silent bool,
deadline time.Duration,
country string,
) TorRelayScanner {
baseURL := "https://onionoo.torproject.org/details?type=relay&running=true&fields=fingerprint,or_addresses,country"
// Use public CORS proxy as a regular proxy in case if onionoo.torproject.org is unreachable
urls := []string{
baseURL,
"https://icors.vercel.app/?" + url.QueryEscape(baseURL),
"https://github.com/ValdikSS/tor-onionoo-mirror/raw/master/details-running-relays-fingerprint-address-only.json",
"https://bitbucket.org/ValdikSS/tor-onionoo-mirror/raw/master/details-running-relays-fingerprint-address-only.json",
}
// Additional urls should be first
if len(urlsList) > 0 {
urls = append(urlsList, urls...)
}
return &torRelayScanner{
poolSize: poolSize,
goal: goal,
timeout: timeout,
urls: urls,
ports: port,
excludePorts: excludePorts,
ipv4: ipv4,
ipv6: ipv6,
silent: silent,
deadline: deadline,
country: country,
}
}
// Grab returns relay list from public addresses
func (t *torRelayScanner) Grab() (relays []ResultRelay) {
resultRelays := t.getRelays()
if len(resultRelays) == 0 {
return nil
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for _, el := range resultRelays {
if len(el.OrAddresses) > 0 {
relays = append(relays, ResultRelay{
Fingerprint: el.Fingerprint,
Address: el.OrAddresses[r.Intn(len(el.OrAddresses))],
})
}
}
return relays
}
// GetJSON returns available relays in json format
func (t *torRelayScanner) GetJSON() []byte {
resultRelays := t.getRelays()
if len(resultRelays) == 0 {
return nil
}
result, _ := json.MarshalIndent(RelayInfo{
Version: t.relayInfo.Version,
BuildRevision: t.relayInfo.BuildRevision,
RelaysPublished: t.relayInfo.RelaysPublished,
Relays: resultRelays,
BridgesPublished: t.relayInfo.BridgesPublished,
Bridges: bridges{},
}, "", " ")
return result
}
func (t *torRelayScanner) getRelays() Relays {
if err := t.loadRelays(); err != nil {
color.Fprintln(os.Stderr, "Tor Relay information can't be downloaded!")
os.Exit(1)
}
chanRelays := make(chan Relay)
go t.testRelays(chanRelays)
bar := t.createProgressBar()
var relays Relays
for i := 1; i <= t.goal; i++ {
select {
case el, opened := <-chanRelays:
if !opened {
return relays
}
relays = append(relays, el)
_ = bar.Add(1)
case <-time.After(t.deadline):
_ = bar.Add(t.goal)
color.Fprintf(os.Stderr, "\nThe program was running for more than the specified time: %.2fs\n", t.deadline.Seconds())
return relays
}
}
return relays
}
func (t *torRelayScanner) testRelays(chanRelays chan Relay) {
p := pool.New().WithMaxGoroutines(t.poolSize)
for _, el := range t.relayInfo.Relays {
el := el
p.Go(func() {
if tcpSocketConnectChecker(el.OrAddresses[0], t.timeout) {
chanRelays <- Relay{
Fingerprint: el.Fingerprint,
OrAddresses: el.OrAddresses,
Country: el.Country,
}
}
})
}
p.Wait()
close(chanRelays)
}
func (t *torRelayScanner) createProgressBar() *progressbar.ProgressBar {
return progressbar.NewOptions(
t.goal,
progressbar.OptionSetDescription("Testing"),
progressbar.OptionSetWidth(15),
progressbar.OptionSetWriter(ansi.NewAnsiStdout()),
progressbar.OptionShowCount(),
progressbar.OptionClearOnFinish(),
progressbar.OptionEnableColorCodes(true),
progressbar.OptionSetPredictTime(false),
progressbar.OptionSetRenderBlankState(true),
progressbar.OptionSetTheme(progressbar.Theme{
Saucer: "[green]=[reset]",
SaucerHead: "[green]>[reset]",
SaucerPadding: " ",
BarStart: "[",
BarEnd: "]",
}),
progressbar.OptionSetVisibility(!t.silent),
)
}
func (t *torRelayScanner) loadRelays() error {
for _, addr := range t.urls {
var err error
t.relayInfo, err = t.grab(addr)
if err == nil {
break
}
}
if len(t.relayInfo.Relays) == 0 {
return errors.New("tor Relay information can't be downloaded")
}
t.relayInfo.Relays = t.filterRelays(t.relayInfo.Relays)
if len(t.relayInfo.Relays) == 0 {
return errors.New("there are no relays within specified port number constrains!\nTry changing port numbers")
}
shuffle(t.relayInfo.Relays)
return nil
}
// filterRelays filters relays by country and addresses
func (t *torRelayScanner) filterRelays(relays Relays) Relays {
var filtered Relays
for _, rel := range relays {
if !t.filterCountry(rel) {
continue
}
orAddresses := t.filterAddresses(rel.OrAddresses)
if len(orAddresses) > 0 {
rel.OrAddresses = orAddresses
filtered = append(filtered, rel)
}
}
return filtered
}
// filterCountry filters relays by country
// if country is empty, it returns false
// if relay's country is in the list of countries, it returns true
func (t *torRelayScanner) filterCountry(relay Relay) bool {
if t.country == "" {
return true
}
return slices.Contains(strings.Split(t.country, ","), relay.Country)
}
func (t *torRelayScanner) filterAddresses(addresses []string) []string {
var filtered []string
for _, addr := range addresses {
if t.skipAddrType(addr) || t.skipPorts(addr) {
continue
}
filtered = append(filtered, addr)
}
return filtered
}
func (t *torRelayScanner) skipPorts(addr string) bool {
u, _ := url.Parse("//" + addr)
var skip bool
if len(t.ports) > 0 &&
!slices.Contains(t.ports, u.Port()) {
skip = true
}
if len(t.excludePorts) > 0 &&
slices.Contains(t.excludePorts, u.Port()) {
skip = true
}
return skip
}
func (t *torRelayScanner) skipAddrType(addr string) bool {
if t.ipv4 && !t.ipv6 {
return addr[0] == '['
}
if t.ipv6 && !t.ipv4 {
return addr[0] != '['
}
return false
}
func (t *torRelayScanner) grab(addr string) (RelayInfo, error) {
var relayInfo RelayInfo
err := requests.
URL(addr).
UserAgent("tor-relay-scanner").
ToJSON(&relayInfo).
Fetch(context.Background())
if err != nil {
return RelayInfo{}, err
}
return relayInfo, nil
}
// tcpSocketConnectChecker just checked network connection with specific host:port
func tcpSocketConnectChecker(addr string, timeout time.Duration) bool {
d := net.Dialer{Timeout: timeout}
conn, err := d.Dial("tcp", addr)
if err != nil {
return false
}
_ = conn.Close()
return true
}