]>
Commit | Line | Data |
---|---|---|
9b67da3d C |
1 | import { logger } from '../helpers/logger' |
2 | import * as express from 'express' | |
3 | import * as http from 'http' | |
4 | import * as bitTorrentTracker from 'bittorrent-tracker' | |
5 | import * as proxyAddr from 'proxy-addr' | |
6 | import { Server as WebSocketServer } from 'ws' | |
6dd9de95 | 7 | import { TRACKER_RATE_LIMITS } from '../initializers/constants' |
cc43831a | 8 | import { VideoFileModel } from '../models/video/video-file' |
89ada4e2 | 9 | import { parse } from 'url' |
09209296 | 10 | import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist' |
6dd9de95 | 11 | import { CONFIG } from '../initializers/config' |
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 | ||
09209296 C |
41 | peersIps[ ip ] = peersIps[ ip ] ? peersIps[ ip ] + 1 : 1 |
42 | peersIpInfoHash[ key ] = peersIpInfoHash[ key ] ? peersIpInfoHash[ key ] + 1 : 1 | |
9b67da3d | 43 | |
31b6ddf8 | 44 | if (CONFIG.TRACKER.REJECT_TOO_MANY_ANNOUNCES && peersIpInfoHash[ key ] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP_PER_INFOHASH) { |
9b67da3d C |
45 | return cb(new Error(`Too many requests (${peersIpInfoHash[ key ]} of ip ${ip} for torrent ${infoHash}`)) |
46 | } | |
47 | ||
09209296 | 48 | try { |
31b6ddf8 C |
49 | if (CONFIG.TRACKER.PRIVATE === false) return cb() |
50 | ||
09209296 C |
51 | const videoFileExists = await VideoFileModel.doesInfohashExist(infoHash) |
52 | if (videoFileExists === true) return cb() | |
cc43831a | 53 | |
09209296 C |
54 | const playlistExists = await VideoStreamingPlaylistModel.doesInfohashExist(infoHash) |
55 | if (playlistExists === true) return cb() | |
56 | ||
57 | return cb(new Error(`Unknown infoHash ${infoHash}`)) | |
58 | } catch (err) { | |
59 | logger.error('Error in tracker filter.', { err }) | |
60 | return cb(err) | |
61 | } | |
9b67da3d C |
62 | } |
63 | }) | |
64 | ||
31b6ddf8 | 65 | if (CONFIG.TRACKER.ENABLED !== false) { |
9b67da3d | 66 | |
31b6ddf8 C |
67 | trackerServer.on('error', function (err) { |
68 | logger.error('Error in tracker.', { err }) | |
69 | }) | |
70 | ||
71 | trackerServer.on('warning', function (err) { | |
72 | logger.warn('Warning in tracker.', { err }) | |
73 | }) | |
74 | } | |
9b67da3d C |
75 | |
76 | const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer) | |
77 | trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' })) | |
78 | trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' })) | |
79 | ||
cef534ed | 80 | function createWebsocketTrackerServer (app: express.Application) { |
9b67da3d | 81 | const server = http.createServer(app) |
89ada4e2 C |
82 | const wss = new WebSocketServer({ noServer: true }) |
83 | ||
9b67da3d | 84 | wss.on('connection', function (ws, req) { |
89ada4e2 | 85 | ws['ip'] = proxyAddr(req, CONFIG.TRUST_PROXY) |
9b67da3d C |
86 | |
87 | trackerServer.onWebSocketConnection(ws) | |
88 | }) | |
89 | ||
89ada4e2 C |
90 | server.on('upgrade', (request, socket, head) => { |
91 | const pathname = parse(request.url).pathname | |
92 | ||
93 | if (pathname === '/tracker/socket') { | |
94 | wss.handleUpgrade(request, socket, head, ws => wss.emit('connection', ws, request)) | |
95 | } | |
96 | ||
97 | // Don't destroy socket, we have Socket.IO too | |
98 | }) | |
99 | ||
9b67da3d C |
100 | return server |
101 | } | |
102 | ||
103 | // --------------------------------------------------------------------------- | |
104 | ||
105 | export { | |
106 | trackerRouter, | |
cef534ed | 107 | createWebsocketTrackerServer |
9b67da3d C |
108 | } |
109 | ||
110 | // --------------------------------------------------------------------------- | |
111 | ||
112 | function runPeersChecker () { | |
113 | setInterval(() => { | |
114 | logger.debug('Checking peers.') | |
115 | ||
116 | for (const ip of Object.keys(peersIpInfoHash)) { | |
117 | if (peersIps[ip] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP) { | |
118 | logger.warn('Peer %s made abnormal requests (%d).', ip, peersIps[ip]) | |
119 | } | |
120 | } | |
121 | ||
122 | peersIpInfoHash = {} | |
123 | peersIps = {} | |
124 | }, TRACKER_RATE_LIMITS.INTERVAL) | |
125 | } |