]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/tracker.ts
Logging ip requesting unknown infoHash
[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 { TRACKER_RATE_LIMITS } from '../initializers/constants'
8 import { VideoFileModel } from '../models/video/video-file'
9 import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
10 import { CONFIG } from '../initializers/config'
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.doesInfohashExistCached(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} requested by ip ${ip}`))
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: express.Request, socket, head) => {
90 if (request.url === '/tracker/socket') {
91 wss.handleUpgrade(request, socket, head, ws => wss.emit('connection', ws, request))
92 }
93
94 // Don't destroy socket, we have Socket.IO too
95 })
96
97 return server
98 }
99
100 // ---------------------------------------------------------------------------
101
102 export {
103 trackerRouter,
104 createWebsocketTrackerServer
105 }
106
107 // ---------------------------------------------------------------------------
108
109 function runPeersChecker () {
110 setInterval(() => {
111 logger.debug('Checking peers.')
112
113 for (const ip of Object.keys(peersIpInfoHash)) {
114 if (peersIps[ip] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP) {
115 logger.warn('Peer %s made abnormal requests (%d).', ip, peersIps[ip])
116 }
117 }
118
119 peersIpInfoHash = {}
120 peersIps = {}
121 }, TRACKER_RATE_LIMITS.INTERVAL)
122 }