Now support to three different webhook channel
- Bot Report webhook - Bot Console webhook - Bot Manager webhook
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
RETRIES=3
|
||||
WEBHOOK=''
|
||||
BOT_BROWSER='uc'
|
||||
PRINT_CONSOLE_LOGS=0
|
||||
MCR_BOT_WEBHOOK=''
|
||||
MCR_BOT_WEBHOOK_NAME='Microsoft Rewards Bot Manager'
|
||||
REPORT_WEBHOOK=''
|
||||
BOT_CONSOLE_WEBHOOK=''
|
||||
BOT_CONSOLE_WEBHOOK_NAME='Microsoft Rewards Bot'
|
||||
MANAGER_WEBHOOK=''
|
||||
MANAGER_WEBHOOK_NAME='Microsoft Rewards Bot Manager'
|
||||
132
index.js
132
index.js
@@ -5,7 +5,13 @@ const os = require('os');
|
||||
const host = os.hostname();
|
||||
const args = process.argv.slice(2);
|
||||
|
||||
async function consoleLog(message, type) {
|
||||
const reportWebhook = process.env.REPORT_WEBHOOK;
|
||||
const botConsoleWebhook = process.env.BOT_CONSOLE_WEBHOOK;
|
||||
const botConsoleWebhookName = process.env.BOT_CONSOLE_WEBHOOK_NAME || 'Microsoft Rewards Bot';
|
||||
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`);
|
||||
}
|
||||
@@ -16,9 +22,36 @@ async function consoleLog(message, type) {
|
||||
console.log(`\x1b[37m${message}\x1b[0m`);
|
||||
}
|
||||
|
||||
if (process.env.MCR_BOT_WEBHOOK) {
|
||||
const webhook = process.env.MCR_BOT_WEBHOOK;
|
||||
const botUsername = `m${process.env.MCR_BOT_WEBHOOK_NAME}`;
|
||||
if (managerWebhook) {
|
||||
const webhook = managerWebhook;
|
||||
const botUsername = `${managerWebhookName}`;
|
||||
const payload = {
|
||||
content: message,
|
||||
username: botUsername,
|
||||
};
|
||||
|
||||
try {
|
||||
await fetch(webhook, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
console.log('Error sending webhook message:', error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function logBotConsole(message) {
|
||||
console.log(message);
|
||||
|
||||
if (botConsoleWebhook) {
|
||||
const webhook = botConsoleWebhook;
|
||||
const botUsername = `${botConsoleWebhookName}`;
|
||||
const payload = {
|
||||
content: message,
|
||||
username: botUsername,
|
||||
@@ -46,18 +79,18 @@ async function checkUpdate() {
|
||||
const currentHead = execSync('git rev-parse HEAD').toString().trim();
|
||||
const remoteHead = execSync('git rev-parse "@{u}"').toString().trim();
|
||||
if (currentHead !== remoteHead) {
|
||||
consoleLog('Updates available! Please run the updater script.', 'info');
|
||||
logManager('Updates available! Please run the updater script.', 'info');
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
consoleLog('Failed to check for updates.', 'error');
|
||||
logManager('Failed to check for updates.', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
async function vpnConnect(vpnName) {
|
||||
if (!vpnName) return consoleLog('Please provide the VPN name as an argument.', 'error');
|
||||
if (!vpnName) return logManager('Please provide the VPN name as an argument.', 'error');
|
||||
|
||||
consoleLog(`[${host}] Disconnecting from VPNs`, 'info');
|
||||
logManager(`[${host}] Disconnecting from VPNs`, 'info');
|
||||
await vpnDisconnect();
|
||||
|
||||
const maxAttempts = process.env.RETRIES;
|
||||
@@ -66,7 +99,7 @@ async function vpnConnect(vpnName) {
|
||||
|
||||
const status = await execSync('nmcli connection show --active').toString().includes(vpnName);
|
||||
if (!status) {
|
||||
consoleLog(`[${host}] Failed to connect to VPN: ${vpnName}. Retrying (attempt ${attempt} of ${maxAttempts})...`, 'error');
|
||||
logManager(`[${host}] Failed to connect to VPN: ${vpnName}. Retrying (attempt ${attempt} of ${maxAttempts})...`, 'error');
|
||||
await sleep(1000);
|
||||
}
|
||||
else {
|
||||
@@ -80,17 +113,17 @@ async function vpnConnect(vpnName) {
|
||||
});
|
||||
const res = await response.json();
|
||||
const ip = res.ip;
|
||||
await consoleLog(`[${host}] VPN connection successfully established to ${vpnName} (IP: ${ip}).`, 'info');
|
||||
await logManager(`[${host}] VPN connection successfully established to ${vpnName} (IP: ${ip}).`, 'info');
|
||||
}
|
||||
catch (err) {
|
||||
const ip = 'Unknown';
|
||||
await consoleLog(`[${host}] VPN connection successfully established to ${vpnName} (IP: ${ip}).`, 'info');
|
||||
await logManager(`[${host}] VPN connection successfully established to ${vpnName} (IP: ${ip}).`, 'info');
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
consoleLog(`[${host}] Maximum number of connection attempts reached. Failed to connect to VPN: ${vpnName}`, 'error');
|
||||
logManager(`[${host}] Maximum number of connection attempts reached. Failed to connect to VPN: ${vpnName}`, 'error');
|
||||
await vpnDisconnect();
|
||||
return 1;
|
||||
}
|
||||
@@ -98,7 +131,7 @@ async function vpnConnect(vpnName) {
|
||||
function vpnDisconnect() {
|
||||
const vpnConnection = execSync('nmcli connection show --active | awk \'/vpn/ {print $1}\'').toString().trim();
|
||||
if (!vpnConnection) {
|
||||
consoleLog(`[${host}] Successfully disconnected from all VPNs.`, 'info');
|
||||
logManager(`[${host}] Successfully disconnected from all VPNs.`, 'info');
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -106,16 +139,16 @@ function vpnDisconnect() {
|
||||
execSync(`nmcli connection down "${vpnConnection}"`);
|
||||
const status = execSync('nmcli connection show --active | awk \'/vpn/ {print $1}\'').toString().trim();
|
||||
if (!status) {
|
||||
consoleLog(`[${host}] Successfully disconnected from all VPNs.`, 'info');
|
||||
logManager(`[${host}] Successfully disconnected from all VPNs.`, 'info');
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
consoleLog(`[${host}] Failed to disconnect from all VPNs.`, 'error');
|
||||
logManager(`[${host}] Failed to disconnect from all VPNs.`, 'error');
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
consoleLog(`[${host}] Failed to disconnect from all VPNs.`, 'error');
|
||||
logManager(`[${host}] Failed to disconnect from all VPNs.`, 'error');
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -125,12 +158,8 @@ async function startBot(accountName) {
|
||||
if (fs.existsSync(accountPath)) {
|
||||
let commandSuffix = '';
|
||||
|
||||
if (process.env.WEBHOOK === '1') {
|
||||
commandSuffix += ` --discord ${process.env.WEBHOOK}`;
|
||||
}
|
||||
|
||||
if (process.env.PRINT_CONSOLE_LOGS === '1') {
|
||||
commandSuffix += ' --print-to-webhook';
|
||||
if (reportWebhook === '1') {
|
||||
commandSuffix += ` --discord ${reportWebhook}`;
|
||||
}
|
||||
|
||||
if (process.env.BOT_BROWSER) {
|
||||
@@ -138,7 +167,7 @@ async function startBot(accountName) {
|
||||
}
|
||||
|
||||
let commandPrefix = `python -u ./Microsoft-Rewards-bot/ms_rewards_farmer.py --accounts-file ../accounts/${accountName}.json --dont-check-for-updates --shuffle --session --superfast --on-finish exit --no-webdriver-manager --skip-unusual`;
|
||||
consoleLog(`[${host}] Script started for ${accountName}`);
|
||||
logManager(`[${host}] Script started for ${accountName}`);
|
||||
|
||||
if (isRootUser()) {
|
||||
console.log('The user is root.');
|
||||
@@ -157,7 +186,7 @@ async function startBot(accountName) {
|
||||
|
||||
childProcess.stdout.on('data', (data) => {
|
||||
const output = data.toString().trim();
|
||||
console.log(`[${host}][${accountName}] STDOUT: ${output}`);
|
||||
logBotConsole(`[${host}][${accountName}] STDOUT: ${output}`);
|
||||
if (output.includes('Press enter') || output.includes('Press any key')) {
|
||||
setTimeout(() => {
|
||||
childProcess.stdin.write('\n');
|
||||
@@ -167,7 +196,7 @@ async function startBot(accountName) {
|
||||
|
||||
childProcess.stderr.on('data', (data) => {
|
||||
const output = data.toString().trim();
|
||||
console.log(`[${host}][${accountName}] STDERR: ${output}`);
|
||||
logBotConsole(`[${host}][${accountName}] STDERR: ${output}`);
|
||||
if (output.includes('Press enter') || output.includes('Press any key')) {
|
||||
setTimeout(() => {
|
||||
childProcess.stdin.write('\n');
|
||||
@@ -177,11 +206,20 @@ async function startBot(accountName) {
|
||||
|
||||
childProcess.on('exit', (code) => {
|
||||
if (code !== 0) {
|
||||
consoleLog(`[${host}] Bot process for ${accountName} exited with code ${code}. Restarting...`, 'error');
|
||||
logManager(`[${host}] Bot process for ${accountName} exited with code ${code}. Restarting...`, 'error');
|
||||
startBot(accountName).then(resolve).catch(reject);
|
||||
}
|
||||
else {
|
||||
const logEntry = {
|
||||
accountName,
|
||||
date: new Date().toISOString(),
|
||||
};
|
||||
fs.appendFile('batch_logs.json', JSON.stringify(logEntry) + '\n', (err) => {
|
||||
if (err) {
|
||||
console.error(`Failed to write to batch_logs.json: ${err}`);
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -190,18 +228,18 @@ async function startBot(accountName) {
|
||||
});
|
||||
|
||||
childProcess.on('error', (err) => {
|
||||
consoleLog(`[${host}] Failed to start bot for ${accountName}.`, 'error');
|
||||
logManager(`[${host}] Failed to start bot for ${accountName}.`, 'error');
|
||||
reject(err);
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
consoleLog(`[${host}] Bot process for ${accountName} exceeded the timeout. Killing the process...`, 'error');
|
||||
logManager(`[${host}] Bot process for ${accountName} exceeded the timeout. Killing the process...`, 'error');
|
||||
childProcess.kill();
|
||||
}, 150 * 60 * 1000);
|
||||
});
|
||||
}
|
||||
else {
|
||||
consoleLog(`[${host}] File ${accountPath} does not exist, skipping starting bot for this VPN!`, 'error');
|
||||
logManager(`[${host}] File ${accountPath} does not exist, skipping starting bot for this VPN!`, 'error');
|
||||
return Promise.resolve({ error: `File ${accountPath} does not exist.` });
|
||||
}
|
||||
}
|
||||
@@ -222,25 +260,18 @@ async function main() {
|
||||
|
||||
if (fs.existsSync('.env')) {
|
||||
require('dotenv').config({ path: '.env' });
|
||||
logManager(`[${host}] Config file: .env.`, 'info');
|
||||
}
|
||||
else {
|
||||
consoleLog(`[${host}] Config file not found.`, 'error');
|
||||
logManager(`[${host}] Config file: not found.`, 'error');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (!process.env.WEBHOOK) {
|
||||
consoleLog(`[${host}] Webhook not set! Updates won't be sent to it.`);
|
||||
}
|
||||
else {
|
||||
if (!process.env.MCR_BOT_WEBHOOK) {
|
||||
process.env.MCR_BOT_WEBHOOK = process.env.WEBHOOK;
|
||||
}
|
||||
consoleLog(`[${host}] Webhook set!`);
|
||||
consoleLog(`[${host}] Starting mcr-bot on host: ${host}`, 'info');
|
||||
}
|
||||
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');
|
||||
|
||||
for (const vpn of vpns) {
|
||||
consoleLog(`[${host}] Switching to VPN: [${vpn}]`, 'info');
|
||||
logManager(`[${host}] Switching to VPN: [${vpn}]`, 'info');
|
||||
const con = await vpnConnect(vpn);
|
||||
if (con) continue;
|
||||
await startBot(vpn);
|
||||
@@ -252,24 +283,17 @@ async function uniqueRun(vpn) {
|
||||
await checkUpdate();
|
||||
if (fs.existsSync('.env')) {
|
||||
require('dotenv').config({ path: '.env' });
|
||||
logManager(`[${host}] Config file: .env.`, 'info');
|
||||
}
|
||||
else {
|
||||
consoleLog(`[${host}] Config file not found.`, 'error');
|
||||
logManager(`[${host}] Config file: not found.`, 'error');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (!process.env.WEBHOOK) {
|
||||
consoleLog(`[${host}] Webhook not set! Updates won't be sent to it.`);
|
||||
}
|
||||
else {
|
||||
if (!process.env.MCR_BOT_WEBHOOK) {
|
||||
process.env.MCR_BOT_WEBHOOK = process.env.WEBHOOK;
|
||||
}
|
||||
consoleLog(`[${host}] Webhook set!`);
|
||||
consoleLog(`[${host}] Starting mcr-bot on host: ${host}`, 'info');
|
||||
}
|
||||
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');
|
||||
|
||||
consoleLog(`[${host}] Switching to VPN: [${vpn}]`, 'info');
|
||||
logManager(`[${host}] Connecting to VPN: [${vpn}]`, 'info');
|
||||
const con = await vpnConnect(vpn);
|
||||
if (con) return 1;
|
||||
await startBot(vpn);
|
||||
@@ -292,7 +316,7 @@ async function uniqueRun(vpn) {
|
||||
await sleep(1000);
|
||||
}
|
||||
catch (error) {
|
||||
consoleLog('An error occurred:\n' + error.message, 'error');
|
||||
logManager('An error occurred:\n' + error.message, 'error');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user