pi-hole / FTL

The Pi-hole FTL engine
https://pi-hole.net
Other
1.39k stars 196 forks source link

Executing Custom Code for Domains Not Found in Any List #2092

Open eternity4318 opened 1 month ago

eternity4318 commented 1 month ago

I want to run custom code when a domain query is not found on any blocklists. I believe the correct place for this is in dnsmasq_interface.c. I tried adding logging there but didn't get any results.

Is there a recommended way to hook into Pi-hole's query handling for this purpose? Any guidance on where to implement this or how to debug would be appreciated.

DL6ER commented 1 month ago

Is there a recommended way to hook into Pi-hole's query handling for this purpose?

No, arbitrary code injection is not regularly supported, you could either patch some function in the binary, or, better, modify the code yourself after each update

https://github.com/pi-hole/FTL/blob/963dec401b0f2488643b2ed97e10e0bcbff3d829/src/dnsmasq_interface.c#L958-L962

You could add your own code after these lines in the style of

if(!blockDomain)
    ...

What are you trying to achieve exactly? If it is something we can think of having a broad(er) application for more users, we could maybe implement this officially, taking away the need for you to manually patch on updates.

P.S. My link above goes to the close-to-release Pi-hole v6.0 code. We are mostly updating outdated documentation at this point and then we'll be waiting from the team from everyone across the globe to have some time simultaneously.

eternity4318 commented 3 weeks ago

I'm trying to run custom code whenever a domain isn't found on any blocklists. To get familiar with the code, I started by adding logging to see what data I have access to and to start hooking into Pi-hole:

if (!blockDomain) {
    // Inline file logging
    FILE *log_file = fopen("/var/log/pihole/logfile.log", "a");
    if (log_file != NULL) {
        fprintf(log_file, "%s\n", name);
        fclose(log_file);
    } else {
        perror("Error opening file"); 
    }
}

For some reason, though, nothing is getting logged. Ideally, I’d like to set this up to call a function in C, Bash, or any other language, passing in the relevant data to decide on blocking. If this seems like a feature that could benefit others, I’d be happy to submit a pull request once it’s fully implemented.

rdwebdesign commented 3 weeks ago

If your intention is to identify if a domain is blocked by Pi-hole, you could try to use curl in a shell script (or any other language) to call this API endpoint:

https://pi.hole/api/search/<DOMAIN>?partial=false

and check the returned JSON.

If results.total == 0, the domain is not present in any list or a regex:

{
   "results": {
      "domains":{ "exact":0, "regex":0 },
      "gravity":{ "allow":0, "block":0 },
      "total":0
   },
   "parameters": { "N":20, "partial":false, "domain":"example.com", "debug":false }
}

Note: The API call needs authentication.

eternity4318 commented 3 weeks ago

I want it to be done as the request is being processed. So that I have full control over what's returned as a response.