Files
mcr-bot/start.sh
2023-06-20 15:47:43 +02:00

166 lines
5.0 KiB
Bash

#!/bin/bash
if [ "$(ps -o comm= $PPID)" != "kgx" ]
then
echo "Script is running under $(ps -o comm= $PPID)"
if command -v kgx >/dev/null; then
echo "gnome-console is already installed"
else
echo "Installing gnome-console"
if command -v dnf >/dev/null; then
sudo dnf install gnome-console -y
elif command -v apt-get >/dev/null; then
sudo apt-get install kgx -y
elif command -v pacman >/dev/null; then
sudo pacman -Sy gnome-console --noconfirm
else
echo "Unsupported package manager, install gnome console (kgx) on your own."
exit 1
fi
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"
if [ -z "$webhook" ]; then
echo "$message"
else
echo "$message"
curl --silent -i -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data "{\"content\": \"$message\", \"username\": \"mMicrosoft Rewards Bot Manager\"}" "$webhook"
fi
}
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 mcr-bot 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
send_webhook "[$host] ERROR: Schedules are not setup correctly!"
exit
fi
send_webhook "[$host] Next schedule: $next_time"
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")
send_webhook "[$host] Switching to VPN: [$next_vpn_index] - $vpn"
if [ "$vpn" != "" ]
then
send_webhook "[$host] Killing potential previous bot..."
pkill -f "gnome-terminal"
send_webhook "[$host] 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)
send_webhook "[$host] VPN connection successfully established (IP: $ip).\nStarting script..."
if [ -f "./Microsoft-Rewards-bot/$vpn.json" ]
then
if [ -z "$webhook" ]; then
command_suffix="--accounts-file $vpn.json"
else
if [[ "$log_all" == 1 ]]; then
command_suffix="--accounts-file $vpn.json --discord $webhook --print-to-webhook"
else
command_suffix="--accounts-file $vpn.json --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 --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 --dont-check-for-updates --shuffle --session --superfast --on-finish exit --no-webdriver-manager --skip-unusual"
fi
gnome-terminal -- sh -c "$command_prefix $command_suffix; exit; exec bash" &
read -t 2
send_webhook "[$host] Script started"
read -t 1
break
else
send_webhook "[$host] ERROR: File ./Microsoft-Rewards-bot/$vpn.json does not exist, skipping starting bot for this VPN"
read -t 3
break
fi
else
send_webhook "[$host] ERROR: Failed to establish VPN connection (attempt $attempt of $retries)"
if [ $attempt -eq $retries ]
then
send_webhook "[$host] ERROR: Max retries reached, switching to the next schedule"
fi
read -t 1
fi
attempt=$(( $attempt + 1 ))
done
else
send_webhook "[$host] ERROR: VPN name is empty"
fi
done