Parenting Control
Hello,
In the continuation of my post “Smart Home Mission” today I will share how I restrict certain websites at home – or in short how I do Parental Control.
What’s the whole idea?
Our children are on the tablet or phone all day. Apple has a built-in family sharing feature with which we can limit the hours in which the device can run, what applications to open and what to download.
However, I want to be able to restrict the opening of certain websites (such as TikTok or Facebook) and when I decide – quickly and easily with the push of a button or by calling “Hey Siri”
What do we need to do Parental Control?
First of all, we need experience with Linux and writing shell scripts. I know this is not for everyone, but I’m sure it can be done by people with a lot of free time and desire 🙂 (in case of difficulty you can write to me in the commanders below the article – I promise to help!)
- We need a working homebridge server to be able to use the Home app on our phone / tablet and Siri to be able to execute our commands.
- PiHole DNS server through which to make the appropriate restrictions on websites.
- Access to our router so that we can set the default DNS server to use our preconfigured and configured PiHole (in my case – 10.0.2.253).
- Minimum skills in writing shell scripts.
Installation and configuration of individual elements.
Here I will not go into details on how to install and configure homebridge and pihole. There is documentation and if you follow step by step you will succeed!
For me, the two separate services work on 2 separate raspberry pi devices, but they can easily live on one raspberry pi 4 with 4GB of memory.
I will give an example of how the scripts are made for both cases (working on two devices and on one device)
If homebridge and pihole work on one device you need:
- Let’s create two scripts for disabling and enabling access to TikTok and one script to check if TikTok is stopped or started:
We create folder called commands :
mkdir -p /var/lib/homebridge/commands/
and we create those scripts state-tiktok.sh, disable-tiktok.sh and enable-tiktok.sh
in disable-tiktok.sh we put this code :
#!/bin/bash sudo pihole -b --regex '(^|\.)muscdn\.com$' '(^|\.)musical\.ly$' '(^|\.)tiktok\.com$' '(^|\.)tiktokcdn\.com$' rm -f /var/lib/homebridge/commands/tiktok.flag
in enable-tiktok.sh we put this code :
#!/bin/bash sudo pihole --regex -d '(^|\.)muscdn\.com$' '(^|\.)musical\.ly$' '(^|\.)tiktok\.com$' '(^|\.)tiktokcdn\.com$' touch /var/lib/homebridge/commands/tiktok.flag
We need to create and delete the flags so that when we press the stop button or start TikTok, its status can be changed accordingly.
in state-tiktok.sh we put this code :
#!/bin/bash STATE_CHECK="$(ls /var/lib/homebridge/commands/tiktok.flag)" if [ -z "$STATE_CHECK" ]; then echo "false" else echo "true" fi
We make all three scripts executable :
chmod +x disable-tiktok.sh enable-tiktok.sh state-tiktok.sh
At this point we can test the script and if we run the stop script the TikTok should stop loading us 😀 If everything is ok we continue with the homebridge configuration.
If homebridge and pihole work on two devices it is necessary :
state-tiktok.sh is the same, but enable-tiktok.sh and disable-tiktok.sh are different :
enable-tiktok.sh looks like this :
#!/bin/bash #sudo pihole --regex -d '(^|\.)muscdn\.com$' '(^|\.)musical\.ly$' '(^|\.)tiktok\.com$' '(^|\.)tiktokcdn\.com$' touch /var/lib/homebridge/commands/tiktok.flag ssh -i /home/homebridge/.ssh/control pi@10.0.2.253 "sudo pihole --regex -d '(^|\.)muscdn\.com$' '(^|\.)musical\.ly$' '(^|\.)tiktok\.com$' '(^|\.)tiktokcdn\.com$' && touch /var/lib/homebridge/commands/tiktok.flag"
disable-tiktok.sh looks like this :
#!/bin/bash #sudo pihole -b --regex '(^|\.)muscdn\.com$' '(^|\.)musical\.ly$' '(^|\.)tiktok\.com$' '(^|\.)tiktokcdn\.com$' rm -f /var/lib/homebridge/commands/tiktok.flag ssh -i /home/homebridge/.ssh/control pi@10.0.2.253 "sudo pihole -b --regex '(^|\.)muscdn\.com$' '(^|\.)musical\.ly$' '(^|\.)tiktok\.com$' '(^|\.)tiktokcdn\.com$' && rm -f /home/homebridge/commands/tiktok.flag"
I use ssh and ssh keys for remote access.
10.0.2.253 is the IP address of our pihole server.
Homebridge
Before we start with the configuration itself, we need to install the Script2 script for homebridge using the command:
npm i homebridge-script2
We should open the homebridge configuration file with our favorite editor (in my case vim) and add the following lines:
vim /var/lib/homebridge/config.json ....... { "bridge": { "name": "Homebridge control", "username": "0E:DF:E6:88:42:DD", "port": 51403, "pin": "123-123-123", "advertiser": "ciao" }, "description": "Homebridge running on raspberry pi 4 - control", "accessories": [ { "accessory": "Script2", "name": "tiktok", "on": "/var/lib/homebridge/commands/enable-tiktok.sh", "off": "/var/lib/homebridge/commands/disable-tiktok.sh", "fileState": "/var/lib/homebridge/commands/tiktok.flag", "state": "/var/lib/homebridge/commands/state-tiktok.sh", "on_value": "true" },
We restart our homebridge with this command :
hb-service restart
The end result
If all goes well, we should already see a new accessory called “tiktok” that we can start or stop through the Home app on our iPhone or iPad. It also means that if we say Siri; Hey Siri, turn off tiktok – she will suspend access to this site.
I will give an example of how I limit both facebook and youtube – as with both sites this becomes easier because they do not use external CDN services like TikTok.
control:/var/lib/homebridge/commands# cat disable-facebook.sh #!/bin/bash #sudo pihole -b facebook.com #rm -f /var/lib/homebridge/commands/facebook.flag ssh -i /home/homebridge/.ssh/control pi@10.0.2.253 'sudo pihole -b facebook.com && rm -f /var/lib/homebridge/commands/facebook.flag' control:/var/lib/homebridge/commands# cat disable-youtube.sh #!/bin/bash #sudo pihole -b youtube.com rm -f /var/lib/homebridge/commands/youtube.flag ssh -i /home/homebridge/.ssh/control pi@10.0.2.253 'sudo pihole -b youtube.com && rm -f /var/lib/homebridge/commands/youtube.flag' control:/var/lib/homebridge/commands# cat enable-facebook.sh #!/bin/bash #sudo pihole -b -d facebook.com #touch /var/lib/homebridge/commands/facebook.flag ssh -i /home/homebridge/.ssh/control pi@10.0.2.253 'sudo pihole -b -d facebook.com && touch /var/lib/homebridge/commands/facebook.flag' control:/var/lib/homebridge/commands# cat enable-youtube.sh #!/bin/bash #sudo pihole -b -d youtube.com touch /var/lib/homebridge/commands/youtube.flag ssh -i /home/homebridge/.ssh/control pi@10.0.2.253 'sudo pihole -b -d youtube.com && touch /var/lib/homebridge/commands/youtube.flag'
That’s it!