Skip to content

A DNS Server and Resolver with ability to add custom Name Address Translation using provided CLI. Does not use any DNS package but instead uses naive approach of writing into bytes manually

Notifications You must be signed in to change notification settings

SohamJoshi25/dns-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DNS Resolver CLI

I’ve developed a Command Line Interface (CLI) tool in Go that serves as an Iterative DNS Resolver, allowing users to override default DNS responses using PostgreSQL. This project utilizes the Cobra library to provide various commands for managing custom DNS records and running the DNS server.

I built this CLI to have a personal solution for overriding DNS when needed. One of the aspects I’m most proud of is the custom DNS packet handling over UDP, implemented without relying on pre-built libraries. Implementing iterative lookups was an insightful experience, deepening my appreciation for the intricate process of Name Address Translation.

The tool supports all Resource Record types and Classes but does not implement TTL, as it's unnecessary for our custom DNS database in PostgreSQL.

Features

  • Custom DNS Parsing: It does not uses any dns packages available in go and uses the basic net/http package to create UDP DNS Response and read from an UDP connection.
  • Iterative DNS Lookup: Automatically performs iterative DNS resolution by querying root servers, TLD servers, and authoritative servers recursively. Also Caches the Itterative Resolution Rsponse.
  • Custom DNS Records: Override DNS answers with custom entries stored in a PostgreSQL database.
  • Record Management: Add, list, and remove DNS records from the database via CLI commands.
  • Modular Architecture: Separation of concerns with distinct modules for CLI, database interactions, and resolver logic.

Setting Up PostgreSQL with Docker as its required

To utilize the custom DNS records feature, ensure you have a running PostgreSQL instance. You can set this up using Docker:

docker run --name my-postgres-container   -e POSTGRES_USER=postgres   -e POSTGRES_PASSWORD=mypassword   -e POSTGRES_DB=postgres   -p 5432:5432   -d postgres:latest

This command pulls the latest PostgreSQL image from Docker Hub and runs it in a container with the specified environment variables. Learn more about using PostgreSQL with Docker.

Usage

Adding a Custom DNS Record

./dns-server add --domain example.com --answer 1.2.3.4 --type A

Listing All DNS Records

./dns-server list

Removing a DNS Record by ID

./dns-server remove 1

Starting the DNS Server

./dns-server start

By default, the server listens on 127.0.0.1:53.

Commands

Database Schema and Records

DNS Lookup

You can see my server looksup the AAAA record from internet but because A and TXT record was preset in Database, it fetched from there

Project Structure

The project follows a modular architecture:

dns-server/
├── cmd/
│   ├── add.go
│   ├── delete.go
│   ├── dns.go
│   ├── list.go
│   └── root.go
├── internal/
│   ├── dnsdb/
│   │   └── db.go
│   ├── dnslookup/
│   │   ├── constants.go
│   │   ├── lookup.go
│   │   └── types.go
│   └── dnsproxy/
│       └── dnsproxy.go
├── docs/
│   └── images/
│       ├── db.png
│       ├── image.png
│       └── lookup.png
├── .gitignore
├── dns-server.exe
├── go.mod
├── go.sum
├── main.go
├── Readme.md
└── Tood.md
  • cmd/: Contains CLI command implementations using Cobra.
  • internal/:
    • dnsdb/: Handles database interactions.
    • dnslookup/: Manages DNS resolution logic.
    • dnsproxy/: Implements DNS proxy functionalities.
  • docs/: Documentation and related images.
  • main.go: The entry point of the application.

Contributing

Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.

License

This project is licensed under the MIT License.

About

A DNS Server and Resolver with ability to add custom Name Address Translation using provided CLI. Does not use any DNS package but instead uses naive approach of writing into bytes manually

Topics

Resources

Stars

Watchers

Forks

Languages