diff options
Diffstat (limited to 'server/controllers/tracker.ts')
-rw-r--r-- | server/controllers/tracker.ts | 49 |
1 files changed, 33 insertions, 16 deletions
diff --git a/server/controllers/tracker.ts b/server/controllers/tracker.ts index 1deb8c402..912f82b86 100644 --- a/server/controllers/tracker.ts +++ b/server/controllers/tracker.ts | |||
@@ -4,9 +4,11 @@ import * as http from 'http' | |||
4 | import * as bitTorrentTracker from 'bittorrent-tracker' | 4 | import * as bitTorrentTracker from 'bittorrent-tracker' |
5 | import * as proxyAddr from 'proxy-addr' | 5 | import * as proxyAddr from 'proxy-addr' |
6 | import { Server as WebSocketServer } from 'ws' | 6 | import { Server as WebSocketServer } from 'ws' |
7 | import { CONFIG, TRACKER_RATE_LIMITS } from '../initializers/constants' | 7 | import { TRACKER_RATE_LIMITS } from '../initializers/constants' |
8 | import { VideoFileModel } from '../models/video/video-file' | 8 | import { VideoFileModel } from '../models/video/video-file' |
9 | import { parse } from 'url' | 9 | import { parse } from 'url' |
10 | import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist' | ||
11 | import { CONFIG } from '../initializers/config' | ||
10 | 12 | ||
11 | const TrackerServer = bitTorrentTracker.Server | 13 | const TrackerServer = bitTorrentTracker.Server |
12 | 14 | ||
@@ -21,7 +23,11 @@ const trackerServer = new TrackerServer({ | |||
21 | udp: false, | 23 | udp: false, |
22 | ws: false, | 24 | ws: false, |
23 | dht: false, | 25 | dht: false, |
24 | filter: function (infoHash, params, cb) { | 26 | filter: async function (infoHash, params, cb) { |
27 | if (CONFIG.TRACKER.ENABLED === false) { | ||
28 | return cb(new Error('Tracker is disabled on this instance.')) | ||
29 | } | ||
30 | |||
25 | let ip: string | 31 | let ip: string |
26 | 32 | ||
27 | if (params.type === 'ws') { | 33 | if (params.type === 'ws') { |
@@ -32,29 +38,40 @@ const trackerServer = new TrackerServer({ | |||
32 | 38 | ||
33 | const key = ip + '-' + infoHash | 39 | const key = ip + '-' + infoHash |
34 | 40 | ||
35 | peersIps[ip] = peersIps[ip] ? peersIps[ip] + 1 : 1 | 41 | peersIps[ ip ] = peersIps[ ip ] ? peersIps[ ip ] + 1 : 1 |
36 | peersIpInfoHash[key] = peersIpInfoHash[key] ? peersIpInfoHash[key] + 1 : 1 | 42 | peersIpInfoHash[ key ] = peersIpInfoHash[ key ] ? peersIpInfoHash[ key ] + 1 : 1 |
37 | 43 | ||
38 | if (peersIpInfoHash[key] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP_PER_INFOHASH) { | 44 | if (CONFIG.TRACKER.REJECT_TOO_MANY_ANNOUNCES && 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}`)) | 45 | return cb(new Error(`Too many requests (${peersIpInfoHash[ key ]} of ip ${ip} for torrent ${infoHash}`)) |
40 | } | 46 | } |
41 | 47 | ||
42 | VideoFileModel.isInfohashExists(infoHash) | 48 | try { |
43 | .then(exists => { | 49 | if (CONFIG.TRACKER.PRIVATE === false) return cb() |
44 | if (exists === false) return cb(new Error(`Unknown infoHash ${infoHash}`)) | 50 | |
51 | const videoFileExists = await VideoFileModel.doesInfohashExist(infoHash) | ||
52 | if (videoFileExists === true) return cb() | ||
45 | 53 | ||
46 | return cb() | 54 | const playlistExists = await VideoStreamingPlaylistModel.doesInfohashExist(infoHash) |
47 | }) | 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 | } | ||
48 | } | 62 | } |
49 | }) | 63 | }) |
50 | 64 | ||
51 | trackerServer.on('error', function (err) { | 65 | if (CONFIG.TRACKER.ENABLED !== false) { |
52 | logger.error('Error in tracker.', { err }) | ||
53 | }) | ||
54 | 66 | ||
55 | trackerServer.on('warning', function (err) { | 67 | trackerServer.on('error', function (err) { |
56 | logger.warn('Warning in tracker.', { err }) | 68 | logger.error('Error in tracker.', { err }) |
57 | }) | 69 | }) |
70 | |||
71 | trackerServer.on('warning', function (err) { | ||
72 | logger.warn('Warning in tracker.', { err }) | ||
73 | }) | ||
74 | } | ||
58 | 75 | ||
59 | const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer) | 76 | const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer) |
60 | trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' })) | 77 | trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' })) |