PowerDNS Recursor
参考链接
https://doc.powerdns.com/recursor/getting-started.html#enterprise-linux
操作步骤
1. 准备系统
我是在下面系统做的实验,CentOS Linux release 7.5.1804 (Core) ,估计其他7.0 也是可以的
2. 更新yum及epel
curl -s -o /etc/yum.repos.d/CentOS-Base.repo \
http://mirrors.aliyun.com/repo/Centos-7.repo
curl -s -o /etc/yum.repos.d/epel.repo \
http://mirrors.aliyun.com/repo/epel-7.repo
3. 安装
yum install pdns-recursor
4. 配置
[root@icdn-pxe-server ~]# cat /etc/pdns-recursor/recursor.conf
#allow-from=103.227.80.162,127.0.0.0/8,38.255.128.0/18,103.123.60.0/23,103.147.62.0/23,103.174.84.0/23,103.174.168.0/23,103.180.240.0/23,103.181.226.0/23,103.190.40.0/23
#auth-zones=/etc/pdns-recursor/custom_zones.conf
allow-from=0.0.0.0/0
forward-zones-recurse=.=8.8.8.8;8.8.4.4
local-address=0.0.0.0
local-port=53
max-cache-entries=1000000
loglevel=4
#query-local-address=0.0.0.0
#security-poll-suffix=
#setgid=pdns-recursor
#setuid=pdns-recursor
etc-hosts-file=/etc/pdns-recursor/hosts
export-etc-hosts=yes
lua-dns-script=/etc/pdns-recursor/lua/combined.lua
#lua-dns-script=/etc/pdns-recursor/lua/log_queries.lua
loglevel=9
[root@icdn-pxe-server ~]# cat /etc/pdns-recursor/lua/combined.lua
-- combined.lua
pdnslog("Script start", pdns.loglevels.Info)
-- 加载 custom.lua 脚本
dofile("/etc/pdns-recursor/lua/custom.lua")
-- 加载 log_queries.lua 脚本
--dofile("/etc/pdns-recursor/lua/log_queries.lua")
[root@icdn-pxe-server ~]# cat /etc/pdns-recursor/lua/custom.lua
-- custom.lua
-- 创建一个 qtype 映射表
local qtype_map = {
[1] = "A",
[2] = "NS",
[5] = "CNAME",
[6] = "SOA",
[12] = "PTR",
[15] = "MX",
[16] = "TXT",
[28] = "AAAA",
[33] = "SRV",
[255] = "ANY"
-- 可以根据需要扩展此表
}
function preresolve(dq)
-- 自定义域名解析部分
local domain = dq.qname:toString()
if domain == "baidu.com." then
dq:addAnswer(dq.qtype, "103.235.46.40")
return true
elseif domain == "twitter.com." then
dq:addAnswer(dq.qtype, "104.244.42.65")
return true
elseif domain == "amazon.com." then
dq:addAnswer(dq.qtype, "13.224.166.154")
return true
elseif domain == "cnn.com." then
dq:addAnswer(dq.qtype, "151.101.67.5")
return true
end
-- 对于其他请求,继续正常解析
return false
end
function postresolve(dq)
-- 日志记录部分
if dq.qname and dq.remoteaddr and dq.qtype then
local query = dq.qname:toString()
local client = dq.remoteaddr:toString()
local qtype = qtype_map[dq.qtype] or tostring(dq.qtype) -- 处理数值类型的 qtype
local answers = dq:getRecords()
if answers then
local answerStr = ""
for _, record in ipairs(answers) do
answerStr = answerStr .. tostring(record) .. ", "
end
answerStr = string.sub(answerStr, 1, -3) -- 移除末尾的逗号和空格
pdnslog("Answers: " .. answerStr, pdns.loglevels.Info)
else
pdnslog("No answers found", pdns.loglevels.Info)
end
local logEntry = string.format("Client: %s, Query: %s, Type: %s", client, query, qtype)
pdnslog(logEntry, pdns.loglevels.Info)
else
pdnslog("Missing fields in dq", pdns.loglevels.Warning)
end
return false
end

浙公网安备 33010602011771号