Cleaned code

This commit is contained in:
2023-06-26 13:58:15 +02:00
parent 1b030935f6
commit 49086c0891

View File

@@ -95,26 +95,10 @@ async function vpnConnect(vpnName) {
await sleep(1000);
}
else {
try {
// const response = await fetch('https://api.ipify.org/?format=json', {
// method: 'GET',
// headers: {
// 'Accept': 'application/json',
// 'Content-Type': 'application/json',
// },
// });
// const res = await response.json();
// const ip = res.ip;
await logManager(`[${host}] VPN connection successfully established to ${vpnName} (IP: ${await publicIpv4()}).`, 'info');
}
catch (err) {
const ip = 'Unknown';
await logManager(`[${host}] VPN connection successfully established to ${vpnName} (IP: ${ip}).`, 'info');
}
await logManager(`[${host}] VPN connection successfully established to ${vpnName} (IP: ${await publicIpv4() || 'Unknown'}).`, 'info');
return 0;
}
}
logManager(`[${host}] Maximum number of connection attempts reached. Failed to connect to VPN: ${vpnName}`, 'error');
await vpnDisconnect();
return 1;
@@ -162,27 +146,19 @@ async function startBot(accountName) {
logManager(`[${host}] Script started for ${accountName}`);
if (isRootUser()) {
console.log('The user is root.');
const containerEnv = fs.readFileSync('/proc/1/environ', 'utf8');
const isLXCContainer = containerEnv.includes('container=lxc') || containerEnv.includes('container=lxc-libvirt');
if (isLXCContainer) {
commandPrefix += ' --virtual-display';
}
if (isLXCContainer) commandPrefix += ' --virtual-display';
}
return new Promise((resolve, reject) => {
// const childProcess = spawn('bash', ['-c', `${commandPrefix} ${commandSuffix}`], { stdio: 'inherit' });
const childProcess = spawn('bash', ['-c', `${commandPrefix} ${commandSuffix}`], {
stdio: ['pipe', 'pipe', 'pipe'],
});
const childProcess = spawn('bash', ['-c', `${commandPrefix} ${commandSuffix}`], { stdio: ['pipe', 'pipe', 'pipe'] });
childProcess.stdout.on('data', (data) => {
const output = data.toString().trim();
logBotConsole(`[${host}][${accountName}] STDOUT: ${output}`);
if (output.includes('Press enter') || output.includes('Press any key')) {
setTimeout(() => {
childProcess.stdin.write('\n');
}, 1000);
setTimeout(() => { childProcess.stdin.write('\n'); }, 1000);
}
});
@@ -190,9 +166,7 @@ async function startBot(accountName) {
const output = data.toString().trim();
logBotConsole(`[${host}][${accountName}] STDERR: ${output}`);
if (output.includes('Press enter') || output.includes('Press any key')) {
setTimeout(() => {
childProcess.stdin.write('\n');
}, 1000);
setTimeout(() => { childProcess.stdin.write('\n'); }, 1000);
}
});
@@ -209,8 +183,7 @@ async function startBot(accountName) {
fs.readFile('batch_logs.json', 'utf8', (err, data) => {
if (err) {
logManager(`Failed to read batch_logs.json: ${err}`, 'error');
resolve();
return;
return resolve();
}
let logEntries = [];
@@ -222,17 +195,11 @@ async function startBot(accountName) {
}
const existingEntryIndex = logEntries.findIndex(entry => entry.accountName === accountName);
if (existingEntryIndex !== -1) {
logEntries[existingEntryIndex] = logEntry;
}
else {
logEntries.push(logEntry);
}
if (existingEntryIndex !== -1) logEntries[existingEntryIndex] = logEntry;
else logEntries.push(logEntry);
fs.writeFile('batch_logs.json', JSON.stringify(logEntries, null, 2), err => {
if (err) {
logManager(`Failed to write to batch_logs.json: ${err}`, 'error');
}
if (err) logManager(`Failed to write to batch_logs.json: ${err}`, 'error');
resolve();
});
});
@@ -240,7 +207,7 @@ async function startBot(accountName) {
});
childProcess.on('close', (code) => {
console.log(`[${accountName}] Child process exited with code ${code}`);
logManager(`[${accountName}] Bot process exited with code ${code}`);
});
childProcess.on('error', (err) => {
@@ -279,11 +246,10 @@ async function runAll() {
const formattedDate = `${currentDate.getDate()}-${currentDate.getMonth() + 1}-${currentDate.getFullYear()}`;
const logEntries = JSON.parse(fs.readFileSync('batch_logs.json', 'utf8'));
const entry = logEntries.find(entries => entries.accountName === vpn);
if (entry && entry.accountName === formattedDate) {
logManager(`[${host}] ${vpn} already runned today!`, 'info');
if (entry && entry.date === formattedDate) {
logManager(`[${host}] ${vpn} already ran today!`, 'info');
continue;
}
console.log(entry && entry.accountName === formattedDate);
logManager(`[${host}] Switching to VPN: [${vpn}]`, 'info');
const con = await vpnConnect(vpn);
if (con) continue;