2
1

tracker.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. const TrackerServer = bitTorrentTracker.Server
  10. const trackerRouter = express.Router()
  11. let peersIps = {}
  12. let peersIpInfoHash = {}
  13. runPeersChecker()
  14. const trackerServer = new TrackerServer({
  15. http: false,
  16. udp: false,
  17. ws: false,
  18. dht: false,
  19. filter: function (infoHash, params, cb) {
  20. let ip: string
  21. if (params.type === 'ws') {
  22. ip = params.socket.ip
  23. } else {
  24. ip = params.httpReq.ip
  25. }
  26. const key = ip + '-' + infoHash
  27. peersIps[ip] = peersIps[ip] ? peersIps[ip] + 1 : 1
  28. peersIpInfoHash[key] = peersIpInfoHash[key] ? peersIpInfoHash[key] + 1 : 1
  29. if (peersIpInfoHash[key] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP_PER_INFOHASH) {
  30. return cb(new Error(`Too many requests (${peersIpInfoHash[ key ]} of ip ${ip} for torrent ${infoHash}`))
  31. }
  32. VideoFileModel.isInfohashExists(infoHash)
  33. .then(exists => {
  34. if (exists === false) return cb(new Error(`Unknown infoHash ${infoHash}`))
  35. return cb()
  36. })
  37. }
  38. })
  39. trackerServer.on('error', function (err) {
  40. logger.error('Error in tracker.', { err })
  41. })
  42. trackerServer.on('warning', function (err) {
  43. logger.warn('Warning in tracker.', { err })
  44. })
  45. const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer)
  46. trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' }))
  47. trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' }))
  48. function createWebsocketServer (app: express.Application) {
  49. const server = http.createServer(app)
  50. const wss = new WebSocketServer({ server: server, path: '/tracker/socket' })
  51. wss.on('connection', function (ws, req) {
  52. const ip = proxyAddr(req, CONFIG.TRUST_PROXY)
  53. ws['ip'] = ip
  54. trackerServer.onWebSocketConnection(ws)
  55. })
  56. return server
  57. }
  58. // ---------------------------------------------------------------------------
  59. export {
  60. trackerRouter,
  61. createWebsocketServer
  62. }
  63. // ---------------------------------------------------------------------------
  64. function runPeersChecker () {
  65. setInterval(() => {
  66. logger.debug('Checking peers.')
  67. for (const ip of Object.keys(peersIpInfoHash)) {
  68. if (peersIps[ip] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP) {
  69. logger.warn('Peer %s made abnormal requests (%d).', ip, peersIps[ip])
  70. }
  71. }
  72. peersIpInfoHash = {}
  73. peersIps = {}
  74. }, TRACKER_RATE_LIMITS.INTERVAL)
  75. }