-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathscanner.go
More file actions
283 lines (238 loc) · 6.05 KB
/
Copy pathscanner.go
File metadata and controls
283 lines (238 loc) · 6.05 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package ipscanner
import (
"crypto/tls"
"net/netip"
"time"
"github.com/bepass-org/ipscanner/internal/engine"
"github.com/bepass-org/ipscanner/internal/statute"
)
type IPScanner struct {
options statute.ScannerOptions
logger statute.Logger
engine *engine.Engine
onChange func([]netip.Addr)
}
func NewScanner(options ...Option) *IPScanner {
p := &IPScanner{
options: statute.ScannerOptions{
UseIPv4: true,
UseIPv6: true,
CidrList: statute.DefaultCFRanges,
SelectedOps: 0,
Logger: statute.DefaultLogger{},
InsecureSkipVerify: true,
RawDialerFunc: statute.DefaultDialerFunc,
TLSDialerFunc: statute.DefaultTLSDialerFunc,
QuicDialerFunc: statute.DefaultQuicDialerFunc,
HttpClientFunc: statute.DefaultHTTPClientFunc,
UseHTTP3: false,
UseHTTP2: false,
DisableCompression: false,
HTTPPath: "/",
Referrer: "",
UserAgent: "Chrome/80.0.3987.149",
Hostname: "www.cloudflare.com",
WarpPresharedKey: "",
WarpPeerPublicKey: "",
WarpPrivateKey: "",
Port: 443,
IPQueueSize: 8,
MaxDesirableRTT: 400,
IPQueueTTL: 30 * time.Second,
IPQueueChangeCallback: statute.DefaultIPQueueChangeCallback,
ConnectionTimeout: 1 * time.Second,
HandshakeTimeout: 1 * time.Second,
TlsVersion: tls.VersionTLS13,
},
logger: statute.DefaultLogger{},
}
for _, option := range options {
option(p)
}
return p
}
type Option func(*IPScanner)
func WithUseIPv4(useIPv4 bool) Option {
return func(i *IPScanner) {
i.options.UseIPv4 = useIPv4
}
}
func WithUseIPv6(useIPv6 bool) Option {
return func(i *IPScanner) {
i.options.UseIPv6 = useIPv6
}
}
func WithDialer(d statute.TDialerFunc) Option {
return func(i *IPScanner) {
i.options.RawDialerFunc = d
}
}
func WithTLSDialer(t statute.TDialerFunc) Option {
return func(i *IPScanner) {
i.options.TLSDialerFunc = t
}
}
func WithQuicDialer(q statute.TQuicDialerFunc) Option {
return func(i *IPScanner) {
i.options.QuicDialerFunc = q
}
}
func WithHttpClientFunc(h statute.THTTPClientFunc) Option {
return func(i *IPScanner) {
i.options.HttpClientFunc = h
}
}
func WithUseHTTP3(useHTTP3 bool) Option {
return func(i *IPScanner) {
i.options.UseHTTP3 = useHTTP3
}
}
func WithUseHTTP2(useHTTP2 bool) Option {
return func(i *IPScanner) {
i.options.UseHTTP2 = useHTTP2
}
}
func WithDisableCompression(disableCompression bool) Option {
return func(i *IPScanner) {
i.options.DisableCompression = disableCompression
}
}
func WithHttpPath(path string) Option {
return func(i *IPScanner) {
i.options.HTTPPath = path
}
}
func WithReferrer(referrer string) Option {
return func(i *IPScanner) {
i.options.Referrer = referrer
}
}
func WithUserAgent(userAgent string) Option {
return func(i *IPScanner) {
i.options.UserAgent = userAgent
}
}
func WithLogger(logger statute.Logger) Option {
return func(i *IPScanner) {
i.options.Logger = logger
}
}
func WithInsecureSkipVerify(insecureSkipVerify bool) Option {
return func(i *IPScanner) {
i.options.InsecureSkipVerify = insecureSkipVerify
}
}
func WithHostname(hostname string) Option {
return func(i *IPScanner) {
i.options.Hostname = hostname
}
}
func WithPort(port uint16) Option {
return func(i *IPScanner) {
i.options.Port = port
}
}
func WithCidrList(cidrList []string) Option {
return func(i *IPScanner) {
i.options.CidrList = cidrList
}
}
func WithHTTPPing() Option {
return func(i *IPScanner) {
i.options.SelectedOps |= statute.HTTPPing
}
}
func WithWarpPing() Option {
return func(i *IPScanner) {
i.options.SelectedOps |= statute.WARPPing
}
}
func WithQUICPing() Option {
return func(i *IPScanner) {
i.options.SelectedOps |= statute.QUICPing
}
}
func WithTCPPing() Option {
return func(i *IPScanner) {
i.options.SelectedOps |= statute.TCPPing
}
}
func WithTLSPing() Option {
return func(i *IPScanner) {
i.options.SelectedOps |= statute.TLSPing
}
}
func WithIPQueueSize(size int) Option {
return func(i *IPScanner) {
i.options.IPQueueSize = size
}
}
func WithMaxDesirableRTT(threshold int) Option {
return func(i *IPScanner) {
i.options.MaxDesirableRTT = threshold
}
}
func WithIPQueueTTL(ttl time.Duration) Option {
return func(i *IPScanner) {
i.options.IPQueueTTL = ttl
}
}
func WithIPQueueChangeCallback(callback statute.TIPQueueChangeCallback) Option {
return func(i *IPScanner) {
i.options.IPQueueChangeCallback = callback
}
}
func WithConnectionTimeout(timeout time.Duration) Option {
return func(i *IPScanner) {
i.options.ConnectionTimeout = timeout
}
}
func WithHandshakeTimeout(timeout time.Duration) Option {
return func(i *IPScanner) {
i.options.HandshakeTimeout = timeout
}
}
func WithTlsVersion(version uint16) Option {
return func(i *IPScanner) {
i.options.TlsVersion = version
}
}
func WithWarpPrivateKey(privateKey string) Option {
return func(i *IPScanner) {
i.options.WarpPrivateKey = privateKey
}
}
func WithWarpPeerPublicKey(peerPublicKey string) Option {
return func(i *IPScanner) {
i.options.WarpPeerPublicKey = peerPublicKey
}
}
func WithWarpPreSharedKey(presharedKey string) Option {
return func(i *IPScanner) {
i.options.WarpPresharedKey = presharedKey
}
}
func (i *IPScanner) SetIPQueueChangeCallback(callback statute.TIPQueueChangeCallback) {
i.options.IPQueueChangeCallback = callback
}
// run engine and in case of new event call onChange callback also if it gets canceled with context
// cancel all operations
func (i *IPScanner) Run() {
statute.FinalOptions = &i.options
if !i.options.UseIPv4 && !i.options.UseIPv6 {
i.logger.Error("Fatal: both IPv4 and IPv6 are disabled, nothing to do")
return
}
i.engine = engine.NewScannerEngine(&i.options)
go i.engine.Run()
}
func (i *IPScanner) Stop() {
i.engine.Cancel()
}
func (i *IPScanner) GetAvailableIPS() []netip.Addr {
if i.engine != nil {
return i.engine.GetAvailableIPs(false)
}
return nil
}
type IPInfo = statute.IPInfo