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