1 import { logger } from '../helpers/logger'
2 import * as express from 'express'
3 import * as http from 'http'
4 import * as bitTorrentTracker from 'bittorrent-tracker'
5 import * as proxyAddr from 'proxy-addr'
6 import { Server as WebSocketServer } from 'ws'
7 import { TRACKER_RATE_LIMITS } from '../initializers/constants'
8 import { VideoFileModel } from '../models/video/video-file'
9 import { parse } from 'url'
10 import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
11 import { CONFIG } from '../initializers/config'
13 const TrackerServer = bitTorrentTracker.Server
15 const trackerRouter = express.Router()
18 let peersIpInfoHash = {}
21 const trackerServer = new TrackerServer({
26 filter: async function (infoHash, params, cb) {
27 if (CONFIG.TRACKER.ENABLED === false) {
28 return cb(new Error('Tracker is disabled on this instance.'))
33 if (params.type === 'ws') {
36 ip = params.httpReq.ip
39 const key = ip + '-' + infoHash
41 peersIps[ ip ] = peersIps[ ip ] ? peersIps[ ip ] + 1 : 1
42 peersIpInfoHash[ key ] = peersIpInfoHash[ key ] ? peersIpInfoHash[ key ] + 1 : 1
44 if (CONFIG.TRACKER.REJECT_TOO_MANY_ANNOUNCES && peersIpInfoHash[ key ] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP_PER_INFOHASH) {
45 return cb(new Error(`Too many requests (${peersIpInfoHash[ key ]} of ip ${ip} for torrent ${infoHash}`))
49 if (CONFIG.TRACKER.PRIVATE === false) return cb()
51 const videoFileExists = await VideoFileModel.doesInfohashExistCached(infoHash)
52 if (videoFileExists === true) return cb()
54 const playlistExists = await VideoStreamingPlaylistModel.doesInfohashExist(infoHash)
55 if (playlistExists === true) return cb()
57 return cb(new Error(`Unknown infoHash ${infoHash}`))
59 logger.error('Error in tracker filter.', { err })
65 if (CONFIG.TRACKER.ENABLED !== false) {
67 trackerServer.on('error', function (err) {
68 logger.error('Error in tracker.', { err })
71 trackerServer.on('warning', function (err) {
72 logger.warn('Warning in tracker.', { err })
76 const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer)
77 trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' }))
78 trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' }))
80 function createWebsocketTrackerServer (app: express.Application) {
81 const server = http.createServer(app)
82 const wss = new WebSocketServer({ noServer: true })
84 wss.on('connection', function (ws, req) {
85 ws['ip'] = proxyAddr(req, CONFIG.TRUST_PROXY)
87 trackerServer.onWebSocketConnection(ws)
90 server.on('upgrade', (request, socket, head) => {
91 const pathname = parse(request.url).pathname
93 if (pathname === '/tracker/socket') {
94 wss.handleUpgrade(request, socket, head, ws => wss.emit('connection', ws, request))
97 // Don't destroy socket, we have Socket.IO too
103 // ---------------------------------------------------------------------------
107 createWebsocketTrackerServer
110 // ---------------------------------------------------------------------------
112 function runPeersChecker () {
114 logger.debug('Checking peers.')
116 for (const ip of Object.keys(peersIpInfoHash)) {
117 if (peersIps[ip] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP) {
118 logger.warn('Peer %s made abnormal requests (%d).', ip, peersIps[ip])
124 }, TRACKER_RATE_LIMITS.INTERVAL)