-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
536 lines (457 loc) · 11.2 KB
/
main.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
package main
import (
"bytes"
"crypto/sha1"
"encoding/binary"
"errors"
"flag"
"fmt"
"math/rand"
"net"
"os"
"runtime"
"strings"
"sync"
"sync/atomic"
"time"
)
func init() {
flag.IntVar(&timeout, "t", 0, "Period of testing (in seconds)")
flag.Int64Var(&requests, "r", -1, "Number of requests per client, -1=unlimited")
flag.IntVar(&clients, "c", 100, "Number of concurrent clients")
flag.StringVar(&target, "u", "", "Target URL (e.g. 127.0.0.1:12345 or tracker.example.org:1337)")
flag.BoolVar(&keepAlive, "k", false, "Re-use connection IDs")
flag.BoolVar(&scrapeMode, "s", false, "Scrape instead of announcing")
flag.BoolVar(&errorReporting, "e", false, "Enable detailed error reporting")
flag.BoolVar(&seeder, "f", false, "Send seeder announces (left=0)")
var infoh = uint64(rand.Int63())
ih = &infoh
var peerid = uint64(rand.Int63())
pid = &peerid
var transid = rand.Uint32()
tid = &transid
}
// Global transactionID, infohash and peerID counters.
var (
tid *uint32
ih *uint64
pid *uint64
)
// Flags.
var (
timeout int
requests int64
clients int
target string
keepAlive bool
scrapeMode bool
errorReporting bool
seeder bool
)
const readTimeout time.Duration = time.Second * 2
type configuration struct {
url string
requests int64
period time.Duration
keepAlive bool
scrapeMode bool
seeder bool
}
type result struct {
connectAttempts int64
failedConnects int64
requests int64
success int64
failed int64
errors map[string]int64
}
type client struct {
conn net.Conn
result *result
infohash *uint64
peerID *uint64
transactionID *uint32
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
flag.Parse()
c := newConfig()
t := make(chan struct{})
if c.period > 0 {
go func() {
<-time.After(c.period)
close(t)
}()
}
wg := &sync.WaitGroup{}
results := make([]*result, clients)
fmt.Printf("Dispatching %d clients\n", clients)
wg.Add(clients)
startTime := time.Now()
for i := 0; i < clients; i++ {
result := &result{
errors: make(map[string]int64),
}
results[i] = result
conn, err := net.Dial("udp", c.url)
if err != nil {
panic(err)
}
client := &client{
conn: conn,
result: result,
infohash: ih,
peerID: pid,
transactionID: tid,
}
go client.do(c, t, wg)
go func() {
if c.period > 0 {
<-t
conn.SetReadDeadline(time.Now())
}
}()
}
fmt.Println("Waiting for results...")
wg.Wait()
printResults(results, time.Since(startTime), c)
}
func newConfig() *configuration {
if target == "" {
flag.Usage()
os.Exit(1)
}
if requests <= 0 && timeout <= 0 {
flag.Usage()
os.Exit(1)
}
if requests > 0 && timeout > 0 {
flag.Usage()
os.Exit(1)
}
configuration := &configuration{
url: target,
requests: int64((1 << 63) - 1),
period: time.Duration(0) * time.Second,
keepAlive: keepAlive,
scrapeMode: scrapeMode,
seeder: seeder,
}
if timeout > 0 {
configuration.period = time.Duration(timeout) * time.Second
}
if requests != -1 {
configuration.requests = requests
}
return configuration
}
func printResults(results []*result, runTime time.Duration, c *configuration) {
var requests int64
var success int64
var failed int64
var connectAttempts int64
var failedConnects int64
var timeouts int64
errors := make(map[string]int64)
for _, result := range results {
requests += result.requests
success += result.success
failed += result.failed
connectAttempts += result.connectAttempts
failedConnects += result.failedConnects
if errorReporting {
for err, count := range result.errors {
_, exists := errors[err]
if !exists {
errors[err] = 0
}
errors[err] += count
if strings.Contains(err, "I/O timeout") {
timeouts += count
}
}
}
}
elapsed := runTime.Seconds()
if elapsed == 0 {
elapsed = 1
}
//compute concurrency
workTime := float64(clients) * elapsed
realTime := workTime - (float64(timeouts) * readTimeout.Seconds())
concurrency := float64(clients) * (realTime / workTime)
fmt.Println()
fmt.Printf("Requests: %10d\n", requests)
fmt.Printf("Successful requests: %10d\n", success)
fmt.Printf("failed requests: %10d\n", failed)
fmt.Printf("Connect attempts: %10d\n", connectAttempts)
fmt.Printf("Failed connects: %10d\n", failedConnects)
fmt.Printf("Successful requests rate: %10.0f hits/sec\n", float64(success)/elapsed)
if c.period > 0 {
fmt.Printf("Approximate concurrency: %10.2f clients running\n", concurrency)
}
fmt.Printf("Test time: %10.2f sec\n", elapsed)
if errorReporting {
fmt.Println("Errors encountered:")
for err, count := range errors {
fmt.Printf(" %30s %10d\n", err, count)
}
}
}
func (r *result) incrementError(err string, t chan struct{}) {
if errorReporting {
if strings.Contains(err, "I/O timeout") {
select {
case <-t:
//timeout because we're done
return
default:
}
}
_, exists := r.errors[err]
if !exists {
r.errors[err] = 0
}
r.errors[err]++
}
}
func (c *client) do(conf *configuration, t chan struct{}, wg *sync.WaitGroup) {
defer wg.Done()
// Prepare a packet.
packet, err := prepareAnnounce(conf.seeder)
if err != nil {
panic(fmt.Sprintf("Unable to create packet: %s", err.Error()))
}
// Prepare a receive buffer.
buf := make([]byte, 1024)
// Get a transaction ID.
transactionID := atomic.AddUint32(c.transactionID, 1)
var connectionID uint64
var connectionIDExpired <-chan time.Time
// Perform requests.
loop:
for c.result.requests < conf.requests {
// Check if time has expired.
select {
case <-t:
return
default:
}
// Check if we have to (re)connect.
if conf.keepAlive {
if connectionIDExpired == nil {
// Initial connect or fast reconnect.
transactionID = atomic.AddUint32(c.transactionID, 1)
connectionID, err = c.connect(transactionID)
if err != nil {
c.result.incrementError(err.Error(), t)
continue loop
}
connectionIDExpired = time.After(time.Minute)
} else {
select {
case <-connectionIDExpired:
// Reconnect.
transactionID = atomic.AddUint32(c.transactionID, 1)
connectionID, err = c.connect(transactionID)
if err != nil {
c.result.incrementError(err.Error(), t)
connectionIDExpired = nil // Do a fast reconnect.
continue loop
}
connectionIDExpired = time.After(time.Minute)
default:
}
}
} else {
// Reconnect on every request.
transactionID = atomic.AddUint32(c.transactionID, 1)
connectionID, err = c.connect(transactionID)
if err != nil {
c.result.incrementError(err.Error(), t)
continue loop
}
}
transactionID = atomic.AddUint32(c.transactionID, 1)
infohash := atomic.AddUint64(c.infohash, 1)
peerID := atomic.AddUint64(c.peerID, 1)
err = c.announce(buf, packet, transactionID, connectionID, infohash, peerID)
c.result.requests++
if err != nil {
c.result.incrementError(err.Error(), t)
c.result.failed++
} else {
c.result.success++
}
}
}
func prepareAnnounce(seeder bool) ([]byte, error) {
bbuf := bytes.NewBuffer(nil)
// Connection id
_, err := bbuf.Write([]byte{0, 0, 0, 0, 0, 0, 0, 0})
if err != nil {
return nil, err
}
// Action
_, err = bbuf.Write([]byte{0, 0, 0, 0x01})
if err != nil {
return nil, err
}
// Transaction ID
_, err = bbuf.Write([]byte{0, 0, 0, 0})
if err != nil {
return nil, err
}
// Infohash
_, err = bbuf.WriteString("xxxxxxxxxxxxxxxxxxxx")
if err != nil {
return nil, err
}
// Peer ID
_, err = bbuf.WriteString("xxxxxxxxxxxxxxxxxxxx")
if err != nil {
return nil, err
}
// Downloaded
_, err = bbuf.Write([]byte{0, 0, 0, 0, 0, 0, 0, 0})
if err != nil {
return nil, err
}
// Left
if seeder {
_, err = bbuf.Write([]byte{0, 0, 0, 0, 0, 0, 0, 0})
} else {
_, err = bbuf.Write([]byte{0, 0, 0, 0, 0, 0, 0, 0x01})
}
if err != nil {
return nil, err
}
// Uploaded
_, err = bbuf.Write([]byte{0, 0, 0, 0, 0, 0, 0, 0})
if err != nil {
return nil, err
}
// Event
_, err = bbuf.Write([]byte{0, 0, 0, 0x02})
if err != nil {
return nil, err
}
// IP Address
_, err = bbuf.Write([]byte{0, 0, 0, 0})
if err != nil {
return nil, err
}
// "Key"
_, err = bbuf.Write([]byte{0, 0, 0, 0})
if err != nil {
return nil, err
}
// Numwant
_, err = bbuf.Write([]byte{0xFF, 0xFF, 0xFF, 0xFF})
if err != nil {
return nil, err
}
// Port
_, err = bbuf.Write([]byte{0x05, 0x00})
if err != nil {
return nil, err
}
return bbuf.Bytes(), nil
}
func (c *client) announce(buf, packet []byte, transactionID uint32, connectionID, infohash, peerID uint64) error {
c.conn.SetReadDeadline(time.Now().Add(readTimeout))
binary.BigEndian.PutUint64(packet[0:8], connectionID)
binary.BigEndian.PutUint32(packet[12:16], transactionID)
infohashBytes := sha1.Sum([]byte{byte(infohash >> 56),
byte(infohash >> 48),
byte(infohash >> 40),
byte(infohash >> 32),
byte(infohash >> 24),
byte(infohash >> 16),
byte(infohash >> 8),
byte(infohash)})
peerIDString := fmt.Sprintf("%020x", peerID)
for i := 0; i < 20; i++ {
packet[16+i] = infohashBytes[i]
packet[36+i] = peerIDString[i]
}
// Send announce.
n, err := c.conn.Write(packet)
if err != nil {
return err
}
if n != 98 {
return errors.New("announce: Did not send 98 bytes")
}
// Receive response.
n, err = c.conn.Read(buf)
if err != nil {
if strings.HasSuffix(err.Error(), "i/o timeout") {
return errors.New("announce: I/O timeout on receive")
}
return fmt.Errorf("announce: %s", err)
}
if n < 20 {
return errors.New("announce: Did not receive at least 20 bytes")
}
// Parse action.
action := binary.BigEndian.Uint32(buf[:4])
if action != 1 {
if action == 3 {
errVal := string(buf[8:n])
return fmt.Errorf("announce: tracker responded with error: %s", errVal)
}
return errors.New("announce: tracker responded with action != 1")
}
transID := binary.BigEndian.Uint32(buf[4:8])
if transID != transactionID {
return errors.New("announce: transaction IDs do not match")
}
return nil
}
func (c *client) connect(transactionID uint32) (u uint64, err error) {
c.conn.SetReadDeadline(time.Now().Add(readTimeout))
c.result.connectAttempts++
defer func() {
if err != nil {
c.result.failedConnects++
}
}()
buf := make([]byte, 16)
buf[2] = 0x04
buf[3] = 0x17
buf[4] = 0x27
buf[5] = 0x10
buf[6] = 0x19
buf[7] = 0x80
binary.BigEndian.PutUint32(buf[12:16], transactionID)
n, err := c.conn.Write(buf)
if err != nil {
return 0, err
}
if n != 16 {
return 0, errors.New("connect: Did not send 16 bytes")
}
buf = make([]byte, 64)
n, err = c.conn.Read(buf)
if err != nil {
if strings.HasSuffix(err.Error(), "i/o timeout") {
return 0, errors.New("connect: I/O timeout on receive")
}
return 0, fmt.Errorf("connect: %s", err)
}
if n != 16 {
return 0, errors.New("connect: Did not receive 16 bytes")
}
b := buf[:n]
action := binary.BigEndian.Uint32(b[:4])
if action != 0 {
return 0, errors.New("connect: action != 0")
}
transID := binary.BigEndian.Uint32(b[4:8])
if transID != transactionID {
return 0, errors.New("connect: transaction IDs do not match")
}
connID := binary.BigEndian.Uint64(b[8:16])
return connID, nil
}