diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..80ce1de --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 4 +trim_trailing_whitespace = true + +[*.yml] +indent_size = 2 diff --git a/Dockerfile b/Dockerfile index c77cdcd..12ccbb3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ FROM alpine:3.19 run apk update && apk add --no-cache wget bash -ADD bash.sh . -entrypoint ["bash","bash.sh"] +ADD best403unlocker . +entrypoint ["bash","best403unlocker"] diff --git a/README.md b/README.md index 4982793..ff31537 100644 --- a/README.md +++ b/README.md @@ -71,10 +71,10 @@ When you run the script, a menu will appear with the following options: ## Getting Started with CLI -You can use this script with **docker** or run in **baremetal** +You can use this script with **docker** or run in **baremetal** ## Baremetal -![output](https://github.com/ArmanTaheriGhaleTaki/speed-test-dns/assets/88885103/d83c954e-5f3c-434e-ae4b-f119d69a4220) +![output](https://github.com/ArmanTaheriGhaleTaki/speed-test-dns/assets/88885103/d83c954e-5f3c-434e-ae4b-f119d69a4220) Also you can run this script on **baremetal** with **sudo** privilege ```sh @@ -89,7 +89,7 @@ You only need **docker** installed and you can edit variables in [best403unlocke git clone --depth 1 https://github.com/ArmanTaheriGhaleTaki/best403unlocker/ && cd best403unlocker && docker build -t best403unlocker . && docker run --env-file best403unlocker.conf best403unlocker ``` -or you can use built image uploaded in dockerhub +or you can use built image uploaded in dockerhub _for that case you need to use [best403unlocker.conf](https://github.com/ArmanTaheriGhaleTaki/best403unlocker/blob/main/best403unlocker.conf) at run time_ ```sh @@ -97,7 +97,7 @@ wget -c https://raw.githubusercontent.com/ArmanTaheriGhaleTaki/best403unlocker/m ``` -## credit +## credit thank [AKishmiish](https://github.com/Kishmiish) for adding TUI to this projcet ## Contact diff --git a/bash.sh b/bash.sh deleted file mode 100644 index d2b1fda..0000000 --- a/bash.sh +++ /dev/null @@ -1,61 +0,0 @@ -# Functions -check_and_source_env() { -if [ -z "$dns" ]; then - if [ ! -f ./best403unlocker.conf ]; then - wget -c https://raw.githubusercontent.com/ArmanTaheriGhaleTaki/best403unlocker/main/best403unlocker.conf - fi - source best403unlocker.conf - fi -} - -check_ip() { - local input=$1 - local regex="^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" - - if [[ $input =~ $regex ]]; then - return 0 - else - return 1 - fi -} - -function change_dns () { - - echo 'nameserver' $1> /etc/resolv.conf - echo '############################' - cat /etc/resolv.conf - echo '$$$$$$$$$$$$$$$$$$$$$$$$$$$$' - -} - -function download() { - timeout $timeout wget -q -O /tmp/$1 --no-dns-cache $file_url -} - -function download_speed() { -du -s /tmp/$1 >> database -rm /tmp/$1 -} - - - - - -# Execute the functions -check_and_source_env -touch database -cp /etc/resolv.conf /etc/resolv.conf.bakup -for i in $dns -do - if check_ip $i; then -change_dns $i -download $i -download_speed $i - fi -done -echo '*********************' -echo best dns server is `sort -rn database| head -1| cut -d'/' -f3 | tee -a output` -echo '*********************' -rm database -cat /etc/resolv.conf.bakup > /etc/resolv.conf - diff --git a/best403unlocker b/best403unlocker new file mode 100644 index 0000000..b396e26 --- /dev/null +++ b/best403unlocker @@ -0,0 +1,87 @@ +#!/usr/bin/env bash +DATABASE=best403unlocker_database_$(date +%s) +DATABASE_PATH=/tmp/$DATABASE +# Functions +check_run_with_root_accsses(){ +if [ "$EUID" -ne 0 ]; then + echo "Please run this script as root (sudo)" + exit 1 +fi +} +check_required_packages_is_installed(){ + if ! [ -x "$(command -v wget)" ] +then +echo "wget is not installed" >&2 +exit 1 +fi +} +check_and_source_env() { + if [ ! -f /etc/best403unlocker.conf ]; then + wget -c https://raw.githubusercontent.com/ArmanTaheriGhaleTaki/best403unlocker/main/.env -O /etc/best403unlocker.conf + fi +source /etc/best403unlocker.conf +} + +check_ip() { + local input=$1 + local regex="^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" + + if [[ $input =~ $regex ]]; then + return 0 + else + return 1 + fi +} + +function change_dns () { + + echo 'nameserver' $1> /etc/resolv.conf + echo '############################' + cat /etc/resolv.conf + echo '$$$$$$$$$$$$$$$$$$$$$$$$$$$$' + +} + +function download() { + timeout $timeout wget -q -O /tmp/$1 --no-dns-cache $file_url +} + +function download_speed() { +du -s /tmp/$1 >> $DATABASE_PATH +rm /tmp/$1 +} + +loggin(){ + echo "$(date '+%Y-%m-%d %H:%M:%S') " + sed -i s/"\/tmp\/"//g $DATABASE_PATH +} + + +# Execute the functions +check_run_with_root_accsses +check_required_packages_is_installed +check_and_source_env +touch touch $DATABASE_PATH +cp /etc/resolv.conf /etc/resolv.conf.bakup +for i in $dns +do + if check_ip $i; then +change_dns $i +download $i +download_speed $i + fi +done +is_network_working=$( sort -rn $DATABASE_PATH | head -1|cut -d $'\t' -f1) + +if [[ "$is_network_working" -eq 0 ]]; then +echo '*********************' +echo 'Network is not reachable' +echo '*********************' +else +echo '*********************' +echo best dns server is `sort -rn $DATABASE_PATH| head -1| cut -d'/' -f3` +echo '*********************' +fi +loggin >> /var/log/best403unlocker.log +cat /etc/resolv.conf.bakup > /etc/resolv.conf +rm /etc/resolv.conf.bakup diff --git a/main.sh b/best403unlocker-tui old mode 100755 new mode 100644 similarity index 62% rename from main.sh rename to best403unlocker-tui index 8c88def..b75222c --- a/main.sh +++ b/best403unlocker-tui @@ -1,5 +1,6 @@ -#!/bin/bash -source best403unlocker.conf +#!/usr/bin/env bash +CONFIG_FILE="/etc/best403unlocker.conf" +LOG_FILE="/var/log/best403unlocker-tui.log" # Function to display the main menu main_menu() { @@ -13,13 +14,13 @@ main_menu() { case "$choice" in "Run DNS analyzer") - best_dns_finder + best_dns_finder ;; "Save file") download_file_with_best_dns ;; "Advance setting") - disconnect_network + change_settings ;; "Exit") exit @@ -38,8 +39,8 @@ best_dns_finder() { # Replace the value of file_url with the value of the file_url environment variable - if grep -q "^file_url=" "best403unlocker.conf" ; then - sed -i "s|^file_url=.*|file_url=$file_url|" best403unlocker.conf + if grep -q "^file_url=" "$CONFIG_FILE" ; then + sed -i "s|^file_url=.*|file_url=$file_url|" $CONFIG_FILE fi choices=$(whiptail --title "choose engine otherwise it runs on system" --checklist "Choose options:" 15 60 1 \ @@ -49,14 +50,14 @@ best_dns_finder() { selected_options=($(echo $choices | tr -d '"')) # Check if "docker" is in the selected options if [[ " ${selected_options[@]} " =~ " docker " ]]; then - docker run --env-file best403unlocker.conf armantaherighaletaki/best403unlocker 2>&1 | tee log.txt + docker run --env-file $CONFIG_FILE armantaherighaletaki/best403unlocker 2>&1 | tee $LOG_FILE status=$? - if [ $status -eq 0 ] && grep -q permission log.txt; then + if [ $status -eq 0 ] && grep -q permission $LOG_FILE; then password_checker - echo "$password" | sudo -S docker run --env-file best403unlocker.conf armantaherighaletaki/best403unlocker | tee log.txt 2>&1 - elif [ $status -eq 0 ]; then - whiptail --title "Error" --yesno "An error occurred. See log.txt for more info.\nDo you want to try again?" 15 60 + echo "$password" | sudo -S docker run --env-file $CONFIG_FILE armantaherighaletaki/best403unlocker | tee $LOG_FILE 2>&1 + elif [ $status -nq 0 ]; then + whiptail --title "Error" --yesno "An error occurred. See $LOG_FILE for more info.\nDo you want to try again?" 15 60 status=$? if [ $status -eq 0 ]; then best_dns_finder @@ -64,19 +65,19 @@ best_dns_finder() { main fi fi - + else password_checker - wget -c https://raw.githubusercontent.com/ArmanTaheriGhaleTaki/best403unlocker/main/bash.sh && echo $password | sudo -S bash bash.sh | tee log.txt 2>&1 + best403unlocker | tee $LOG_FILE fi - DNS=$(grep best log.txt| cut -d' ' -f5 ) + DNS=$(grep best $LOG_FILE| cut -d' ' -f5 ) whiptail --title "DNS analyzer" --msgbox "Best DNS:\n$DNS" 15 60 selected_options=($(echo $choices | tr -d '"')) if whiptail --title "Confirmation" --yesno "set DNS to system" 10 60 ;then password_checker - echo $password | sudo -S bash -c "echo 'nameserver $DNS' > /run/systemd/resolve/stub-resolv.conf" + echo $password | sudo -S bash -c "echo 'nameserver $DNS' > /etc/resolv.conf" fi } @@ -90,8 +91,8 @@ download_file_with_best_dns() { # Replace the value of file_url with the value of the file_url environment variable - if grep -q "^file_url=" "best403unlocker.conf" ; then - sed -i "s|^file_url=.*|file_url=$file_url|" best403unlocker.conf + if grep -q "^file_url=" "$CONFIG_FILE" ; then + sed -i "s|^file_url=.*|file_url=$file_url|" $CONFIG_FILE fi save_filepath=$(echo "$file_url" | grep -o '[^/]*$') save_filepath=$HOME/Downloads/$save_filepath @@ -99,7 +100,7 @@ download_file_with_best_dns() { if [ $? -eq 1 ]; then main - fi + fi choices=$(whiptail --title "choose engine otherwise it runs on system" --checklist "Choose options:" 15 60 1 \ "docker" "(suggested)" ON \ @@ -108,28 +109,36 @@ download_file_with_best_dns() { selected_options=($(echo $choices | tr -d '"')) # Check if "docker" is in the selected options if [[ " ${selected_options[@]} " =~ " docker " ]]; then - docker run --env-file best403unlocker.conf armantaherighaletaki/best403unlocker 2>&1 | tee log.txt + docker run --env-file $CONFIG_FILE armantaherighaletaki/best403unlocker 2>&1 | tee $LOG_FILE status=$? - - if [ $status -eq 0 ] && grep -q permission log.txt; then + + if [ $status -eq 0 ] && grep -q permission $LOG_FILE; then password_checker - echo "$password" | sudo -S docker run --env-file best403unlocker.conf armantaherighaletaki/best403unlocker | tee log.txt 2>&1 + echo "$password" | sudo -S docker run --env-file $CONFIG_FILE armantaherighaletaki/best403unlocker | tee $LOG_FILE 2>&1 fi - + else password_checker - wget -c https://raw.githubusercontent.com/ArmanTaheriGhaleTaki/best403unlocker/main/bash.sh && echo $password | sudo -S bash bash.sh | tee log.txt 2>&1 + best403unlocker | tee $LOG_FILE + fi - DNS=$(grep best log.txt| cut -d' ' -f5 ) + DNS=$(grep best $LOG_FILE| cut -d' ' -f5 ) password_checker - echo "$password" | sudo -S bash -c "echo 'nameserver $DNS' > /run/systemd/resolve/stub-resolv.conf" - wget --no-dns-cache $file_url -O $save_filepath - echo "$password" | sudo -S bash -c "cp /etc/resolv.conf.bakup /run/systemd/resolve/stub-resolv.conf" + echo "$password" | sudo -S bash -c "cp /etc/resolv.conf /etc/resolv.conf.bakup" + echo "$password" | sudo -S bash -c "echo 'nameserver $DNS' > /etc/resolv.conf" + wget --no-dns-cache $file_url -O $save_filepath 2>&1 | tee $LOG_FILE + if grep -q "No such file or directory" $LOG_FILE ; then + whiptail --title "Error" --msgbox "An error occurred. See $LOG_FILE for more info." 15 60 + else + whiptail --title "Download Complete" --msgbox "The download has been completed successfully!" 15 60 + fi + echo "$password" | sudo -S bash -c "mv /etc/resolv.conf.bakup /etc/resolv.conf" + } -disconnect_network() { - echo hello +change_settings() { + echo 'hello' } # Checks the password if a sudo command is executed @@ -151,8 +160,23 @@ password_checker(){ fi } +check_and_source_env() { + if [ ! -f $CONFIG_FILE ]; then + wget -cq https://raw.githubusercontent.com/ArmanTaheriGhaleTaki/best403unlocker/main/.env -O $CONFIG_FILE + fi +source $CONFIG_FILE +} +check_run_with_root_accsses(){ +if [ "$EUID" -ne 0 ]; then + echo "Please run this script as root (sudo)" + exit 1 +fi +} # Main function main() { + + check_run_with_root_accsses + check_and_source_env while true; do main_menu done @@ -160,4 +184,3 @@ main() { # Main execution main -