[dnsdist] How to execute SpoofAction in LuaResponseAction #14795
-
Now I try to add a tag to configlocal function readCloudflareFastIPs()
local cloudflare_fast = ReadFile("/etc/dnsdist/data/cloudflare_fast.txt")
return cloudflare_fast
end
function makeQueryRestartable(dq)
-- make it possible to restart that query later
-- by keeping a copy of the initial DNS payload around
dq:setRestartable()
return DNSAction.None
end
-- if response has cloudflare ip ,tag it and restart
local function tagResponseCloudflare(dr)
if dr.pool == 'local' then
return DNSResponseAction.None
end
local packet = dr:getContent()
if string.len(packet) == 0 then
return DNSResponseAction.Drop
end
local overlay = newDNSPacketOverlay(packet)
local count = overlay:getRecordsCountInSection(DNSSection.Answer)
for i = 0, count - 1 do
local record = overlay:getRecord(i)
local parser = parsers[recordTypes[record.type]]
if parser then
local ca = parser(packet, record.contentOffset + 1, record.contentLength)
if cloudflare_netmask_group:match(ca) then
print("cloudflare IP: " .. ca:tostring() .. ", need replace")
dr:setTag("cloudflare_fast", "need_replace")
dr:restart()
print("dropping resp")
return DNSResponseAction.Drop
end
end
end
return DNSResponseAction.None
end
local cloudflare_fast_bypass = SetTagAction("cloudflare_fast", "bypass")
local cloudflare_fast_replace = TagRule("cloudflare_fast", "need_replace")
local fast_cloudflare_action = SpoofAction(readCloudflareFastIPs())
local cloudflare_fast_qtype = OrRule({ QTypeRule(DNSQType.A), QTypeRule(DNSQType.AAAA) })
local cloudflare_fast_not_optimized = NotRule(TagRule("cloudflare_fast"))
local cloudflare_fast_need_optimize = AndRule({ cloudflare_fast_not_optimized, cloudflare_fast_qtype })
-- if dq is set cloudflare_fast, spoof
addAction(cloudflare_fast_replace, fast_cloudflare_action, { name = "cloudflare_fast_spoof" })
-- unmatch china domains and proxy domains request will use local pool first and proecss in response actions
addAction(AllRule(), LuaAction(makeQueryRestartable), { name = "make_query_restartable" })
addAction(AllRule(), PoolAction("local"), { name = "default_use_local" })
-- post process
addResponseAction(cloudflare_fast_need_optimize, LuaResponseAction(tagResponseCloudflare),
{ name = "tag_cloudflare_resp_fast" }) cloudflare_fast.txt
testI tried to execute the request
The log shows the following
Therefore, it can be confirmed that the request is correctly routed to However, the returned result is not replaced, and I see on the webui that |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
What is happening is that you seem to expect the "query" rules (the ones added via |
Beta Was this translation helpful? Give feedback.
So, what I'm suggesting is:
addCacheMissAction
instead ofaddAction
, matching the tag as needed, and there you can callSpoofAction
.