mirror of
https://github.com/KubaPro010/fm-dx-webserver.git
synced 2026-07-31 01:09:18 +02:00
Compare commits
5
Commits
79819ce26d
...
50156b5b26
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
50156b5b26
|
||
|
|
0efb11a0e5
|
||
|
|
6da7819498
|
||
|
|
a402c45c9c
|
||
|
|
73e23630c7
|
+52
-7
@@ -1,11 +1,14 @@
|
|||||||
const storage = require('./storage');
|
const storage = require('./storage');
|
||||||
const WebSocket = require('ws');
|
const WebSocket = require('ws');
|
||||||
|
const crypto = require('crypto');
|
||||||
const xrd = new WebSocket.Server({ noServer: true });
|
const xrd = new WebSocket.Server({ noServer: true });
|
||||||
|
const { serverConfig } = require('./server_config');
|
||||||
|
|
||||||
let currentUsers = 0;
|
let currentUsers = 0;
|
||||||
|
let clients = []
|
||||||
|
|
||||||
function send_to_xrd(data) {
|
function send_to_xrd(data) {
|
||||||
xrd.clients.forEach((client) => {
|
clients.forEach((client) => {
|
||||||
if (client.readyState === WebSocket.OPEN) client.send(data);
|
if (client.readyState === WebSocket.OPEN) client.send(data);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -14,11 +17,44 @@ function send_xrd_online(fmusers) {
|
|||||||
send_to_xrd(`o${currentUsers},${fmusers}\n`);
|
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 { initialData } = require('./datahandler');
|
||||||
const { isAdminAuthenticated } = request.session || {};
|
|
||||||
if(!isAdminAuthenticated) {
|
const salt = randomString(16);
|
||||||
ws.close(1008, "No admin");
|
ws.send(`${salt}\n`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await xrd_auth(ws, salt);
|
||||||
|
} catch (err) {
|
||||||
|
ws.send("a0\n");
|
||||||
|
ws.close(1008, err.message);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,9 +67,18 @@ xrd.on('connection', (ws, request) => {
|
|||||||
ws.send(`F${initialData.bw}\n`);
|
ws.send(`F${initialData.bw}\n`);
|
||||||
ws.send(`W${initialData.bw}\n`);
|
ws.send(`W${initialData.bw}\n`);
|
||||||
ws.send(`OK\n`);
|
ws.send(`OK\n`);
|
||||||
|
clients.concat(ws);
|
||||||
|
|
||||||
ws.on('message', (message) => storage.ctl_output.write(message));
|
ws.on('message', (message) => {
|
||||||
ws.on('close', (code, reason) => {
|
const data = message.toString();
|
||||||
|
|
||||||
|
if (!storage.ctl_output.write(data)) {
|
||||||
|
ws.pause();
|
||||||
|
storage.ctl_output.once('drain', () => ws.resume());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.on('close', () => {
|
||||||
currentUsers--;
|
currentUsers--;
|
||||||
send_xrd_online(initialData.users);
|
send_xrd_online(initialData.users);
|
||||||
});
|
});
|
||||||
|
|||||||
+3
-6
@@ -93,12 +93,9 @@ var _3LAS = /** @class */ (function () {
|
|||||||
if (this.WakeLock)
|
if (this.WakeLock)
|
||||||
this.WakeLock.Begin();
|
this.WakeLock.Begin();
|
||||||
try {
|
try {
|
||||||
if (window.location.protocol === 'https:') {
|
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||||
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));
|
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));
|
||||||
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));
|
|
||||||
}
|
|
||||||
this.Logger.Log("Init of WebSocketClient succeeded");
|
this.Logger.Log("Init of WebSocketClient succeeded");
|
||||||
this.Logger.Log("Trying to connect to server.");
|
this.Logger.Log("Trying to connect to server.");
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -134,7 +134,7 @@ function OnPlayButtonClick(_ev) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function updateVolume() {
|
function updateVolume() {
|
||||||
if(firefox) {
|
if(!firefox) {
|
||||||
const newVolume = $(this).val();
|
const newVolume = $(this).val();
|
||||||
if (Stream) Stream.setVolume(newVolume);
|
if (Stream) Stream.setVolume(newVolume);
|
||||||
else console.warn("Stream is not initialized.");
|
else console.warn("Stream is not initialized.");
|
||||||
|
|||||||
Reference in New Issue
Block a user