ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

149 lines
4.4 KiB

  1. import { Server as TrackerServer } from 'bittorrent-tracker'
  2. import express from 'express'
  3. import { createServer } from 'http'
  4. import { LRUCache } from 'lru-cache'
  5. import proxyAddr from 'proxy-addr'
  6. import { WebSocketServer } from 'ws'
  7. import { logger } from '../helpers/logger.js'
  8. import { CONFIG } from '../initializers/config.js'
  9. import { LRU_CACHE, TRACKER_RATE_LIMITS } from '../initializers/constants.js'
  10. import { VideoFileModel } from '../models/video/video-file.js'
  11. import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist.js'
  12. const trackerRouter = express.Router()
  13. const blockedIPs = new LRUCache<string, boolean>({
  14. max: LRU_CACHE.TRACKER_IPS.MAX_SIZE,
  15. ttl: TRACKER_RATE_LIMITS.BLOCK_IP_LIFETIME
  16. })
  17. let peersIps = {}
  18. let peersIpInfoHash = {}
  19. runPeersChecker()
  20. const trackerServer = new TrackerServer({
  21. http: false,
  22. udp: false,
  23. ws: false,
  24. filter: async function (infoHash, params, cb) {
  25. if (CONFIG.TRACKER.ENABLED === false) {
  26. return cb(new Error('Tracker is disabled on this instance.'))
  27. }
  28. let ip: string
  29. if (params.type === 'ws') {
  30. ip = params.ip
  31. } else {
  32. ip = params.httpReq.ip
  33. }
  34. const key = ip + '-' + infoHash
  35. peersIps[ip] = peersIps[ip] ? peersIps[ip] + 1 : 1
  36. peersIpInfoHash[key] = peersIpInfoHash[key] ? peersIpInfoHash[key] + 1 : 1
  37. if (CONFIG.TRACKER.REJECT_TOO_MANY_ANNOUNCES && 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. try {
  41. if (CONFIG.TRACKER.PRIVATE === false) return cb()
  42. const videoFileExists = await VideoFileModel.doesInfohashExistCached(infoHash)
  43. if (videoFileExists === true) return cb()
  44. const playlistExists = await VideoStreamingPlaylistModel.doesInfohashExistCached(infoHash)
  45. if (playlistExists === true) return cb()
  46. cb(new Error(`Unknown infoHash ${infoHash} requested by ip ${ip}`))
  47. // Close socket connection and block IP for a few time
  48. if (params.type === 'ws') {
  49. blockedIPs.set(ip, true)
  50. // setTimeout to wait filter response
  51. setTimeout(() => params.socket.close(), 0)
  52. }
  53. } catch (err) {
  54. logger.error('Error in tracker filter.', { err })
  55. return cb(err)
  56. }
  57. }
  58. })
  59. if (CONFIG.TRACKER.ENABLED !== false) {
  60. trackerServer.on('error', function (err) {
  61. logger.error('Error in tracker.', { err })
  62. })
  63. trackerServer.on('warning', function (err) {
  64. const message = err.message || ''
  65. if (CONFIG.LOG.LOG_TRACKER_UNKNOWN_INFOHASH === false && message.includes('Unknown infoHash')) {
  66. return
  67. }
  68. logger.warn('Warning in tracker.', { err })
  69. })
  70. }
  71. const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer)
  72. trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' }))
  73. trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' }))
  74. function createWebsocketTrackerServer (app: express.Application) {
  75. const server = createServer(app)
  76. const wss = new WebSocketServer({ noServer: true })
  77. wss.on('connection', function (ws, req) {
  78. ws['ip'] = proxyAddr(req, CONFIG.TRUST_PROXY)
  79. trackerServer.onWebSocketConnection(ws)
  80. })
  81. server.on('upgrade', (request: express.Request, socket, head) => {
  82. if (request.url === '/tracker/socket') {
  83. const ip = proxyAddr(request, CONFIG.TRUST_PROXY)
  84. if (blockedIPs.has(ip)) {
  85. logger.debug('Blocking IP %s from tracker.', ip)
  86. socket.write('HTTP/1.1 403 Forbidden\r\n\r\n')
  87. socket.destroy()
  88. return
  89. }
  90. return wss.handleUpgrade(request, socket, head, ws => wss.emit('connection', ws, request))
  91. }
  92. // Don't destroy socket, we have Socket.IO too
  93. })
  94. return { server, trackerServer }
  95. }
  96. // ---------------------------------------------------------------------------
  97. export {
  98. trackerRouter,
  99. createWebsocketTrackerServer
  100. }
  101. // ---------------------------------------------------------------------------
  102. function runPeersChecker () {
  103. setInterval(() => {
  104. logger.debug('Checking peers.')
  105. for (const ip of Object.keys(peersIpInfoHash)) {
  106. if (peersIps[ip] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP) {
  107. logger.warn('Peer %s made abnormal requests (%d).', ip, peersIps[ip])
  108. }
  109. }
  110. peersIpInfoHash = {}
  111. peersIps = {}
  112. }, TRACKER_RATE_LIMITS.INTERVAL)
  113. }