1
0
mirror of https://github.com/KubaPro010/fm-dx-webserver.git synced 2026-02-26 22:13:53 +01:00

offline optimization, new signal chart, ui optimization

This commit is contained in:
Marek Farkaš
2025-02-23 15:27:46 +01:00
parent fcfc0691fa
commit 3c91c8d06c
24 changed files with 373 additions and 287 deletions

View File

@@ -84,16 +84,20 @@ function handleConnect(clientIp, currentUsers, ws, callback) {
}
});
}).on("error", (err) => {
console.error("Error fetching location data:", err);
consoleCmd.logError("Error fetching location data:", err.code);
callback("User allowed");
});
}
function processConnection(clientIp, locationInfo, currentUsers, ws, callback) {
const options = { year: "numeric", month: "numeric", day: "numeric", hour: "2-digit", minute: "2-digit" };
const connectionTime = new Date().toLocaleString([], options);
let bannedASCache = { data: null, timestamp: 0 };
https.get("https://fmdx.org/banned_as.json", (banResponse) => {
function fetchBannedAS(callback) {
const now = Date.now();
if (bannedASCache.data && now - bannedASCache.timestamp < 10 * 60 * 1000) {
return callback(null, bannedASCache.data);
}
const req = https.get("https://fmdx.org/banned_as.json", { family: 4 }, (banResponse) => {
let banData = "";
banResponse.on("data", (chunk) => {
@@ -103,39 +107,60 @@ function processConnection(clientIp, locationInfo, currentUsers, ws, callback) {
banResponse.on("end", () => {
try {
const bannedAS = JSON.parse(banData).banned_as || [];
if (bannedAS.some((as) => locationInfo.as?.includes(as))) {
return callback("User banned");
}
const userLocation =
locationInfo.country === undefined
? "Unknown"
: `${locationInfo.city}, ${locationInfo.regionName}, ${locationInfo.countryCode}`;
storage.connectedUsers.push({
ip: clientIp,
location: userLocation,
time: connectionTime,
instance: ws,
});
consoleCmd.logInfo(
`Web client \x1b[32mconnected\x1b[0m (${clientIp}) \x1b[90m[${currentUsers}]\x1b[0m Location: ${userLocation}`
);
callback("User allowed");
bannedASCache = { data: bannedAS, timestamp: now };
callback(null, bannedAS);
} catch (error) {
console.error("Error parsing banned AS list:", error);
callback("User allowed");
callback(null, []); // Default to allowing user
}
});
}).on("error", (err) => {
});
// Set timeout for the request (5 seconds)
req.setTimeout(5000, () => {
console.error("Error: Request timed out while fetching banned AS list.");
req.abort();
callback(null, []); // Default to allowing user
});
req.on("error", (err) => {
console.error("Error fetching banned AS list:", err);
callback("User allowed");
callback(null, []); // Default to allowing user
});
}
function processConnection(clientIp, locationInfo, currentUsers, ws, callback) {
const options = { year: "numeric", month: "numeric", day: "numeric", hour: "2-digit", minute: "2-digit" };
const connectionTime = new Date().toLocaleString([], options);
fetchBannedAS((error, bannedAS) => {
if (error) {
console.error("Error fetching banned AS list:", error);
}
if (bannedAS.some((as) => locationInfo.as?.includes(as))) {
return callback("User banned");
}
const userLocation =
locationInfo.country === undefined
? "Unknown"
: `${locationInfo.city}, ${locationInfo.regionName}, ${locationInfo.countryCode}`;
storage.connectedUsers.push({
ip: clientIp,
location: userLocation,
time: connectionTime,
instance: ws,
});
consoleCmd.logInfo(
`Web client \x1b[32mconnected\x1b[0m (${clientIp}) \x1b[90m[${currentUsers}]\x1b[0m Location: ${userLocation}`
);
callback("User allowed");
});
}
function formatUptime(uptimeInSeconds) {
const secondsInMinute = 60;

View File

@@ -469,11 +469,9 @@ wss.on('connection', (ws, request) => {
});
ws.on('close', (code, reason) => {
if (clientIp !== '::ffff:127.0.0.1' ||
(request.connection && request.connection.remoteAddress && request.connection.remoteAddress !== '::ffff:127.0.1') ||
(request.headers && request.headers['origin'] && request.headers['origin'].trim() !== '')) {
currentUsers--;
}
if (clientIp !== '::ffff:127.0.0.1' || (request.connection && request.connection.remoteAddress && request.connection.remoteAddress !== '::ffff:127.0.0.1') || (request.headers && request.headers['origin'] && request.headers['origin'].trim() !== '')) {
currentUsers--;
}
dataHandler.showOnlineUsers(currentUsers);
const index = storage.connectedUsers.findIndex(user => user.ip === clientIp);

View File

@@ -13,8 +13,14 @@ let usStatesGeoJson = null; // To cache the GeoJSON data for US states
// Load the US states GeoJSON data
async function loadUsStatesGeoJson() {
if (!usStatesGeoJson) {
const response = await fetch(usStatesGeoJsonUrl);
usStatesGeoJson = await response.json();
try {
const response = await fetch(usStatesGeoJsonUrl);
if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`);
usStatesGeoJson = await response.json();
} catch (error) {
console.error("Failed to load US States GeoJSON:", error);
usStatesGeoJson = null; // Ensure it's null so it can retry later
}
}
}
@@ -79,18 +85,17 @@ async function fetchTx(freq, piCode, rdsPs) {
const url = "https://maps.fmdx.org/api/?freq=" + freq;
return fetch(url, {
redirect: 'manual'
})
.then(response => response.json())
.then(async (data) => {
cachedData[freq] = data;
await loadUsStatesGeoJson();
return processData(data, piCode, rdsPs);
})
.catch(error => {
console.error("Error fetching data:", error);
});
try {
const response = await fetch(url, { redirect: 'manual' });
if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`);
const data = await response.json();
cachedData[freq] = data;
await loadUsStatesGeoJson();
return processData(data, piCode, rdsPs);
} catch (error) {
console.error("Error fetching data:", error);
return null; // Return null to indicate failure
}
}
async function processData(data, piCode, rdsPs) {
@@ -182,29 +187,28 @@ function checkEs() {
let esSwitch = false;
if (now - esSwitchCache.lastCheck < esFetchInterval) {
esSwitch = esSwitchCache.esSwitch;
} else if (serverConfig.identification.lat > 20) {
esSwitchCache.lastCheck = now;
fetch(url)
.then(response => response.json())
.then(data => {
if (serverConfig.identification.lon < -32) {
if (data.north_america.max_frequency != "No data") {
esSwitch = true;
}
} else {
if (data.europe.max_frequency != "No data") {
esSwitch = true;
}
}
esSwitchCache.esSwitch = esSwitch;
})
.catch(error => {
console.error("Error fetching data:", error);
});
return esSwitchCache.esSwitch;
}
return esSwitch;
if (serverConfig.identification.lat > 20) {
esSwitchCache.lastCheck = now;
fetch(url)
.then(response => {
if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`);
return response.json();
})
.then(data => {
if ((serverConfig.identification.lon < -32 && data.north_america.max_frequency !== "No data") ||
(serverConfig.identification.lon >= -32 && data.europe.max_frequency !== "No data")) {
esSwitchCache.esSwitch = true;
}
})
.catch(error => {
console.error("Error fetching Es data:", error);
});
}
return esSwitchCache.esSwitch;
}
function haversine(lat1, lon1, lat2, lon2) {