Switched to ES6, Added public IP module, Code optimization.
This commit is contained in:
@@ -5,7 +5,11 @@
|
||||
"es6": true
|
||||
},
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 2021
|
||||
"ecmaVersion": 2021,
|
||||
"sourceType": "module",
|
||||
"ecmaFeatures": {
|
||||
"jsx": true
|
||||
}
|
||||
},
|
||||
"rules": {
|
||||
"arrow-spacing": ["warn", { "before": true, "after": true }],
|
||||
|
||||
137
index.js
137
index.js
@@ -1,6 +1,7 @@
|
||||
const { execSync, spawn } = require('child_process');
|
||||
const fs = require('node:fs');
|
||||
const os = require('os');
|
||||
import { execSync, spawn } from 'child_process';
|
||||
import { publicIpv4 } from 'public-ip';
|
||||
import fs from 'fs';
|
||||
import os from 'os';
|
||||
|
||||
const host = os.hostname();
|
||||
const args = process.argv.slice(2);
|
||||
@@ -12,26 +13,18 @@ const managerWebhook = process.env.MANAGER_WEBHOOK;
|
||||
const managerWebhookName = process.env.MANAGER_WEBHOOK_NAME || 'Microsoft Rewards Bot Manager';
|
||||
|
||||
async function logManager(message, type) {
|
||||
if (type === 'info') {
|
||||
console.log(`\x1b[34m${message}\x1b[0m`);
|
||||
}
|
||||
else if (type === 'error') {
|
||||
console.log(`\x1b[31m${message}\x1b[0m`);
|
||||
}
|
||||
else {
|
||||
console.log(`\x1b[37m${message}\x1b[0m`);
|
||||
}
|
||||
if (type === 'info') console.log(`\x1b[34m${message}\x1b[0m`);
|
||||
else if (type === 'error') console.log(`\x1b[31m${message}\x1b[0m`);
|
||||
else console.log(`\x1b[37m${message}\x1b[0m`);
|
||||
|
||||
if (managerWebhook) {
|
||||
const webhook = managerWebhook;
|
||||
const botUsername = `${managerWebhookName}`;
|
||||
const payload = {
|
||||
content: message,
|
||||
username: botUsername,
|
||||
username: managerWebhookName,
|
||||
};
|
||||
|
||||
try {
|
||||
await fetch(webhook, {
|
||||
await fetch(managerWebhook, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
@@ -41,7 +34,7 @@ async function logManager(message, type) {
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
console.log('Error sending webhook message:', error.message);
|
||||
console.error(`[${host}] Error sending webhook message: ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,15 +43,13 @@ async function logBotConsole(message) {
|
||||
console.log(message);
|
||||
|
||||
if (botConsoleWebhook) {
|
||||
const webhook = botConsoleWebhook;
|
||||
const botUsername = `${botConsoleWebhookName}`;
|
||||
const payload = {
|
||||
content: message,
|
||||
username: botUsername,
|
||||
username: botConsoleWebhookName,
|
||||
};
|
||||
|
||||
try {
|
||||
await fetch(webhook, {
|
||||
await fetch(botConsoleWebhook, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
@@ -68,7 +59,7 @@ async function logBotConsole(message) {
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
console.log('Error sending webhook message:', error.message);
|
||||
console.error(`[${host}] Error sending webhook message: ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,16 +70,16 @@ async function checkUpdate() {
|
||||
const currentHead = execSync('git rev-parse HEAD').toString().trim();
|
||||
const remoteHead = execSync('git rev-parse "@{u}"').toString().trim();
|
||||
if (currentHead !== remoteHead) {
|
||||
logManager('Updates available! Please run the updater script.', 'info');
|
||||
logManager(`[${host}] Updates available! Please run the updater script.`, 'info');
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
logManager('Failed to check for updates.', 'error');
|
||||
logManager('[${host}] Failed to check for updates.', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
async function vpnConnect(vpnName) {
|
||||
if (!vpnName) return logManager('Please provide the VPN name as an argument.', 'error');
|
||||
if (!vpnName) return logManager(`[${host}] Please provide the VPN name as an argument.`, 'error');
|
||||
|
||||
logManager(`[${host}] Disconnecting from VPNs`, 'info');
|
||||
await vpnDisconnect();
|
||||
@@ -104,16 +95,16 @@ async function vpnConnect(vpnName) {
|
||||
}
|
||||
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: ${ip}).`, 'info');
|
||||
// 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';
|
||||
@@ -278,13 +269,41 @@ function isRootUser() {
|
||||
return process.getuid && process.getuid() === 0;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
async function runAll() {
|
||||
await checkUpdate();
|
||||
const vpns = execSync('nmcli connection show | awk \'/vpn/ {print $1}\'').toString().trim().split('\n');
|
||||
|
||||
for (const vpn of vpns) {
|
||||
const currentDate = new Date();
|
||||
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');
|
||||
continue;
|
||||
}
|
||||
console.log(entry && entry.accountName === formattedDate);
|
||||
logManager(`[${host}] Switching to VPN: [${vpn}]`, 'info');
|
||||
const con = await vpnConnect(vpn);
|
||||
if (con) continue;
|
||||
await startBot(vpn);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
async function runBatch(vpn) {
|
||||
logManager(`[${host}] Connecting to VPN: [${vpn}]`, 'info');
|
||||
const con = await vpnConnect(vpn);
|
||||
if (con) return 1;
|
||||
await startBot(vpn);
|
||||
return 0;
|
||||
}
|
||||
|
||||
(async () => {
|
||||
await checkUpdate();
|
||||
if (fs.existsSync('.env')) {
|
||||
require('dotenv').config({ path: '.env' });
|
||||
logManager(`[${host}] Config file: .env`, 'info');
|
||||
logManager(`[${host}] Config file: .env.`, 'info');
|
||||
}
|
||||
else {
|
||||
logManager(`[${host}] Config file: not found.`, 'error');
|
||||
@@ -304,61 +323,25 @@ async function main() {
|
||||
}
|
||||
logManager(`[${host}] Log file: batch_logs.json`, 'info');
|
||||
|
||||
for (const vpn of vpns) {
|
||||
const currentDate = new Date();
|
||||
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) continue;
|
||||
logManager(`[${host}] Switching to VPN: [${vpn}]`, 'info');
|
||||
const con = await vpnConnect(vpn);
|
||||
if (con) continue;
|
||||
await startBot(vpn);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
async function uniqueRun(vpn) {
|
||||
await checkUpdate();
|
||||
if (fs.existsSync('.env')) {
|
||||
require('dotenv').config({ path: '.env' });
|
||||
logManager(`[${host}] Config file: .env.`, 'info');
|
||||
}
|
||||
else {
|
||||
logManager(`[${host}] Config file: not found.`, 'error');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
logManager(`[${host}] Bot Console Webhook: ${botConsoleWebhook ? 'True' : 'False'}\n[${host}] Bot Report Webhook: ${reportWebhook ? 'True' : 'False'}\n[${host}] Bot Manager Webhook: ${managerWebhook ? 'True' : 'False'}`);
|
||||
logManager(`[${host}] Starting mcr-bot on host: ${host}`, 'info');
|
||||
|
||||
logManager(`[${host}] Connecting to VPN: [${vpn}]`, 'info');
|
||||
const con = await vpnConnect(vpn);
|
||||
if (con) return 1;
|
||||
await startBot(vpn);
|
||||
return 0;
|
||||
}
|
||||
|
||||
(async () => {
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
if (args[i].startsWith('--')) {
|
||||
if (args[i] === '--accounts' && i + 1 < args.length) {
|
||||
const accounts = args[i + 1];
|
||||
await uniqueRun(accounts);
|
||||
break;
|
||||
return await runBatch(accounts);
|
||||
}
|
||||
else if (args[i] === '--all') {
|
||||
const n = true;
|
||||
while (n) {
|
||||
try {
|
||||
await main();
|
||||
await runAll();
|
||||
await sleep(1000);
|
||||
}
|
||||
catch (error) {
|
||||
logManager('An error occurred:\n' + error.message, 'error');
|
||||
logManager(`[${host}] An error occurred: ${error.message}`, 'error');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
logManager(`[${host}] Missing arguments`);
|
||||
})();
|
||||
@@ -5,8 +5,9 @@
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "node index.js"
|
||||
"start": "node index.js --all"
|
||||
},
|
||||
"type": "module",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://git.justw.tf/Lightemerald/mcr-bot.git"
|
||||
|
||||
Reference in New Issue
Block a user