-
Notifications
You must be signed in to change notification settings - Fork 1
/
gotracker.go
198 lines (176 loc) · 4.53 KB
/
gotracker.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
package gotracker
import (
"errors"
"github.com/gorilla/mux"
"github.com/tumdum/bencoding"
"html/template"
"io"
"log"
"net"
"net/http"
"net/url"
"strconv"
"sync"
)
var (
NilRequestError = errors.New("nil request")
MissingPortError = errors.New("port is missing form request")
MissingPeerIdError = errors.New("peer_id is missing from request")
MissingInfoHashError = errors.New("info_hash is missing from request")
MalformedRemoteAddr = errors.New("RemoteAddr seems to be broken")
)
type Peer struct {
Ip string `bencoding:"ip"`
Port int `bencoding:"port"`
PeerId string `bencoding:"id"`
}
type RequestData struct {
Peer
InfoHash string
NumWant *int
}
func extractRequestData(r *http.Request) (*RequestData, error) {
if r == nil {
return nil, NilRequestError
}
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return nil, MalformedRemoteAddr
}
port, err := strconv.Atoi(r.FormValue("port"))
if err != nil {
return nil, MissingPortError
}
peer_id := r.FormValue("peer_id")
if peer_id == "" {
return nil, MissingPeerIdError
}
info_hash := url.QueryEscape(r.FormValue("info_hash"))
if info_hash == "" {
return nil, MissingInfoHashError
}
ret := &RequestData{Peer: Peer{ip, port, peer_id}, InfoHash: info_hash}
numWant, err := strconv.Atoi(r.FormValue("numwant"))
if err == nil {
ret.NumWant = &numWant
}
return ret, nil
}
type peerSet struct {
m sync.Mutex
peers map[Peer]struct{}
}
func newPeerSet() *peerSet {
ps := &peerSet{}
ps.peers = make(map[Peer]struct{})
return ps
}
type Tracker struct {
Logger *log.Logger
m sync.Mutex
managedTorrents map[string]*peerSet
config Server
}
func MakeTracker(logSink io.Writer, serverConfig *Server) *Tracker {
t := new(Tracker)
t.Logger = log.New(logSink, "gotracker ", log.LstdFlags)
t.config = *serverConfig
t.managedTorrents = make(map[string]*peerSet)
return t
}
type TrackerResponse struct {
Interval int `bencoding:"interval"`
Peers []Peer `bencoding:"peers"`
}
func (t *Tracker) logAndFail(reason string, err error, w http.ResponseWriter) {
t.Logger.Printf("Tracker failed due to '%v' with reason '%v'", err, reason)
_, err = w.Write([]byte(reason))
if err != nil {
t.Logger.Printf("Failed to write response in logAndFail!")
}
}
func (t *Tracker) addPeer(req *RequestData) {
t.m.Lock()
defer t.m.Unlock()
if _, ok := t.managedTorrents[req.InfoHash]; !ok {
t.managedTorrents[req.InfoHash] = newPeerSet()
}
t.managedTorrents[req.InfoHash].peers[req.Peer] = struct{}{}
}
func (t *Tracker) collectPeers(req *RequestData) []Peer {
max := t.config.DefaultNumWant
if req.NumWant != nil {
max = *req.NumWant
}
if max == 0 {
return []Peer{}
}
t.m.Lock()
peers := t.managedTorrents[req.InfoHash].peers
t.managedTorrents[req.InfoHash].m.Lock()
t.m.Unlock()
defer t.managedTorrents[req.InfoHash].m.Unlock()
ret := []Peer{}
for k, _ := range peers {
if k == req.Peer {
continue
}
ret = append(ret, k)
if len(ret) == max {
break
}
}
return ret
}
// Records req.Peer as one interested in req.Info hash and return other
// peers interested in that torrent.
func (t *Tracker) prepareResponse(req *RequestData) TrackerResponse {
t.addPeer(req)
return TrackerResponse{t.config.Interval, t.collectPeers(req)}
}
func (t *Tracker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
req, err := extractRequestData(r)
if err != nil {
t.logAndFail("Incorrect request", err, w)
return
}
resp := t.prepareResponse(req)
b, _ := bencoding.Marshal(resp)
_, err = w.Write(b)
if err != nil {
t.Logger.Printf("Failed to write response due to: '%v'", err)
}
}
var listTemplateStr = `
<html><head></head><body>
List of all managed torrents:</br>
{{range $peer, $info := . }}
<a href="/info/{{$peer}}">{{$peer}}</a>
{{end}}
</body></html>`
var listTemplate *template.Template
func init() {
listTemplate = template.Must(template.New("list").Parse(listTemplateStr))
}
func (t Tracker) ListAll(w http.ResponseWriter, r *http.Request) {
listTemplate.Execute(w, t.managedTorrents)
}
var infoTemplateStr = `
<html><head></head><body>
Peers:</br>
{{range $peer, $ignore := .}}
{{$peer.Ip}}:{{$peer.Port}} {{$peer.PeerId}}
{{end}}
</body></html>`
var infoTemplate *template.Template
func init() {
infoTemplate = template.Must(template.New("info").Parse(infoTemplateStr))
}
func (t Tracker) Info(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
hash := url.QueryEscape(vars["hash"])
peers, ok := t.managedTorrents[hash]
if ok {
infoTemplate.Execute(w, peers.peers)
}
}