forked from jakenology/PSS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPi-hole_SafeSearch.sh
271 lines (244 loc) · 6.48 KB
/
Pi-hole_SafeSearch.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/bin/bash
# SafeSearch List Generator for Pi-hole 4.0 and above
# Created by JakeNology
## Define Global Variables
## ENABLE IN PIHOLE?
YOUTUBE=True
me=`basename "$0"`
VERSION="1.6.1" # Fixed IP Address for Duckduckgo. Added SafeSearch for pixabay..., also fixed spelling error
file="/tmp/safesearch.txt"
conf="/etc/dnsmasq.d/05-restrict.conf"
url="https://www.google.com/supported_domains"
## Logging Variables
log="/var/log/${me}.log"
maxRuns=10
## Arrays
# Host Records!!!
hostRecords=(
"host-record=forcesafesearch.google.com,216.239.38.120"
"host-record=safe.duckduckgo.com,52.142.126.100"
"host-record=restrict.youtube.com,216.239.38.120"
"host-record=restrictmoderate.youtube.com,216.239.38.119"
"host-record=strict.bing.com,204.79.197.220"
"host-record=safesearch.pixabay.com,176.9.158.70"
)
ytSS=(
"cname=www.youtube.com,restrictmoderate.youtube.com"
"cname=m.youtube.com,restrictmoderate.youtube.com"
"cname=youtubei.googleapis.com,restrictmoderate.youtube.com"
"cname=youtube.googleapis.com,restrictmoderate.youtube.com"
"cname=www.youtube-nocookie.com,restrictmoderate.youtube.com"
)
bingSS=(
"cname=bing.com,www.bing.com,strict.bing.com"
)
badEXACT=(
"www.ecosia.org"
"images.search.yahoo.com"
"video.search.yahoo.com"
"search.aol.com"
"gibiru.com"
"www.startpage.com"
)
duckduckgoSS=(
"cname=duckduckgo.com,www.duckduckgo.com,start.duckduckgo.com,safe.duckduckgo.com"
"cname=duck.com,www.duck.com,safe.duckduckgo.com"
)
pixabaySS=(
"cname=pixabay.com,safesearch.pixabay.com"
)
REGEX=(
"(^|\.).+xxx$"
"(^|\.).+sexy$"
"(^|\.).+webcam$"
"(^|\.).+sex$"
"(^|\.).+porn$"
"(^|\.).+tube$"
"(^|\.).+cam$"
"(^|\.).+adult$"
)
## Setup Logging
exec 2>>$log
logger() {
write() {
echo [`date '+%Y-%m-%d %H:%M:%S:%3N'`]: "$*" >> $log
}
print() {
echo [`date '+%Y-%m-%d %H:%M:%S:%3N'`]: "$*"
}
all() {
write "$*"
print "$*"
}
pass() {
echo "$*"
}
error() {
write "$*"
pass "$*"
}
begin() {
# Enforce Run Count
runNum=$(cat $log | grep 'STARTED' | wc -l)
if [ $runNum == $maxRuns ]; then
print FLUSHING LOG
rm -rf $log
fi
write STARTED
}
end() {
write STOPPED
# https://wtanaka.com/node/7719
end=$(cat $log|awk '{print length}'|sort -nr|head -1)
# https://stackoverflow.com/questions/5349718/how-can-i-repeat-a-character-in-bash
line=$(for ((i=1; i<=$end; i++)); do echo -n =; done)
pass $line >> $log
}
# Take Input
"$@"
}
## Check Sudo
if [ "$EUID" -ne 0 ];then
echo "Please run this script with sudo!"
exit 1
fi
## START LOGGING EVERYTHING
logger begin
silently() {
"$@" &>/dev/null
}
preCheck() {
# Check for sudo rights
checkSudo
# Is there an old file?
if [ -f "$file" ]; then
logger all Removing "$file"
rm "$file"
fi
}
generate() {
# Download List into an Array
logger all Retrieving List from Google
domains=($(curl $url 2>/dev/null))
# Append File Header
echo "# $file generated on $(date '+%m/%d/%Y %H:%M') by $(hostname)" >> "${file}"
echo "# Google SafeSearch Implementation" >> "${file}"
# Add host records
for line in "${hostRecords[@]}"; do
echo "$line" >> "${file}"
done
# Generate list of domains
for domain in "${domains[@]}"; do
dom=$(echo $domain | cut -c 2-)
echo cname=$dom,"www""$domain",forcesafesearch.google.com >> "${file}"
done
# Get the number of domains
count=$(cat $file | grep 'forcesafesearch.google.com' | wc -l)
total=$(($count * 2))
logger all ''$count' TLDs'
logger all ''$total' Domains'
# YouTube SafeSearch
if [ "$YOUTUBE" == "True" ]; then
for line in "${ytSS[@]}"
do echo "$line" >> "${file}"
done
fi
# DuckDuckGo SafeSearch
for line in "${duckduckgoSS[@]}"
do echo "$line" >> "${file}"
done
# Bing Strict Setting
for line in "${bingSS[@]}"
do echo "$line" >> "${file}"
done
# Pixabay
for line in "${pixabaySS[@]}"
do echo "$line" >> "${file}"
done
# Enable In Hosts and Pi-hole
if [ "$ENABLE" == "True" ]; then
logger all 'ENABLING SAFESEARCH FOR PI-HOLE'
if [ -f "$conf" ]; then
rm -Rf "$conf"
cp -R "$file" "$conf"
else
cp -R "$file" "$conf"
fi
for host in "${ssHosts[@]}"; do
if ! grep -Fxq "$host" "$hosts"; then
echo "$host" >> "$hosts"
fi
done
# Extra Blocking
logger all 'BLOCKING OTHER BAD SITES'
silently pihole -b "${badEXACT[@]}"
silently pihole --regex "${REGEX[@]}"
# We do not need to do "pihole restartdns"
# The above commands reload it every time...
fi
}
main() {
preCheck
generate
}
quiet() {
silently main
}
web() {
silently main
logger pass $file
}
help() {
# Log Invalid Arguments
if [ ! -z "$*" ]; then
# https://linuxconfig.org/how-do-i-print-all-arguments-submitted-on-a-command-line-from-a-bash-script
args=("$@")
logger error INVALID ARGUMENT: "${args[@]}"
sleep 1
clear
fi
# Print Usage Information
clear
logger pass "$me version $version
Usage: $me [options]
Example: '$me --web'
-e, --enable Enable SafeSearch
-d, --disable Disable SafeSearch
-w, --web For use with PHP Script
-s, --silent Execute Script Silently
-v, --version Display the Script's Version
-h, --help Display this help message"
}
enable() {
ENABLE=True
main
}
disable() {
logger all 'Removing Temp File'
rm -rf "$file"
logger all 'Removing Config File'
rm -rf "$conf"
logger all 'Unblocking Domains and TLDs'
silently pihole regex --delmode "${REGEX[@]}"
silently pihole blacklist --delmode "${badEXACT[@]}"
logger all 'Restarting DNS'
silently pihole restartdns
logger all 'SafeSearch is Disabled!'
}
## Check for user input
if [[ $# = 0 ]]; then
main
else
logger write "ARGUMENTS: $1"
case "${1}" in
*e | *enable ) enable;;
*d | *disable) disable;;
*w | *web ) web;;
*s | *silent* ) quiet;;
*v | *version) echo -e 'Current Version:\t' "$VERSION";;
*h | *help ) help;;
* ) help "$@";;
esac
fi
## STOP LOGGING EVERYTHING
logger end