From 6c28e77aea95a6871016f87115061623b34a6aa8 Mon Sep 17 00:00:00 2001 From: Lightemerald Date: Tue, 20 Jun 2023 17:14:12 +0200 Subject: [PATCH] Updated Script to V2 - New bot manager using timeout instead of schedules - Moved accounts to an accounts folder - New dynamic Updater - Improved VPN Importer to avoid duplicate - Switched back to master Bot - New config file --- .gitignore | 1 + README.md | 10 +- config.txt.sample | 6 +- setup-linux.sh | 6 +- setup-vpn.sh | 9 +- start.sh | 248 +++++++++++++++++++--------------------------- update.sh | 28 +++++- 7 files changed, 148 insertions(+), 160 deletions(-) diff --git a/.gitignore b/.gitignore index 1c5d309..d091410 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ vpn/ +accounts/ Microsoft-Rewards-bot/ config.txt \ No newline at end of file diff --git a/README.md b/README.md index f1f13b0..b256dd4 100644 --- a/README.md +++ b/README.md @@ -11,12 +11,14 @@ This repo contains the script to manage multiple mcr bot on a single VM/Containe ├── update.sh ├── Microsoft-Rewards-bot │ ├── ms_rewards_farmer.py -│ ├── vpnname1.json -│ ├── vpnname2.json +│ ├── ... +├── accounts +│ ├── name1.json +│ ├── name2.json │ ├── ... ├── vpn -│ ├── vpnname1.ovpn -│ ├── vpnname2.ovpn +│ ├── name1.ovpn +│ ├── name2.ovpn │ ├── ... ``` diff --git a/config.txt.sample b/config.txt.sample index 456737b..0785d9e 100644 --- a/config.txt.sample +++ b/config.txt.sample @@ -1,5 +1,5 @@ -interval=300 -schedule=("01:00" "03:15" "05:30" "07:45" "10:00" "12:15" "14:30" "16:45" "19:00" "21:15") retries=3 webhook='' -log_all=0 \ No newline at end of file +print_log_to_webhook=0 +mcr_bot_webhook='' +mcr_bot_webhook_name='Microsoft Rewards Bot Manager' \ No newline at end of file diff --git a/setup-linux.sh b/setup-linux.sh index 5cec1c8..b43eba5 100644 --- a/setup-linux.sh +++ b/setup-linux.sh @@ -42,9 +42,9 @@ update_package_manager install_requirements git clone https://git.justw.tf/Lightemerald/mcr-bot && cd mcr-bot -git clone https://github.com/farshadz1997/Microsoft-Rewards-bot && cd Microsoft-Rewards-bot && git checkout 03476776e9c58029dedaeee431958b5d04364bcf && pip install -r requirements.txt -cd .. && mkdir vpn +git clone https://github.com/farshadz1997/Microsoft-Rewards-bot && cd Microsoft-Rewards-bot && pip install -r requirements.txt +cd .. && mkdir vpn && mkdir accounts cp ./config.txt.sample config.txt chmod +x ./*.sh clear -echo -e "Edit config.txt\nAdd your VPN files (.ovpn) in ./vpn\nThen run setup-vpn.sh\nAdd your accounts files (.json) with the same name as the VPN in ./Microsoft-Rewards-bot\nFinally, run start.sh" \ No newline at end of file +echo -e "Edit config.txt\nAdd your VPN files (.ovpn) in ./vpn\nThen run setup-vpn.sh\nAdd your accounts files (.json) with the same name as the VPN in ./accounts\nFinally, run start.sh" \ No newline at end of file diff --git a/setup-vpn.sh b/setup-vpn.sh index 8f7681e..1de7958 100644 --- a/setup-vpn.sh +++ b/setup-vpn.sh @@ -1,9 +1,14 @@ #!/bin/bash -folder_path="vpn" +vpn_connections=$(nmcli connection show | grep vpn | awk '{print $1}') +for vpn in $vpn_connections; do + nmcli connection delete "$vpn" +done + +folder_path="vpn" for file in "$folder_path"/*; do if [[ -f "$file" ]]; then - nmcli con import type openvpn file ./$file + nmcli con import type openvpn file ./"$file" fi done \ No newline at end of file diff --git a/start.sh b/start.sh index abeb971..14d0d5a 100644 --- a/start.sh +++ b/start.sh @@ -1,165 +1,123 @@ #!/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() { +function console_log() { local message="$1" - if [ -z "$webhook" ]; then - echo "$message" + 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 "$message" - curl --silent -i -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data "{\"content\": \"$message\", \"username\": \"mMicrosoft Rewards Bot Manager\"}" "$webhook" + 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 } -vpns=$(nmcli connection show | grep vpn | awk '{print $1}') -host=$(hostname -f) +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 +} -config_file="config.txt" -if [ -f "$config_file" ]; then - source "$config_file" -else - echo "Config file not found." - exit 1 -fi +function vpn_connect() { + local vpn_name=$1 + local max_attempts=$retries -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 + if [[ -z $vpn_name ]]; then + console_log "Please provide the VPN name as an argument." "error" + return 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 + 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}') - 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))) + 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 - next_seconds=$(($(date -d "$next_time" +%s) - $(date +%s))) + console_log "[$host] Failed to disconnect from all VPNs." "error" + return 1 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 +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 - 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 + command_suffix="--discord $webhook" fi - attempt=$(( $attempt + 1 )) - done + 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 - send_webhook "[$host] ERROR: VPN name is empty" + console_log "[$host] File ./accounts/$account_name.json does not exist, skipping starting bot for this VPN!" "error" + return 1 fi -done +} + +function main() { + check_update + local vpns=$(nmcli connection show | grep vpn | awk '{print $1}') + local host=$(hostname -f) + + config_file="config.txt" + if [ -f "$config_file" ]; then + source "$config_file" + 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 +} +main \ No newline at end of file diff --git a/update.sh b/update.sh index 3c36493..1faf1ba 100644 --- a/update.sh +++ b/update.sh @@ -1,6 +1,28 @@ +git fetch +if [ "$(git rev-parse HEAD)" != "$(git rev-parse @{u})" ]; then + git stash && git pull + exec "$0" +fi + +if [ ! -d "accounts" ]; then + mkdir "accounts" +fi + +if [ ! -d "vpn" ]; then + mkdir "vpn" +fi + +if [ ! -d "Microsoft-Rewards-bot" ]; then + git clone https://github.com/farshadz1997/Microsoft-Rewards-bot +fi + sudo dnf up -y -git stash && git pull -cd Microsoft-Rewards-bot && git stash && git pull && git checkout 03476776e9c58029dedaeee431958b5d04364bcf && pip install -r requirements.txt -clear +cd Microsoft-Rewards-bot +git fetch +if [ "$(git rev-parse HEAD)" != "$(git rev-parse @{u})" ]; then + git stash && git pull && git checkout master && pip install -r requirements.txt +fi + cd .. && chmod +x ./*.sh +clear echo "Update done!" \ No newline at end of file