我已经构建了一个带有 avahi 感知的扭曲的迷你 dns 服务器。
它的作用是寻找以 .local 结尾的请求;如果它是主机的 url,比如{{hostname}}.local
dns 服务器,让操作系统解析地址。如果它类似于{{subdomain}}.{{hostname}}.local
服务器将其路由到{{hostname}}.local
.
带来这个程序的功能尚不清楚,它让您可以轻松地使用子域,而无需将您需要的每个子域添加到 /etc/hosts,而且它还支持本地网络中的机器并且还具有avahi dnsconfd 服务器正在运行。
这是代码:
编辑:现在服务器返回一个带有正确 ip 的答案
import socket
from twisted.internet import reactor
from twisted.names import dns
from twisted.names import client, server
hostname = socket.gethostbyaddr(socket.gethostname())[0]
magic_number = + 5 + 1 + len(hostname)
class Resolver(client.Resolver):
def lookupAddress(self, name, timeout = None):
if name.endswith('.local'):
local_name = name[-magic_number:]
ip = reactor.resolve(local_name)
if local_name == name:
ip = reactor.resolve(local_name)
def answer(adress):
a = dns.RRHeader(name=name, ttl=0)
payload = dns.Record_A(adress)
a.payload = payload
return ([a], [], [])
d = ip.addCallback(answer)
return d
else:
def answer(adress):
a = dns.RRHeader(name=name, type=dns.A, ttl=10)
payload = dns.Record_A(adress, ttl=10)
a.payload = payload
return ([a], [], [])
d = ip.addCallback(answer)
return d
else:
return self._lookup(name, dns.IN, dns.A, timeout)
resolver = Resolver(servers=[('212.27.40.241', 53)])
factory = server.DNSServerFactory(clients=[resolver])
protocol = dns.DNSDatagramProtocol(factory)
reactor.listenUDP(53, protocol)
reactor.listenTCP(53, factory)
reactor.run()
我用 dig 做了一些测试,一切看起来都很好。我的主机名是tachtev
.
这是 dig www.tachtev.local 的输出
; <<>> DiG 9.7.3 <<>> www.tachtev.local
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 12794
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;www.tachtev.local. IN A
;; ANSWER SECTION:
www.tachtev.local. 500 IN CNAME tachtev.local.
;; Query time: 2 msec
;; SERVER: 192.168.0.6#53(192.168.0.6)
;; WHEN: Sun Jun 26 15:51:41 2011
;; MSG SIZE rcvd: 49
这是 nslookup 的输出
root@tachtev:~# nslookup -debug www.tachtev.local
Server: 127.0.0.1
Address: 127.0.0.1#53
------------
QUESTIONS:
www.tachtev.local, type = A, class = IN
ANSWERS:
-> www.tachtev.local
internet address = 192.168.0.4
ttl = 10
AUTHORITY RECORDS:
ADDITIONAL RECORDS:
------------
Non-authoritative answer:
Name: www.tachtev.local
Address: 192.168.0.4
当我挖掘网址时,一切看起来都很好。但是现在当我尝试 curl www.tachtev.local 时,我得到一个找不到主机的错误。
错误来自哪里?
编辑:依赖项:avahi + avahi-dnsconfd + twisted + twisted 名称(不能被 pip'ed)编辑:我没有找到解决方案,但是有一个软件可以实现我想要实现的http://pow.cx /