-
Notifications
You must be signed in to change notification settings - Fork 0
/
retracker.go
137 lines (118 loc) · 2.84 KB
/
retracker.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
package main
import (
"encoding/hex"
"github.com/iamwavecut/tool"
"log"
"net"
"net/http"
"sync"
"time"
)
type peer struct {
ID string
IP string
Port string
LastSeen int64
}
type swarm struct {
Peers map[string]*peer
Mutex sync.RWMutex
}
var swarms = make(map[string]*swarm)
func main() {
if !hostnameResolvesToLocalhost("retracker.local") {
log.Println(
"Warning: retracker.local does not resolve to current host interface. " +
"This may cause problems with local torrent client.",
)
}
mux := http.NewServeMux()
mux.HandleFunc("/announce", announceHandler)
wg := sync.WaitGroup{}
for _, listenAddr := range []string{":80", ":8080"} {
wg.Add(1)
go func(listenAddr string) {
defer wg.Done()
tool.Try((&http.Server{
Addr: listenAddr,
Handler: mux,
ReadTimeout: 1 * time.Second,
}).ListenAndServe(), true)
}(listenAddr)
}
wg.Wait()
}
func announceHandler(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
infoHash := query.Get("info_hash")
if infoHash == "" {
http.Error(w, "Missing info_hash", http.StatusBadRequest)
return
}
peerID := query.Get("peer_id")
if peerID == "" {
http.Error(w, "Missing peer_id", http.StatusBadRequest)
return
}
port := query.Get("port")
if port == "" {
http.Error(w, "Missing port", http.StatusBadRequest)
return
}
ip := r.RemoteAddr
if query.Get("ip") != "" {
ip = query.Get("ip")
}
host, _, err := net.SplitHostPort(ip)
if err != nil {
http.Error(w, "Invalid IP address", http.StatusBadRequest)
return
}
sw, exists := swarms[infoHash]
if !exists {
sw = &swarm{Peers: make(map[string]*peer)}
swarms[infoHash] = sw
}
p := &peer{ID: peerID, IP: host, Port: port, LastSeen: time.Now().Unix()}
sw.Mutex.Lock()
sw.Peers[peerID] = p
sw.Mutex.Unlock()
log.Println("Announced:", hex.EncodeToString([]byte(infoHash)), peerID, host, port)
go cleanSwarm(infoHash)
sw.Mutex.RLock()
peers := make([]string, 0, len(sw.Peers))
for _, p := range sw.Peers {
peers = append(peers, net.JoinHostPort(p.IP, p.Port))
}
sw.Mutex.RUnlock()
w.Header().Set("Content-Type", "text/plain")
for _, p := range peers {
_ = tool.Err(w.Write([]byte(p + "\n")))
}
}
func cleanSwarm(infoHash string) {
sw := swarms[infoHash]
sw.Mutex.Lock()
defer sw.Mutex.Unlock()
now := time.Now().Unix()
for id, p := range sw.Peers {
if now-p.LastSeen > 1800 {
delete(sw.Peers, id)
}
}
}
func hostnameResolvesToLocalhost(hostname string) bool {
defer tool.Catch(func(err error) {
log.Println("Couldn't check the host name:", err)
})
addrs := tool.MustReturn(net.LookupIP(hostname))
localAddrs := tool.MustReturn(net.InterfaceAddrs())
for _, addr := range addrs {
for _, localAddr := range localAddrs {
if localAddr, ok := localAddr.(*net.IPNet); ok && localAddr.Contains(addr) {
return true
}
}
}
return false
}