Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ Shortcut
--------

Manual usage is too board. So I make shortcut to use easily.
You just replace ``aiohttp.ClientSession`` to ``aiohttp_doh.ClientSession``.
You just replace ``aiohttp.ClientSession`` to ``aiohttp_doh.DNSOverHTTPSClientSession``.

.. code-block:: python3

from aiohttp_doh import ClientSession
from aiohttp_doh import DNSOverHTTPSClientSession

async def main():
async with ClientSession() as session:
async with DNSOverHTTPSClientSession() as session:
async with session.get('http://example.com') as resp:
data = await resp.text()

Expand All @@ -68,7 +68,7 @@ endpoints
List of str. DNS over HTTPS endpoints.
Shortcut use `'https://dns.google.com/resolve'`
and `'https://cloudflare-dns.com/dns-query'` both in default.
You can also use others instead.
You can also use others instead, make sure DNS endpoints support JSON response format (application/dns-json)

json_loads
Function for loads json. default is Python builtin json module's one.
Expand Down
27 changes: 8 additions & 19 deletions aiohttp_doh.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from aiohttp.connector import TCPConnector
from aiohttp.resolver import DefaultResolver

__all__ = 'ClientSession', 'DNSOverHTTPSResolver', 'RecordType'
__all__ = 'DNSOverHTTPSClientSession', 'DNSOverHTTPSResolver', 'RecordType'


class RecordType(enum.Enum):
Expand All @@ -22,13 +22,7 @@ class RecordType(enum.Enum):
class DNSOverHTTPSResolver(AbstractResolver):
"""DNS over HTTPS Resolver"""

def __init__(
self,
*,
endpoints: List[str],
json_loads=json.loads,
resolver_class=None,
) -> None:
def __init__(self, *, endpoints: List[str], json_loads=json.loads, resolver_class=None) -> None:
self.endpoints = endpoints
self.json_loads = json_loads
if resolver_class is None:
Expand All @@ -42,15 +36,14 @@ async def _resolve(self, endpoint: str, host, port, family):
record_type = RecordType.A

params = {
'ct': 'application/dns-json',
'name': host,
'type': record_type.name,
}

resolver = self.resolveer_class()
connector = TCPConnector(resolver=resolver)
async with CS(connector=connector) as session:

async with CS(connector=connector, headers={"accept": "application/dns-json"}) as session:
async with session.get(endpoint, params=params) as resp:
data = self.json_loads(await resp.text())

Expand Down Expand Up @@ -79,19 +72,15 @@ async def resolve(self, host, port=0, family=socket.AF_INET):
self._resolve(endpoint, host, port, family)
for endpoint in self.endpoints
]
done, pending = await asyncio.wait(
tasks,
return_when=asyncio.FIRST_COMPLETED,
)
for p in pending:
p.cancel()
return list(done)[0].result()
done = await asyncio.gather(*tasks)
return done[0]

async def close(self):
pass


def ClientSession(*args, **kwargs) -> CS: # noqa
# noinspection PyTypeChecker
def DNSOverHTTPSClientSession(*args, **kwargs) -> CS: # noqa
"""Shortcut of aiohttp.ClientSession and DNSOverHTTPSResolver"""

endpoints = kwargs.pop(
Expand Down
27 changes: 27 additions & 0 deletions tests/test_aiohttp_doh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest
from aiohttp import ClientSession, TCPConnector

import aiohttp_doh
from aiohttp_doh import DNSOverHTTPSClientSession

pytest_plugins = ('pytest_asyncio',)


@pytest.mark.asyncio
async def test_DNSOverHTTPSResolver():
endpoints = [
'https://dns.google.com/resolve',
'https://cloudflare-dns.com/dns-query',
]
resolver = aiohttp_doh.DNSOverHTTPSResolver(endpoints=endpoints)
connector = TCPConnector(resolver=resolver)
async with ClientSession(connector=connector) as session:
async with session.head("https://example.com") as response:
assert response.status == 200


@pytest.mark.asyncio
async def test_DNSOverHTTPSClientSession():
async with DNSOverHTTPSClientSession() as session:
async with session.head("https://example.com") as response:
assert response.status == 200