How do I block ads on YouTube?

We were talking about YouTube specifically.

So we know that ads and videos are served from the same YouTube domains. So you can’t block based on that.

We also know that when you click a video YouTube calls the YouTube api and asks for what to show - likely an ad url and or a video url. Of course uBlock can filter on that an pi hole can’t.

What I was actually talking about was if you are in the US that YouTube api knows you are there ‘somehow’ and sends back 1 of a million YouTube ads for the US.

If on the other hand you are in some tiny obscure country it will send you 1 of maybe 100 ads available - in other words you will see a lot less ads in a small obscure country than the US.

Now the question is how does it know which country you are in? There’s a few possibilities:

  1. it checks which ip the request comes from ie your ip.

  2. when you request the dns for some domain let’s say, the dns server sends the ip of a google server setup to respond with ads AND videos for your country. Basically a regional server that handles all YouTube api requests. We still can’t block this ip as it’s needed by YouTube for everything.

I would have assumed it was 1) BUT testing out some vpn servers which show the ip as US I see Dubai ads.

This leads me to think this vpn setup may be using a Dubai dns server which incorectly sends back ips for google servers in Dubai. Ok their setup is broken but it raises the question that could 2) be how youtube serve you ads for your country?

Most public dns servers should serve you local ips based on where you are requesting from - but some probabaly don’t. Has anyone tried an alternative dns and actually got ads from a different country ever?

Someone further up said they did exactly that, I was skeptical but after the vpn test I did it may be worth a second look.

Edit: I am neither in US or Dubai so it’s got nothing to do with where I really am!

Tell us :wink:

CDN, Content Delivery Network

These server can even be located inside the nerwork of your ISP. If you use the DNS of your ISP you will stay completely inside the nerwork of your ISP.

Google will have installed a tracking system on those CDN to not miss out of userdata.

If you use VPN you will go directly to servers in the internet connection node and those can be area, country or several countries.

VPN let you appear as you surfing from a different location but the DNS is inside the network of you VPN provider.

You can get in trouble if you use DNS of your ISP then it could point to a CDN that is not reachable through the VPN you are using.

So the source address is the best indication where are and ofcourse Alphabet and other tracking firms have tools installed on you mobile devices to track you always and everywhere.

This particular vpn uses its own dns. The ips it returns can’t be reached outside of the vpn network whereas all other YouTube addresses seem to be reachable from anywhere in the world.

It’s likely Dubai have special youtube nodes to comply with local policy, but that wasn’t really my point.

What this means is that an IP address in the US (the vpn end point) was able to be served ads from Dubai because of the dns resolution. So in that case we could redirect some host (which one?) to a tiny countries youtube node to reduce the ads significantly.

Anyway it needs more investigation. It’s also possible that the app itself is connecting to a site or non http/s protocol somewhere to get its external up like STUN does.

I don’t think that this will fly in the end.

You are using DNS nodes that havesome kind of repression forced on them. Lets say we are going to usea DNS on a small country or island that is free. It will serve general general DNS information because it will not a CDN infrastructure/datacenters.

The Y-o-u-t-u-b-e behaviour is not a problem on my PC or Android devices because software filters out that stuff.

If you want to outsmart them you have find a domain rewrite program that sits in front of Pi-hole

More I read this thread, the more I believe that a complimentary client-side plug-in is needed. Client-side code should communicate with pi-hole back-end to automatically update black-list queries.

Those are already there and they do detect and block. uBlock is such a add-on.

Adding a dynamic part to Pi-hole will complicate all kind of things.

Client side is no good for smart TVs.

Then how fo you run then an add-on on the smart TV to steer Pi-hole.

To me this is a ship that sailed.

Better think of a system that acts like a proxy and removes the advertisment.

You used to just block the ad servers and change the TV’s DNS settings. You can still kinda do it but you also need something that emulates a YouTube server and says “no ad, just play the video”.

If I’m not mistaking every device has some sort of Ad tracking id and perhaps a standard way to pass it in the data exchange between client/server. If this could be sniffed-out and blocked it would work?

The apps and website seem to work slightly differently.


  1. App starts and connects to, this returns a media endpoint.

  2. app sends a large post to the endpoint above to authenticate, get an access key and allow acces, if you blocked redirector nothing is sent yet. (This access key is used from now on to all media endpoints)

  3. app connects to YouTube api etc and gets the video list etc

-> click a video in app

  1. site connects and asks for ad and media endpoints.

  2. app connects to both endpoints (ad and video). App sends large post to authenticate IF you blocked redirector, to the first of the endpoints connected - could be video could be ad.

  3. app starts to receive from video endpoint then pauses. Simultaneously it receives from ad endpoint - which may return nothing.

  4. if no ad in 5) or ad is finished the app resumes receiving the video.

You can’t block the above using domains. It’s impossible. If you can install your own certificate you CAN intercept the ssl traffic and block the ad request to the api.

Even if you block at a specific time to block the api request and not the rest the app tries numerous backup methods to get the ad including using a random media endpoint and asking for a random ad. I believe the normal way is to ask for a targeted ad and fallback is to ask for any ad directly from a random media endpoint.

Looking at the above it’s basically imposssible to block this without intercepting the traffic. To do this off device you need to do ssl mitm which means your own CA and connecting devices trusting it.

Oh and YouTube apps also use QUIC as their primary connect for media. Then fallback to tcp https if it fails.


I haven’t looked at this extensively but I’m guessing it’s not far from the above. Using a browser plugin you can intercept before ssl encryption so you can basically filter the traffic.

Final notes:
I strongly suspect that the endgame here is that YouTube will create an access flow - get ad, when ad is finished or no ad send video access key - request and play video.

They could still do this by only pre sending parts of the video until the access key is given.

At this point blocking will become impossible.

With youtube red etc being pushed heavily now, and ads increasing I think this is a major focus for youtube now - we probabaly won’t win this one :confused:


See my post below. To sniff you need to intercept ssl, and to do that your device needs to have capability to trust a third party certificate.

Mobile phones, computers etc can do this, smart TVs etc probabaly can’t.

This kind of blocking would require a squid proxy of sorts with built-in adblocking


Yeah the squid part is the easy bit - getting dumb devices to trust your custom cert is the hard / impossible bit!

Try this regex blocker:


1 Like

What is it supposed to do? I mean how does it solve the problem with the app repeatedly looking for ad servers and not playing the video?

This regex is working well so far, no ads. Seems to block the ads before (preroll) but continue to load the actual video. Also no ads during the video. This is all on the YouTube app on a Roku device.

Edit: Never mind, it doesn’t seem to be blocking every time. It also blocks all video recommendations after the video finishes. Oh well, was worth a try.


Still that’s a very useful improvement. If it removes most of the ads that’s great, and I can live without the recommendations as I never look at them anyway.

I’ll test it out, see how effective it is here.

To be honest I don’t think the regex did anything, as I’ve had these two lists which have blocked 90% of the ads here so far: