]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/tracker.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / controllers / tracker.ts
CommitLineData
41fb13c3
C
1import { Server as TrackerServer } from 'bittorrent-tracker'
2import express from 'express'
3import { createServer } from 'http'
83002a82 4import { LRUCache } from 'lru-cache'
41fb13c3 5import proxyAddr from 'proxy-addr'
e874edd9 6import { WebSocketServer } from 'ws'
db48de85
C
7import { logger } from '../helpers/logger'
8import { CONFIG } from '../initializers/config'
1fed9cb8 9import { LRU_CACHE, TRACKER_RATE_LIMITS } from '../initializers/constants'
cc43831a 10import { VideoFileModel } from '../models/video/video-file'
09209296 11import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
9b67da3d 12
9b67da3d
C
13const trackerRouter = express.Router()
14
1fed9cb8
C
15const blockedIPs = new LRUCache<string, boolean>({
16 max: LRU_CACHE.TRACKER_IPS.MAX_SIZE,
17 ttl: TRACKER_RATE_LIMITS.BLOCK_IP_LIFETIME
18})
19
9b67da3d
C
20let peersIps = {}
21let peersIpInfoHash = {}
22runPeersChecker()
23
24const trackerServer = new TrackerServer({
25 http: false,
26 udp: false,
27 ws: false,
09209296 28 filter: async function (infoHash, params, cb) {
31b6ddf8
C
29 if (CONFIG.TRACKER.ENABLED === false) {
30 return cb(new Error('Tracker is disabled on this instance.'))
31 }
32
9b67da3d
C
33 let ip: string
34
35 if (params.type === 'ws') {
b14e8e46 36 ip = params.ip
9b67da3d
C
37 } else {
38 ip = params.httpReq.ip
39 }
40
41 const key = ip + '-' + infoHash
42
a1587156
C
43 peersIps[ip] = peersIps[ip] ? peersIps[ip] + 1 : 1
44 peersIpInfoHash[key] = peersIpInfoHash[key] ? peersIpInfoHash[key] + 1 : 1
9b67da3d 45
a1587156
C
46 if (CONFIG.TRACKER.REJECT_TOO_MANY_ANNOUNCES && peersIpInfoHash[key] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP_PER_INFOHASH) {
47 return cb(new Error(`Too many requests (${peersIpInfoHash[key]} of ip ${ip} for torrent ${infoHash}`))
9b67da3d
C
48 }
49
09209296 50 try {
31b6ddf8
C
51 if (CONFIG.TRACKER.PRIVATE === false) return cb()
52
35f28e94 53 const videoFileExists = await VideoFileModel.doesInfohashExistCached(infoHash)
09209296 54 if (videoFileExists === true) return cb()
cc43831a 55
d988e9a2 56 const playlistExists = await VideoStreamingPlaylistModel.doesInfohashExistCached(infoHash)
09209296
C
57 if (playlistExists === true) return cb()
58
db48de85
C
59 cb(new Error(`Unknown infoHash ${infoHash} requested by ip ${ip}`))
60
61 // Close socket connection and block IP for a few time
62 if (params.type === 'ws') {
1fed9cb8 63 blockedIPs.set(ip, true)
db48de85
C
64
65 // setTimeout to wait filter response
66 setTimeout(() => params.socket.close(), 0)
67 }
09209296
C
68 } catch (err) {
69 logger.error('Error in tracker filter.', { err })
70 return cb(err)
71 }
9b67da3d
C
72 }
73})
74
31b6ddf8 75if (CONFIG.TRACKER.ENABLED !== false) {
31b6ddf8
C
76 trackerServer.on('error', function (err) {
77 logger.error('Error in tracker.', { err })
78 })
79
80 trackerServer.on('warning', function (err) {
f614635d
C
81 const message = err.message || ''
82
83 if (CONFIG.LOG.LOG_TRACKER_UNKNOWN_INFOHASH === false && message.includes('Unknown infoHash')) {
84 return
a4152bed
C
85 }
86
31b6ddf8
C
87 logger.warn('Warning in tracker.', { err })
88 })
89}
9b67da3d
C
90
91const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer)
92trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' }))
93trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' }))
94
cef534ed 95function createWebsocketTrackerServer (app: express.Application) {
41fb13c3 96 const server = createServer(app)
89ada4e2
C
97 const wss = new WebSocketServer({ noServer: true })
98
9b67da3d 99 wss.on('connection', function (ws, req) {
89ada4e2 100 ws['ip'] = proxyAddr(req, CONFIG.TRUST_PROXY)
9b67da3d
C
101
102 trackerServer.onWebSocketConnection(ws)
103 })
104
a1587156 105 server.on('upgrade', (request: express.Request, socket, head) => {
4832e415 106 if (request.url === '/tracker/socket') {
db48de85
C
107 const ip = proxyAddr(request, CONFIG.TRUST_PROXY)
108
1fed9cb8
C
109 if (blockedIPs.has(ip)) {
110 logger.debug('Blocking IP %s from tracker.', ip)
db48de85 111
1fed9cb8
C
112 socket.write('HTTP/1.1 403 Forbidden\r\n\r\n')
113 socket.destroy()
114 return
115 }
db48de85 116
1fed9cb8
C
117 // FIXME: typings
118 return wss.handleUpgrade(request, socket as any, head, ws => wss.emit('connection', ws, request))
89ada4e2
C
119 }
120
121 // Don't destroy socket, we have Socket.IO too
122 })
123
58477244 124 return { server, trackerServer }
9b67da3d
C
125}
126
127// ---------------------------------------------------------------------------
128
129export {
130 trackerRouter,
cef534ed 131 createWebsocketTrackerServer
9b67da3d
C
132}
133
134// ---------------------------------------------------------------------------
135
136function runPeersChecker () {
137 setInterval(() => {
138 logger.debug('Checking peers.')
139
140 for (const ip of Object.keys(peersIpInfoHash)) {
141 if (peersIps[ip] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP) {
142 logger.warn('Peer %s made abnormal requests (%d).', ip, peersIps[ip])
143 }
144 }
145
146 peersIpInfoHash = {}
147 peersIps = {}
148 }, TRACKER_RATE_LIMITS.INTERVAL)
149}