#!/bin/bash if [ "$(ps -o comm= $PPID)" != "kgx" ] then echo "Script is running under $(ps -o comm= $PPID)" if rpm -q gnome-console >/dev/null; then echo "gnome-console is already installed" else echo "Installing gnome-console" sudo dnf install gnome-console -y fi echo "Restarting script in gnome-console..." if grep -q "container=lxc" /proc/1/environ || grep -q "container=lxc-libvirt" /proc/1/environ; then Xvfb :1 -screen 0 1024x768x16 & DISPLAY=:1.0 kgx -e "/bin/bash -c 'cd $(dirname "$(readlink -f "$0")"); ./$(basename "$0")'" & else kgx -e "/bin/bash -c 'cd $(dirname "$(readlink -f "$0")"); ./$(basename "$0")'" & fi read -t 2 pkill -f "gnome-terminal" exit fi function send_webhook() { local message="$1" curl --silent -i -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data "{\"content\": \"$message\", \"username\": \"mcr-bot\"}" "$webhook" } vpns=$(nmcli connection show | grep vpn | awk '{print $1}') host=$(hostname -f) config_file="config.txt" if [ -f "$config_file" ]; then source "$config_file" else echo "Config file not found." exit 1 fi if [ -z "$webhook" ]; then echo "Webhook not set! Updates won't be sent to it" else echo "Webhook set!" send_webhook "Starting script on host: $host" fi while true do current_time=$(date +%H:%M) next_index=-1 for i in "${!schedule[@]}" do if [[ "$current_time" < "${schedule[$i]}" ]] then next_index=$i break fi done if [ $next_index -eq -1 ] then next_index=0 fi next_time="${schedule[$next_index]}" if [[ "$current_time" > "${schedule[$next_index]}" ]] then next_seconds=$(($(date -d "$next_time +1 day" +%s) - $(date +%s))) else next_seconds=$(($(date -d "$next_time" +%s) - $(date +%s))) fi if [ $next_seconds -lt 0 ] then if [ -n "$webhook" ]; then send_webhook "ERROR: Schedules are not setup correctly!" fi echo 'schedule error, exiting...' exit fi if [ -n "$webhook" ]; then send_webhook "Next schedule: $next_time" fi while [ $next_seconds -gt 0 ] do echo -ne "\033[0K\rWaiting for $next_time... (in $next_seconds s) " read -t 1 next_seconds=$(( $next_seconds - 1 )) done echo "" next_vpn_index=$(( $next_index + 1 )) vpn=$(echo "$vpns" | sed -n "${next_vpn_index}p") echo "VPN: [$next_vpn_index] - $vpn" if [ -n "$webhook" ]; then send_webhook "Switching to VPN: [$next_vpn_index] - $vpn" fi if [ "$vpn" != "" ] then echo "Killing potential previous bot..." pkill -f "gnome-terminal" echo "Switching VPN..." nmcli connection down $(nmcli connection show --active | grep vpn | awk '{print $1}') attempt=1 while [ $attempt -le $retries ] do nmcli connection up $vpn read -t 2 if nmcli connection show --active | grep -q $vpn then ip=$(curl -s https://api.ipify.org) echo "VPN connection successfully established (IP: $ip)" echo "Starting script..." if [ -n "$webhook" ]; then send_webhook "VPN connection successfully established (IP: $ip).\nStarting script..." fi if [ -f "./Microsoft-Rewards-bot/$vpn.json" ] then if [ -z "$webhook" ]; then if grep -q "container=lxc" /proc/1/environ || grep -q "container=lxc-libvirt" /proc/1/environ; then gnome-terminal -- sh -c "cd Microsoft-Rewards-bot && python ms_rewards_farmer.py --dont-check-for-updates --shuffle --session --superfast --on-finish exit --no-webdriver-manager --virtual-display --accounts-file $vpn.json; exit; exec bash" & else gnome-terminal -- sh -c "cd Microsoft-Rewards-bot && python ms_rewards_farmer.py --dont-check-for-updates --shuffle --session --superfast --on-finish exit --no-webdriver-manager --accounts-file $vpn.json; exit; exec bash" & fi else if grep -q "container=lxc" /proc/1/environ || grep -q "container=lxc-libvirt" /proc/1/environ; then if [[ "$log_all" == 1 ]]; then gnome-terminal -- sh -c "cd Microsoft-Rewards-bot && python ms_rewards_farmer.py --dont-check-for-updates --shuffle --session --superfast --on-finish exit --no-webdriver-manager --virtual-display --print-to-webhook --accounts-file $vpn.json --discord $webhook; exit; exec bash" & else gnome-terminal -- sh -c "cd Microsoft-Rewards-bot && python ms_rewards_farmer.py --dont-check-for-updates --shuffle --session --superfast --on-finish exit --no-webdriver-manager --virtual-display --accounts-file $vpn.json --discord $webhook; exit; exec bash" & fi else if [[ "$log_all" == 1 ]]; then gnome-terminal -- sh -c "cd Microsoft-Rewards-bot && python ms_rewards_farmer.py --dont-check-for-updates --shuffle --session --superfast --on-finish exit --no-webdriver-manager --print-to-webhook --accounts-file $vpn.json --discord $webhook; exit; exec bash" & else gnome-terminal -- sh -c "cd Microsoft-Rewards-bot && python ms_rewards_farmer.py --dont-check-for-updates --shuffle --session --superfast --on-finish exit --no-webdriver-manager --accounts-file $vpn.json --discord $webhook; exit; exec bash" & fi fi fi read -t 2 echo "Script started" read -t 1 break else echo "File ./Microsoft-Rewards-bot/$vpn.json does not exist, skipping starting bot for this VPN" if [ -n "$webhook" ]; then send_webhook "ERROR: File ./Microsoft-Rewards-bot/$vpn.json does not exist, skipping starting bot for this VPN" fi read -t 3 break fi else echo "Failed to establish VPN connection (attempt $attempt of $retries)" if [ -n "$webhook" ]; then send_webhook "ERROR: Failed to establish VPN connection (attempt $attempt of $retries)" fi if [ $attempt -eq $retries ] then echo "Max retries reached, switching to the next schedule" if [ -n "$webhook" ]; then send_webhook "ERROR: Max retries reached, switching to the next schedule" fi fi read -t 1 fi attempt=$(( $attempt + 1 )) done else echo "ERROR: VPN name is empty" if [ -n "$webhook" ]; then send_webhook "ERROR: VPN name is empty" fi fi done