Toggling web ad-blocking via a Squid proxy

Squid is a popular open-source proxy that can be configured to block adverts. I run it on a Raspberry Pi & have my iPad Air and Samsung Galaxy S21 Ultra configured to use the proxy when connected to my Wifi at home. There are good articles on the web which show you how to set all this up, so I won’t go over it in this post.

One thing I needed was a way to easily turn the ad-blocking off & back on, since blocking ads sometimes breaks sites or links to sites (especially affiliate tracking links).

You can of course go into the network settings & remove the proxy configuration, then put it all back after you’re done, but this is a pain & I really wanted a 1-tap solution which would work on iOS & Android.

First, I wrote a bash script which would toggle the ads on/off.. this works by emptying/populating the adblock.txt file that Squid consumes & telling Squid to re-read it. Here’s the script I came up with;

/etc/squid/squid_ads_toggle.sh

if [ -s "/etc/squid/adblock.txt" ]; then
        : > /etc/squid/adblock.txt
        /usr/sbin/squid -k reconfigure
        echo "Adverts ENABLED" > /var/www/html/proxyadstoggle.out
else
        curl -sS -L --compressed "http://pgl.yoyo.org/adservers/serverlist.php?hostformat=squid-dstdom-regex&showintro=0&mimetype=plaintext" > /etc/squid/adblock.txt
        /usr/sbin/squid -k reconfigure
        echo "Adverts DISABLED" > /var/www/html/proxyadstoggle.out
fi

Testing this from a command prompt worked, so the next thing I needed to do was be able to trigger it from a web server call. My Raspberry Pi already has Apache & PHP running, so that was the natural place to create a quick PHP page that executes the bash script;

/var/www/html/proxyadstoggle.php

<?php
        print ("Toggling ads on Squid proxy [");
        $exec = "sudo /etc/squid/squid_ads_toggle.sh";
        exec($exec, $output, $retval);
        print($retval."] - ");

        $file = file_get_contents('/var/www/html/proxyadstoggle.out', true);
        print($file);
?>

The last thing I needed to give permission for the web service to run the bash script, which uses visudo to allow the ‘www-data’ user to run it. Now, I realise this isn’t ideal, but I couldn’t find a better solution; please understand the security risks of doing this if you’re going to follow this guide.

sudo visudo

www-data ALL=(ALL) NOPASSWD: /etc/squid/squid_ads_toggle.sh

Testing this in a web browser worked, so next I could move on to triggering it from my iPad and Android phone.. I could just go to the page in a browser, but that felt a bit clunky & there are nicer ways to do this! :)

Shortcuts on iOS

‘Shortcuts’ is a really nice automation app that’s built into iOS; with just 2 steps, I can call the PHP page & output the result into a native popup notification. Here’s how it looks;

The shortcuts can be added to the slide-out widget panel in iOS, which make it really convenient to access!

Running the shortcut will display a notification like this;

Automate on Android

There isn’t a built-in equivalent of ‘Shortcuts’ on Android, but I do have Automate, which I’ve previously used in projects I’ve blogged about on here.

Here’s how the ‘Flow’ looks;

The HTTP Request block needs to save the output to a variable, like this;

And the Toast notification can then display what’s in the variable like this;

You can add the Flow to your home screen as a shortcut (via Automate, or through Widgets), and when you tap it you’ll see a notification like this;

And there you have it! An easy way to toggle ad-blocking on your phone or tablet. Those apps on iOS and Android open up a world of opportunities to integrate with other systems & do some really neat stuff.

Leave a comment