Total Pageviews

Wednesday 11 October 2017

利用dnsmasq的并发查询功能来解决内外网域名服务器切换问题

很多公司,都架设了自己的域名服务器,使得在内网的机器可以通过子域名而不是ip来轻松访问内网资源。但是,这里又有所划分:
  • 有些域名,比如intranet.company.com,仅仅能够内网访问,只有内网的域名服务器才可以解析
  • 有些域名,比如mail.company.com,不仅可以内网访问,公网也可以访问。只是不同的网络,解析出来的ip地址不同。
对于我这样的,喜欢自己使用dnsmasq来做个人电脑dns加速的,在这种场景下,就不得不经常切换dns服务器设置。举个例子,在公司,可能用server=/company.com/111.111.111.111来解析intranet.compay.com这样的域名。离开公司,又要切换到server=/company.com/114.114.114.114。有没有办法可以一劳永逸的解决,不需要切换呢。于是翻看dnsmasq的官网,终于找到这样一段描述:
--all-servers
By default, when dnsmasq has more than one upstream server available, it will send queries to just one server. Setting this flag forces dnsmasq to send all queries to all available servers. The reply from the server which answers first will be returned to the original requester.
大致意思就是,当你为某个域名配置了多个解析服务器时,dnsmasq默认只会使用其中的一个。加入该选项,则dnsmasq会并发查询,然后选择第一个收到的回执。
于是,我们只需要在dnsmasq.conf中使用如下配置即可完成一劳永逸,不需要在内外网切换时,切换域名服务器:
all-servers  
server=/company.com/111.111.111.111  
server=/company.com/114.114.114.114  
这样配置后,比如intranet.company.com,因为114解析不到,自然就会使用111的结果。而在公网时,因为111没法访问到,自然都会使用114的结果。
dig测试一下,没有问题。

No comments:

Post a Comment