Easily build a DNS server using Twisted

What is this?

Twisted makes it easy to build a DNS server, and until I knew that, I was building a paradise with / etc / hosts. I searched to get rid of the bloated / etc / hosts. Speaking of which, if you use Twisted, why not use that library, Twisted, which is full of various things on that network? I thought. So, when I looked it up, I found that with the twistd command (Python script) that comes in after installing Twisted, if you pass dns as an argument, all you have to do is write a simple zone file.

So I will write a memo about building a DNS server (easy) using Twisted.

Simple flow from environment maintenance to construction

--Prepare the environment for Python 2.7.3 --In Version 12.3.0, Library is also supported in Python 3.3 (except for some), but it is in operation From the aspect, I decided to select the latest stable version of 2.x series.

--Install Twisted as a matter of course. --I don't want to pollute the environment, so I prepare the operating environment with easy_install & pip & virtualenv.

--Prepare the zone file --In the format of pyzone, write records such as NS and CNAME in the list and prepare a configuration file. --It seems that BIND9 format is also supported, but pyzone format is easier.

--Start the server with the twistd command --I don't know if the expression "start the server" is correct, but pass the parameter dns to start it.

Machine that has been verified for operation

Detailed procedure from environment maintenance to startup

Until the environment where Twisted can be used is prepared

> easy_install-2.7 pip
> pip install virtualenv
> rehash
> mkdir -p ~/TwisedDNS/
> cd ~/TwistedDNS
> virtualenv python 
> source python/bin/activate
(python)> rehash
(python)> pip install twisted

zone file description

--Unko.zone.

unko.zone


zone = [
    SOA(
        'unko.com',
        mname = 'ns1.unko.com',
        serial = 2013011901,
        refresh = '1H',
        retry = '1H',
        expire = '1H',
        minimum = '1H' 
    ),
   
    # NS Record 
    NS('unko.com', 'ns1.unko.com'),

    # A Record
    A('unko.com', '10.0.0.10'),
    A('blog.unko.com', '10.0.1.10'),

    # CNAME Record
    CNAME('www.unko.com', 'unko.com') 
]

Start the server

--Start the server using the twistd command.

> sudo twistd -n dns --recursive --cache --pyzone unko.zone
# -n is--With the nodaemon option, if this is attached, it will start as a process on the shell instead of the daemon. This time it's a test, so I've enabled it.
# --It feels like recursively querying an external name server with recurcive.
# --cache enables the domain cache.
# --pyzone is used to specify the zone file.

Inquire immediately with the dig command

--Use the dig command to make an inquiry.

> dig @127.0.0.1 unko.com 

; <<>> DiG 9.8.3-P1 <<>> @127.0.0.1 unko.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 47522
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;unko.com.INA

;; ANSWER SECTION:
unko.com.3600INA10.0.0.10
> dig @127.0.0.1 blog.unko.com 

; <<>> DiG 9.8.3-P1 <<>> @127.0.0.1 blog.unko.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 6450
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;blog.unko.com.INA

;; ANSWER SECTION:
blog.unko.com.3600INA10.0.1.10

;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Sat Jan 19 15:48:31 2013
;; MSG SIZE rcvd: 47
> dig @127.0.0.1 www.unko.com 

; <<>> DiG 9.8.3-P1 <<>> @127.0.0.1 www.unko.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 10221
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;www.unko.com.INA

;; ANSWER SECTION:
www.unko.com.3600INCNAMEunko.com.
unko.com.3600INA10.0.0.10

;; Query time: 3 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Sat Jan 19 15:48:36 2013
;; MSG SIZE rcvd: 60

Also try to contact unmanaged domains.

> dig @127.0.0.1 yahoo.co.jp 

; <<>> DiG 9.8.3-P1 <<>> @127.0.0.1 yahoo.co.jp
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 37453
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;yahoo.co.jp.INA

;; ANSWER SECTION:
yahoo.co.jp.224INA124.83.187.140
yahoo.co.jp.224INA203.216.243.240

;; Query time: 13 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Sat Jan 19 15:54:20 2013
;; MSG SIZE rcvd: 61

I was able to make a perfect inquiry. If you want to run it as a daemon process, you can take the -n (--nodaemon) option. I think that the twistd.pid file and the twistd.log file are created in the directory where the command is started, so you can use them as appropriate. .. unbound, nsd, and that's [BIND](https://www.isc. If you want to use it in a specific service, that is, in the internal network without using org / software / bind), this is enough.

that's all.

reference

Creating and working with a names (DNS) server

Recommended Posts

Easily build a DNS server using Twisted
Set up a mail server using Twisted
Build a web API server at explosive speed using hug
Build a go environment using Docker
Build a Pypi cache server on QNAP
Easily build a development environment with Laragon
Create your own DNS server with Twisted
Build a simple WebDAV server on Linux
Build a Samba server on Arch Linux
Build a web server on your Chromebook
Build a proxy server with nginx on multiple remote servers using Ansible
Build a local server with a single command [Mac]
Write a TCP server using the SocketServer module
Prepare a pseudo API server using GitHub Actions
Build a Django development environment using pyenv-virtualenv on Mac
Set up a file server on Ubuntu 20.04 using Samba
[Part 2] Let's build a web server on EC2 Linux
I want to easily build a model-based development environment
Build a Python virtual environment using venv (Django + MySQL ①)
Build a Python environment on your Mac using pyenv
Create a pseudo REST API server using GitHub Pages
Learning neural networks using Chainer-Creating a Web API server
CTF beginner tried to build a problem server (web) [Problem]
[Python] I tried running a local server using flask
Build a Python development environment using pyenv on MacOS
Let's easily make a math gif using Google Colaboratory
Build a lightweight Fast API development environment using Docker
DNS server in Python ....
SSH connection to a private server using a bastion server on EC2
Build a seq2seq model using keras's Functional API Model building & learning
Easily build a GCP environment for Kaggle at high speed
Build a Python environment and transfer data to the server
Build a speed of light web API server with Falcon
<Python> Build a dedicated server for Jupyter Notebook data analysis
Build a Flask development environment at low cost using Docker
Build a python environment on CentOS 7.7 for your home server