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