-
-
Notifications
You must be signed in to change notification settings - Fork 615
/
tracker_protocol.go
66 lines (59 loc) · 1.78 KB
/
tracker_protocol.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
package webtorrent
import (
"fmt"
"math"
"github.com/pion/webrtc/v3"
)
type AnnounceRequest struct {
Numwant int `json:"numwant"`
Uploaded int64 `json:"uploaded"`
Downloaded int64 `json:"downloaded"`
Left int64 `json:"left"`
Event string `json:"event,omitempty"`
Action string `json:"action"`
InfoHash string `json:"info_hash"`
PeerID string `json:"peer_id"`
Offers []Offer `json:"offers"`
}
type Offer struct {
OfferID string `json:"offer_id"`
Offer webrtc.SessionDescription `json:"offer"`
}
type AnnounceResponse struct {
InfoHash string `json:"info_hash"`
Action string `json:"action"`
Interval *int `json:"interval,omitempty"`
Complete *int `json:"complete,omitempty"`
Incomplete *int `json:"incomplete,omitempty"`
PeerID string `json:"peer_id,omitempty"`
ToPeerID string `json:"to_peer_id,omitempty"`
Answer *webrtc.SessionDescription `json:"answer,omitempty"`
Offer *webrtc.SessionDescription `json:"offer,omitempty"`
OfferID string `json:"offer_id,omitempty"`
}
// I wonder if this is a defacto standard way to decode bytes to JSON for webtorrent. I don't really
// care.
func binaryToJsonString(b []byte) string {
var seq []rune
for _, v := range b {
seq = append(seq, rune(v))
}
return string(seq)
}
func jsonStringToInfoHash(s string) (ih [20]byte, err error) {
defer func() {
r := recover()
if r == nil {
return
}
panic(fmt.Sprintf("%q", s))
}()
for i, c := range []rune(s) {
if c < 0 || c > math.MaxUint8 {
err = fmt.Errorf("bad infohash string: %v", s)
return
}
ih[i] = byte(c)
}
return
}