Change routes based on DNS queries

hi all,
I am relatively new to openWrt but I have been playing around with it for a while. I am however new to this forum, so I apologize if I am in the wrong thread.

I am using it on a TPLink router to provide access to a nordvpn connection on my smart tv.
The problem is that there are some smartTV applications for which I need one VPN, some for which I need another, and others for which I need a non-vpn connection. I therefore tried domain-based routing using VPN-Policy-Routing. But the number of domains accessed by each application is large (CDNs) and I do not exclude that multiple applications that I need to use different VPNs are using the same akamai-based domain.

So I thought of the following solution.
Since the SmartTV uses one application at a time, I would like to react to the first dns request, which I can easily track, and switch to the right VPN connection.
Is there an API that allows me to react to DNS queries? Or do I need to use the logging feature?

In particular, I was thinking I could use the logging feature of dnsmasq, and build a small local log server that receives log entries, reacts to requests for the right domains and switches routes. This would work, but I wonder if there would be a better solution.

thanks

bye

Dave

sounds like a job for iptables+ipset

not easy... but there are examples of CDN ipset in action in the wild... ( this is how your logging idea would be practical )... then it just getting those ipsets translated to the relevant processes.

be aware these methods are almost always imperfect at some level.