]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/tracker.ts
Merge branch 'develop' into cli-wrapper
[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
10 const TrackerServer = bitTorrentTracker.Server
11
12 const trackerRouter = express.Router()
13
14 let peersIps = {}
15 let peersIpInfoHash = {}
16 runPeersChecker()
17
18 const 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
41 VideoFileModel.isInfohashExists(infoHash)
42 .then(exists => {
43 if (exists === false) return cb(new Error(`Unknown infoHash ${infoHash}`))
44
45 return cb()
46 })
47 }
48 })
49
50 trackerServer.on('error', function (err) {
51 logger.error('Error in tracker.', { err })
52 })
53
54 trackerServer.on('warning', function (err) {
55 logger.warn('Warning in tracker.', { err })
56 })
57
58 const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer)
59 trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' }))
60 trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' }))
61
62 function 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
77 export {
78 trackerRouter,
79 createWebsocketServer
80 }
81
82 // ---------------------------------------------------------------------------
83
84 function 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 }