1
0
mirror of https://github.com/KubaPro010/fm-dx-webserver.git synced 2026-02-26 22:13:53 +01:00
Files
fm-dx-webserver/index.js
NoobishSVK 5e3a0a466c refactor
2024-01-22 20:33:45 +01:00

109 lines
3.3 KiB
JavaScript

/* Libraries / Imports */
const express = require('express');
const app = express();
const http = require('http');
const httpServer = http.createServer(app);
const WebSocket = require('ws');
const wss = new WebSocket.Server({ noServer: true });
const path = require('path');
const net = require('net');
const client = new net.Socket();
const crypto = require('crypto');
const dataHandler = require('./datahandler');
const consoleCmd = require('./console');
const config = require('./userconfig');
const { webServerHost, webServerPort, webServerName, xdrdServerHost, xdrdServerPort, xdrdPassword, qthLatitude, qthLongitude } = config;
const { logInfo, logDebug } = consoleCmd;
let receivedSalt = '';
let receivedPassword = false;
let currentUsers = 0;
/* webSocket handlers */
wss.on('connection', (ws, request) => {
const clientIp = request.connection.remoteAddress;
currentUsers++;
dataHandler.showOnlineUsers(currentUsers);
consoleCmd.logInfo(`Web client \x1b[32mconnected\x1b[0m (${clientIp}) \x1b[90m[${currentUsers}]`);
ws.on('message', (message) => {
consoleCmd.logDebug('Received message from client:', message.toString());
newFreq = message.toString() * 1000;
client.write("T" + newFreq + '\n');
});
ws.on('close', (code, reason) => {
currentUsers--;
dataHandler.showOnlineUsers(currentUsers);
consoleCmd.logInfo(`Web client \x1b[31mdisconnected\x1b[0m (${clientIp}) \x1b[90m[${currentUsers}]`);
});
ws.on('error', console.error);
});
/* Serving of HTML files */
app.use(express.static(path.join(__dirname, 'web')));
// Function to authenticate with the xdrd server
function authenticateWithXdrd(client, salt, password) {
const sha1 = crypto.createHash('sha1');
const saltBuffer = Buffer.from(salt, 'utf-8');
const passwordBuffer = Buffer.from(password, 'utf-8');
sha1.update(saltBuffer);
sha1.update(passwordBuffer);
const hashedPassword = sha1.digest('hex');
client.write(hashedPassword + '\n');
client.write('x\n');
}
// WebSocket client connection
client.connect(xdrdServerPort, xdrdServerHost, () => {
consoleCmd.logInfo('Connected to xdrd successfully.');
client.once('data', (data) => {
const receivedData = data.toString();
const lines = receivedData.split('\n');
// Salt reading, so we can authenticate
if (lines.length > 0 && !receivedPassword) {
receivedSalt = lines[0].trim();
authenticateWithXdrd(client, receivedSalt, xdrdPassword);
receivedPassword = true;
}
});
});
client.on('data', (data) => {
const receivedData = data.toString();
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
dataHandler.handleData(client, receivedData);
}
});
});
client.on('close', () => {
console.log('Disconnected from xdrd');
});
/* HTTP Server */
httpServer.on('upgrade', (request, socket, head) => {
wss.handleUpgrade(request, socket, head, (ws) => {
wss.emit('connection', ws, request);
});
});
httpServer.listen(webServerPort, webServerHost, () => {
consoleCmd.logInfo(`Web server is running at \x1b[34mhttp://${webServerHost}:${webServerPort}\x1b[0m.`);
});
/* Static data are being sent through here on connection - these don't change when the server is running */
app.get('/static_data', (req, res) => {
res.json({ qthLatitude, qthLongitude, webServerName });
});