]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/tracker.ts
Update FAQ.md
[github/Chocobozzz/PeerTube.git] / server / controllers / tracker.ts
... / ...
CommitLineData
1import * as bitTorrentTracker from 'bittorrent-tracker'
2import * as express from 'express'
3import * as http from 'http'
4import * as proxyAddr from 'proxy-addr'
5import { Server as WebSocketServer } from 'ws'
6import { Redis } from '@server/lib/redis'
7import { logger } from '../helpers/logger'
8import { CONFIG } from '../initializers/config'
9import { TRACKER_RATE_LIMITS } from '../initializers/constants'
10import { VideoFileModel } from '../models/video/video-file'
11import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
12
13const TrackerServer = bitTorrentTracker.Server
14
15const trackerRouter = express.Router()
16
17let peersIps = {}
18let peersIpInfoHash = {}
19runPeersChecker()
20
21const trackerServer = new TrackerServer({
22 http: false,
23 udp: false,
24 ws: 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.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.doesInfohashExistCached(infoHash)
54 if (playlistExists === true) return cb()
55
56 cb(new Error(`Unknown infoHash ${infoHash} requested by ip ${ip}`))
57
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 }))
62
63 // setTimeout to wait filter response
64 setTimeout(() => params.socket.close(), 0)
65 }
66 } catch (err) {
67 logger.error('Error in tracker filter.', { err })
68 return cb(err)
69 }
70 }
71})
72
73if (CONFIG.TRACKER.ENABLED !== false) {
74
75 trackerServer.on('error', function (err) {
76 logger.error('Error in tracker.', { err })
77 })
78
79 trackerServer.on('warning', function (err) {
80 logger.warn('Warning in tracker.', { err })
81 })
82}
83
84const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer)
85trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' }))
86trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' }))
87
88function createWebsocketTrackerServer (app: express.Application) {
89 const server = http.createServer(app)
90 const wss = new WebSocketServer({ noServer: true })
91
92 wss.on('connection', function (ws, req) {
93 ws['ip'] = proxyAddr(req, CONFIG.TRUST_PROXY)
94
95 trackerServer.onWebSocketConnection(ws)
96 })
97
98 server.on('upgrade', (request: express.Request, socket, head) => {
99 if (request.url === '/tracker/socket') {
100 const ip = proxyAddr(request, CONFIG.TRUST_PROXY)
101
102 Redis.Instance.doesTrackerBlockIPExist(ip)
103 .then(result => {
104 if (result === true) {
105 logger.debug('Blocking IP %s from tracker.', ip)
106
107 socket.write('HTTP/1.1 403 Forbidden\r\n\r\n')
108 socket.destroy()
109 return
110 }
111
112 return wss.handleUpgrade(request, socket, head, ws => wss.emit('connection', ws, request))
113 })
114 .catch(err => logger.error('Cannot check if tracker block ip exists.', { err }))
115 }
116
117 // Don't destroy socket, we have Socket.IO too
118 })
119
120 return server
121}
122
123// ---------------------------------------------------------------------------
124
125export {
126 trackerRouter,
127 createWebsocketTrackerServer
128}
129
130// ---------------------------------------------------------------------------
131
132function runPeersChecker () {
133 setInterval(() => {
134 logger.debug('Checking peers.')
135
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])
139 }
140 }
141
142 peersIpInfoHash = {}
143 peersIps = {}
144 }, TRACKER_RATE_LIMITS.INTERVAL)
145}