forked from die-net/dhtproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracker.go
60 lines (46 loc) · 1.47 KB
/
tracker.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
package main
import (
"net/http"
"strings"
"time"
bencode "github.com/jackpal/bencode-go"
"github.com/nictuku/dht"
)
type TrackerResponse struct {
Interval int64 "interval" //nolint:govet // Bencode-go uses non-comformant struct tags
MinInterval int64 "min interval" //nolint:govet // Bencode-go uses non-comformant struct tags
Complete int "complete" //nolint:govet // Bencode-go uses non-comformant struct tags
Incomplete int "incomplete" //nolint:govet // Bencode-go uses non-comformant struct tags
Peers string "peers" //nolint:govet // Bencode-go uses non-comformant struct tags
}
func trackerHandler(w http.ResponseWriter, r *http.Request) {
if r.FormValue("compact") != "1" {
http.Error(w, "Only compact protocol supported.", 400)
return
}
infoHash := dht.InfoHash(r.FormValue("info_hash"))
if len(infoHash) != 20 {
http.Error(w, "Bad info_hash.", 400)
return
}
response := TrackerResponse{
Interval: 300,
MinInterval: 60,
}
peers, ok := peerCache.Get(string(infoHash))
dhtNode.Find(infoHash)
if !ok || len(peers) == 0 {
response.Interval = 30
response.MinInterval = 10
time.Sleep(5 * time.Second)
peers, ok = peerCache.Get(string(infoHash))
}
if ok && len(peers) > 0 {
response.Incomplete = len(peers)
response.Peers = strings.Join(peers, "")
}
w.Header().Set("Content-Type", "application/octet-stream")
if err := bencode.Marshal(w, response); err != nil {
http.Error(w, err.Error(), 500)
}
}