diff --git a/index.js b/index.js index 659856e..a6a2759 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); @@ -28,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 = ''; @@ -433,7 +440,7 @@ wss.on('connection', (ws, request) => { ws.on('error', console.error); }); - +// Handle upgrade requests to /text and proxy /audio WebSocket connections httpServer.on('upgrade', (request, socket, head) => { if (request.url === '/text') { sessionMiddleware(request, {}, () => { @@ -441,10 +448,13 @@ httpServer.on('upgrade', (request, socket, head) => { 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'))); 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", 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.");