]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/tracker.ts
add quarantine videos feature (#1637)
[github/Chocobozzz/PeerTube.git] / server / controllers / tracker.ts
index 53f1653b5bcd5fea221917feb2221ea4fa82bd74..8b77d9de77945f2f0582e8eabcf468aa5b49cb41 100644 (file)
@@ -6,6 +6,8 @@ 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
 
@@ -20,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') {
@@ -31,19 +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}`))
     }
 
-    VideoFileModel.isInfohashExists(infoHash)
-      .then(exists => {
-        if (exists === false) return cb(new Error(`Unknown infoHash ${infoHash}`))
+    try {
+      const videoFileExists = await VideoFileModel.doesInfohashExist(infoHash)
+      if (videoFileExists === true) return cb()
 
-        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)
+    }
   }
 })
 
@@ -61,14 +69,24 @@ trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { act
 
 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
 }