CZ-NIC / knot-resolver

Knot Resolver - resolve DNS names like it's 2024
https://www.knot-resolver.cz/
Other
362 stars 59 forks source link

Forward DNS Query Based on Specific Answer (Matching IP Address) #114

Open Ealireza opened 1 month ago

Ealireza commented 1 month ago

I need to forward DNS queries to a secondary DNS server if a specific value (IP address) is returned in the DNS response. Specifically, if the answer contains 192.168.1.1, I want the request to be forwarded to 10.10.10.1 for re-resolution. Expected Behavior:

A user queries for a domain (e.g., dig alibaba.com).
If the result contains the IP address 192.168.1.1, the query should be automatically forwarded to another DNS server (e.g., 10.10.10.1) for further resolution.

Current Attempt:

lua


policy.add(policy.all(function (state, req)
    log("info Policy function triggered")

    -- Get the DNS answer section
    local answer = req:answer()
    if answer then
        for _, record in ipairs(answer) do
            -- Check if the response is an A record and contains the IP 192.168.1.1
            if record.stype == kres.type.A and tostring(record.rdata) == '192.168.1.1' then
                log("info IP is 192.168.1.1, forwarding to 10.10.10.1")
                -- Forward the query to the specified DNS server
                return policy.FORWARD({'10.10.10.1'})
            end
        end
    else
        log("info No answer found")
    end

    return kres.DONE
end), true)

Issue:

The function triggers correctly, but the query is not being forwarded to the specified DNS server when the condition (record.rdata == '192.168.1.1') is met. Steps to Reproduce:

Add the above Lua code to the Knot Resolver configuration.
Query for a domain (dig alibaba.com).
If the result contains the IP 192.168.1.1, the query should be forwarded, but it does not.

Environment:

Knot Resolver Version: [Include version]
Operating System: [Your OS]
Configuration: [Any relevant additional configuration]

Desired Solution:

I would like the query to forward correctly to 10.10.10.1 whenever the answer contains 192.168.1.1. Any guidance on why the forward might not be triggered or if additional configurations are needed would be appreciated.

vcunat commented 1 month ago

So far we don't have ability to restart the resolution (based on results), and I don't expect it anytime soon, I'm afraid.

Ealireza commented 1 month ago

do you know any dns solution provide this feature ?

vcunat commented 1 month ago

I don't. But it's not like I know such details in many alternatives.