From b87a286b83c4b127ba6f3aeec1db17eeca65f6d5 Mon Sep 17 00:00:00 2001 From: Mark de Bruijn Date: Mon, 19 Feb 2024 20:58:08 +0100 Subject: [PATCH 1/6] Add proxying for /audio to localhost:8081 --- index.js | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index c6eae03..3e2d69f 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ const express = require('express'); const session = require('express-session'); const bodyParser = require('body-parser'); const http = require('http'); +const httpProxy = require('http-proxy'); const https = require('https'); const app = express(); const httpServer = http.createServer(app); @@ -18,6 +19,13 @@ const path = require('path'); const net = require('net'); const client = new net.Socket(); +// Create a WebSocket proxy instance +const proxy = httpProxy.createProxyServer({ + target: 'ws://localhost:8081', // WebSocket httpServer's address + ws: true, // Enable WebSocket proxying + changeOrigin: true // Change the origin of the host header to the target URL +}); + // Other files and libraries const crypto = require('crypto'); const fs = require('fs'); @@ -428,14 +436,23 @@ wss.on('connection', (ws, request) => { ws.on('error', console.error); }); - +// Handle upgrade requests to proxy WebSocket connections httpServer.on('upgrade', (request, socket, head) => { - sessionMiddleware(request, {}, () => { - wss.handleUpgrade(request, socket, head, (ws) => { - wss.emit('connection', ws, request); + + logInfo(request.url); + if (request.url === '/text') { + sessionMiddleware(request, {}, () => { + wss.handleUpgrade(request, socket, head, (ws) => { + wss.emit('connection', ws, request); + }); }); - }); -}); + } else if (request.url === '/audio/') { + proxy.ws(request, socket, head); + } else { + socket.destroy(); + } +} +); /* Serving of HTML files */ app.use(express.static(path.join(__dirname, 'web'))); From b6be0a20457bdc7aa4e3cce0de0d691f3a942b96 Mon Sep 17 00:00:00 2001 From: Mark de Bruijn Date: Mon, 19 Feb 2024 21:05:38 +0100 Subject: [PATCH 2/6] Fix: audio path is /audio not /audio/ --- index.js | 2 +- web/js/3las/3las.js | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 3e2d69f..abe3f79 100644 --- a/index.js +++ b/index.js @@ -446,7 +446,7 @@ httpServer.on('upgrade', (request, socket, head) => { wss.emit('connection', ws, request); }); }); - } else if (request.url === '/audio/') { + } else if (request.url === '/audio') { proxy.ws(request, socket, head); } else { socket.destroy(); diff --git a/web/js/3las/3las.js b/web/js/3las/3las.js index 92b79cb..53686a2 100644 --- a/web/js/3las/3las.js +++ b/web/js/3las/3las.js @@ -71,11 +71,11 @@ var _3LAS = /** @class */ (function () { if (this.WakeLock) this.WakeLock.Begin(); try { - if (window.location.protocol === 'https:') { - this.WebSocket = new WebSocketClient(this.Logger, 'wss://' + this.Settings.SocketHost + ':' + window.location.pathname + 'stream' + this.Settings.SocketPath, this.OnSocketError.bind(this), this.OnSocketConnect.bind(this), this.OnSocketDataReady.bind(this), this.OnSocketDisconnect.bind(this)); + if (window.location.protocol === 'https:') { + this.WebSocket = new WebSocketClient(this.Logger, 'wss://' + this.Settings.SocketHost + ':' + location.port.toString() + window.location.pathname + 'audio' , this.OnSocketError.bind(this), this.OnSocketConnect.bind(this), this.OnSocketDataReady.bind(this), this.OnSocketDisconnect.bind(this)); } - else { - this.WebSocket = new WebSocketClient(this.Logger, 'ws://' + this.Settings.SocketHost + ':' + this.Settings.SocketPort.toString() + this.Settings.SocketPath, this.OnSocketError.bind(this), this.OnSocketConnect.bind(this), this.OnSocketDataReady.bind(this), this.OnSocketDisconnect.bind(this)); + else { + this.WebSocket = new WebSocketClient(this.Logger, 'ws://' + this.Settings.SocketHost + ':' + location.port.toString() + window.location.pathname + 'audio' , this.OnSocketError.bind(this), this.OnSocketConnect.bind(this), this.OnSocketDataReady.bind(this), this.OnSocketDisconnect.bind(this)); } this.Logger.Log("Init of WebSocketClient succeeded"); this.Logger.Log("Trying to connect to server."); From fb5d9567b205eea6088300a8f1fd8093b61c2677 Mon Sep 17 00:00:00 2001 From: Mark de Bruijn Date: Mon, 19 Feb 2024 21:06:51 +0100 Subject: [PATCH 3/6] Fix: remove debugging line --- index.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/index.js b/index.js index abe3f79..710b341 100644 --- a/index.js +++ b/index.js @@ -438,8 +438,6 @@ wss.on('connection', (ws, request) => { // Handle upgrade requests to proxy WebSocket connections httpServer.on('upgrade', (request, socket, head) => { - - logInfo(request.url); if (request.url === '/text') { sessionMiddleware(request, {}, () => { wss.handleUpgrade(request, socket, head, (ws) => { From 8d71c1d42d0439db8379a79bbdcaad595b7f7234 Mon Sep 17 00:00:00 2001 From: Mark de Bruijn Date: Mon, 19 Feb 2024 21:10:19 +0100 Subject: [PATCH 4/6] Update package.json --- package-lock.json | 47 +++++++++++++++++++++++++++++++++++++++++++++-- package.json | 1 + 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 33877c2..76f025d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "fm-dx-webserver", - "version": "1.0.6", + "version": "1.0.8", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "fm-dx-webserver", - "version": "1.0.6", + "version": "1.0.8", "license": "ISC", "dependencies": { "@mapbox/node-pre-gyp": "^1.0.11", @@ -16,6 +16,7 @@ "express": "4.18.2", "express-session": "^1.18.0", "http": "^0.0.1-security", + "http-proxy": "^1.18.1", "https": "1.0.0", "koffi": "2.7.2", "net": "1.0.2", @@ -456,6 +457,11 @@ "node": ">= 0.6" } }, + "node_modules/eventemitter3": { + "version": "4.0.7", + "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.7.tgz", + "integrity": "sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==" + }, "node_modules/express": { "version": "4.18.2", "resolved": "https://registry.npmjs.org/express/-/express-4.18.2.tgz", @@ -622,6 +628,25 @@ "node": ">= 0.8" } }, + "node_modules/follow-redirects": { + "version": "1.15.5", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz", + "integrity": "sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, "node_modules/forwarded": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", @@ -813,6 +838,19 @@ "node": ">= 0.8" } }, + "node_modules/http-proxy": { + "version": "1.18.1", + "resolved": "https://registry.npmjs.org/http-proxy/-/http-proxy-1.18.1.tgz", + "integrity": "sha512-7mz/721AbnJwIVbnaSv1Cz3Am0ZLT/UBwkC92VlxhXv/k/BBQfM2fXElQNC27BVGr0uwUpplYPQM9LnaBMR5NQ==", + "dependencies": { + "eventemitter3": "^4.0.0", + "follow-redirects": "^1.0.0", + "requires-port": "^1.0.0" + }, + "engines": { + "node": ">=8.0.0" + } + }, "node_modules/https": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/https/-/https-1.0.0.tgz", @@ -1267,6 +1305,11 @@ "node": ">= 6" } }, + "node_modules/requires-port": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz", + "integrity": "sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ==" + }, "node_modules/rimraf": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz", diff --git a/package.json b/package.json index 7668ff5..01e004f 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "express": "4.18.2", "express-session": "^1.18.0", "http": "^0.0.1-security", + "http-proxy": "^1.18.1", "https": "1.0.0", "koffi": "2.7.2", "net": "1.0.2", From 2bed6c1aabbc7e242c89316017718dc0aa042621 Mon Sep 17 00:00:00 2001 From: Mark de Bruijn Date: Mon, 19 Feb 2024 21:43:05 +0100 Subject: [PATCH 5/6] Fix comment --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 710b341..a72a9ba 100644 --- a/index.js +++ b/index.js @@ -436,7 +436,7 @@ wss.on('connection', (ws, request) => { ws.on('error', console.error); }); -// Handle upgrade requests to proxy WebSocket connections +// Handle upgrade requests to /text and proxy /audio WebSocket connections httpServer.on('upgrade', (request, socket, head) => { if (request.url === '/text') { sessionMiddleware(request, {}, () => { From 52e7f26fd7fe628ce1c5a8dc38b5f803b16e348d Mon Sep 17 00:00:00 2001 From: Mark de Bruijn Date: Mon, 19 Feb 2024 22:07:02 +0100 Subject: [PATCH 6/6] Let the proxy use the configured audio port --- index.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 42efd69..a6a2759 100644 --- a/index.js +++ b/index.js @@ -19,13 +19,6 @@ const path = require('path'); const net = require('net'); const client = new net.Socket(); -// Create a WebSocket proxy instance -const proxy = httpProxy.createProxyServer({ - target: 'ws://localhost:8081', // WebSocket httpServer's address - ws: true, // Enable WebSocket proxying - changeOrigin: true // Change the origin of the host header to the target URL -}); - // Other files and libraries const crypto = require('crypto'); const fs = require('fs'); @@ -36,9 +29,15 @@ const consoleCmd = require('./console'); const audioStream = require('./stream/index.js'); const { parseAudioDevice } = require('./stream/parser.js'); const { configName, serverConfig, configUpdate, configSave } = require('./server_config'); - const { logDebug, logError, logInfo, logWarn } = consoleCmd; +// Create a WebSocket proxy instance +const proxy = httpProxy.createProxyServer({ + target: 'ws://localhost:'+ serverConfig.webserver.audioPort, // WebSocket httpServer's address + ws: true, // Enable WebSocket proxying + changeOrigin: true // Change the origin of the host header to the target URL +}); + let currentUsers = 0; let streamEnabled = false; let incompleteDataBuffer = '';