]>
Commit | Line | Data |
---|---|---|
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' | |
7 | import { CONFIG, TRACKER_RATE_LIMITS } from '../initializers/constants' | |
8 | import { VideoFileModel } from '../models/video/video-file' | |
9 | import { parse } from 'url' | |
10 | import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist' | |
11 | ||
12 | const TrackerServer = bitTorrentTracker.Server | |
13 | ||
14 | const trackerRouter = express.Router() | |
15 | ||
16 | let peersIps = {} | |
17 | let peersIpInfoHash = {} | |
18 | runPeersChecker() | |
19 | ||
20 | const trackerServer = new TrackerServer({ | |
21 | http: false, | |
22 | udp: false, | |
23 | ws: false, | |
24 | dht: false, | |
25 | filter: async function (infoHash, params, cb) { | |
26 | let ip: string | |
27 | ||
28 | if (params.type === 'ws') { | |
29 | ip = params.socket.ip | |
30 | } else { | |
31 | ip = params.httpReq.ip | |
32 | } | |
33 | ||
34 | const key = ip + '-' + infoHash | |
35 | ||
36 | peersIps[ ip ] = peersIps[ ip ] ? peersIps[ ip ] + 1 : 1 | |
37 | peersIpInfoHash[ key ] = peersIpInfoHash[ key ] ? peersIpInfoHash[ key ] + 1 : 1 | |
38 | ||
39 | if (peersIpInfoHash[ key ] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP_PER_INFOHASH) { | |
40 | return cb(new Error(`Too many requests (${peersIpInfoHash[ key ]} of ip ${ip} for torrent ${infoHash}`)) | |
41 | } | |
42 | ||
43 | try { | |
44 | const videoFileExists = await VideoFileModel.doesInfohashExist(infoHash) | |
45 | if (videoFileExists === true) return cb() | |
46 | ||
47 | const playlistExists = await VideoStreamingPlaylistModel.doesInfohashExist(infoHash) | |
48 | if (playlistExists === true) return cb() | |
49 | ||
50 | return cb(new Error(`Unknown infoHash ${infoHash}`)) | |
51 | } catch (err) { | |
52 | logger.error('Error in tracker filter.', { err }) | |
53 | return cb(err) | |
54 | } | |
55 | } | |
56 | }) | |
57 | ||
58 | trackerServer.on('error', function (err) { | |
59 | logger.error('Error in tracker.', { err }) | |
60 | }) | |
61 | ||
62 | trackerServer.on('warning', function (err) { | |
63 | logger.warn('Warning in tracker.', { err }) | |
64 | }) | |
65 | ||
66 | const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer) | |
67 | trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' })) | |
68 | trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' })) | |
69 | ||
70 | function createWebsocketTrackerServer (app: express.Application) { | |
71 | const server = http.createServer(app) | |
72 | const wss = new WebSocketServer({ noServer: true }) | |
73 | ||
74 | wss.on('connection', function (ws, req) { | |
75 | ws['ip'] = proxyAddr(req, CONFIG.TRUST_PROXY) | |
76 | ||
77 | trackerServer.onWebSocketConnection(ws) | |
78 | }) | |
79 | ||
80 | server.on('upgrade', (request, socket, head) => { | |
81 | const pathname = parse(request.url).pathname | |
82 | ||
83 | if (pathname === '/tracker/socket') { | |
84 | wss.handleUpgrade(request, socket, head, ws => wss.emit('connection', ws, request)) | |
85 | } | |
86 | ||
87 | // Don't destroy socket, we have Socket.IO too | |
88 | }) | |
89 | ||
90 | return server | |
91 | } | |
92 | ||
93 | // --------------------------------------------------------------------------- | |
94 | ||
95 | export { | |
96 | trackerRouter, | |
97 | createWebsocketTrackerServer | |
98 | } | |
99 | ||
100 | // --------------------------------------------------------------------------- | |
101 | ||
102 | function runPeersChecker () { | |
103 | setInterval(() => { | |
104 | logger.debug('Checking peers.') | |
105 | ||
106 | for (const ip of Object.keys(peersIpInfoHash)) { | |
107 | if (peersIps[ip] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP) { | |
108 | logger.warn('Peer %s made abnormal requests (%d).', ip, peersIps[ip]) | |
109 | } | |
110 | } | |
111 | ||
112 | peersIpInfoHash = {} | |
113 | peersIps = {} | |
114 | }, TRACKER_RATE_LIMITS.INTERVAL) | |
115 | } |