X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Ftracker.ts;h=1deb8c40292f6ae997eb77004b858c1165481e56;hb=73471b1a52f242e86364ffb077ea6cadb3b07ae2;hp=42f5aea8101b8de40229a8b6acd2ea7c679771de;hpb=9b67da3d9bc951c624f17dce7821036f8518d893;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/tracker.ts b/server/controllers/tracker.ts index 42f5aea81..1deb8c402 100644 --- a/server/controllers/tracker.ts +++ b/server/controllers/tracker.ts @@ -5,6 +5,8 @@ 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' const TrackerServer = bitTorrentTracker.Server @@ -37,7 +39,12 @@ const trackerServer = new TrackerServer({ return cb(new Error(`Too many requests (${peersIpInfoHash[ key ]} of ip ${ip} for torrent ${infoHash}`)) } - return cb() + VideoFileModel.isInfohashExists(infoHash) + .then(exists => { + if (exists === false) return cb(new Error(`Unknown infoHash ${infoHash}`)) + + return cb() + }) } }) @@ -53,16 +60,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 +87,7 @@ function createWebsocketServer (app: express.Application) { export { trackerRouter, - createWebsocketServer + createWebsocketTrackerServer } // ---------------------------------------------------------------------------