1 import * as bitTorrentTracker from 'bittorrent-tracker'
2 import * as express from 'express'
3 import * as http from 'http'
4 import * as proxyAddr from 'proxy-addr'
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'
9 import { TRACKER_RATE_LIMITS } from '../initializers/constants'
10 import { VideoFileModel } from '../models/video/video-file'
11 import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
13 const TrackerServer = bitTorrentTracker.Server
15 const trackerRouter = express.Router()
18 let peersIpInfoHash = {}
21 const trackerServer = new TrackerServer({
25 filter: async function (infoHash, params, cb) {
26 if (CONFIG.TRACKER.ENABLED === false) {
27 return cb(new Error('Tracker is disabled on this instance.'))
32 if (params.type === 'ws') {
35 ip = params.httpReq.ip
38 const key = ip + '-' + infoHash
40 peersIps[ip] = peersIps[ip] ? peersIps[ip] + 1 : 1
41 peersIpInfoHash[key] = peersIpInfoHash[key] ? peersIpInfoHash[key] + 1 : 1
43 if (CONFIG.TRACKER.REJECT_TOO_MANY_ANNOUNCES && peersIpInfoHash[key] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP_PER_INFOHASH) {
44 return cb(new Error(`Too many requests (${peersIpInfoHash[key]} of ip ${ip} for torrent ${infoHash}`))
48 if (CONFIG.TRACKER.PRIVATE === false) return cb()
50 const videoFileExists = await VideoFileModel.doesInfohashExistCached(infoHash)
51 if (videoFileExists === true) return cb()
53 const playlistExists = await VideoStreamingPlaylistModel.doesInfohashExistCached(infoHash)
54 if (playlistExists === true) return cb()
56 cb(new Error(`Unknown infoHash ${infoHash} requested by ip ${ip}`))
58 // Close socket connection and block IP for a few time
59 if (params.type === 'ws') {
60 Redis.Instance.setTrackerBlockIP(ip)
61 .catch(err => logger.error('Cannot set tracker block ip.', { err }))
63 // setTimeout to wait filter response
64 setTimeout(() => params.socket.close(), 0)
67 logger.error('Error in tracker filter.', { err })
73 if (CONFIG.TRACKER.ENABLED !== false) {
75 trackerServer.on('error', function (err) {
76 logger.error('Error in tracker.', { err })
79 trackerServer.on('warning', function (err) {
80 logger.warn('Warning in tracker.', { err })
84 const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer)
85 trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' }))
86 trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' }))
88 function createWebsocketTrackerServer (app: express.Application) {
89 const server = http.createServer(app)
90 const wss = new WebSocketServer({ noServer: true })
92 wss.on('connection', function (ws, req) {
93 ws['ip'] = proxyAddr(req, CONFIG.TRUST_PROXY)
95 trackerServer.onWebSocketConnection(ws)
98 server.on('upgrade', (request: express.Request, socket, head) => {
99 if (request.url === '/tracker/socket') {
100 const ip = proxyAddr(request, CONFIG.TRUST_PROXY)
102 Redis.Instance.doesTrackerBlockIPExist(ip)
104 if (result === true) {
105 logger.debug('Blocking IP %s from tracker.', ip)
107 socket.write('HTTP/1.1 403 Forbidden\r\n\r\n')
112 return wss.handleUpgrade(request, socket, head, ws => wss.emit('connection', ws, request))
114 .catch(err => logger.error('Cannot check if tracker block ip exists.', { err }))
117 // Don't destroy socket, we have Socket.IO too
123 // ---------------------------------------------------------------------------
127 createWebsocketTrackerServer
130 // ---------------------------------------------------------------------------
132 function runPeersChecker () {
134 logger.debug('Checking peers.')
136 for (const ip of Object.keys(peersIpInfoHash)) {
137 if (peersIps[ip] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP) {
138 logger.warn('Peer %s made abnormal requests (%d).', ip, peersIps[ip])
144 }, TRACKER_RATE_LIMITS.INTERVAL)