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.ts148
1 files changed, 0 insertions, 148 deletions
diff --git a/server/controllers/tracker.ts b/server/controllers/tracker.ts
deleted file mode 100644
index 9a8aa88bc..000000000
--- a/server/controllers/tracker.ts
+++ /dev/null
@@ -1,148 +0,0 @@
1import { Server as TrackerServer } from 'bittorrent-tracker'
2import express from 'express'
3import { createServer } from 'http'
4import { LRUCache } from 'lru-cache'
5import proxyAddr from 'proxy-addr'
6import { WebSocketServer } from 'ws'
7import { logger } from '../helpers/logger'
8import { CONFIG } from '../initializers/config'
9import { LRU_CACHE, TRACKER_RATE_LIMITS } from '../initializers/constants'
10import { VideoFileModel } from '../models/video/video-file'
11import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
12
13const trackerRouter = express.Router()
14
15const blockedIPs = new LRUCache<string, boolean>({
16 max: LRU_CACHE.TRACKER_IPS.MAX_SIZE,
17 ttl: TRACKER_RATE_LIMITS.BLOCK_IP_LIFETIME
18})
19
20let peersIps = {}
21let peersIpInfoHash = {}
22runPeersChecker()
23
24const trackerServer = new TrackerServer({
25 http: false,
26 udp: false,
27 ws: false,
28 filter: async function (infoHash, params, cb) {
29 if (CONFIG.TRACKER.ENABLED === false) {
30 return cb(new Error('Tracker is disabled on this instance.'))
31 }
32
33 let ip: string
34
35 if (params.type === 'ws') {
36 ip = params.ip
37 } else {
38 ip = params.httpReq.ip
39 }
40
41 const key = ip + '-' + infoHash
42
43 peersIps[ip] = peersIps[ip] ? peersIps[ip] + 1 : 1
44 peersIpInfoHash[key] = peersIpInfoHash[key] ? peersIpInfoHash[key] + 1 : 1
45
46 if (CONFIG.TRACKER.REJECT_TOO_MANY_ANNOUNCES && peersIpInfoHash[key] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP_PER_INFOHASH) {
47 return cb(new Error(`Too many requests (${peersIpInfoHash[key]} of ip ${ip} for torrent ${infoHash}`))
48 }
49
50 try {
51 if (CONFIG.TRACKER.PRIVATE === false) return cb()
52
53 const videoFileExists = await VideoFileModel.doesInfohashExistCached(infoHash)
54 if (videoFileExists === true) return cb()
55
56 const playlistExists = await VideoStreamingPlaylistModel.doesInfohashExistCached(infoHash)
57 if (playlistExists === true) return cb()
58
59 cb(new Error(`Unknown infoHash ${infoHash} requested by ip ${ip}`))
60
61 // Close socket connection and block IP for a few time
62 if (params.type === 'ws') {
63 blockedIPs.set(ip, true)
64
65 // setTimeout to wait filter response
66 setTimeout(() => params.socket.close(), 0)
67 }
68 } catch (err) {
69 logger.error('Error in tracker filter.', { err })
70 return cb(err)
71 }
72 }
73})
74
75if (CONFIG.TRACKER.ENABLED !== false) {
76 trackerServer.on('error', function (err) {
77 logger.error('Error in tracker.', { err })
78 })
79
80 trackerServer.on('warning', function (err) {
81 const message = err.message || ''
82
83 if (CONFIG.LOG.LOG_TRACKER_UNKNOWN_INFOHASH === false && message.includes('Unknown infoHash')) {
84 return
85 }
86
87 logger.warn('Warning in tracker.', { err })
88 })
89}
90
91const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer)
92trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' }))
93trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' }))
94
95function createWebsocketTrackerServer (app: express.Application) {
96 const server = createServer(app)
97 const wss = new WebSocketServer({ noServer: true })
98
99 wss.on('connection', function (ws, req) {
100 ws['ip'] = proxyAddr(req, CONFIG.TRUST_PROXY)
101
102 trackerServer.onWebSocketConnection(ws)
103 })
104
105 server.on('upgrade', (request: express.Request, socket, head) => {
106 if (request.url === '/tracker/socket') {
107 const ip = proxyAddr(request, CONFIG.TRUST_PROXY)
108
109 if (blockedIPs.has(ip)) {
110 logger.debug('Blocking IP %s from tracker.', ip)
111
112 socket.write('HTTP/1.1 403 Forbidden\r\n\r\n')
113 socket.destroy()
114 return
115 }
116
117 return wss.handleUpgrade(request, socket, head, ws => wss.emit('connection', ws, request))
118 }
119
120 // Don't destroy socket, we have Socket.IO too
121 })
122
123 return { server, trackerServer }
124}
125
126// ---------------------------------------------------------------------------
127
128export {
129 trackerRouter,
130 createWebsocketTrackerServer
131}
132
133// ---------------------------------------------------------------------------
134
135function runPeersChecker () {
136 setInterval(() => {
137 logger.debug('Checking peers.')
138
139 for (const ip of Object.keys(peersIpInfoHash)) {
140 if (peersIps[ip] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP) {
141 logger.warn('Peer %s made abnormal requests (%d).', ip, peersIps[ip])
142 }
143 }
144
145 peersIpInfoHash = {}
146 peersIps = {}
147 }, TRACKER_RATE_LIMITS.INTERVAL)
148}