Skip to content

Instantly share code, notes, and snippets.

@hoyhoy
Created November 25, 2023 05:01
Show Gist options
  • Select an option

  • Save hoyhoy/52c6fc9f13b01c6d626c9cf74976dff2 to your computer and use it in GitHub Desktop.

Select an option

Save hoyhoy/52c6fc9f13b01c6d626c9cf74976dff2 to your computer and use it in GitHub Desktop.
macOS Launch Daemon to Automatically Update StevenBlack's Host List and Convert to Privoxy Format and Restart Privoxy
[Ad Hosts]
# /usr/local/sbhosts/adhosts2privoxy.conf
File=/etc/hosts
Keep=1
Type=hosts
Encoding=UTF-8
<?xml version="1.0" encoding="UTF-8"?>
<!-- /Library/LaunchDaemons/sb-update-hosts.plist -->
<!DOCTYPE plist PUBLIC "-/Apple/DTD PLIST 1.0/EN" "http:/www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>sb-update-hosts</string>
<key>StartInterval</key>
<integer>43200</integer>
<key>StandardErrorPath</key>
<string>/var/log/sbhosts/stderr.log</string>
<key>StandardOutPath</key>
<string>/var/log/sbhosts/stdout.log</string>
<key>RunAtLoad</key>
<true/>
<key>WorkingDirectory</key>
<string>/usr/local/sbhosts</string>
<key>UserName</key>
<string>root</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/sbhosts/bin/update_hosts.sh</string>
</array>
</dict>
</plist>
#various CLI setup...
sudo mkdir /var/log/sbhosts
sudo chown -R root:wheel /var/log/sbhosts
sudo mkdir -p /usr/local/sbhosts/bin
sudo chown -R root:wheel /usr/local/sbhosts
sudo chown root:wheel ~/bin/update_hosts.sh
cd /usr/local/sbhosts
sudo git clone https://github.com/StevenBlack/hosts.git
sudo wget https://raw.githubusercontent.com/lcferrum/adhosts2privoxy/master/adhosts2privoxy.py
# /Users/$USER/backup/myhosts is a list of local hostnames to resolve...
#!/bin/bash
# /usr/local/sbhosts/bin/update_hosts.sh
# must be run as root because updateHostsFile flushes the DNS cache
cd /usr/local/sbhosts
git fetch --prune
git reset --hard origin/master
git pull
cp /Users/$USER/backup/myhosts /usr/local/sbhosts/myhosts
[ -e /var/log/sbhosts/sbhost.log ] && rm /var/log/sbhosts/sbhost.log
python3 ./updateHostsFile.py -a -f -r -e fakenews > /var/log/sbhosts/sbhost.log
/usr/local/bin/python ./adhosts2privoxy.py /usr/local/sbhosts/adhosts2privoxy.conf
mv sb.action /usr/local/etc/privoxy
launchctl kickstart -k /Library/LaunchDaemons/privoxy.plist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment