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