mirror of
https://github.com/KubaPro010/fm-dx-webserver.git
synced 2026-07-29 16:29:19 +02:00
move some stuff around, change some things slightly
This commit is contained in:
+1
-1
@@ -7,7 +7,7 @@ const LOG_FILE = process.argv.includes('--config') && process.argv[process.argv.
|
||||
? `serverlog_${process.argv[process.argv.indexOf('--config') + 1]}.txt`
|
||||
: 'serverlog.txt';
|
||||
const ANSI_ESCAPE_CODE_PATTERN = /\x1b\[[0-9;]*m/g;
|
||||
const MAX_LOG_LINES = 5000;
|
||||
const MAX_LOG_LINES = 3500;
|
||||
const FLUSH_INTERVAL = 60000;
|
||||
const logs = [];
|
||||
const maxConsoleLogLines = 250;
|
||||
|
||||
@@ -56,7 +56,6 @@ const filterMappings = {
|
||||
'G00': { eq: 0, ims: 0 }
|
||||
};
|
||||
|
||||
|
||||
var legacyRdsPiBuffer = null;
|
||||
var lastUpdateTime = Date.now();
|
||||
const initialData = { ...dataToSend };
|
||||
|
||||
+1
-1
@@ -466,7 +466,7 @@ router.get('/tunnelservers', async (req, res) => {
|
||||
{ value: "eu", host: "eu.fmtuner.org", label: "Europe" },
|
||||
{ value: "us", host: "us.fmtuner.org", label: "Americas" },
|
||||
{ value: "sg", host: "sg.fmtuner.org", label: "Asia & Oceania" },
|
||||
{ value: "pldx", host: "pldx.fmtuner.org", label: "Poland (k201)" },
|
||||
{ value: "pldx", host: "pldx.duckdns.org", label: "Poland (k201)" },
|
||||
];
|
||||
|
||||
const results = await Promise.all(
|
||||
|
||||
+1
-2
@@ -23,8 +23,7 @@ function send(request) {
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success && data.token) {
|
||||
if (!serverConfig.identification.token)
|
||||
{
|
||||
if (!serverConfig.identification.token) {
|
||||
logInfo("Registered to FM-DX Server Map successfully.");
|
||||
serverConfig.identification.token = data.token;
|
||||
configSave();
|
||||
|
||||
+1
-12
@@ -292,17 +292,6 @@ function kickClient(ipAddress) {
|
||||
} else consoleCmd.logInfo(`Kicking client ${ipAddress} failed. No suitable client found.`);
|
||||
}
|
||||
|
||||
function checkIPv6Support(callback) {
|
||||
const server = net.createServer();
|
||||
|
||||
server.listen(0, '::1', () => {
|
||||
server.close(() => callback(true));
|
||||
}).on('error', (error) => {
|
||||
if (error.code === 'EADDRNOTAVAIL') callback(false);
|
||||
else callback(false);
|
||||
});
|
||||
}
|
||||
|
||||
function checkLatency(host, port = 80, timeout = 2000) {
|
||||
return new Promise(resolve => {
|
||||
const start = Date.now();
|
||||
@@ -459,7 +448,7 @@ function getIpAddress(request) {
|
||||
module.exports = {
|
||||
authenticateWithXdrd, parseMarkdown, handleConnect,
|
||||
removeMarkdown, formatUptime, resolveDataBuffer,
|
||||
kickClient, checkIPv6Support, checkLatency,
|
||||
kickClient, checkLatency,
|
||||
antispamProtection, escapeHtml, findServerFiles,
|
||||
startPluginsWithDelay, getIpAddress
|
||||
}
|
||||
+36
-40
@@ -10,7 +10,7 @@ const WebSocket = require('ws');
|
||||
const path = require('path');
|
||||
const net = require('net');
|
||||
const { SerialPort } = require('serialport');
|
||||
const tunnel_connect = require('./tunnel');
|
||||
const tunnel = require('./tunnel');
|
||||
const { createChatServer } = require('./chat');
|
||||
const figlet = require('figlet');
|
||||
|
||||
@@ -71,9 +71,9 @@ app.use(sessionMiddleware);
|
||||
app.use(bodyParser.json());
|
||||
createChatServer(storage);
|
||||
|
||||
tunnel.download();
|
||||
connectToXdrd();
|
||||
connectToSerial();
|
||||
tunnel_connect();
|
||||
|
||||
// Serialport retry code when port is open but communication is lost (additional code in datahandler.js)
|
||||
dataHandler.state.isSerialportRetrying = false;
|
||||
@@ -285,7 +285,7 @@ app.use('/', endpoints);
|
||||
const tunerLockTracker = new WeakMap();
|
||||
const ipConnectionCounts = new Map(); // Per-IP limit variables
|
||||
const ipLogTimestamps = new Map();
|
||||
const MAX_CONNECTIONS_PER_IP = 5;
|
||||
const MAX_CONNECTIONS_PER_IP = 4;
|
||||
const IP_LOG_INTERVAL_MS = 60000;
|
||||
// Remove old per-IP limit addresses
|
||||
setInterval(() => {
|
||||
@@ -502,20 +502,23 @@ pluginsWss.on('connection', (ws, request) => {
|
||||
// Anti-spam
|
||||
helpers.antispamProtection(message, clientIp, ws, userCommands, lastWarn, userCommandHistory, '10', 'data_plugins');
|
||||
|
||||
let messageData;
|
||||
|
||||
try { // JS Requires the try statement to have braces, unlike the if statement. This extends the huge list of proofs that this is a fucking toy language
|
||||
messageData = JSON.parse(message); // Attempt to parse the JSON
|
||||
} catch (error) {
|
||||
// console.error("Failed to parse message:", error); // Log the error
|
||||
return; // Exit if parsing fails
|
||||
}
|
||||
|
||||
const modifiedMessage = JSON.stringify(messageData);
|
||||
try {
|
||||
let messageData = JSON.parse(message); // Attempt to parse the JSON
|
||||
|
||||
if (messageData.type === "GPS" && messageData.value) {
|
||||
const gpsData = messageData.value;
|
||||
const { status, time, lat, lon, alt, mode } = gpsData;
|
||||
|
||||
if (status === "active") {
|
||||
Latitude = parseFloat(lat);
|
||||
Longitude = parseFloat(lon);
|
||||
}
|
||||
}
|
||||
} catch (error) {}
|
||||
|
||||
// Broadcast the message to all other clients
|
||||
pluginsWss.clients.forEach(client => {
|
||||
if (client.readyState === WebSocket.OPEN) client.send(modifiedMessage); // Send the message to all clients
|
||||
if (client.readyState === WebSocket.OPEN) client.send(message); // Send the message to all clients
|
||||
});
|
||||
});
|
||||
|
||||
@@ -524,7 +527,6 @@ pluginsWss.on('connection', (ws, request) => {
|
||||
});
|
||||
});
|
||||
|
||||
// Websocket register for /text, /audio and /chat paths
|
||||
httpServer.on('upgrade', (request, socket, head) => {
|
||||
const clientIp = helpers.getIpAddress(request);
|
||||
if (serverConfig.webserver.banlist?.includes(clientIp)) {
|
||||
@@ -543,30 +545,24 @@ httpServer.on('upgrade', (request, socket, head) => {
|
||||
});
|
||||
|
||||
app.use(express.static(path.join(__dirname, '../web'))); // Serve the entire web folder to the user
|
||||
fmdxList.update();
|
||||
|
||||
helpers.checkIPv6Support((isIPv6Supported) => {
|
||||
const ipv4Address = serverConfig.webserver.webserverIp === '0.0.0.0' ? 'localhost' : serverConfig.webserver.webserverIp;
|
||||
const ipv6Address = '::'; // This will bind to all available IPv6 interfaces
|
||||
const port = serverConfig.webserver.webserverPort;
|
||||
|
||||
const logServerStart = (address, isIPv6) => {
|
||||
const formattedAddress = isIPv6 ? `[${address}]` : address;
|
||||
logInfo(`Web server has started on address \x1b[34mhttp://${formattedAddress}:${port}\x1b[0m.`);
|
||||
};
|
||||
|
||||
const startServer = (address, isIPv6) => {
|
||||
httpServer.listen(port, address, () => {
|
||||
if (!isIPv6 && !configExists()) logInfo(`Open your browser and proceed to \x1b[34mhttp://${address}:${port}\x1b[0m to continue with setup.`);
|
||||
else logServerStart(address, isIPv6);
|
||||
});
|
||||
};
|
||||
|
||||
if (isIPv6Supported) {
|
||||
startServer(ipv4Address, false); // Start on IPv4
|
||||
startServer(ipv6Address, true); // Start on IPv6
|
||||
} else startServer(ipv4Address, false); // Start only on IPv4
|
||||
});
|
||||
|
||||
pluginsApi.registerServerContext({ wss, pluginsWss, httpServer, serverConfig });
|
||||
|
||||
const ipv4Address = serverConfig.webserver.webserverIp === '0.0.0.0' ? 'localhost' : serverConfig.webserver.webserverIp;
|
||||
const port = serverConfig.webserver.webserverPort;
|
||||
|
||||
const logServerStart = (address) => {
|
||||
logInfo(`Web server has started on address \x1b[34mhttp://${address}:${port}\x1b[0m.`);
|
||||
};
|
||||
|
||||
const startServer = (address) => {
|
||||
httpServer.listen(port, address, () => {
|
||||
if (!configExists()) logInfo(`Open your browser and proceed to \x1b[34mhttp://${address}:${port}\x1b[0m to continue with setup.`);
|
||||
else logServerStart(address);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
startServer(ipv4Address);
|
||||
tunnel.connect();
|
||||
fmdxList.update();
|
||||
module.exports = { wss, pluginsWss, httpServer, serverConfig };
|
||||
@@ -20,7 +20,7 @@ let serverConfig = {
|
||||
banlist: [],
|
||||
chatEnabled: true,
|
||||
tuningLimit: false,
|
||||
tuningLowerLimit: "100",
|
||||
tuningLowerLimit: "65",
|
||||
tuningUpperLimit: "108",
|
||||
presets: [
|
||||
"87.5",
|
||||
|
||||
+10
-2
@@ -12,7 +12,7 @@ const readline = require('readline');
|
||||
|
||||
const fileExists = path => new Promise(resolve => fs.access(path, fs.constants.F_OK).then(() => resolve(true)).catch(() => resolve(false)));
|
||||
|
||||
async function connect() {
|
||||
async function download() {
|
||||
if (serverConfig.tunnel?.enabled === true) {
|
||||
const librariesDir = path.resolve(__dirname, '../libraries');
|
||||
if (!await fileExists(librariesDir)) await fs.mkdir(librariesDir);
|
||||
@@ -33,6 +33,14 @@ async function connect() {
|
||||
logInfo('Downloading of frpc is completed.')
|
||||
if (os.platform() === 'linux' || os.platform() === 'darwin') await fs.chmod(frpcPath, 0o770);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function connect() {
|
||||
if (serverConfig.tunnel?.enabled === true) {
|
||||
const librariesDir = path.resolve(__dirname, '../libraries');
|
||||
const frpcPath = path.resolve(librariesDir, 'frpc' + (os.platform() === 'win32' ? '.exe' : ''));
|
||||
|
||||
const cfg = ejs.render(frpcConfigTemplate, {
|
||||
cfg: serverConfig.tunnel,
|
||||
host: serverConfig.tunnel.community.enabled ? serverConfig.tunnel.community.host : ((serverConfig.tunnel.region == "pldx") ? "pldx.duckdns.org" : (serverConfig.tunnel.region + ".fmtuner.org")),
|
||||
@@ -92,4 +100,4 @@ httpPassword = "<%= cfg.httpPassword %>"
|
||||
<% } %>
|
||||
`;
|
||||
|
||||
module.exports = connect;
|
||||
module.exports = { connect, download};
|
||||
+1
-31
@@ -42,35 +42,6 @@ if (typeof algorithms[algoSetting] !== 'undefined') {
|
||||
// Build the TX database.
|
||||
setTimeout(buildTxDatabase, 3000);
|
||||
|
||||
if (serverConfig.identification.gpsMode) {
|
||||
// 5-second delay before activation of GPS lat/lon websocket
|
||||
setTimeout(() => {
|
||||
const websocket = new WebSocket(externalWsUrl);
|
||||
consoleCmd.logInfo('Set up GPS websocket for lat/lon');
|
||||
// Event listener to receive data
|
||||
websocket.on('message', (data) => {
|
||||
try {
|
||||
// Parse the received data
|
||||
const parsedData = JSON.parse(data);
|
||||
|
||||
// Check if the dataset is of type GPS
|
||||
if (parsedData.type === "GPS" && parsedData.value) {
|
||||
const gpsData = parsedData.value;
|
||||
const { status, time, lat, lon, alt, mode } = gpsData;
|
||||
|
||||
if (status === "active") {
|
||||
Latitude = parseFloat(lat);
|
||||
Longitude = parseFloat(lon);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
consoleCmd.logError("Error processing WebSocket data:", error);
|
||||
}
|
||||
});
|
||||
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
// Function to build local TX database from FMDX Maps endpoint.
|
||||
async function buildTxDatabase() {
|
||||
if (Latitude.length > 0 && Longitude.length > 0) {
|
||||
@@ -362,12 +333,11 @@ function checkEs() {
|
||||
}
|
||||
|
||||
function haversine(lat1, lon1, lat2, lon2) {
|
||||
const R = 6371;
|
||||
const dLat = deg2rad(lat2 - lat1);
|
||||
const dLon = deg2rad(lon2 - lon1);
|
||||
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
|
||||
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||
const distance = R * c;
|
||||
const distance = 6371 * c;
|
||||
|
||||
const y = Math.sin(dLon) * Math.cos(deg2rad(lat2));
|
||||
const x = Math.cos(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) - Math.sin(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(dLon);
|
||||
|
||||
Reference in New Issue
Block a user