-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathscrape.go
39 lines (33 loc) · 844 Bytes
/
scrape.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
package main
import (
"crypto/sha1"
"fmt"
"net/http"
"github.com/jackpal/bencode-go"
)
type ScrapeResponse struct {
Files map[string]Stat `bencode:"files"`
}
type Stat struct {
Complete int `bencode:"complete"`
Incomplete int `bencode:"incomplete"`
// Downloaded uint `bencode:"downloaded"`
}
func scrape(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
infoHash := query.Get("info_hash")
swarmHash := sha1.Sum([]byte(r.PathValue("room") + infoHash))
numSeeders, numLeechers := GetStats(swarmHash)
resp := ScrapeResponse{
Files: map[string]Stat{
infoHash: {
Complete: numSeeders,
Incomplete: numLeechers,
},
},
}
w.Header().Add("X-PrivTracker", fmt.Sprintf("s:%d l:%d", numSeeders, numLeechers))
if err := bencode.Marshal(w, resp); err != nil {
http.Error(w, err.Error(), 400)
}
}