Files
mcr-bot/start.sh

128 lines
4.0 KiB
Bash

#!/bin/bash
function console_log() {
local message="$1"
local type="$2"
if [ "$type" == "info" ]; then
echo -e "\033[34m$message\033[0m"
elif [ "$type" == "error" ]; then
echo -e "\033[31m$message\033[0m"
else
echo -e "\033[37m$message\033[0m"
fi
if [ -n "$mcr_bot_webhook" ]; then
curl --silent -i -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data "{\"content\": \"$message\", \"username\": \"m$mcr_bot_webhook_name\"}" "$mcr_bot_webhook"
fi
}
function check_update() {
git fetch
if [ "$(git rev-parse HEAD)" != "$(git rev-parse "@{u}")" ]; then
console_log "Updates available! Please run the updater script." "info"
fi
}
function vpn_connect() {
local vpn_name=$1
local max_attempts=$retries
if [[ -z $vpn_name ]]; then
console_log "Please provide the VPN name as an argument." "error"
return 1
fi
for ((attempt = 1; attempt <= max_attempts; attempt++)); do
nmcli connection up "$vpn_name"
status=$(nmcli connection show --active | grep "$vpn_name")
if [[ -n $status ]]; then
ip=$(curl -s https://api.ipify.org)
console_log "[$host] VPN connection successfully established to $vpn_name (IP: $ip)." "info"
return 0
else
console_log "[$host] Failed to connect to VPN: $vpn_name. Retrying (attempt $attempt of $max_attempts)..." "error"
sleep 1
fi
done
console_log "[$host] Maximum number of connection attempts reached. Failed to connect to VPN: $vpn_name" "error"
return 1
}
function vpn_disconnect() {
nmcli connection down "$(nmcli connection show --active | grep vpn | awk '{print $1}')"
status=$(nmcli connection show --active | awk '/vpn/ {print $1}')
if [[ -z $status ]]; then
console_log "[$host] Successfully disconnected from all VPNs." "info"
return 0
else
console_log "[$host] Failed to disconnect from all VPNs." "error"
return 1
fi
}
function start_bot() {
local account_name=$1
if [ -f "./accounts/$account_name.json" ]; then
if [ -n "$webhook" ]; then
if [[ "$print_log_to_webhook" == 1 ]]; then
command_suffix="--discord $webhook --print-to-webhook"
else
command_suffix="--discord $webhook"
fi
fi
if grep -q "container=lxc" /proc/1/environ || grep -q "container=lxc-libvirt" /proc/1/environ; then
command_prefix="cd Microsoft-Rewards-bot && python ms_rewards_farmer.py --accounts-file ../accounts/$account_name.json --dont-check-for-updates --shuffle --session --superfast --on-finish exit --no-webdriver-manager --skip-unusual --virtual-display"
else
command_prefix="cd Microsoft-Rewards-bot && python ms_rewards_farmer.py --accounts-file ../accounts/$account_name.json --dont-check-for-updates --shuffle --session --superfast --on-finish exit --no-webdriver-manager --skip-unusual"
fi
console_log "[$host] Script started for $account_name"
timeout 150m sh -c "$command_prefix $command_suffix; exit; exec bash"
return 0
else
console_log "[$host] File ./accounts/$account_name.json does not exist, skipping starting bot for this VPN!" "error"
return 1
fi
}
function main() {
check_update
local vpns
vpns=$(nmcli connection show | grep vpn | awk '{print $1}')
local host
host=$(hostname -f)
if [ -f "config.txt" ]; then
source "config.txt"
else
console_log "[$host] Config file not found." "error"
exit 1
fi
if [ -z "$webhook" ]; then
console_log "[$host] Webhook not set! Updates won't be sent to it"
else
if [ -z "$mcr_bot_webhook" ]; then
mcr_bot_webhook=$webhook
fi
console_log "[$host] Webhook set!"
console_log "[$host] Starting mcr-bot on host: $host" "info"
fi
for vpn in $vpns; do
console_log "[$host] Switching to VPN: [$vpn]" "info"
if vpn_connect "$vpn"; then
start_bot "$vpn"
console_log "[$host] Disconnecting from VPN: [$vpn]" "info"
vpn_disconnect
else
console_log "[$host] Failed to connect to VPN: [$vpn]. Skipping to the next VPN." "error"
fi
done
}
while true; do
main
:
done