From 1695fd37f6bbb788a31294efff2421a537e6698e Mon Sep 17 00:00:00 2001 From: Lightemerald Date: Thu, 18 May 2023 19:34:36 +0200 Subject: [PATCH] Added webhook dynamic support for the bot, webhook logs, automatic LXC detection and updated setup-fedora.sh --- setup-fedora.sh | 3 +- start-lxc.sh | 114 ------------------------------------------------ start.sh | 56 ++++++++++++++++++++++-- 3 files changed, 54 insertions(+), 119 deletions(-) delete mode 100644 start-lxc.sh diff --git a/setup-fedora.sh b/setup-fedora.sh index df2d132..f1c66c0 100644 --- a/setup-fedora.sh +++ b/setup-fedora.sh @@ -11,5 +11,6 @@ chmod +x ./*.sh clear echo "edit config.txt" echo "Add your VPN files (.ovpn) in ./vpn" +echo "Then run setup-vpn.sh" echo "Add your accounts files (.json) with the same name as the vpn in ./Microsoft-Rewards-bot" -echo "finally run setup-vpn.sh" \ No newline at end of file +echo "Finally run start.sh" \ No newline at end of file diff --git a/start-lxc.sh b/start-lxc.sh deleted file mode 100644 index 086dc99..0000000 --- a/start-lxc.sh +++ /dev/null @@ -1,114 +0,0 @@ -#!/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..." - kgx -e "/bin/bash -c 'cd $(dirname "$(readlink -f "$0")"); ./$(basename "$0")'" & - read -t 2 - pkill -f "gnome-terminal" - exit -fi - -vpns=$(nmcli connection show | grep vpn | awk '{print $1}') - -config_file="config.txt" -if [ -f "$config_file" ]; then - source "$config_file" -else - echo "Config file not found." - exit 1 -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 - echo 'schedule error, exiting...' - exit - 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 [ "$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 - echo "VPN connection successfully established" - echo "Starting script..." - if [ -d "./Microsoft-Rewards-bot/$vpn.json" ] - 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 --discord $webhook; exit; exec bash" & - 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" - read -t 3 - break - fi - else - echo "Failed to establish VPN connection (attempt $attempt of $retries)" - if [ $attempt -eq $retries ] - then - echo "Max retries reached, switching to the next available VPN connection" - fi - read -t 1 - fi - attempt=$(( $attempt + 1 )) - done - fi -done \ No newline at end of file diff --git a/start.sh b/start.sh index ea4bc05..a8924a8 100644 --- a/start.sh +++ b/start.sh @@ -17,6 +17,7 @@ then fi vpns=$(nmcli connection show | grep vpn | awk '{print $1}') +host=$(hostname -f) config_file="config.txt" if [ -f "$config_file" ]; then @@ -26,6 +27,13 @@ else 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) @@ -56,6 +64,9 @@ do if [ $next_seconds -lt 0 ] then + if [ -n "$webhook_url" ]; then + send_webhook "ERROR: Schedules are not setup correctly!" + fi echo 'schedule error, exiting...' exit fi @@ -71,7 +82,10 @@ do next_vpn_index=$(( $next_index + 1 )) vpn=$(echo "$vpns" | sed -n "${next_vpn_index}p") - echo "VPN: $next_vpn_index $vpn" + echo "VPN: [$next_vpn_index] - $vpn" + if [ -n "$webhook_url" ]; then + send_webhook "Switching to VPN: [$next_vpn_index] - $vpn" + fi if [ "$vpn" != "" ] then @@ -88,27 +102,61 @@ do then echo "VPN connection successfully established" echo "Starting script..." + if [ -n "$webhook_url" ]; then + send_webhook "VPN connection successfully established.\nStarting script..." + fi if [ -f "./Microsoft-Rewards-bot/$vpn.json" ] 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 --accounts-file $vpn.json --discord $webhook; exit; exec bash" & + 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 + 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" & + 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 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_url" ]; 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_url" ]; 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 available VPN connection" + echo "Max retries reached, switching to the next schedule" + if [ -n "$webhook_url" ]; 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_url" ]; then + send_webhook "ERROR: VPN name is empty" + fi fi -done \ No newline at end of file +done + +send_webhook() { + local message="$1" + curl -i -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data "{\"content\": \"$message\"}" "$webhook" +}