aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/tracker.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-06-25 16:27:35 +0200
committerChocobozzz <me@florianbigard.com>2020-06-25 16:28:07 +0200
commitdb48de8597897e5024f8e9ed5acb1a8f40748169 (patch)
tree5703f2fe67de886acffb39867c6dc7f2ea24368b /server/controllers/tracker.ts
parentd4bf24df8ed7032d6db1b04a716e3881679bbf46 (diff)
downloadPeerTube-db48de8597897e5024f8e9ed5acb1a8f40748169.tar.gz
PeerTube-db48de8597897e5024f8e9ed5acb1a8f40748169.tar.zst
PeerTube-db48de8597897e5024f8e9ed5acb1a8f40748169.zip
Block infohash spammers from tracker
Diffstat (limited to 'server/controllers/tracker.ts')
-rw-r--r--server/controllers/tracker.ts34
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 @@
1import { logger } from '../helpers/logger' 1import * as bitTorrentTracker from 'bittorrent-tracker'
2import * as express from 'express' 2import * as express from 'express'
3import * as http from 'http' 3import * as http from 'http'
4import * as bitTorrentTracker from 'bittorrent-tracker'
5import * as proxyAddr from 'proxy-addr' 4import * as proxyAddr from 'proxy-addr'
6import { Server as WebSocketServer } from 'ws' 5import { Server as WebSocketServer } from 'ws'
6import { Redis } from '@server/lib/redis'
7import { logger } from '../helpers/logger'
8import { CONFIG } from '../initializers/config'
7import { TRACKER_RATE_LIMITS } from '../initializers/constants' 9import { TRACKER_RATE_LIMITS } from '../initializers/constants'
8import { VideoFileModel } from '../models/video/video-file' 10import { VideoFileModel } from '../models/video/video-file'
9import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist' 11import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
10import { CONFIG } from '../initializers/config'
11 12
12const TrackerServer = bitTorrentTracker.Server 13const 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