diff options
Diffstat (limited to 'server/controllers/tracker.ts')
-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 |