Ad blocking in SailfishOS in 2023 - What are the options?

Yes, there is, You can install DNScrypt-proxy and then DNScrypt-proxy blocklist alone.

I reworked my hosts-based adblock script based on these insights:

If I use a combination of different hosts-based domain lists (as above) I get 87% and 65% resp. with those two test tools.

If I convert and add domains from ublock’s lists to that I get 91% and 87% resp., even though this adds “only” a few thousand unique entries.

Therefore the script has grown quite complex:

  • it converts uBO’s uAssets as far as possible to be used by /etc/hosts
  • it downloads everything from firebog.net’s meta list plus Steven Black’s largest hosts file
  • it combines and unique-sorts everything, adds a header and finally overwrites the old /etc/hosts

Ultimately it comes down to get a timely combination of as many sources as possible. I’m already re-inventing the wheel here; various more configurable solutions exist. But then, the whole thing is simple enough.

Keep in mind that uBO does more than domain blocking, and many adblock-specific rules can never be part of hosts-based blocking.
OTOH hosts-based blocking is always system-wide and very light on resources.

Anyhow, get the script with systemd service/timer here:
Notabug.org or
Framagit.org

Adjust to your liking.
Copy the shell script to /usr/local/bin/hostsupdate and the systemd files to /etc/systemd/system.
Start & enable the timer: systemctl start --now hosts.timer && systemctl enable hosts.timer

8 Likes

Thanks @ohnonot for this!
Two questions:

  1. Have you considered packaging this as a rpm? Might be just adapting an existing spec.
  2. The script is not compatible with busybox, one needs to zypper in gnu-bash. Is there any “bashism” unavoidable?

P.S. Good job, I now have a 18M hosts, rebooting :slight_smile:

4 Likes

Is that a Roman or metric “M”?

:rofl: Roman Megabytes octets :wink:

1 Like
  1. Yes but it might take me a while. Thanks for the vote of confidence (well that’s how I choose to interpret it).
  2. Good point, and done. Please check it out - it works for me. (my /bin/sh still points to busybox).

Dependencies:
curl comes pre-installed afair.
Apart from that busybox has versions of all utils used: mktemp tar grep sed sort but I have no idea how to check which version the script actually uses?

PS: there were also some bugs to fix.

4 Likes

Thanks for your effort. However, I can see something in the output what I don’t understand:

./hostsupdate: line 92: 0: not found

Am I doing something wrong or is this to be expected?

1 Like

Firefox with ublock is all I need. For Youtube I have Libretube, and I guess I could get streamlink to work somehow if I were so inclined to watch twitch streams and whatever.

I guess your /bin/sh is linked to bash. I have changed the offending bit now so it should work both with bash and busybox (see previous comments). Please try again.

1 Like

Thank you very much for the update! Now it worked. My Shell is busybox, though.

/bin/sh -> ../usr/bin/busybox

Anyway, thanks a lot!

Small new thing I discovered is this: (but I don’t really want to bother you any further)

rm: can't remove 'meta-list': No such file or directory
1 Like

It’s no bother. It should’ve been meta.list instead of meta-list. Fixed, pull again.

Btw and @everybody: this will result in pretty strict domain blocking. Not to everyone’s taste. If you want less maybe you can choose one of Steven Black’s ready-made files instead.

Just out of curiosity - isn’t it possible to create a rpm package from your scripts that can be put on openrepos/chum so everybody can install it easily without the need to ssh to the device?
Not everybody is able/willing to do this - but i guess everybody would appreciate an rpm package that could be easily installed via a store.

What do you think?

1 Like

This issue has been just raised. A RPM package can run scripts but they are not designed for that task.

This kind of changes would go into something like a configuration manager but SFOS is missing completely such a tool. On the other hand, the tool that could have fulfill that role was Patch Manager but the evolution of PM gone in the direction of applying in-memory-only patches that do not survive to a reboot because otherwise a patch can mess up the system. This choice, as demonstrated in trying to patches some system configuration files, prevent to have a configuration manager tool with the minimum effort starting from the current PM.

However, because a configuration manager can potentially interfere also with the RPM installation / removing / update and for sure with the SFOS version upgrade no any way of applying persistent changes have been made but SSH. Hence, when you use SSH, it is your fault!

Welcome aboard, sailor! :hugs:

It exists a package as described into DNS CACHING section of this guide

The name of that package is dns alternative.

It is not part of the SFOS, but the OpenRepos community contributions.

No.

What was asked was to package the script in to an installable RPM file to make the script available to a user at leisure.

What was not asked was to run said script during installation of an RPM.


Now we have that misunderstanding out of the way: Patchmanager has nothing to do with anything asked here, and you still haven’t completely grokked how it actually works.
But that’s harmless, as it’s not really relevant to the topic of this thread.

Back on Topic, doing stuff with DNS, be it the way you propose in your links or otherwise is a valid approach and on topic in this thread, but the participants here apparently prefer the /etc/hosts/ approach. (Which in a way is a DNS approach, just without DNS.)

5 Likes

I already stated earlier:

Yes but it might take me a while. Thanks for the vote of confidence (well that’s how I choose to interpret it).

2 Likes

The question was specific but in general what is required is a configuration manager.

How to provide a configuration manager?

Can be an RPM that install a set of scripts and allows to run them by a GUI providing parameters.

Because a configuration manager should be available also in rescue mode (no graphic interface available) or via remote console (e.g. SSH).

Is this enough?

Nope, if the configuration manager does not manage also the package installation set, as well.

This is possible only if there is a centralized organisation of the RPM repositories which is not the case of SFOS.

Changing the DNS is not the only thing that people wish to do, but just one of many.

Where is the best place to put a configuration manager considering the peculiarities of the smartphone market?

Into a rescue image, not in the system.

Oplà! :blush:

Hello,
my most promising solution for ad block is the modification of the hosts file. AdBlocking on Sailfish OS via /etc/hosts | dt.iki.fi developed a suitable solution. However for me it was still very complicated to process all the steps. Therefore I wrote a bash script that includes all steps.

just download and run the following script with root permission and after reboot ad block should work out of the box: adblock-script/adblock-script.sh at main · teertinker/adblock-script · GitHub

I hope this helps some users.

2 Likes

This usually works for me.
Type in terminal as root -

curl https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts -o /etc/hosts

Naturally, you should be able to link it to any other hosts file of your choice.

After you do that, reboot the device, and that’s it.

5 Likes

Just to clarify:

There’s nothing wrong with using a single hosts file, but I wanted to go the extra mile, as discussed in this thread.

Therefore the script has grown quite complex:

  • it converts uBO’s uAssets as far as possible to be used by /etc/hosts
  • it downloads everything from firebog.net’s meta list plus Steven Black’s largest hosts file
  • it combines and unique-sorts everything, adds a header and finally overwrites the old /etc/hosts

My tutorial mostly describes how to make updates automatic; the script itself can be treated as opaque.

Thanks to @FelixWilke for automating that in turn, and directly linking to my script. If you want you can use this link instead and I’ll try my best to make sure not to break compatibility in future updates.

4 Likes

That sounds pretty cool. Thanks for making it.

1 Like