gpt4 book ai didi

具有自定义后端的 Python DNS 服务器

转载 作者:太空狗 更新时间:2023-10-29 20:37:34 24 4
gpt4 key购买 nike

是否有任何用 python 编写的 DNS 服务器,我可以在其中轻松使用自定义后端?

基本上,我只想用我自己的 IP 回答对某些域名的查询,但将其余查询传递给真正的 DNS 服务器。

最佳答案

我最近写了这样一个东西,或许你可以拿它来举例子。它使用 DHT 作为后端并在那里查找所有 .kad 域。如果您只是将 P2PMapping 替换为您自己的映射(即类似 {'google.com' : '127.0.0.1'} 的字典),它应该可以满足您的要求。

"""
Created on 16.08.2010

@author: Jochen Ritzel
"""

import dht

from twisted.names import dns, server, client, cache
from twisted.application import service, internet

class P2PMapping(dht.EntangledDHT):

def __contains__(self, key):
return key.endswith('.kad')

class MapResolver(client.Resolver):
"""
Resolves names by looking in a mapping.
If `name in mapping` then mapping[name] should return a IP
else the next server in servers will be asked for name
"""
def __init__(self, mapping, servers):
self.mapping = mapping
client.Resolver.__init__(self, servers=servers)
self.ttl = 10

def lookupAddress(self, name, timeout = None):
# find out if this is a .kad. request
if name in self.mapping:
result = self.mapping[name] # get the result
def packResult( value ):
return [
(dns.RRHeader(name, dns.A, dns.IN, self.ttl, dns.Record_A(value, self.ttl)),), (), ()
]
result.addCallback(packResult) # put it in a A Record
return result
else:
return self._lookup(name, dns.IN, dns.A, timeout)


## this sets up the application


application = service.Application('dnsserver', 1, 1)


## set up the DHT
mapping = P2PMapping(bootstrap=[('127.0.0.1', 4001)])
mapping['jochen.kad'] = '99.99.99.99' # "register" domain with IP


# set up a resolver that uses the mapping or a secondary nameserver
p2presolver = MapResolver(mapping, servers=[('192.168.178.1', 53)])


# create the protocols
f = server.DNSServerFactory(caches=[cache.CacheResolver()], clients=[p2presolver])
p = dns.DNSDatagramProtocol(f)
f.noisy = p.noisy = False


# register as tcp and udp
ret = service.MultiService()
PORT=53

for (klass, arg) in [(internet.TCPServer, f), (internet.UDPServer, p)]:
s = klass(PORT, arg)
s.setServiceParent(ret)


# run all of the above as a twistd application
ret.setServiceParent(service.IServiceCollection(application))


# run it through twistd!
if __name__ == '__main__':
import sys
print "Usage: twistd -y %s" % sys.argv[0]

关于具有自定义后端的 Python DNS 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4399512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com