Skip to content

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

@Ealireza

Description

@Ealireza

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions