tracker.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 { parse } from 'url'
  10. import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
  11. import { CONFIG } from '../initializers/config'
  12. const TrackerServer = bitTorrentTracker.Server
  13. const trackerRouter = express.Router()
  14. let peersIps = {}
  15. let peersIpInfoHash = {}
  16. runPeersChecker()
  17. const trackerServer = new TrackerServer({
  18. http: false,
  19. udp: false,
  20. ws: false,
  21. dht: false,
  22. filter: async function (infoHash, params, cb) {
  23. if (CONFIG.TRACKER.ENABLED === false) {
  24. return cb(new Error('Tracker is disabled on this instance.'))
  25. }
  26. let ip: string
  27. if (params.type === 'ws') {
  28. ip = params.socket.ip
  29. } else {
  30. ip = params.httpReq.ip
  31. }
  32. const key = ip + '-' + infoHash
  33. peersIps[ ip ] = peersIps[ ip ] ? peersIps[ ip ] + 1 : 1
  34. peersIpInfoHash[ key ] = peersIpInfoHash[ key ] ? peersIpInfoHash[ key ] + 1 : 1
  35. if (CONFIG.TRACKER.REJECT_TOO_MANY_ANNOUNCES && peersIpInfoHash[ key ] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP_PER_INFOHASH) {
  36. return cb(new Error(`Too many requests (${peersIpInfoHash[ key ]} of ip ${ip} for torrent ${infoHash}`))
  37. }
  38. try {
  39. if (CONFIG.TRACKER.PRIVATE === false) return cb()
  40. const videoFileExists = await VideoFileModel.doesInfohashExist(infoHash)
  41. if (videoFileExists === true) return cb()
  42. const playlistExists = await VideoStreamingPlaylistModel.doesInfohashExist(infoHash)
  43. if (playlistExists === true) return cb()
  44. return cb(new Error(`Unknown infoHash ${infoHash}`))
  45. } catch (err) {
  46. logger.error('Error in tracker filter.', { err })
  47. return cb(err)
  48. }
  49. }
  50. })
  51. if (CONFIG.TRACKER.ENABLED !== false) {
  52. trackerServer.on('error', function (err) {
  53. logger.error('Error in tracker.', { err })
  54. })
  55. trackerServer.on('warning', function (err) {
  56. logger.warn('Warning in tracker.', { err })
  57. })
  58. }
  59. const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer)
  60. trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' }))
  61. trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' }))
  62. function createWebsocketTrackerServer (app: express.Application) {
  63. const server = http.createServer(app)
  64. const wss = new WebSocketServer({ noServer: true })
  65. wss.on('connection', function (ws, req) {
  66. ws['ip'] = proxyAddr(req, CONFIG.TRUST_PROXY)
  67. trackerServer.onWebSocketConnection(ws)
  68. })
  69. server.on('upgrade', (request, socket, head) => {
  70. const pathname = parse(request.url).pathname
  71. if (pathname === '/tracker/socket') {
  72. wss.handleUpgrade(request, socket, head, ws => wss.emit('connection', ws, request))
  73. }
  74. // Don't destroy socket, we have Socket.IO too
  75. })
  76. return server
  77. }
  78. // ---------------------------------------------------------------------------
  79. export {
  80. trackerRouter,
  81. createWebsocketTrackerServer
  82. }
  83. // ---------------------------------------------------------------------------
  84. function runPeersChecker () {
  85. setInterval(() => {
  86. logger.debug('Checking peers.')
  87. for (const ip of Object.keys(peersIpInfoHash)) {
  88. if (peersIps[ip] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP) {
  89. logger.warn('Peer %s made abnormal requests (%d).', ip, peersIps[ip])
  90. }
  91. }
  92. peersIpInfoHash = {}
  93. peersIps = {}
  94. }, TRACKER_RATE_LIMITS.INTERVAL)
  95. }