mirror of
https://github.com/KubaPro010/fm-dx-webserver.git
synced 2026-07-31 09:19:16 +02:00
Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
50156b5b26
|
||
|
|
0efb11a0e5
|
||
|
|
6da7819498
|
||
|
|
a402c45c9c
|
||
|
|
73e23630c7
|
+52
-7
@@ -1,11 +1,14 @@
|
||||
const storage = require('./storage');
|
||||
const WebSocket = require('ws');
|
||||
const crypto = require('crypto');
|
||||
const xrd = new WebSocket.Server({ noServer: true });
|
||||
const { serverConfig } = require('./server_config');
|
||||
|
||||
let currentUsers = 0;
|
||||
let clients = []
|
||||
|
||||
function send_to_xrd(data) {
|
||||
xrd.clients.forEach((client) => {
|
||||
clients.forEach((client) => {
|
||||
if (client.readyState === WebSocket.OPEN) client.send(data);
|
||||
});
|
||||
}
|
||||
@@ -14,11 +17,44 @@ function send_xrd_online(fmusers) {
|
||||
send_to_xrd(`o${currentUsers},${fmusers}\n`);
|
||||
}
|
||||
|
||||
xrd.on('connection', (ws, request) => {
|
||||
function randomString(length) {
|
||||
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
let result = '';
|
||||
for (let i = 0; i < length; i++) {
|
||||
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function xrd_auth(ws, salt) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const expected = crypto.createHash('sha1')
|
||||
.update(salt + serverConfig.password.adminPass).digest('hex');
|
||||
|
||||
const timeout = setTimeout(() => {
|
||||
reject(new Error('Auth timeout'));
|
||||
}, 10_000);
|
||||
|
||||
ws.once('message', (message) => {
|
||||
clearTimeout(timeout);
|
||||
const received = message.toString().trim();
|
||||
if (received === expected) resolve();
|
||||
else reject(new Error('Invalid credentials'));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
xrd.on('connection', async (ws, request) => {
|
||||
const { initialData } = require('./datahandler');
|
||||
const { isAdminAuthenticated } = request.session || {};
|
||||
if(!isAdminAuthenticated) {
|
||||
ws.close(1008, "No admin");
|
||||
|
||||
const salt = randomString(16);
|
||||
ws.send(`${salt}\n`);
|
||||
|
||||
try {
|
||||
await xrd_auth(ws, salt);
|
||||
} catch (err) {
|
||||
ws.send("a0\n");
|
||||
ws.close(1008, err.message);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -31,9 +67,18 @@ xrd.on('connection', (ws, request) => {
|
||||
ws.send(`F${initialData.bw}\n`);
|
||||
ws.send(`W${initialData.bw}\n`);
|
||||
ws.send(`OK\n`);
|
||||
clients.concat(ws);
|
||||
|
||||
ws.on('message', (message) => storage.ctl_output.write(message));
|
||||
ws.on('close', (code, reason) => {
|
||||
ws.on('message', (message) => {
|
||||
const data = message.toString();
|
||||
|
||||
if (!storage.ctl_output.write(data)) {
|
||||
ws.pause();
|
||||
storage.ctl_output.once('drain', () => ws.resume());
|
||||
}
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
currentUsers--;
|
||||
send_xrd_online(initialData.users);
|
||||
});
|
||||
|
||||
+3
-6
@@ -93,12 +93,9 @@ 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 + ':' + this.Settings.SocketPort.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() + window.location.pathname + 'audio' , this.OnSocketError.bind(this), this.OnSocketConnect.bind(this), this.OnSocketDataReady.bind(this), this.OnSocketDisconnect.bind(this));
|
||||
}
|
||||
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||
const url = `${wsProtocol}//${location.hostname}/audio`;
|
||||
this.WebSocket = new WebSocketClient(this.Logger, url , 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.");
|
||||
}
|
||||
|
||||
+1
-1
@@ -134,7 +134,7 @@ function OnPlayButtonClick(_ev) {
|
||||
}
|
||||
|
||||
function updateVolume() {
|
||||
if(firefox) {
|
||||
if(!firefox) {
|
||||
const newVolume = $(this).val();
|
||||
if (Stream) Stream.setVolume(newVolume);
|
||||
else console.warn("Stream is not initialized.");
|
||||
|
||||
Reference in New Issue
Block a user