Skip to main content

DNS Name Server Framework

Project description

NServer: a high-level Python DNS Name Server Framework.

PyPi Python Versions License

NServer is a Python (3.6+) framework for building customised DNS name servers with a focuses on ease of use over completeness. It implements high level APIs for interacting with DNS queries whilst making very few assumptions about how responses are generated.

It is not intended to act like traditional DNS servers such as BIND or CoreDNS and should not be considered a general DNS resolver.

NServer has been built upon dnslib however uses high level abstractions that does not give access to the full DNS specification. If this is your desired behaviour I suggest using dnslib and its server API.

NServer has been inspired by easy to use high level frameworks such as Flask or Requests.

NServer is currently Alpha software and does not have complete documentation, testing, or implementation of certain features. Specific APIs may change in the future, but NServer uses semantic versioning so you can have more stable interfaces using version specifiers (e.g. nserver>=0.0.1,<1.0).

Installation

Install via pip

pip3 install nserver

Quick Start

from nserver import NameServer, Response, A, NS, TXT

ns = NameServer("example")

# Responses can have answers records, additional records and authority records.
@ns.rule("example.com", ["NS"])
def my_name_servers(query):
    response = Response()
    for i in range(4):
        my_ns = "ns{}.example.com".format(i + 1)
        response.answers.append(NS(query.name, my_ns))
        response.additional.append(A(my_ns, "1.1.1.1"))
    return response


# Rules can use wildcards for both single-segment (*) or many-segment (**)
# wildcards will not match mising segments (equivilent to regex `+`)
# You can also construct simple responses by return records directly.


@ns.rule("**.example.com", ["A"])
def wildcard_example(query):
    """All example.com subdomains are at 1.2.3.4."""
    return A(query.name, "1.2.3.4")


# Rules are processed in the order they are registered so if a query matches
# this rule, it will not reach the `australian_catchall` below.
# Wildcards can appear in names, but the above segment limits apply.


@ns.rule("www.*.com.au", ["A"])
def all_australian_www(query):
    return A(query.name, "5.6.7.8")


# Wildcard domains also allow special substitutions.
# base_name will provide the domain less any subdomains.


@ns.rule("hello.{base_domain}", ["TXT"])
def say_hello(query):
    if query.name.endswith(".com.au"):
        return TXT(query.name, "G'day mate")
    return TXT(query.name, "Hello friend")


# Rules can apply to multiple query types.


@ns.rule("**.com.au", ["A", "AAAA", "ANY"])
def australian_catchall(query):
    return Response()  # Empty response, avoids default NXDOMAIN


# Anything that does not match will return NXDOMAIN

if __name__ == "__main__":
    ns.settings.SERVER_PORT = 9001  # It's over 9000!
    ns.run()

Once running, interact using dig:

dig -p 9001 @localhost NS example.com

dig -p 9001 @localhost A foo.example.com
dig -p 9001 @localhost A foo.bar.example.com

dig -p 9001 @localhost A www.foo.com.au
dig -p 9001 @localhost A www.bar.com.au

dig -p 9001 @localhost TXT hello.foo.com
dig -p 9001 @localhost TXT hello.foo.com.au

dig -p 9001 @localhost A foo.com.au
dig -p 9001 @localhost AAAA foo.com.au
dig -p 9001 @localhost ANY foo.com.au

Bugs, Feature Requests etc

TLDR: Please submit an issue on github.

In the case of bug reports, please help me help you by following best practices 1 2.

In the case of feature requests, please provide background to the problem you are trying to solve so to help find a solution that makes the most sense for the library as well as your usecase. Before making a feature request consider looking at my (roughly written) design notes.

Development

The only development dependency is bash and docker. All actions are run within docker for ease of use. See ./dev.sh help for commands. Typical commands are format, lint, test, repl, build.

I am still working through open source licencing and contributing, so not taking PRs at this point in time. Instead raise and issue and I'll try get to it as soon a feasible.

Licence

This project is licenced under the MIT Licence - see LICENCE.

This project may include other open source licenced software - see NOTICE.

Authors

A project by Nicholas Hairs - www.nicholashairs.com.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nserver-0.2.0.tar.gz (18.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

nserver-0.2.0.26e6ab0-py3-none-any.whl (18.4 kB view details)

Uploaded

nserver-0.2.0-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file nserver-0.2.0.tar.gz.

File metadata

  • Download URL: nserver-0.2.0.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.9

File hashes

Hashes for nserver-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c0bf6926cae0073cfe1f74547926a7c468707869da069f44f09511c95344929c
MD5 cac358ba88ae1ec9f6c4847bfd302e30
BLAKE2b-256 162c8a49fc28a5a4ec9ba6ea79ecc0a707a151e44d2f07e93ab60d237d5ae59b

See more details on using hashes here.

File details

Details for the file nserver-0.2.0.26e6ab0-py3-none-any.whl.

File metadata

  • Download URL: nserver-0.2.0.26e6ab0-py3-none-any.whl
  • Upload date:
  • Size: 18.4 kB
  • Tags:
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.9

File hashes

Hashes for nserver-0.2.0.26e6ab0-py3-none-any.whl
Algorithm Hash digest
SHA256 f9a89650f849fcf182fd153d128dda0f30ef63165f1e3fcb981084786d22be6e
MD5 99064edb50083439d1d6ace89ddd0ad1
BLAKE2b-256 f18cb4a00a8f801ca05735dcbf51b5788c6ea30b9e3cc5cef4dcb0b65971dfbf

See more details on using hashes here.

File details

Details for the file nserver-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: nserver-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.9

File hashes

Hashes for nserver-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ca9c94174900fec64a18b7445d38a733f3bb33809c30a908e5826db2f1793d66
MD5 9e8148a723d17f0729c55c767ea1f358
BLAKE2b-256 1dcb0e996708c4bc1a46e2ffe7838e64615efa74acbfd5a68a6fc37dc73318e6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page