]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Fix socket.io websocket connection
authorChocobozzz <me@florianbigard.com>
Tue, 8 Jan 2019 14:51:52 +0000 (15:51 +0100)
committerChocobozzz <chocobozzz@cpy.re>
Wed, 9 Jan 2019 10:15:15 +0000 (11:15 +0100)
server/controllers/tracker.ts
server/tests/api/activitypub/refresher.ts
server/tests/api/server/email.ts
server/tests/api/users/index.ts
server/tests/api/users/user-notifications.ts
server/tests/api/users/users-verification.ts
shared/utils/miscs/email.ts

index 53f1653b5bcd5fea221917feb2221ea4fa82bd74..1deb8c40292f6ae997eb77004b858c1165481e56 100644 (file)
@@ -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
 }
 
index 332ea7ed14d891ec27a356d7207f9fee3d1578ab..62ad8a0b566e97013c4a999174979dddaa2b11a5 100644 (file)
@@ -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)
 
index b8d29ef81dc5c4b9fd526c8db7039f8af0c5f3bc..f96c57b6636721360a23e2d1145d7fe2602c413b 100644 (file)
@@ -251,6 +251,7 @@ describe('Test emails', function () {
   })
 
   after(async function () {
+    MockSmtpServer.Instance.kill()
     killallServers([ server ])
   })
 })
index 63e6e827ab5510c84d644af038ec00daaa1bc7ca..52ba6984eb7cdd96dfaa5adfaec066af9e785351 100644 (file)
@@ -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'
index ad68d8e69af142d12e300e4d76bd29cc1eba257a..5260d64cc163e5a057fca5b036a0e462a1d5e9c9 100644 (file)
@@ -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)
   })
 })
index afc8a00598a035b2f74d8d7d41c4c262d5f1b6ad..babeda2b892327a66f251d24ed5d990d60f8ae80 100644 (file)
@@ -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
index 108f7d3d93b8bb1c2f2f91e0bd33456d5e19504a..6fac7621f2bb3204c48b22d7c16127e6ffaad8a3 100644 (file)
@@ -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())
   }