From 89ada4e26ca1df8ff0dd02acda1d1661f121a294 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 8 Jan 2019 15:51:52 +0100 Subject: Fix socket.io websocket connection --- server/controllers/tracker.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/tracker.ts b/server/controllers/tracker.ts index 53f1653b5..1deb8c402 100644 --- a/server/controllers/tracker.ts +++ b/server/controllers/tracker.ts @@ -6,6 +6,7 @@ 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 @@ -61,14 +62,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 } -- cgit v1.2.3