Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
eternity4318 opened this issue Oct 20, 2024 · 7 comments
Open

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

eternity4318 opened this issue Oct 20, 2024 · 7 comments

Comments

@eternity4318
Copy link

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.

@eternity4318 eternity4318 changed the title Custom Code Hook for Non-Blocked Domains Executing Custom Code for Domains Not Found in Any List Oct 20, 2024
@DL6ER
Copy link
Member

DL6ER commented Oct 21, 2024

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

FTL/src/dnsmasq_interface.c

Lines 958 to 962 in 963dec4

// Check if this should be blocked only for active queries
// (skipped for internally generated ones, e.g., DNSSEC)
if(!internal_query)
blockDomain = FTL_check_blocking(queryID, domainID, clientID);

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.

@DL6ER DL6ER added the Question label Oct 21, 2024
@eternity4318
Copy link
Author

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
Copy link
Member

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
Copy link
Author

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.

Copy link

This issue is stale because it has been open 30 days with no activity. Please comment or update this issue or it will be closed in 5 days.

@github-actions github-actions bot added the stale label Dec 12, 2024
@eternity4318
Copy link
Author

Any update?

@github-actions github-actions bot removed the stale label Dec 12, 2024
@DL6ER
Copy link
Member

DL6ER commented Dec 12, 2024

No, I don't think there is anything we are supposed to do right now. Do you have a specific question we can answer? The code snippet you posted above lacks context, e.g., where precisely you have tried adding it. Without this, it's unclear what you want. Offloading into a separate (external) environment is not necessarily a trivial task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants