overall v1
This commit is contained in:
@@ -1,14 +1,10 @@
|
||||
import {parentPort, workerData} from 'worker_threads';
|
||||
import {JsonManager} from '../helpers/json_manager';
|
||||
import {UdpClient} from '../network/udp/udp_client';
|
||||
import {TcpCommunicator} from "../helpers/tcp_communicator";
|
||||
import {operationCodes} from "../network/operation_codes";
|
||||
import {ParsedMessage} from "../network/message_handler";
|
||||
import { parentPort, workerData } from 'worker_threads';
|
||||
import { NetworkScanner } from '../helpers/network_scanner';
|
||||
|
||||
// Define the structure of workerData
|
||||
interface WorkerData {
|
||||
udpPort: number;
|
||||
tcpPort: number
|
||||
tcpPort: number;
|
||||
okPage: string;
|
||||
errorPage: string;
|
||||
databaseResetPage: string;
|
||||
@@ -17,151 +13,7 @@ interface WorkerData {
|
||||
}
|
||||
|
||||
// Extract the data passed to the worker
|
||||
const {udpPort, tcpPort, okPage, errorPage, databaseResetPage, userConfigPath, applicationInfoPath}: WorkerData = workerData;
|
||||
const { udpPort, tcpPort, okPage, errorPage, databaseResetPage, userConfigPath, applicationInfoPath }: WorkerData = workerData;
|
||||
|
||||
// Create a JsonManager instance for application info
|
||||
const applicationInfo = new JsonManager(applicationInfoPath);
|
||||
const userConfig = new JsonManager(userConfigPath);
|
||||
|
||||
let appStarted = false;
|
||||
let intervalIds: NodeJS.Timeout[] = []; // Store interval IDs for future clearing
|
||||
|
||||
// Flags to prevent overlapping executions
|
||||
let ucCheckBusy = false;
|
||||
let ipLookupBusy = false;
|
||||
let sendLoginBusy = false;
|
||||
|
||||
// Function to schedule the UC check task with dynamic UDP client creation
|
||||
function startUCCheck(udpPort: number, okPage: string, errorPage: string, interval: number = 5000): void {
|
||||
const intervalId = setInterval(async () => {
|
||||
if (ucCheckBusy) return; // If already running, skip this iteration
|
||||
ucCheckBusy = true; // Mark as busy
|
||||
|
||||
try {
|
||||
console.log('UC Check running...');
|
||||
const udpClient = new UdpClient(udpPort); // Create UdpClient with the provided port
|
||||
const aliveClients = await udpClient.getAliveClients();
|
||||
const storedIp = await applicationInfo.readValue('serverIp');
|
||||
const foundClient = aliveClients.length > 0;
|
||||
|
||||
if (foundClient) {
|
||||
const ipAddress = aliveClients[0]; // Just using the first alive client
|
||||
|
||||
if (!storedIp || storedIp !== ipAddress) {
|
||||
await applicationInfo.writeValue('serverIp', ipAddress);
|
||||
if (!appStarted) {
|
||||
parentPort?.postMessage({type: 'changeContent', page: okPage});
|
||||
}
|
||||
appStarted = true;
|
||||
} else if (!appStarted) {
|
||||
parentPort?.postMessage({type: 'changeContent', page: okPage});
|
||||
appStarted = true;
|
||||
}
|
||||
} else {
|
||||
parentPort?.postMessage({type: 'changeContent', page: errorPage});
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error checking UC:', err);
|
||||
parentPort?.postMessage({type: 'changeContent', page: errorPage});
|
||||
} finally {
|
||||
ucCheckBusy = false; // Mark as not busy
|
||||
}
|
||||
}, interval);
|
||||
|
||||
intervalIds.push(intervalId);
|
||||
}
|
||||
|
||||
// Function to schedule the IP lookup task, storing the active addresses in memory
|
||||
function startUserIPLookup(udpPort: number, interval: number = 10000): void {
|
||||
const intervalId = setInterval(async () => {
|
||||
if (ipLookupBusy) return; // If already running, skip this iteration
|
||||
ipLookupBusy = true; // Mark as busy
|
||||
|
||||
try {
|
||||
console.log('IP Lookup running...');
|
||||
const serverIp = await applicationInfo.readValue('serverIp');
|
||||
const udpClient = new UdpClient(udpPort); // Create a UDP client with the provided port
|
||||
const activeIPs = await udpClient.getAliveClients(); // Get the list of active IPs
|
||||
|
||||
// Filter out the serverIp from the list of active clients
|
||||
const filteredIPs = activeIPs.filter(ip => ip !== serverIp);
|
||||
|
||||
// Save the filtered IPs to 'users_ip'
|
||||
await applicationInfo.writeValue('users_ip', filteredIPs);
|
||||
} catch (err) {
|
||||
console.error('Error during user IP lookup:', err);
|
||||
} finally {
|
||||
ipLookupBusy = false; // Mark as not busy
|
||||
}
|
||||
}, interval);
|
||||
|
||||
intervalIds.push(intervalId);
|
||||
}
|
||||
|
||||
function sendLoginRequest(databaseResetPage: string, interval: number = 5000): void {
|
||||
// Read user_info from userConfig for email and password
|
||||
const intervalId = setInterval(async () => {
|
||||
if (sendLoginBusy && !appStarted) return;
|
||||
sendLoginBusy = true;
|
||||
|
||||
const userInfo = await userConfig.readValue('user_info');
|
||||
if (!userInfo || !userInfo.email || !userInfo.password) {
|
||||
console.error("Email or password not found in user config.");
|
||||
return;
|
||||
}
|
||||
|
||||
const app_type = await userConfig.readValue('app_type');
|
||||
|
||||
const email = userInfo.email;
|
||||
const password = userInfo.password;
|
||||
|
||||
// Initialize the TCP communicator with the server IP from applicationInfo
|
||||
const serverIp = await applicationInfo.readValue('serverIp');
|
||||
if (!serverIp) {
|
||||
console.error("Server IP not found in application info.");
|
||||
return;
|
||||
}
|
||||
|
||||
const tcpCommunicator = new TcpCommunicator(serverIp, tcpPort);
|
||||
|
||||
if (!await tcpCommunicator.connect()) {
|
||||
console.error("Failed to connect to the server.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Prepare the login request data
|
||||
const metaInfo = {email, password, app_type};
|
||||
if (!await tcpCommunicator.sendMessage(operationCodes.LOGIN, metaInfo)) {
|
||||
console.error("Failed to send login request.");
|
||||
await tcpCommunicator.disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
// Await and process the response
|
||||
const response = await waitForResponse(tcpCommunicator);
|
||||
if (response?.operationCode !== operationCodes.OK) {
|
||||
await userConfig.resetFile();
|
||||
await userConfig.writeValue('app_type', app_type);
|
||||
parentPort?.postMessage({type: 'changeContent', page: databaseResetPage});
|
||||
return;
|
||||
}
|
||||
}, interval)
|
||||
intervalIds.push(intervalId); // Store the interval ID for later clearing if needed
|
||||
}
|
||||
|
||||
// Helper function to wait for a response
|
||||
async function waitForResponse(tcpCommunicator: TcpCommunicator): Promise<ParsedMessage | null> {
|
||||
return new Promise((resolve) => {
|
||||
const checkInterval = setInterval(() => {
|
||||
if (tcpCommunicator.hasResponseArrived()) {
|
||||
clearInterval(checkInterval);
|
||||
resolve(tcpCommunicator.getLastResult());
|
||||
}
|
||||
}, 100);
|
||||
});
|
||||
}
|
||||
|
||||
// Start the UC Check and User IP Lookup tasks
|
||||
startUCCheck(udpPort, okPage, errorPage);
|
||||
startUserIPLookup(udpPort);
|
||||
sendLoginRequest(databaseResetPage);
|
||||
// Start the NetworkScanner instance
|
||||
const networkScanner = new NetworkScanner(applicationInfoPath, userConfigPath, udpPort, tcpPort, okPage, errorPage, databaseResetPage);
|
||||
|
||||
Reference in New Issue
Block a user