-
Notifications
You must be signed in to change notification settings - Fork 66
/
maza
executable file
·227 lines (197 loc) · 5.7 KB
/
maza
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
#!/usr/bin/env bash
# START
set -e
# VARIABLES
URL_DNS_LIST="https://pgl.yoyo.org/adservers/serverlist.php?showintro=0&mimetype=plaintext"
URL_DNS_LIST_CUSTOM=""
NAME_OSX="Darwin"
THIS_OS=$(uname -mrs)
PROGNAME=$(basename "$0")
[[ -z "${XDG_CONFIG_HOME}" ]] && CONFIG=$HOME/.config/maza/ || CONFIG=$XDG_CONFIG_HOME/maza/
HOST_FILE=/etc/hosts
COLOR_RED=$(tput setaf 1)
COLOR_GREEN=$(tput setaf 2)
COLOR_RESET=$(tput sgr0)
LIST="list"
LIST_DNSMASQ="dnsmasq.conf"
CUSTOM_DOMAINS="custom-domains"
IGNORE_LIST_DEFAULT="localhost \
localhost.localdomain \
local \
broadcasthost \
ip6-localhost \
ip6-loopback \
ip6-localnet \
ip6-mcastprefix \
ip6-allnodes \
ip6-allrouters \
ip6-allhosts \
0.0.0.0"
IGNORE_LIST_FILE="ignore"
START_TAG="## MAZA - List ad blocking"
PROJECT="### https://github.com/tanrax/maza-ad-blocking"
AUTHOR="### Created by Andros Fenollosa (https://andros.dev/)"
END_TAG="## END MAZA"
# Create sed cross system
custom-sed() {
if [[ $THIS_OS = *$NAME_OSX* ]]; then
# Check if OSX and install GSED
if [ -x "$(command -v gsed)" ]; then
gsed "$@"
else
echo "${COLOR_RED}ERROR. You must install gsed if you are using OSX${COLOR_RESET}"
exit 1
fi
else
# Linux
sed "$@"
fi
}
export -f custom-sed
# FUNCTIONS
## HELP
usage() {
if [ "$*" != "" ] ; then
echo "Error: $*"
fi
cat << EOF
Usage: $PROGNAME [OPTION]
Simple and efficient local ad blocking throughout the network.
Options:
status Check if it's active or not
update Update the list of DNS to be blocked
start Activate blocking DNS.
stop Stop blocking DNS.
--help Display this usage message and exit
EOF
exit 1
}
status() {
if grep -qF "$START_TAG" "$HOST_FILE";then
echo "${COLOR_GREEN}ENABLED${COLOR_RESET}"
else
echo "${COLOR_RED}DISABLED${COLOR_RESET}"
fi
}
update() {
local local_url_dns_list="$URL_DNS_LIST" # Default
if [ -n "$URL_DNS_LIST_CUSTOM" ]; then
echo "si"
local_url_dns_list="$URL_DNS_LIST_CUSTOM" # Custom
fi
# Make conf folder
rm -f "$CONFIG$LIST"
rm -f "$CONFIG$LIST_DNSMASQ"
mkdir -p "$CONFIG"
# Download DNS list
curl -L -s "$local_url_dns_list" -o "$CONFIG$LIST"
## Remove comments
### Start with #
custom-sed -i.bak '/^#/ d' "$CONFIG$LIST"
### Remove comments in middle of line
custom-sed -i.bak 's/#.*$//g' "$CONFIG$LIST"
### Remove spaces in end of line
custom-sed -i.bak 's/ *$//g' "$CONFIG$LIST"
## Remove "0.0.0.0" or "127.0.0.1"
custom-sed -i.bak 's/0.0.0.0 //g' "$CONFIG$LIST"
custom-sed -i.bak 's/127.0.0.1 //g' "$CONFIG$LIST"
# Make ignore list
if [ ! -f "$CONFIG$IGNORE_LIST_FILE" ]; then
echo "$IGNORE_LIST_DEFAULT" | tr " " "\n" > "$CONFIG$IGNORE_LIST_FILE"
fi
# Remove ignore list
while read -r line; do
custom-sed -i.bak "/$line/d" "$CONFIG$LIST"
done < "$CONFIG$IGNORE_LIST_FILE"
# Make custom domains
## Check if file exists
if [ ! -f "$CONFIG$CUSTOM_DOMAINS" ]; then
## Create file
touch "$CONFIG$CUSTOM_DOMAINS"
fi
## Include custom domains
cat "$CONFIG$CUSTOM_DOMAINS" >> "$CONFIG$LIST"
echo "" >> "$CONFIG$LIST"
# Remove empty lines
custom-sed -i.bak '/^$/d' "$CONFIG$LIST"
# Update HOST_FILE
## Remove old list
custom-sed -i "/$START_TAG/,/$END_TAG/d" "$HOST_FILE"
## Add List to host file.
# Add start tag DNS list in first line
echo "$START_TAG" >> "$HOST_FILE"
echo "$PROJECT" >> "$HOST_FILE"
echo "$AUTHOR" >> "$HOST_FILE"
# Transform "mydomain.com" to "127.0.0.1 mydomain.com" except comments
custom-sed "/^#/!s/^/127.0.0.1 /g" "$CONFIG$LIST" >> "$HOST_FILE"
# Add end tag DNS list in first line
echo "$END_TAG" >> "$HOST_FILE"
# Make dnsmasq format
cp "$CONFIG$LIST" "$CONFIG$LIST_DNSMASQ"
## Remove triple "-". Example: "my---domain.com" to "my-domain.com". Only in dnsmasq.
custom-sed -i.bak "s/---/-/g" "$CONFIG$LIST_DNSMASQ"
## Remove double "-". Example: "my--domain.com" to "my-domain.com". Only in dnsmasq.
custom-sed -i.bak "s/--/-/g" "$CONFIG$LIST_DNSMASQ"
## mydomain.com to address=/mydomain.com/127.0.0.1
custom-sed -i.bak "s|\(.*\)|address=/\1/127.0.0.1|" "$CONFIG$LIST_DNSMASQ"
## Add start tag DNS list in first line
custom-sed -i.bak "1i\\$AUTHOR" "$CONFIG$LIST"
custom-sed -i.bak "1i\\$PROJECT" "$CONFIG$LIST"
custom-sed -i.bak "1i\\$START_TAG" "$CONFIG$LIST"
## Add end tag DNS list in first line
echo "$END_TAG" >> "$CONFIG$LIST"
## Add start tag DNS dnsmasq in first line
custom-sed -i.bak "1i\\$AUTHOR" "$CONFIG$LIST_DNSMASQ"
custom-sed -i.bak "1i\\$PROJECT" "$CONFIG$LIST_DNSMASQ"
custom-sed -i.bak "1i\\$START_TAG" "$CONFIG$LIST_DNSMASQ"
## Add end tag DNS DNSMASQ in first line
echo "$END_TAG" >> "$CONFIG$LIST_DNSMASQ"
# Remove temp file
rm "$CONFIG$LIST.bak"
rm "$CONFIG$LIST_DNSMASQ.bak"
# Notify user
echo "${COLOR_GREEN}List updated!${COLOR_RESET}"
}
start() {
update
# Notify user
echo "${COLOR_GREEN}ENABLED!${COLOR_RESET}"
}
stop() {
# Remove list to host file
custom-sed -i "/$START_TAG/,/$END_TAG/d" "$HOST_FILE"
# Remove DNSMASQ
cat /dev/null > "$CONFIG$LIST_DNSMASQ"
# Notify user
echo "${COLOR_GREEN}DISABLED!${COLOR_RESET}"
}
# CONTROLE ARGUMENTS
isArg=""
while [ $# -gt 0 ] ; do
case "$1" in
--help)
usage
;;
status)
isArg="1"
status
;;
update)
isArg="1"
update
;;
start)
isArg="1"
start
;;
stop)
isArg="1"
stop
;;
*)
esac
shift
done
if [ -z $isArg ] ; then
usage "Not enough arguments"
fi