Git Product home page Git Product logo

dnserver's Issues

Enhancement Request: Facilitate "Catch All"

As a testing/development DNS server, it would be useful to allow some kind of "catch all". For example, it might be useful for dnserver to respond to a request for an A record with a specific IP address (e.g. 10.11.12.13) regardless of what domain was actually queried.

Use cases might be:

  1. Debugging DNS functionality.
  2. Analysing malware where the domain to be requested is not yet known, but we want to redirect to a server under the analyst's control.

An example toml entry might be:

[[zones]]
host = '__CATCH_ALL'
type = 'A'
answer = '10.11.12.13'

Websites

I used your code, and the server works, but websites dont load

Update zones while the server is running

I use dnserver in addition to pytest-httpserver to build a full fake network environment for unit tests.

pytest-httpserver can open a httpserver on a random port, and has utilities to control the server response within the unit tests, which is very convenient:

def test_my_client(httpserver):
    httpserver.expect_request("/foobar").respond_with_json({"foo": "bar"})
    assert requests.get(httpserver.url_for("/foobar")).json() == {"foo": "bar"}

I a unit test scenario, suppose I want to run a DNS SRV request to get the hostname and the port for a given service (for instance caldav), then simulate that service with pytest-httpserver.

I would

  • need to choose a port and write it in a fixture toml zone file
  • hope that the port will be free at the moment the test will be ran
  • run a test httpserver that will listen to that specific port

Having to rely on the port being free at the moment the test is running is far from ideal. Manually setting a port for a pytest-httpserver is quite cumbersome and makes you write a lot of fixtures.

For those reasons I think having a way to dynamically set the DNS responses while the server is running (in fixtures or directly in unit tests) would be great. I dream of something like this:

async def test_caldav(dnserver, httpserver):
    dnserver.add_zone(host='caldav.example.com', type='A', answer='127.0.0.1')
    dnserver.add_zone(host='_caldav._tcp.example.com', type='SRV', answer=[0, 1, httpserver.port, 'caldav.example.com.'])

    httpserver.expect_request("/").respond_with_data("some caldav response")

    resolver_kwargs = dict(nameservers=[dnserver.host], tcp_port=dnserver.port, udp_port=dnserver.port)
    http_resolver = aiohttp.resolver.AsyncResolver(**resolver_kwargs)
    dns_resolver = aiodns.DNSResolver(**resolver_kwargs)

    dns_answer = dns_resolver.query(f"_caldav._tcp.example.com", "SRV")
    caldav_url = f"http://{dns_answer[0].host}:{dns_answer[0].port}"

    connector = aiohttp.TCPConnector(resolver=http_resolver)
    async with aiohttp.ClientSession(connector=connector) as session:
        async with session.get(caldav_url) as result:
            assert (await result.text()) == "some caldav response"

Though some of this might be out of scope for dnserver and better suited in a pytest-dnserver package.

Any thoughts?

Let upstream be `None`

Hi. Thank you again for your amazing work!

I can now use dnserver in unit tests ๐Ÿš€

@pytest.fixture(scope="session")
def dns_server():
    from dnserver import DNSServer

    server = DNSServer(
        zones_file="tests/fixtures/zones.toml", port=DNS_PORT, upstream=None
    )
    server.start()
    yield server
    server.stop()

However I would like to be able to disable any upstream DNS server, so my unit tests won't make any network I/O. At the moment this is prevented by this line:

self.upstream: str = DEFAULT_UPSTREAM if upstream is None else upstream

What do you think?

Release on pypi

Once (an if) #2 is merged, this would be awesome to release the project on pypi so the DNSServer class could be used in other projects unit tests.

What do you think?

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.