Skip to content

Commit

Permalink
Feat/portability (#26)
Browse files Browse the repository at this point in the history
* chore(): Add editorconfig

* chore(): rename scripts to project's names

* feat(): Add configfile
feat(): Add log file
feat(): Make the script idempotent
fix(): Adding error for failing download in TUI mode
BREAKING CHANGE: script is now uses config file and only runs with root access

* fix(): make Dockerfile compatible with new changes

* chore(): change .env to best403unlocker.conf

BREAKING CHANGE: Add config file to /etc/ and Make the script idempotent .
  • Loading branch information
ArmanTaheriGhaleTaki authored Jul 1, 2024
1 parent d420dec commit 7426521
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 98 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -89,15 +89,15 @@ 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
wget -c https://raw.githubusercontent.com/ArmanTaheriGhaleTaki/best403unlocker/main/best403unlocker.conf && docker run --env-file best403unlocker.conf armantaherighaletaki/best403unlocker
```


## credit
## credit
thank [AKishmiish](https://github.com/Kishmiish) for adding TUI to this projcet
## Contact

Expand Down
61 changes: 0 additions & 61 deletions bash.sh

This file was deleted.

87 changes: 87 additions & 0 deletions best403unlocker
Original file line number Diff line number Diff line change
@@ -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
85 changes: 54 additions & 31 deletions main.sh → best403unlocker-tui
100755 → 100644
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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
Expand All @@ -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 \
Expand All @@ -49,34 +50,34 @@ 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
else
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
}

Expand All @@ -90,16 +91,16 @@ 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
save_filepath=$(whiptail --title "save file as " --inputbox "choose the location to save the file" 15 60 "$save_filepath" 3>&1 1>&2 2>&3)

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 \
Expand All @@ -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
Expand All @@ -151,13 +160,27 @@ 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
}

# Main execution
main

0 comments on commit 7426521

Please sign in to comment.