Go: WHOIS request

Go: WHOIS request

In this article, we'll explore how to make a WHOIS query using the Go (also known as Golang) programming language.

The WHOIS protocol is an essential tool for obtaining information about resources on the Internet, such as domains, IP addresses and much more. In this article, we'll explore how to make a WHOIS query using the Go (also known as Golang) programming language. Go is known for its efficiency and ease of use, making it a great choice for implementing this type of feature.

What is WHOIS?

WHOIS is a protocol used to query public databases that contain information about the holders of domain names, IP addresses, and other resources on the Internet. Information available through the WHOIS protocol includes registrant name, contact information, and domain registration and expiration dates.

Preparation of the environment

To get started, you need to have Go installed on your system. You can download Go from the official site ( https://golang.org/dl/) and follow the installation instructions appropriate for your operating system.

Once Go is installed, you can create a new Go project in your preferred working directory.

Using Go's net package

Go offers a standard net package that allows you to easily make network requests. Let's start by creating a Go program that will make a WHOIS query to a remote WHOIS server. Here is a code example:


     package main

     import (
         "fmt"
         "net"
         "os"
     )
    
     func main() {
         // Address of the WHOIS server we want to connect to.
         whoisServer := "whois.example.com:43"
    
         // Domain for which we want to obtain WHOIS information.
         domain := "example.com"
    
         // Create a TCP connection to the WHOIS server.
         conn, err := net.Dial("tcp", whoisServer)
         if err != nil {
             fmt.Println("Error connecting to WHOIS server:", err)
             os.Exit(1)
         }
         defer conn.Close()
    
         // Send the WHOIS query to the server.
         query := domain + "\r\n"
         conn.Write([]byte(query))
    
         // We read the response from the WHOIS server.
         buffer := make([]bytes, 1024)
         n, err := conn.Read(buffer)
         if err != nil {
             fmt.Println("Error reading WHOIS response:", err)
             os.Exit(1)
         }
    
         // Print the WHOIS response.
         fmt.Println(string(buffer[:n]))
     }

In the above code, we define the address of the WHOIS server we want to connect to, the domain we want to get WHOIS information for, and then create a TCP connection to the WHOIS server using the net.Dial function .

Next, we send a WHOIS query to the server and read the response into a buffer. Finally, we print the answer on screen.

Code Execution

To run the code, be sure to replace whois.example.com with the address of the WHOIS server you want to use and example.com with the domain you want get WHOIS information.

Once you have made the changes, save the file with the ".go" extension (for example, "whois.go") in your project directory and open the terminal. Use the go run command to run the program:


go run whois.go

The program will connect to the specified WHOIS server, send the query and print the answer on the screen.

Conclusions

In this article, we have seen how to make a WHOIS query using the Go programming language. Go offers a simple and powerful library for managing network communications, making it easy to query WHOIS servers and obtain information about resources on the Internet. This is just a basic example; you can further expand the code to fit your needs or create a custom library to handle WHOIS requests more comprehensively.