Package: async-dns
async_dnsThis is the documentation for v2.x. Clickherefor 1.x documentation.FeaturesPrerequisiteInstallationCLIResolverServerAPIClientRoutingDoH supportDNS SpoofingTestLoggingReferencesFeaturesBuilt withasyncioin pure Python, no third party dependency is requiredSupport DNS over UDP / TCPSupport DNS over HTTPSSupport DNS over TLSPrerequisitePython >=3.6Installation$pip3installasync_dns# or$pip3installgit+https://github.com/gera2ld/async_dns.gitCLIResolverusage: python3 -m async_dns.resolver [-h] [-n NAMESERVERS [NAMESERVERS ...]] [-t TYPES [TYPES ...]]
                                     hostnames [hostnames ...]

Async DNS resolver

positional arguments:
  hostnames             the hostnames to query

optional arguments:
  -h, --help            show this help message and exit
  -n NAMESERVERS [NAMESERVERS ...], --nameservers NAMESERVERS [NAMESERVERS ...]
                        name servers
  -t TYPES [TYPES ...], --types TYPES [TYPES ...]
                        query types, default as `any`Examples:# Resolve an IP$python3-masync_dns.resolverwww.google.com
$python3-masync_dns.resolver-tmx--gmail.com# Query via TCP$python3-masync_dns.resolver-ntcp://127.0.0.1--www.google.com# Query via TLS$python3-masync_dns.resolver-ntcps://dns.alidns.com--www.google.com# Query from non-standard ports$python3-masync_dns.resolver-nudp://127.0.0.1:1053--www.google.com# Query from HTTPS$python3-masync_dns.resolver-nhttps://dns.alidns.com/dns-query--www.google.comNote:--is required beforehostnames if the previous option can have multiple arguments.Serverusage: python3 -m async_dns.server [-h] [-b BIND] [--hosts HOSTS] [-x [PROXY [PROXY ...]]]

DNS server by Gerald.

optional arguments:
  -h, --help            show this help message and exit
  -b BIND, --bind BIND  the address for the server to bind
  --hosts HOSTS         the path of a hosts file, `none` to disable hosts, `local` to read from
                        local hosts file
  -x [PROXY [PROXY ...]], --proxy [PROXY [PROXY ...]]
                        the proxy DNS servers, `none` to serve as a recursive server, `default` to
                        proxy to default nameserversNote:TLS and HTTPS are not supported inasync_dnsserver. Considerasync-dohfor DoH server support.Examples:# Start a DNS proxy server on :53$python3-masync_dns.server-b:53--hosts/etc/hosts# Start a DNS server over TCP proxy$python3-masync_dns.server-xtcp://114.114.114.114# Start a DNS recursive server$python3-masync_dns.server-xnoneAPIimportasynciofromasync_dns.coreimporttypesfromasync_dns.resolverimportProxyResolverresolver=ProxyResolver()res,cached=asyncio.run(resolver.query('www.baidu.com',types.A))print(res)ClientThe client sends a request to a remote server and returns the message directly. Unlike resolvers, client does not have a cache and does not modify the response.importasynciofromasync_dns.coreimporttypes,Addressfromasync_dns.resolverimportDNSClientasyncdefquery():client=DNSClient()res=awaitclient.query('www.google.com',types.A,Address.parse('8.8.8.8'))print(res)print(res.aa)asyncio.run(query())RoutingProxyResolver supports routing based on domains:resolver=ProxyResolver(proxies=[('*.lan',['192.168.1.1']),# query 'udp://192.168.1.1:53' for '*.lan' domains(lambdad:d.endswith('.local'),['tcp://127.0.0.1']),# query tcp://127.0.0.1:53 for domains ending with '.local''8.8.8.8',# equivalent to (None, ['8.8.8.8']), matches all others])DoH supportThis library contains a simple implementation of DoH (aka DNS over HTTPS) client with partial HTTP protocol implemented.If you need a more powerful DoH client based onaiohttp, or a DoH server, considerasync-doh.DNS SpoofingYou can easily add records to the cache with a hosts file or the cache API.Start a server with a custom hosts file:$python3-masync_dns.server-b:53--hosts/path/to/custom/hostsAdd some additional records to a resolver:fromasync_dns.coreimportparse_hosts_file,typesforname,qtype,datainparse_hosts_file(hosts):resolver.cache.add(name,qtype,data)resolver.cache.add('www.example.com',types.A,['127.0.0.1'])Test$python3-munittest# Or with tox$tox-epyLoggingLogging does not work out of the box in v2. It requires at least minimalloggingconfiguration.logging.basicConfig(level=logging.INFO)You can also add a formatter for the logger:importloggingfromasync_dns.coreimportloggerlogger.setLevel(logging.INFO)handler=logging.StreamHandler()fmt=logging.Formatter('%(asctime)s%(levelname)s:%(message)s')handler.setFormatter(fmt)logger.addHandler(handler)Referenceshttps://tools.ietf.org/html/rfc1034https://tools.ietf.org/html/rfc1035https://tools.ietf.org/html/rfc1464TXThttps://tools.ietf.org/html/rfc2915NAPTR