Skip to content

Commit

Permalink
Restore to cloudflare if no backup found
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir Rezazadeh authored and Amir Rezazadeh committed May 7, 2021
1 parent 9f65dfd commit e5f7070
Showing 1 changed file with 49 additions and 6 deletions.
55 changes: 49 additions & 6 deletions shecanman
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
# If you have found any issue or have some feature request:
# Please raise them here: https://github.com/amirz98/shecanman/issues

VRESION="0.2.0"
VRESION="0.3.0"

SHEACN_DNS_ARRAY=("178.22.122.100" "185.51.200.2")

SHECAN_DNS="# Shecan dns server
nameserver 178.22.122.100
nameserver 185.51.200.2"


# check if stdout is a terminal
if test -t 1; then
# see if it supports colors
Expand All @@ -25,6 +24,7 @@ if test -t 1; then
normal="$(tput sgr0)"
red="$(tput setaf 1)"
green="$(tput setaf 2)"
yellow="$(tput setaf 3)"
fi
fi

Expand Down Expand Up @@ -59,6 +59,7 @@ function exit_if_not_root() {
}

function is_shecan_on() {
# read dns config file line by line
while read -r line; do
# ignore the line if does not begin with nameserver
[[ ! $line =~ ^nameserver.* ]] && continue
Expand All @@ -81,7 +82,7 @@ function switch_on() {
else
exit_if_not_root
cp /etc/resolv.conf /etc/resolv.conf.backup
echo "$SHECAN_DNS" >/etc/resolv.conf
echo -e "$SHECAN_DNS" >/etc/resolv.conf
echo "${bold}${green} Shecan DNS switched on.${normal}"
fi
}
Expand All @@ -91,9 +92,51 @@ function switch_off() {
echo "${bold}${green} Shecan is not in use.${normal}"
else
exit_if_not_root
cp /etc/resolv.conf.backup /etc/resolv.conf
rm /etc/resolv.conf.backup
echo "${bold}${green} Shecan DNS switched off.${normal}"
# restore backup configs if exists
if [[ -f /etc/resolv.conf.backup ]]; then
cp /etc/resolv.conf.backup /etc/resolv.conf
rm /etc/resolv.conf.backup
echo "${bold}${green} Shecan DNS switched off.${normal}"
else
# dns config backup file not found; this happens only when the user changes the dns configs manually
# restruct a valid dns config based on current dns config file
new_config="# Restored by ShecanMan\n"
non_shecan_dns_count=0
# read dns config file line by line, keep dns servers that are not for Shecan.ir
while read -r line; do
# ignore the line if does not begin with nameserver
[[ ! $line =~ ^nameserver.* ]] && continue

is_shecan_dns=1
for shecan in "${SHEACN_DNS_ARRAY[@]}"; do
if [[ "$line" =~ .*"$shecan".* ]]; then
# found a non-shecan dns
is_shecan_dns=0
break
fi
done

# keep the dns if it's not a shecan dns
if [[ $is_shecan_dns == 1 ]]; then
new_config+="$line\n"
non_shecan_dns_count=$((non_shecan_dns_count + 1))
fi
done </etc/resolv.conf

# if no non-shecan dns found, use Cloudflare dns server as a single dns entry
if [[ $non_shecan_dns_count == 0 ]]; then
new_config+="nameserver 1.1.1.1"
fi

# save the configs
echo -e $new_config >/etc/resolv.conf

if [[ $non_shecan_dns_count == 0 ]]; then
echo "${bold}${yellow} Backup DNS not found! Notice that your DNS is now set to Cloudflare (1.1.1.1).${normal}"
fi

echo "${bold}${green} Shecan DNS switched off.${normal}"
fi
fi
}

Expand Down

0 comments on commit e5f7070

Please sign in to comment.