From 89ada4e26ca1df8ff0dd02acda1d1661f121a294 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 8 Jan 2019 15:51:52 +0100 Subject: [PATCH] Fix socket.io websocket connection --- server/controllers/tracker.ts | 17 ++++++++++++++--- server/tests/api/activitypub/refresher.ts | 2 +- server/tests/api/server/email.ts | 1 + server/tests/api/users/index.ts | 4 ++-- server/tests/api/users/user-notifications.ts | 4 +++- server/tests/api/users/users-verification.ts | 3 ++- shared/utils/miscs/email.ts | 17 +++++++++++------ 7 files changed, 34 insertions(+), 14 deletions(-) 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 } diff --git a/server/tests/api/activitypub/refresher.ts b/server/tests/api/activitypub/refresher.ts index 332ea7ed1..62ad8a0b5 100644 --- a/server/tests/api/activitypub/refresher.ts +++ b/server/tests/api/activitypub/refresher.ts @@ -22,7 +22,7 @@ describe('Test AP refresher', function () { let videoUUID3: string before(async function () { - this.timeout(30000) + this.timeout(60000) servers = await flushAndRunMultipleServers(2) diff --git a/server/tests/api/server/email.ts b/server/tests/api/server/email.ts index b8d29ef81..f96c57b66 100644 --- a/server/tests/api/server/email.ts +++ b/server/tests/api/server/email.ts @@ -251,6 +251,7 @@ describe('Test emails', function () { }) after(async function () { + MockSmtpServer.Instance.kill() killallServers([ server ]) }) }) diff --git a/server/tests/api/users/index.ts b/server/tests/api/users/index.ts index 63e6e827a..52ba6984e 100644 --- a/server/tests/api/users/index.ts +++ b/server/tests/api/users/index.ts @@ -1,6 +1,6 @@ +import './users-verification' +import './user-notifications' import './blocklist' import './user-subscriptions' -import './user-notifications' import './users' import './users-multiple-servers' -import './users-verification' diff --git a/server/tests/api/users/user-notifications.ts b/server/tests/api/users/user-notifications.ts index ad68d8e69..5260d64cc 100644 --- a/server/tests/api/users/user-notifications.ts +++ b/server/tests/api/users/user-notifications.ts @@ -175,7 +175,7 @@ describe('Test users notifications', function () { }) it('Should send a new video notification if the user follows the local video publisher', async function () { - this.timeout(10000) + this.timeout(15000) await addUserSubscription(servers[0].url, userAccessToken, 'root_channel@localhost:9001') await waitJobs(servers) @@ -1010,6 +1010,8 @@ describe('Test users notifications', function () { }) after(async function () { + MockSmtpServer.Instance.kill() + killallServers(servers) }) }) diff --git a/server/tests/api/users/users-verification.ts b/server/tests/api/users/users-verification.ts index afc8a0059..babeda2b8 100644 --- a/server/tests/api/users/users-verification.ts +++ b/server/tests/api/users/users-verification.ts @@ -4,7 +4,7 @@ import * as chai from 'chai' import 'mocha' import { registerUser, flushTests, getUserInformation, getMyUserInformation, killallServers, - userLogin, login, runServer, ServerInfo, verifyEmail, updateCustomSubConfig + userLogin, login, runServer, ServerInfo, verifyEmail, updateCustomSubConfig, wait } from '../../../../shared/utils' import { setAccessTokensToServers } from '../../../../shared/utils/users/login' import { MockSmtpServer } from '../../../../shared/utils/miscs/email' @@ -123,6 +123,7 @@ describe('Test users account verification', function () { }) after(async function () { + MockSmtpServer.Instance.kill() killallServers([ server ]) // Keep the logs if the test failed diff --git a/shared/utils/miscs/email.ts b/shared/utils/miscs/email.ts index 108f7d3d9..6fac7621f 100644 --- a/shared/utils/miscs/email.ts +++ b/shared/utils/miscs/email.ts @@ -1,22 +1,20 @@ -import * as child from 'child_process' +import { fork, ChildProcess } from 'child_process' class MockSmtpServer { private static instance: MockSmtpServer private started = false - private emailChildProcess: child.ChildProcess + private emailChildProcess: ChildProcess private emails: object[] private constructor () { - this.emailChildProcess = child.fork(`${__dirname}/email-child-process`, [], { silent: true }) + this.emailChildProcess = fork(`${__dirname}/email-child-process`, []) + this.emailChildProcess.on('message', (msg) => { if (msg.email) { return this.emails.push(msg.email) } }) - process.on('exit', () => { - this.emailChildProcess.kill() - }) } collectEmails (emailsCollection: object[]) { @@ -43,6 +41,13 @@ class MockSmtpServer { }) } + kill () { + process.kill(this.emailChildProcess.pid) + + this.emailChildProcess = null + MockSmtpServer.instance = null + } + static get Instance () { return this.instance || (this.instance = new this()) } -- 2.25.1