From 1d5555de70e012b683ebcbafdba4cd0234110054 Mon Sep 17 00:00:00 2001 From: Lightemerald Date: Tue, 20 Jun 2023 14:06:11 +0200 Subject: [PATCH] Improved script and added support for arch and debian based distro. --- README.md | 6 ++-- start.sh | 89 ++++++++++++++++++++++++------------------------------- 2 files changed, 42 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index f9bb714..f1f13b0 100644 --- a/README.md +++ b/README.md @@ -34,13 +34,13 @@ wget -O- https://git.justw.tf/Lightemerald/mcr-bot/raw/branch/main/setup-linux.s Add your VPN files (.ovpn) to `./vpn` then run ```bash -sh setup-vpn.sh +bash setup-vpn.sh ``` Copy your accounts file named as [vpnname].json in the Bot folder then run ```bash -sh start.sh +bash start.sh ``` ### If using LXC @@ -54,5 +54,5 @@ ps: container must be powered off before and restarted only after ## How to update ```bash -sh update.sh +bash update.sh ``` \ No newline at end of file diff --git a/start.sh b/start.sh index d77b44d..c034f49 100644 --- a/start.sh +++ b/start.sh @@ -3,11 +3,20 @@ if [ "$(ps -o comm= $PPID)" != "kgx" ] then echo "Script is running under $(ps -o comm= $PPID)" - if rpm -q gnome-console >/dev/null; then + if command -v kgx >/dev/null; then echo "gnome-console is already installed" else echo "Installing gnome-console" - sudo dnf install gnome-console -y + 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 @@ -17,7 +26,6 @@ then kgx -e "/bin/bash -c 'cd $(dirname "$(readlink -f "$0")"); ./$(basename "$0")'" & fi - read -t 2 pkill -f "gnome-terminal" exit @@ -25,7 +33,12 @@ 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" + 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}') @@ -43,7 +56,7 @@ 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" + send_webhook "Starting mcr-bot on host: $host" fi while true @@ -76,16 +89,11 @@ do if [ $next_seconds -lt 0 ] then - if [ -n "$webhook" ]; then - send_webhook "ERROR: Schedules are not setup correctly!" - fi - echo 'schedule error, exiting...' + send_webhook "[$host] ERROR: Schedules are not setup correctly!" exit fi - if [ -n "$webhook" ]; then - send_webhook "Next schedule: $next_time" - fi + send_webhook "[$host] Next schedule: $next_time" while [ $next_seconds -gt 0 ] do @@ -98,16 +106,13 @@ do 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 + send_webhook "[$host] Switching to VPN: [$next_vpn_index] - $vpn" if [ "$vpn" != "" ] then - echo "Killing potential previous bot..." + send_webhook "[$host] Killing potential previous bot..." pkill -f "gnome-terminal" - echo "Switching VPN..." + send_webhook "[$host] Switching VPN..." nmcli connection down $(nmcli connection show --active | grep vpn | awk '{print $1}') attempt=1 while [ $attempt -le $retries ] @@ -117,57 +122,41 @@ do 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 + send_webhook "[$host] VPN connection successfully established (IP: $ip).\nStarting script..." 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 + command_suffix="--accounts-file $vpn.json" 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 + if [[ "$log_all" == 1 ]]; then + command_suffix="--accounts-file $vpn.json --discord $webhook --print-to-webhook" 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 + 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 - echo "Script started" + send_webhook "[$host] 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" + send_webhook "[$host] 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 + send_webhook "[$host] ERROR: Failed to establish VPN connection (attempt $attempt of $retries)" 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 + send_webhook "[$host] ERROR: Max retries reached, switching to the next schedule" fi read -t 1 fi @@ -176,7 +165,7 @@ do else echo "ERROR: VPN name is empty" if [ -n "$webhook" ]; then - send_webhook "ERROR: VPN name is empty" + send_webhook "[$host] ERROR: VPN name is empty" fi fi done