]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/tracker.ts
Dislike bar in the same color as the button
[github/Chocobozzz/PeerTube.git] / server / controllers / tracker.ts
CommitLineData
9b67da3d
C
1import { logger } from '../helpers/logger'
2import * as express from 'express'
3import * as http from 'http'
4import * as bitTorrentTracker from 'bittorrent-tracker'
5import * as proxyAddr from 'proxy-addr'
6import { Server as WebSocketServer } from 'ws'
7import { CONFIG, TRACKER_RATE_LIMITS } from '../initializers/constants'
cc43831a 8import { VideoFileModel } from '../models/video/video-file'
9b67da3d
C
9
10const TrackerServer = bitTorrentTracker.Server
11
12const trackerRouter = express.Router()
13
14let peersIps = {}
15let peersIpInfoHash = {}
16runPeersChecker()
17
18const trackerServer = new TrackerServer({
19 http: false,
20 udp: false,
21 ws: false,
22 dht: false,
23 filter: function (infoHash, params, cb) {
24 let ip: string
25
26 if (params.type === 'ws') {
27 ip = params.socket.ip
28 } else {
29 ip = params.httpReq.ip
30 }
31
32 const key = ip + '-' + infoHash
33
34 peersIps[ip] = peersIps[ip] ? peersIps[ip] + 1 : 1
35 peersIpInfoHash[key] = peersIpInfoHash[key] ? peersIpInfoHash[key] + 1 : 1
36
37 if (peersIpInfoHash[key] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP_PER_INFOHASH) {
38 return cb(new Error(`Too many requests (${peersIpInfoHash[ key ]} of ip ${ip} for torrent ${infoHash}`))
39 }
40
cc43831a
C
41 VideoFileModel.isInfohashExists(infoHash)
42 .then(exists => {
43 if (exists === false) return cb(new Error(`Unknown infoHash ${infoHash}`))
44
45 return cb()
46 })
9b67da3d
C
47 }
48})
49
50trackerServer.on('error', function (err) {
51 logger.error('Error in tracker.', { err })
52})
53
54trackerServer.on('warning', function (err) {
55 logger.warn('Warning in tracker.', { err })
56})
57
58const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer)
59trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' }))
60trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' }))
61
62function createWebsocketServer (app: express.Application) {
63 const server = http.createServer(app)
64 const wss = new WebSocketServer({ server: server, path: '/tracker/socket' })
65 wss.on('connection', function (ws, req) {
66 const ip = proxyAddr(req, CONFIG.TRUST_PROXY)
67 ws['ip'] = ip
68
69 trackerServer.onWebSocketConnection(ws)
70 })
71
72 return server
73}
74
75// ---------------------------------------------------------------------------
76
77export {
78 trackerRouter,
79 createWebsocketServer
80}
81
82// ---------------------------------------------------------------------------
83
84function runPeersChecker () {
85 setInterval(() => {
86 logger.debug('Checking peers.')
87
88 for (const ip of Object.keys(peersIpInfoHash)) {
89 if (peersIps[ip] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP) {
90 logger.warn('Peer %s made abnormal requests (%d).', ip, peersIps[ip])
91 }
92 }
93
94 peersIpInfoHash = {}
95 peersIps = {}
96 }, TRACKER_RATE_LIMITS.INTERVAL)
97}