DEV Community

Ericson Willians
Ericson Willians

Posted on

Building a Robust Domain Checker with DNS and WHOIS in Python

In today's fast-paced digital landscape, securing the perfect domain name for your startup or project is more crucial than ever. With thousands of domain names and TLDs available, manually checking whether your desired domain is available can be a daunting task. In this article, I'll show you how to build a robust domain checker in Python that performs DNS queries and optional WHOIS lookups—all while providing an elegant command-line interface (CLI) powered by Typer and beautiful terminal output using Rich.

I've hosted the complete, well-documented code on GitHub Gist. You can view and download it from the link below:

Check out the complete domain checker script on GitHub Gist


What Does This Domain Checker Do?

This script enables you to:

  • Check DNS Records:

    Leverage dnspython to query multiple DNS record types (A, AAAA, MX, NS, CNAME) for a domain. This gives you a clear idea if a domain is actively resolving.

  • Perform WHOIS Lookups:

    Optionally perform WHOIS queries (using python-whois) to further verify if a domain is registered. This adds an extra layer of validation beyond DNS responses.

  • Concurrent Checks:

    Use Python’s concurrent.futures to run domain checks in parallel, significantly speeding up the process, especially when checking multiple TLDs.

  • Beautiful Output:

    Display the results in a well-formatted table with progress tracking using Rich. The output is colorized for easy reading.


Why Build Your Own Domain Checker?

While there are several online tools to check domain availability, building your own has many advantages:

  • Customization:

    You can tailor the tool to meet your specific needs—whether it's integrating additional checks or customizing the output format.

  • Learning Opportunity:

    This project is a great way to deepen your understanding of DNS, WHOIS, and Python's concurrency capabilities.

  • Avoiding Shady Practices:

    By running your own tool locally, you reduce the risk of exposing your domain search queries to third-party services that might engage in questionable practices like domain name front running.


The Tools & Technologies

  • Python 3.12+: The programming language used to build the tool.
  • Typer: For creating a sleek and user-friendly CLI.
  • Rich: To generate beautiful, formatted terminal output.
  • dnspython: To perform advanced DNS queries.
  • python-whois: To retrieve WHOIS information.

Before running the script, install the required packages:

pip install typer rich dnspython python-whois
Enter fullscreen mode Exit fullscreen mode

A Closer Look at the Code

The code is divided into several key sections:

1. Module Imports and Initialization

The script begins by importing necessary modules. It attempts to import dnspython for DNS resolution and python-whois for WHOIS lookups. If the WHOIS module is not installed, the script will still work but will skip the WHOIS checks.

2. DNS Query Function

The perform_dns_check function queries the domain for various DNS record types (A, AAAA, MX, NS, and CNAME). It then returns the results as a dictionary, which forms the backbone of our domain availability check.

3. WHOIS Lookup Function

The perform_whois_check function uses the WHOIS protocol to determine if a domain is registered. It returns a simple status: "Taken" if the domain is registered or "Available" if not.

4. Domain Check Aggregator

The check_domain function combines both the DNS and WHOIS checks to determine the overall status of the domain. It uses key DNS records (A, AAAA, and NS) as a heuristic to decide if the domain is active, and refines the result with WHOIS data when available.

5. Concurrent Execution and Rich Output

Finally, the main function leverages concurrent.futures.ThreadPoolExecutor to run the domain checks concurrently. The results are then displayed in a color-coded table using Rich, with progress tracking provided by Rich’s track function.


Domain Name Front Running: The Hidden Dangers of Online Domain Searches

One of the lesser-discussed yet highly concerning issues when searching for domain names online is domain name front running. This practice involves unscrupulous registrars or third-party services that monitor your domain name queries. When you search for a domain, these services may quickly register the name themselves—often before you have a chance to purchase it. The registered domain is then resold at a significantly inflated price or held for auction.

Why Is This Shady?

  • Exploitation of User Interest:

    Your search for a domain can signal market interest, prompting some services to preemptively register the domain to profit off your idea.

  • Inflated Prices:

    Once a domain is front-run, you're often forced to pay a premium, sometimes paying far more than the original registration cost.

  • Limited Transparency:

    Most users have no idea that their search queries are being logged or exploited. This lack of transparency puts you at a disadvantage.

  • Lost Opportunity:

    For startups and new projects, securing the ideal domain is critical. Front running can force you to settle for a less desirable name or pay an unreasonable price.

How Does Our Domain Checker Help?

By building and running your own domain checker locally, you can avoid some of these pitfalls:

  • Local Resolution:

    The tool performs DNS queries directly from your machine, avoiding centralized search services that might log and exploit your queries.

  • Transparency and Control:

    With access to the source code, you can ensure that your search queries are handled securely and without unnecessary external communication.

  • Comprehensive Checks:

    Combining DNS and WHOIS lookups provides a more reliable picture of a domain's status, reducing the uncertainty that can lead to front running.


Running the Script

After installing the dependencies, run the script from your terminal. For example:

python domain_checker.py mycoolstartup --tlds "com,net,org,io" --whois --timeout 5
Enter fullscreen mode Exit fullscreen mode

This command checks the availability of mycoolstartup across the specified TLDs, performs WHOIS lookups, and sets a 5-second timeout for DNS queries. The output is presented in a beautifully formatted table, showing the status of each domain along with DNS records and WHOIS results.


Conclusion

Building a robust domain checker is not only a practical tool for securing the perfect domain name but also an excellent opportunity to learn about DNS, WHOIS, and Python's powerful concurrency features. Moreover, by developing your own solution, you protect yourself from shady practices like domain name front running, ensuring that your domain search queries remain private and secure.

I hope you find this project as exciting and useful as I did. If you have any comments, suggestions, or improvements, please feel free to share them. Happy coding!


Link to the full code on GitHub Gist: https://gist.github.com/EricsonWillians/77a6e7568c8a72aff22ab44b23ec56fb


Enter fullscreen mode Exit fullscreen mode

Top comments (0)