Skip to content

Instantly share code, notes, and snippets.

@Tristor
Created December 15, 2020 23:49
Show Gist options
  • Save Tristor/78be4d7093af7aebc2e37a68407d773a to your computer and use it in GitHub Desktop.
Save Tristor/78be4d7093af7aebc2e37a68407d773a to your computer and use it in GitHub Desktop.
Force Pi-Hole SafeSearch
# This DNSMasq configuration forces restrictions to be enabled for YouTube, Bing, and Google Search
# for more information see: https://learn.akamai.com/en-us/webhelp/enterprise-threat-protector/enterprise-threat-protector/GUID-0FCB1221-60DC-4A01-9B09-0C25B16BF3D0.html
# or see: https://support.google.com/youtube/answer/6214622?hl=en
# Put this file in /etc/dnsmasq.d/
## YouTube
cname=www.youtube.com,restrictmoderate.youtube.com
cname=m.youtube.com,restrictmoderate.youtube.com
cname=youtube.googleapis.com,restrictmoderate.youtube.com
cname=youtubei.googleapis.com,restrictmoderate.youtube.com
cname=www.youtube-nocookie.com,restrictmoderate.youtube.com
## Bing
cname=www.bing.com,strict.bing.com
## Google Search
cname=www.google.com,forcesafesearch.google.com
# Append this to /etc/hosts
216.239.38.120 restrict.youtube.com
216.239.38.119 restrictmoderate.youtube.com
204.79.197.220 strict.bing.com
216.239.38.120 forcesafesearch.google.com
#!/usr/bin/env bash
# Put this in /usr/local/bin/ and chmod +x
update_ip_address() {
file=/etc/hosts
if [[ $(grep $1 $file) ]]; then
IP=$(dig +noall +answer +short @8.8.8.8 $1 | \
grep -oE '((1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.){3}((1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5]))')
sed -i "/$1/ s/.*/$IP\t$1/g" $file
fi
}
update_ip_address restrict.youtube.com
update_ip_address restrictmoderate.youtube.com
update_ip_address strict.bing.com
update_ip_address forcesafesearch.google.com
@mhanstead
Copy link

mhanstead commented Mar 31, 2021

This update_hosts script has stopped working for me. I get an error that says "update-hosts.sh: 5: update-hosts.sh: [[: not found" for each attempted update. It seems to have something to do with the grep command. I have tested the dig on its own and it seems to work, but get nothing back from the grep. That command is beyond me.

@Tristor
Copy link
Author

Tristor commented Mar 31, 2021

I don't have my Pi-Hole running anymore, I've been doing something with unbound instead. I'll need to set up a test environment to figure out what the issue is. May be a week or so before I can get around to that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment