-
Notifications
You must be signed in to change notification settings - Fork 3
/
zenmode.sh
executable file
·209 lines (203 loc) · 4.54 KB
/
zenmode.sh
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
#!/bin/bash
target=""
add="127.0.0.1 "
addv6="fe80::1%lo0 "
scriptname=$(readlink "$0")
base="$(cd -P "$(dirname "$scriptname")" && pwd)"
stop_zen() {
echo "Stopping zen mode ..."
target=$(read_file /etc/.hosts "\n")
sudo rm /etc/.hosts
aux=$(echo -e $target | sudo tee /etc/hosts)
}
start_zen() {
echo "Starting zen mode ..."
script="$base/denials"
#add="$add$(crawl $script) "
#addv6="$addv6$(crawl $script) "
target=$(read_file /etc/hosts "\n")
set1=$(construct_series $script $add)
set2=$(construct_series $script $addv6)
aux=$(echo -e $target | sudo tee /etc/.hosts)
target="$target\n$set1\n$set2"
aux=$(echo -e $target | sudo tee /etc/hosts)
}
construct_series() {
domains=$(crawl $1)
prefix=$2
ret=""
for domain in $domains; do
ret="$ret $prefix $domain\n"
done
echo $ret
}
read_file() {
c=""
while read line
do
c="$c$line$2"
done < $1
l=$((${#c} - 2))
echo ${c:0:l}
}
crawl() {
script=$1
add=""
if [ -a $script ]; then
script="$script/*"
for file in $script
do
add="$add www.${file##*\/} ${file##*\/}"
done
fi
echo $add
}
watch() {
echo "Started watching..."
files=$(crawl "$base/watches")
ison=0
canbeon=0
while [ ${#files} -gt 0 ]; do
aux=$(ps aux)
canbeon=0
for watch in $files
do
if [ $(echo $aux | grep -c $watch) -gt 0 ]; then
echo "Found $watch running"
if [ $ison -eq 0 ]; then
start_zen
ison=1
fi
canbeon=1
fi
done
if [ $ison -eq 1 ] && [ $canbeon -eq 0 ]; then
stop_zen
ison=0
fi
sleep 1;
done
}
clean(){
if [[ -a /etc/.hosts ]]; then
stop_zen
fi
}
case "$1" in
start|-s)
if [[ -a /etc/.hosts ]]; then
echo "Zen mode already started"
else
start_zen
fi
;;
stop|-k)
if [[ -a /etc/.hosts ]]; then
stop_zen
else
echo "Zen mode already stopped"
fi
;;
restart|reload|-r)
[[ -a /etc/.hosts ]] && stop_zen
start_zen
;;
allow|-a)
if [ -a "$base/denials/$2" ]; then
rm "$base/denials/$2"
else
echo "Website not denied!"
exit 1
fi
[[ -a /etc/.hosts ]] && stop_zen && start_zen
;;
deny|-d)
if [ -a "$base/denials/$2" ]; then
echo "Website already denied!"
exit 1
else
touch "$base/denials/$2"
fi
[[ -a /etc/.hosts ]] && stop_zen && start_zen
;;
list|-l)
list="\n"$(crawl "$base/denials")
list=${list//\ /"\n"}"\n"
echo -e $list
;;
install)
target="/usr/local/bin"
if [ -a "$target/zm" ] || [ -h "$target/zm" ];
then
sudo rm "$target/zm"
fi
if [ -a "$target/zenmode" ] || [ -h "$target/zenmode" ]; then
sudo rm "$target/zenmode"
fi
if [ "$2" = "--short" ];
then
target="$target/zm"
else
target="$target/zenmode"
fi
file=$(readlink "$0")
if [ "$scriptname" == "" ]; then
file=$0
else
file=$scriptname
fi
file=${file##*/}
sudo ln -s "$base/$file" $target
;;
watch|-w)
clean
watch
;;
addwatch|addWatch|-aw)
if [ -a "$base/watches/$2" ]; then
echo "Watch already listed!"
exit 1
else
touch "$base/watches/$2"
fi
;;
removewatch|removeWatch|-rw)
if [ -a "$base/watches/$2" ]; then
rm "$base/watches/$2"
else
echo "Watch does not exist!"
exit 1
fi
;;
listwatches|listWatches|-lw)
files=$(crawl "$base/watches")
echo
for watch in $files
do
echo $watch
done
echo
;;
clean|-c)
clean
;;
*)
echo -e "Usage: \033[1;31mzenmode \033[1;34m[options]\033[0m"
echo
echo -e "\033[1;34mOptions\033[0m available : "
echo -e "\033[1;32mstart \033[1;36m(-s)\033[0m : Start Zen Mode"
echo -e "\033[1;32mstop \033[1;36m(-k)\033[0m : Stop Zen Mode"
echo -e "\033[1;32mrestart \033[1;36m(-r)\033[1;32m / reload \033[0m: Restart Zen Mode"
echo -e "\033[1;32mallow \033[1;36m(-a) \033[1;31m<website>\033[0m : Enable access to the specified website domain"
echo -e "\033[1;32mdeny \033[1;36m(-d) \033[1;31m<website>\033[0m : Deny access to the specified website domain"
echo -e "\033[1;32mlist \033[1;36m(-l)\033[0m : Print the denial list"
echo -e "\033[1;32minstall \033[1;36m(--short)\033[0m : Install ZenMode (--short installs under the name 'zm')"
echo -e "\033[1;32mwatch \033[1;36m(-w)\033[0m : Watch for applications runnig and deny access if active."
echo -e "\033[1;32maddwatch \033[1;36m(addWatch|-aw)\033[0m : Add a new application to the watch list."
echo -e "\033[1;32mremovewatch \033[1;36m(removeWatch|-rw)\033[0m : Remove the application to the watch list."
echo -e "\033[1;32mlistwatches \033[1;36m(listWatches|-lw)\033[0m : Print the watch list."
echo -e "\033[1;32mclean \033[1;36m(-c)\033[0m : Clean the cache."
echo
exit 1
;;
esac