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