]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/tracker.ts
Add ability to disable tracker
[github/Chocobozzz/PeerTube.git] / server / controllers / tracker.ts
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 { CONFIG, 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
12 const TrackerServer = bitTorrentTracker.Server
13
14 const trackerRouter = express.Router()
15
16 let peersIps = {}
17 let peersIpInfoHash = {}
18 runPeersChecker()
19
20 const trackerServer = new TrackerServer({
21 http: false,
22 udp: false,
23 ws: false,
24 dht: false,
25 filter: async function (infoHash, params, cb) {
26 if (CONFIG.TRACKER.ENABLED === false) {
27 return cb(new Error('Tracker is disabled on this instance.'))
28 }
29
30 let ip: string
31
32 if (params.type === 'ws') {
33 ip = params.socket.ip
34 } else {
35 ip = params.httpReq.ip
36 }
37
38 const key = ip + '-' + infoHash
39
40 peersIps[ ip ] = peersIps[ ip ] ? peersIps[ ip ] + 1 : 1
41 peersIpInfoHash[ key ] = peersIpInfoHash[ key ] ? peersIpInfoHash[ key ] + 1 : 1
42
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}`))
45 }
46
47 try {
48 if (CONFIG.TRACKER.PRIVATE === false) return cb()
49
50 const videoFileExists = await VideoFileModel.doesInfohashExist(infoHash)
51 if (videoFileExists === true) return cb()
52
53 const playlistExists = await VideoStreamingPlaylistModel.doesInfohashExist(infoHash)
54 if (playlistExists === true) return cb()
55
56 return cb(new Error(`Unknown infoHash ${infoHash}`))
57 } catch (err) {
58 logger.error('Error in tracker filter.', { err })
59 return cb(err)
60 }
61 }
62 })
63
64 if (CONFIG.TRACKER.ENABLED !== false) {
65
66 trackerServer.on('error', function (err) {
67 logger.error('Error in tracker.', { err })
68 })
69
70 trackerServer.on('warning', function (err) {
71 logger.warn('Warning in tracker.', { err })
72 })
73 }
74
75 const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer)
76 trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' }))
77 trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' }))
78
79 function createWebsocketTrackerServer (app: express.Application) {
80 const server = http.createServer(app)
81 const wss = new WebSocketServer({ noServer: true })
82
83 wss.on('connection', function (ws, req) {
84 ws['ip'] = proxyAddr(req, CONFIG.TRUST_PROXY)
85
86 trackerServer.onWebSocketConnection(ws)
87 })
88
89 server.on('upgrade', (request, socket, head) => {
90 const pathname = parse(request.url).pathname
91
92 if (pathname === '/tracker/socket') {
93 wss.handleUpgrade(request, socket, head, ws => wss.emit('connection', ws, request))
94 }
95
96 // Don't destroy socket, we have Socket.IO too
97 })
98
99 return server
100 }
101
102 // ---------------------------------------------------------------------------
103
104 export {
105 trackerRouter,
106 createWebsocketTrackerServer
107 }
108
109 // ---------------------------------------------------------------------------
110
111 function runPeersChecker () {
112 setInterval(() => {
113 logger.debug('Checking peers.')
114
115 for (const ip of Object.keys(peersIpInfoHash)) {
116 if (peersIps[ip] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP) {
117 logger.warn('Peer %s made abnormal requests (%d).', ip, peersIps[ip])
118 }
119 }
120
121 peersIpInfoHash = {}
122 peersIps = {}
123 }, TRACKER_RATE_LIMITS.INTERVAL)
124 }