-
Notifications
You must be signed in to change notification settings - Fork 1
/
scanner.go
250 lines (220 loc) · 5.54 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
package scanner
import (
"context"
"crypto/rand"
"errors"
"math/big"
"net"
"net/url"
"os"
"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,
ipv4 bool,
ipv6 bool,
silent bool,
deadline time.Duration,
) TorRelayScanner {
baseURL := "https://onionoo.torproject.org/details?type=relay&running=true&fields=fingerprint,or_addresses"
// 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,
ipv4: ipv4,
ipv6: ipv6,
silent: silent,
deadline: deadline,
}
}
// Grab returns relay list from public addresses
func (t *torRelayScanner) Grab() (relays []ResultRelay) {
resultRelays := t.getRelays()
if len(resultRelays) == 0 {
return nil
}
for _, el := range resultRelays {
n, _ := rand.Int(rand.Reader, big.NewInt(int64(len(el.OrAddresses))))
relays = append(relays, ResultRelay{
Fingerprint: el.Fingerprint,
Address: el.OrAddresses[n.Uint64()],
})
}
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 func() {
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,
}
}
})
}
p.Wait()
}()
bar := 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),
)
var relays Relays
for i := 0; i < t.goal; i++ {
select {
case el := <-chanRelays:
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())
goto loop
}
}
loop:
return relays
}
func (t *torRelayScanner) loadRelays() (err error) {
for _, addr := range t.urls {
t.relayInfo, err = t.grab(addr)
if err != nil {
continue
}
break
}
if len(t.relayInfo.Relays) == 0 {
return errors.New("tor Relay information can't be downloaded")
}
var filtered Relays
for _, rel := range t.relayInfo.Relays {
var orAddresses []string
for _, addr := range rel.OrAddresses {
if t.skipAddrType(addr) {
continue
}
if t.checkPorts(addr) {
continue
}
orAddresses = append(orAddresses, addr)
}
if len(orAddresses) > 0 {
rel.OrAddresses = orAddresses
filtered = append(filtered, rel)
}
}
if len(filtered) == 0 {
return errors.New("there are no relays within specified port number constrains!\nTry changing port numbers")
}
t.relayInfo.Relays = filtered
shuffle(t.relayInfo.Relays)
return nil
}
func (t *torRelayScanner) checkPorts(addr string) bool {
if len(t.ports) > 0 {
u, _ := url.Parse("//" + addr)
var keep bool
for _, p := range t.ports {
if u.Port() == p {
keep = true
}
}
if !keep {
return true
}
}
return false
}
func (t *torRelayScanner) skipAddrType(addr string) bool {
if t.ipv4 && !t.ipv6 {
if addr[0] == '[' {
return true
}
}
if t.ipv6 && !t.ipv4 {
if addr[0] != '[' {
return true
}
}
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{Deadline: time.Now().Add(timeout)}
_, err := d.Dial("tcp", addr)
return err == nil
}