aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers')
-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