Giter VIP home page Giter VIP logo

async-doh's Introduction

async-doh

PyPI

DNS over HTTPS based on aiohttp and async_dns.

Installation

$ pip install async-doh

Usage

Command-line

usage: python3 -m async_doh.client [-h] [-n NAMESERVERS [NAMESERVERS ...]] [-t TYPES [TYPES ...]] hostnames [hostnames ...]

Async DNS resolver with DoH

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:

$ python3 -m async_doh.client -n https://223.5.5.5/dns-query -t ANY -- www.google.com

Client

import asyncio
import aiohttp
from async_doh.client import DoHClient

async def main():
    async with DoHClient() as client:
        result = await client.query('https://1.1.1.1/dns-query', 'www.google.com', 'A')
        print('query:', result)
        result = await client.query_json('https://1.1.1.1/dns-query', 'www.google.com', 'A')
        print('query_json:', result)

asyncio.run(main())

Server

from aiohttp import web
from async_doh.server import application

web.run(application)

Now you have http://localhost:8080/dns-query as an endpoint.

Patching async_dns

By importing the patch, async_dns will use aiohttp to send queries through HTTPS (aka DNS over HTTPS):

import asyncio
from async_dns.core import types
from async_dns.resolver import ProxyResolver
from async_doh.client import patch

async def main():
    revoke = await patch()
    resolver = ProxyResolver(proxies=['https://dns.alidns.com/dns-query'])
    res, _ = await resolver.query('www.google.com', types.A)
    print(res)
    await revoke()

asyncio.run(main())

References

async-doh's People

Contributors

gera2ld avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

async-doh's Issues

AssertionError while querying TXT record

Hello, First of all, amazing work on the doh library!
I was trying to get the TXT record for example.com which errored out the ParseError.

Code:

import asyncio
from async_doh.client import DoHClient


async def async_query(domain):
    async with DoHClient() as client:
        return await client.query("https://1.1.1.1/dns-query", domain, "TXT")

result = asyncio.run(async_query("example.com"))
print(result)

Error Traceback:

Traceback (most recent call last):
  File "gen.py", line 10, in <module>
    result = asyncio.run(async_query("example.com"))
  File "/Users/scorpionk/.pyenv/versions/3.8.6/lib/python3.8/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/Users/scorpionk/.pyenv/versions/3.8.6/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "gen.py", line 7, in async_query
    return await client.query("https://1.1.1.1/dns-query", domain, "TXT")
  File "/Users/scorpionk/.pyenv/versions/3.8.6/lib/python3.8/site-packages/async_doh/client.py", line 49, in query
    return await self.request_message(url, req, method)
  File "/Users/scorpionk/.pyenv/versions/3.8.6/lib/python3.8/site-packages/async_doh/client.py", line 41, in request_message
    result = DNSMessage.parse(data)
  File "/Users/scorpionk/.pyenv/versions/3.8.6/lib/python3.8/site-packages/async_dns/core/record.py", line 313, in parse
    l, ans.an = ans.parse_entry(RESPONSE, data, l, an)
  File "/Users/scorpionk/.pyenv/versions/3.8.6/lib/python3.8/site-packages/async_dns/core/record.py", line 294, in parse_entry
    l = r.parse(data, l)
  File "/Users/scorpionk/.pyenv/versions/3.8.6/lib/python3.8/site-packages/async_dns/core/record.py", line 196, in parse
    _, self.data = utils.load_message(data, l)
  File "/Users/scorpionk/.pyenv/versions/3.8.6/lib/python3.8/site-packages/async_dns/core/utils.py", line 39, in load_message
    assert cursor is not None, ParseError(data, offset)
AssertionError

If I do the curl of example.com TXT record using Cloudflare DNS then I get the following result.
Command:
curl -H 'accept: application/dns-json' 'https://cloudflare-dns.com/dns-query?name=example.com&type=TXT' | python -m json.tool

Output:

 {
    "Status": 0,
    "TC": false,
    "RD": true,
    "RA": true,
    "AD": true,
    "CD": false,
    "Question": [
        {
            "name": "example.com",
            "type": 16
        }
    ],
    "Answer": [
        {
            "name": "example.com",
            "type": 16,
            "TTL": 86400,
            "data": "\"v=spf1 -all\""
        },
        {
            "name": "example.com",
            "type": 16,
            "TTL": 86400,
            "data": "\"8j5nfqld20zpcyr8xjw0ydcfq9rk8hgm\""
        }
    ]
}

It would be great if this can be fixed.
Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.