forked from rezdh/shecanman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shecanman
executable file
·234 lines (204 loc) · 6.88 KB
/
shecanman
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/bin/bash
# Author: Amir Rezazadeh (@amirz98)
# Project: ShecanMan (https://github.com/amirz98/shecanman)
#
# If you have found any issue or have some feature request:
# Please raise them here: https://github.com/amirz98/shecanman/issues
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"
SHECAN_DNS_NETWORKMANAGER="# Shecan dns server
[global-dns-domain-*]
servers=178.22.122.100,185.51.200.2"
# check if stdout is a terminal
if test -t 1; then
# see if it supports colors
ncolors=$(tput colors)
if test -n "$ncolors" && test $ncolors -ge 8; then
bold="$(tput bold)"
normal="$(tput sgr0)"
red="$(tput setaf 1)"
green="$(tput setaf 2)"
yellow="$(tput setaf 3)"
fi
fi
# Check if NetworkManager is running
if pgrep "NetworkManager" >/dev/null; then
echo "using NetworkManager"
networkmanager_running=true
# store path to dns files since they are used many times
dns_file="/etc/NetworkManager/conf.d/dns-servers.conf"
dns_file_backup="/etc/NetworkManager/conf.d/dns-servers.conf.backup"
else
networkmanager_running=false
fi
function show_usage() {
echo -e "
╭━━━┳╮╱╱╱╱╱╱╱╱╱╱╱╱╱╭━╮╭━╮
┃╭━╮┃┃╱╱╱╱╱╱╱╱╱╱╱╱╱┃┃╰╯┃┃
┃╰━━┫╰━┳━━┳━━┳━━┳━╮┃╭╮╭╮┣━━┳━╮
╰━━╮┃╭╮┃┃━┫╭━┫╭╮┃╭╮┫┃┃┃┃┃╭╮┃╭╮╮
┃╰━╯┃┃┃┃┃━┫╰━┫╭╮┃┃┃┃┃┃┃┃┃╭╮┃┃┃┃
╰━━━┻╯╰┻━━┻━━┻╯╰┻╯╰┻╯╰╯╰┻╯╰┻╯╰╯
ShecanMan lets you switch on/off Shecan.ir DNS configs temporarily on linux.
Usage: ${bold}shecanman [${green}command${normal}]
Commands:
${bold}${green} on \t${normal}Switch on Shecan DNS configs; keeps a backup of current configs
${bold}${green} off \t${normal}Switch off Shecan DNS; This will restore previous DNS configs
${bold}${green} status \t${normal}Show current status; Check whether ShecanMan is on
${bold}${green} install \t${normal}Install ShecanMan system-wide, so you can call it everywhere
${bold}${green} uninstall \t${normal}Uninstall ShecanMan, so it's no longer accessible system-wide
${bold}${green} version \t${normal}Show ShecanMan version
${bold}${green} help \t${normal}Show this help"
}
function exit_if_not_root() {
if [[ $EUID -ne 0 ]]; then
echo "${bold}${red} Permission denied. Please run as root.${normal}"
exit 1
fi
}
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
for shecan in "${SHEACN_DNS_ARRAY[@]}"; do
if [[ "$line" =~ .*"$shecan".* ]]; then
# found a valid shecan dns
return 0
fi
done
# only first valid line matters, so ignore others
break
done </etc/resolv.conf
return 1
}
function switch_on() {
if is_shecan_on; then
echo "${bold}${green} Shecan is already in use.${normal}"
return
fi
exit_if_not_root
if $networkmanager_running; then
# Check if dns file exists
if [[ -f $dns_file ]]; then
cp $dns_file $dns_file_backup
fi
echo -e "$SHECAN_DNS_NETWORKMANAGER" >$dns_file
systemctl restart NetworkManager
else
cp /etc/resolv.conf /etc/resolv.conf.backup
echo -e "$SHECAN_DNS" >/etc/resolv.conf
fi
echo "${bold}${green} Shecan DNS switched on.${normal}"
}
function switch_off() {
if ! is_shecan_on; then
echo "${bold}${green} Shecan is not in use.${normal}"
return
fi
exit_if_not_root
if $networkmanager_running; then
# restore backup configs if exists
if [[ -f $dns_file_backup ]]; then
cp $dns_file_backup $dns_file
rm $dns_file_backup
systemctl restart NetworkManager
echo "${bold}${green} Shecan DNS switched off.${normal}"
else
# Check if dns-servers.conf is using shecan
if [[ $(head -n 1 $dns_file) == "# Shecan"* ]]; then
rm $dns_file
echo "${bold}${green} Shecan DNS switched off.${normal}"
else
echo "backup file not found you can verify entries in /etc/NetworkManager/conf.d/dns-servers.conf"
fi
fi
systemctl restart NetworkManager
return
fi
# 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}"
return
fi
# 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}"
}
function show_status() {
if is_shecan_on; then
echo "${bold}${green} Shecan is in use.${normal}"
else
echo "${bold}${green} Shecan is not in use.${normal}"
fi
}
function install() {
exit_if_not_root
chmod +x shecanman
mkdir -p /usr/local/bin
cp shecanman /usr/local/bin/
cp shecanman /usr/sbin/
echo "${bold}${green} Congratulations! shecanman now is accessible everywhere.${normal}"
if ! is_shecan_on; then
echo "${bold}${green} Use [shecanman on] to switch on Shecan.ir DNS configs.${normal}"
fi
}
function uninstall() {
exit_if_not_root
if is_shecan_on; then
switch_off
fi
if [ -f /usr/local/bin/shecanman ]; then
rm /usr/local/bin/shecanman
fi
echo "${bold}${green} shecanman uninstalled successfully.${normal}"
}
function show_version() {
echo "${bold}${green} ShecanMan v$VRESION ${normal}"
}
function main() {
case "$1" in
"on") switch_on ;;
"off") switch_off ;;
"status") show_status ;;
"install") install ;;
"uninstall") uninstall ;;
"version") show_version ;;
*) show_usage ;;
esac
}
main "$@"