MyDNS is a DNS server application written in Go. It is designed to be simple, efficient, and easy to use.
- Lightweight and fast
- Easy to configure
- Basic DNS functionalities: query and block domains
- Load balancing
- Go 1.16 or later
- Redis 6.0 or later (optional)
-
Clone the repository:
git clone https://github.com/darwineee/MyDNS.git cd mydns
-
Build the application:
make build
The executable file will be generated in the
bin
directory.
-
Create a configuration file named
config.yaml
in the root directory of the application. Available options are demonstrated in source codeconfig.yaml
file as default values. If you want to use the default values, you can skip this step. -
Create
blacklist
andknown_hosts
files in the root directory of the application. Theblacklist
file contains a list of domain names only, whereasknown_hosts
contains a map of domain names and IP addresses. Put each entry one per line. Theblacklist
file can be used to block access to certain domains, while theknown_hosts
file can be used to resolve them. Both files are optional. Skip this step if you don't need them. -
Run the application:
make run
-
The application will start and listen for DNS queries on port 2053 using UDP by default. You can change these settings in the configuration file. However, in practice, you don't need to change them.
-
To stop the application, press
Ctrl + C
or type the commandstop
. More commands will be supported in the future. -
This application uses the system resolver. You can add more foreign name servers to the OS's resolver configuration file. For example, it is
/etc/resolv.conf
on Unix-like systems.
This application implements the RFC-1035 specification.
Local Host | Foreign
|
+---------+ +----------+ | +--------+
| | user queries | |queries | | |
| User |-------------->| |---------|->| Foreign|
| Program | | MyDNS | | | Name |
| |<--------------| |<--------|--| Server |
| | user responses| |responses| | |
+---------+ +----------+ | +--------+
| A |
cache additions | | references |
V | |
+----------+ |
| Redis | |
+----------+ |
Redis is used as a caching layer.
To handle high QPS, I implemented an event loop to handle incoming DNS queries concurrently. The event loop is as simple as below: