From 31b6ddf86652502e0c96d77fa10861ce4af11aa4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 10 Apr 2019 09:23:18 +0200 Subject: Add ability to disable tracker --- server/controllers/api/config.ts | 3 +++ server/controllers/tracker.ts | 23 ++++++++++++++++------- server/initializers/checker-before-init.ts | 3 ++- server/initializers/constants.ts | 5 +++++ server/tests/api/server/tracker.ts | 26 +++++++++++++++++++++++--- 5 files changed, 49 insertions(+), 11 deletions(-) (limited to 'server') diff --git a/server/controllers/api/config.ts b/server/controllers/api/config.ts index 5c4f455ee..0d7fc8625 100644 --- a/server/controllers/api/config.ts +++ b/server/controllers/api/config.ts @@ -136,6 +136,9 @@ async function getConfig (req: express.Request, res: express.Response) { videos: { intervalDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS } + }, + tracker: { + enabled: CONFIG.TRACKER.ENABLED } } diff --git a/server/controllers/tracker.ts b/server/controllers/tracker.ts index 8b77d9de7..56a3424a3 100644 --- a/server/controllers/tracker.ts +++ b/server/controllers/tracker.ts @@ -23,6 +23,10 @@ const trackerServer = new TrackerServer({ ws: false, dht: false, filter: async function (infoHash, params, cb) { + if (CONFIG.TRACKER.ENABLED === false) { + return cb(new Error('Tracker is disabled on this instance.')) + } + let ip: string if (params.type === 'ws') { @@ -36,11 +40,13 @@ const trackerServer = new TrackerServer({ peersIps[ ip ] = peersIps[ ip ] ? peersIps[ ip ] + 1 : 1 peersIpInfoHash[ key ] = peersIpInfoHash[ key ] ? peersIpInfoHash[ key ] + 1 : 1 - if (peersIpInfoHash[ key ] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP_PER_INFOHASH) { + if (CONFIG.TRACKER.REJECT_TOO_MANY_ANNOUNCES && peersIpInfoHash[ key ] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP_PER_INFOHASH) { return cb(new Error(`Too many requests (${peersIpInfoHash[ key ]} of ip ${ip} for torrent ${infoHash}`)) } try { + if (CONFIG.TRACKER.PRIVATE === false) return cb() + const videoFileExists = await VideoFileModel.doesInfohashExist(infoHash) if (videoFileExists === true) return cb() @@ -55,13 +61,16 @@ const trackerServer = new TrackerServer({ } }) -trackerServer.on('error', function (err) { - logger.error('Error in tracker.', { err }) -}) +if (CONFIG.TRACKER.ENABLED !== false) { -trackerServer.on('warning', function (err) { - logger.warn('Warning in tracker.', { err }) -}) + trackerServer.on('error', function (err) { + logger.error('Error in tracker.', { err }) + }) + + trackerServer.on('warning', function (err) { + logger.warn('Warning in tracker.', { err }) + }) +} const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer) trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' })) diff --git a/server/initializers/checker-before-init.ts b/server/initializers/checker-before-init.ts index 3095913a3..6b43debfb 100644 --- a/server/initializers/checker-before-init.ts +++ b/server/initializers/checker-before-init.ts @@ -25,7 +25,8 @@ function checkMissedConfig () { 'instance.name', 'instance.short_description', 'instance.description', 'instance.terms', 'instance.default_client_route', 'instance.is_nsfw', 'instance.default_nsfw_policy', 'instance.robots', 'instance.securitytxt', 'services.twitter.username', 'services.twitter.whitelisted', - 'followers.instance.enabled', 'followers.instance.manual_approval' + 'followers.instance.enabled', 'followers.instance.manual_approval', + 'tracker.enabled', 'tracker.private', 'tracker.reject_too_many_announces' ] const requiredAlternatives = [ [ // set diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index 097199f84..3f02572db 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -243,6 +243,11 @@ const CONFIG = { REPORT_ONLY: config.get('csp.report_only'), REPORT_URI: config.get('csp.report_uri') }, + TRACKER: { + ENABLED: config.get('tracker.enabled'), + PRIVATE: config.get('tracker.private'), + REJECT_TOO_MANY_ANNOUNCES: config.get('tracker.reject_too_many_announces') + }, ADMIN: { get EMAIL () { return config.get('admin.email') } }, diff --git a/server/tests/api/server/tracker.ts b/server/tests/api/server/tracker.ts index 25ca00029..41803aef1 100644 --- a/server/tests/api/server/tracker.ts +++ b/server/tests/api/server/tracker.ts @@ -2,7 +2,7 @@ import * as magnetUtil from 'magnet-uri' import 'mocha' -import { getVideo, killallServers, runServer, ServerInfo, uploadVideo } from '../../../../shared/utils' +import { getVideo, killallServers, reRunServer, runServer, ServerInfo, uploadVideo } from '../../../../shared/utils' import { flushTests, setAccessTokensToServers } from '../../../../shared/utils/index' import { VideoDetails } from '../../../../shared/models/videos' import * as WebTorrent from 'webtorrent' @@ -34,7 +34,7 @@ describe('Test tracker', function () { } }) - it('Should return an error when adding an incorrect infohash', done => { + it('Should return an error when adding an incorrect infohash', function (done) { this.timeout(10000) const webtorrent = new WebTorrent() @@ -49,7 +49,7 @@ describe('Test tracker', function () { torrent.on('done', () => done(new Error('No error on infohash'))) }) - it('Should succeed with the correct infohash', done => { + it('Should succeed with the correct infohash', function (done) { this.timeout(10000) const webtorrent = new WebTorrent() @@ -64,6 +64,26 @@ describe('Test tracker', function () { torrent.on('done', done) }) + it('Should disable the tracker', function (done) { + this.timeout(20000) + + killallServers([ server ]) + reRunServer(server, { tracker: { enabled: false } }) + .then(() => { + const webtorrent = new WebTorrent() + + const torrent = webtorrent.add(goodMagnet) + + torrent.on('error', done) + torrent.on('warning', warn => { + const message = typeof warn === 'string' ? warn : warn.message + if (message.indexOf('disabled ') !== -1) return done() + }) + + torrent.on('done', () => done(new Error('Tracker is enabled'))) + }) + }) + after(async function () { killallServers([ server ]) }) -- cgit v1.2.3