PDNS

最近aliyun的月饼事件上了纽约时报, 然后在知乎看到了这篇 阿里巴巴做了哪些恶? 提到 万网封停慧聪网域名事件

前情提要

自己大学申请了schemacs.com一直用到现在,看了下whois信息,显示是31-oct-2010创建的。

  1. 之前一直托管在godaddy,域名他家买的,自然用他家的hosting。话说狗爹家的导入导出做得非常好。1
  2. 后来用过一段时间dnspod,有API,然后貌似国内解析比godaddy好很多。
  3. 再后来听说namecheap有优惠又买了codercan.com的域名,就又都迁移到namecheap家的托管,但是没用多久。
  4. 后来听说Google也开始搞域名后,就又迁移到了Google Domains,发现连基本导入导出都没有,却有动态dns更新接口。2
  5. 最后折腾来折腾去,自己用twisted写了几行,就自己托管了,还搞了个基于geoip的解析。
  6. 这些天想搞个动态dns,自己又不想折腾twisted了,就想搞个数据库,然后用django的后台来更新,正谋划着搞个api来想起了pdns。

Self-Hosting

自己托管DNS相对第三方托管:

Pros

  • 存储格式自己定,zone格式,sqlite/mysql等db,或者LDAP,lua都行。
  • 如果是sql,基本可以自定义的管理后台,比如 django-powerdns-manager
  • 合适够用的通用API,这个跟上一条有关系。而PowerDNS则已经内置基本的API

Cons

  • HA,这个做master-slave能解决。
  • DDOS,不好解决啊,先把递归解析关了吧。

PowerDNS

这个不多说,支持各种后端,尤其是数据库。 我平时基本就三条命令:

sudo pdnsutil edit-zone schemacs.com  # 打开EDITOR编辑zone
sudo pdnsutil increase-serial schemacs.com  # 增加SOA的Serial NO.
sudo pdns_control notify schemacs.com  # 通知slave更新

配置DDNS

最重要一条,不要设置recursion,容易当作DNS放大攻击的肉鸡。我就因为那个简易twisted dns server收到vps provider发的如下邮件:

Example

You appear to be running an open recursive resolver at IP address xxx.xxx.xxx.xxx that participated in an attack against a customer of ours, generating large UDP responses to spoofed queries, with those responses becoming fragmented because of their size.
Please consider reconfiguring your resolver in one or more of these ways:
- To only serve your customers and not respond to outside IP addresses (in BIND, this is done by defining a limited set of hosts in "allow-query"; with a Windows DNS server, you would need to use firewall rules to block external access to UDP port 53)
- To only serve domains that it is authoritative for (in BIND, this is done by defining a limited set of hosts in "allow-query" for the server overall but setting "allow-query" to "any" for each zone)
- To rate-limit responses to individual source IP addresses (such as by using DNS Response Rate Limiting or iptables rules)

如果之前有bind配置和zone文件,可以用pdns自带的zone2sql工具直接转换成sql再导入进去 zone2sql --named-conf=bind.conf -gsqlite | sqlite3 /var/lib/powerdns/pdns.sqlite3

配置API

增加配置文件 /etc/powerdns/pdns.d/local.conf

allow-recursion=127.0.0.1
api=yes
api-key=xxx
webserver=yes
webserver-address=0.0.0.0

获取公网ip

curl 'http://www.ip.cn/'|grep -o -E '[0-9.]+'
curl http://checkip.amazonaws.com/ # 这个如果你不是墙外...

更新DNS记录

curl -X PATCH --data \
        '{"rrsets": [ {"name": "xxx.example.com.", "type": "A", "ttl": 300, "changetype": "REPLACE", "records": [ {"content": "XXX_IP", "disabled": false } ] } ] }' \
        -H 'X-API-Key: XXX' \
        https://example.com/api/v1/servers/localhost/zones/example.com. # 此处正常返回状态码204
curl -H 'X-API-Key: XXX' https://example.com/api/v1/servers/localhost/zones/example.com. | jq .

  1. 记得狗爹家当时还附赠了几百M的FTP空间,自己就用FileZilla连上去上传和下载自己博客WordPress的图片。 

  2. 突然有天谷歌发邮件让确认域名的owner信息,我也不知道怎么了就去看了一眼(一般我都是直接忽略。。。),一下子惊呆了,我之前填的是随机信息(不知道G家免费提供域名隐私保护),那个邮件地址是一次性的,也就是说我没法验证我的旧邮箱来更新为我的真实邮箱。根据Internet Corporation for Assigned Numbers Authority(ICANN)规则,我基本是没法证明这个域名是属于我的了。当然后来联系了客服,很快更正过来了。 

Comments