diff options
author | Chocobozzz <me@florianbigard.com> | 2020-06-25 16:27:35 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2020-06-25 16:28:07 +0200 |
commit | db48de8597897e5024f8e9ed5acb1a8f40748169 (patch) | |
tree | 5703f2fe67de886acffb39867c6dc7f2ea24368b /server/controllers | |
parent | d4bf24df8ed7032d6db1b04a716e3881679bbf46 (diff) | |
download | PeerTube-db48de8597897e5024f8e9ed5acb1a8f40748169.tar.gz PeerTube-db48de8597897e5024f8e9ed5acb1a8f40748169.tar.zst PeerTube-db48de8597897e5024f8e9ed5acb1a8f40748169.zip |
Block infohash spammers from tracker
Diffstat (limited to 'server/controllers')
-rw-r--r-- | server/controllers/tracker.ts | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/server/controllers/tracker.ts b/server/controllers/tracker.ts index cacff36ec..c962fada5 100644 --- a/server/controllers/tracker.ts +++ b/server/controllers/tracker.ts | |||
@@ -1,13 +1,14 @@ | |||
1 | import { logger } from '../helpers/logger' | 1 | import * as bitTorrentTracker from 'bittorrent-tracker' |
2 | import * as express from 'express' | 2 | import * as express from 'express' |
3 | import * as http from 'http' | 3 | import * as http from 'http' |
4 | import * as bitTorrentTracker from 'bittorrent-tracker' | ||
5 | import * as proxyAddr from 'proxy-addr' | 4 | import * as proxyAddr from 'proxy-addr' |
6 | import { Server as WebSocketServer } from 'ws' | 5 | import { Server as WebSocketServer } from 'ws' |
6 | import { Redis } from '@server/lib/redis' | ||
7 | import { logger } from '../helpers/logger' | ||
8 | import { CONFIG } from '../initializers/config' | ||
7 | import { TRACKER_RATE_LIMITS } from '../initializers/constants' | 9 | import { TRACKER_RATE_LIMITS } from '../initializers/constants' |
8 | import { VideoFileModel } from '../models/video/video-file' | 10 | import { VideoFileModel } from '../models/video/video-file' |
9 | import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist' | 11 | import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist' |
10 | import { CONFIG } from '../initializers/config' | ||
11 | 12 | ||
12 | const TrackerServer = bitTorrentTracker.Server | 13 | const TrackerServer = bitTorrentTracker.Server |
13 | 14 | ||
@@ -53,7 +54,16 @@ const trackerServer = new TrackerServer({ | |||
53 | const playlistExists = await VideoStreamingPlaylistModel.doesInfohashExist(infoHash) | 54 | const playlistExists = await VideoStreamingPlaylistModel.doesInfohashExist(infoHash) |
54 | if (playlistExists === true) return cb() | 55 | if (playlistExists === true) return cb() |
55 | 56 | ||
56 | return cb(new Error(`Unknown infoHash ${infoHash} requested by ip ${ip}`)) | 57 | cb(new Error(`Unknown infoHash ${infoHash} requested by ip ${ip}`)) |
58 | |||
59 | // Close socket connection and block IP for a few time | ||
60 | if (params.type === 'ws') { | ||
61 | Redis.Instance.setTrackerBlockIP(ip) | ||
62 | .catch(err => logger.error('Cannot set tracker block ip.', { err })) | ||
63 | |||
64 | // setTimeout to wait filter response | ||
65 | setTimeout(() => params.socket.close(), 0) | ||
66 | } | ||
57 | } catch (err) { | 67 | } catch (err) { |
58 | logger.error('Error in tracker filter.', { err }) | 68 | logger.error('Error in tracker filter.', { err }) |
59 | return cb(err) | 69 | return cb(err) |
@@ -88,7 +98,21 @@ function createWebsocketTrackerServer (app: express.Application) { | |||
88 | 98 | ||
89 | server.on('upgrade', (request: express.Request, socket, head) => { | 99 | server.on('upgrade', (request: express.Request, socket, head) => { |
90 | if (request.url === '/tracker/socket') { | 100 | if (request.url === '/tracker/socket') { |
91 | wss.handleUpgrade(request, socket, head, ws => wss.emit('connection', ws, request)) | 101 | const ip = proxyAddr(request, CONFIG.TRUST_PROXY) |
102 | |||
103 | Redis.Instance.doesTrackerBlockIPExist(ip) | ||
104 | .then(result => { | ||
105 | if (result === true) { | ||
106 | logger.debug('Blocking IP %s from tracker.', ip) | ||
107 | |||
108 | socket.write('HTTP/1.1 403 Forbidden\r\n\r\n') | ||
109 | socket.destroy() | ||
110 | return | ||
111 | } | ||
112 | |||
113 | return wss.handleUpgrade(request, socket, head, ws => wss.emit('connection', ws, request)) | ||
114 | }) | ||
115 | .catch(err => logger.error('Cannot check if tracker block ip exists.', { err })) | ||
92 | } | 116 | } |
93 | 117 | ||
94 | // Don't destroy socket, we have Socket.IO too | 118 | // Don't destroy socket, we have Socket.IO too |