aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/tracker.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/tracker.ts')
-rw-r--r--server/controllers/tracker.ts91
1 files changed, 91 insertions, 0 deletions
diff --git a/server/controllers/tracker.ts b/server/controllers/tracker.ts
new file mode 100644
index 000000000..42f5aea81
--- /dev/null
+++ b/server/controllers/tracker.ts
@@ -0,0 +1,91 @@
1import { logger } from '../helpers/logger'
2import * as express from 'express'
3import * as http from 'http'
4import * as bitTorrentTracker from 'bittorrent-tracker'
5import * as proxyAddr from 'proxy-addr'
6import { Server as WebSocketServer } from 'ws'
7import { CONFIG, TRACKER_RATE_LIMITS } from '../initializers/constants'
8
9const TrackerServer = bitTorrentTracker.Server
10
11const trackerRouter = express.Router()
12
13let peersIps = {}
14let peersIpInfoHash = {}
15runPeersChecker()
16
17const trackerServer = new TrackerServer({
18 http: false,
19 udp: false,
20 ws: false,
21 dht: false,
22 filter: function (infoHash, params, cb) {
23 let ip: string
24
25 if (params.type === 'ws') {
26 ip = params.socket.ip
27 } else {
28 ip = params.httpReq.ip
29 }
30
31 const key = ip + '-' + infoHash
32
33 peersIps[ip] = peersIps[ip] ? peersIps[ip] + 1 : 1
34 peersIpInfoHash[key] = peersIpInfoHash[key] ? peersIpInfoHash[key] + 1 : 1
35
36 if (peersIpInfoHash[key] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP_PER_INFOHASH) {
37 return cb(new Error(`Too many requests (${peersIpInfoHash[ key ]} of ip ${ip} for torrent ${infoHash}`))
38 }
39
40 return cb()
41 }
42})
43
44trackerServer.on('error', function (err) {
45 logger.error('Error in tracker.', { err })
46})
47
48trackerServer.on('warning', function (err) {
49 logger.warn('Warning in tracker.', { err })
50})
51
52const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer)
53trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' }))
54trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' }))
55
56function createWebsocketServer (app: express.Application) {
57 const server = http.createServer(app)
58 const wss = new WebSocketServer({ server: server, path: '/tracker/socket' })
59 wss.on('connection', function (ws, req) {
60 const ip = proxyAddr(req, CONFIG.TRUST_PROXY)
61 ws['ip'] = ip
62
63 trackerServer.onWebSocketConnection(ws)
64 })
65
66 return server
67}
68
69// ---------------------------------------------------------------------------
70
71export {
72 trackerRouter,
73 createWebsocketServer
74}
75
76// ---------------------------------------------------------------------------
77
78function runPeersChecker () {
79 setInterval(() => {
80 logger.debug('Checking peers.')
81
82 for (const ip of Object.keys(peersIpInfoHash)) {
83 if (peersIps[ip] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP) {
84 logger.warn('Peer %s made abnormal requests (%d).', ip, peersIps[ip])
85 }
86 }
87
88 peersIpInfoHash = {}
89 peersIps = {}
90 }, TRACKER_RATE_LIMITS.INTERVAL)
91}