]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/tracker.ts
Limit user tokens cache
[github/Chocobozzz/PeerTube.git] / server / controllers / tracker.ts
index 42f5aea8101b8de40229a8b6acd2ea7c679771de..8b77d9de77945f2f0582e8eabcf468aa5b49cb41 100644 (file)
@@ -5,6 +5,9 @@ import * as bitTorrentTracker from 'bittorrent-tracker'
 import * as proxyAddr from 'proxy-addr'
 import { Server as WebSocketServer } from 'ws'
 import { CONFIG, TRACKER_RATE_LIMITS } from '../initializers/constants'
+import { VideoFileModel } from '../models/video/video-file'
+import { parse } from 'url'
+import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
 
 const TrackerServer = bitTorrentTracker.Server
 
@@ -19,7 +22,7 @@ const trackerServer = new TrackerServer({
   udp: false,
   ws: false,
   dht: false,
-  filter: function (infoHash, params, cb) {
+  filter: async function (infoHash, params, cb) {
     let ip: string
 
     if (params.type === 'ws') {
@@ -30,14 +33,25 @@ const trackerServer = new TrackerServer({
 
     const key = ip + '-' + infoHash
 
-    peersIps[ip] = peersIps[ip] ? peersIps[ip] + 1 : 1
-    peersIpInfoHash[key] = peersIpInfoHash[key] ? peersIpInfoHash[key] + 1 : 1
+    peersIps[ ip ] = peersIps[ ip ] ? peersIps[ ip ] + 1 : 1
+    peersIpInfoHash[ key ] = peersIpInfoHash[ key ] ? peersIpInfoHash[ key ] + 1 : 1
 
-    if (peersIpInfoHash[key] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP_PER_INFOHASH) {
+    if (peersIpInfoHash[ key ] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP_PER_INFOHASH) {
       return cb(new Error(`Too many requests (${peersIpInfoHash[ key ]} of ip ${ip} for torrent ${infoHash}`))
     }
 
-    return cb()
+    try {
+      const videoFileExists = await VideoFileModel.doesInfohashExist(infoHash)
+      if (videoFileExists === true) return cb()
+
+      const playlistExists = await VideoStreamingPlaylistModel.doesInfohashExist(infoHash)
+      if (playlistExists === true) return cb()
+
+      return cb(new Error(`Unknown infoHash ${infoHash}`))
+    } catch (err) {
+      logger.error('Error in tracker filter.', { err })
+      return cb(err)
+    }
   }
 })
 
@@ -53,16 +67,26 @@ const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer)
 trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' }))
 trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' }))
 
-function createWebsocketServer (app: express.Application) {
+function createWebsocketTrackerServer (app: express.Application) {
   const server = http.createServer(app)
-  const wss = new WebSocketServer({ server: server, path: '/tracker/socket' })
+  const wss = new WebSocketServer({ noServer: true })
+
   wss.on('connection', function (ws, req) {
-    const ip = proxyAddr(req, CONFIG.TRUST_PROXY)
-    ws['ip'] = ip
+    ws['ip'] = proxyAddr(req, CONFIG.TRUST_PROXY)
 
     trackerServer.onWebSocketConnection(ws)
   })
 
+  server.on('upgrade', (request, socket, head) => {
+    const pathname = parse(request.url).pathname
+
+    if (pathname === '/tracker/socket') {
+      wss.handleUpgrade(request, socket, head, ws => wss.emit('connection', ws, request))
+    }
+
+    // Don't destroy socket, we have Socket.IO too
+  })
+
   return server
 }
 
@@ -70,7 +94,7 @@ function createWebsocketServer (app: express.Application) {
 
 export {
   trackerRouter,
-  createWebsocketServer
+  createWebsocketTrackerServer
 }
 
 // ---------------------------------------------------------------------------