Skip to content

Commit

Permalink
Add whois
Browse files Browse the repository at this point in the history
  • Loading branch information
alex27riva committed Oct 18, 2024
1 parent cdad95c commit 6d2f472
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
54 changes: 54 additions & 0 deletions cmd/whois.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright © 2024 Alessandro Riva
Licensed under the MIT License.
See the LICENSE file for details.
*/
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"soc-cli/internal/apis"
)

func displayData(domainInfo apis.DomainInfo) {

fmt.Println("Domain Information:")
fmt.Printf("Domain Name: %s\n", domainInfo.Domain.Domain)
fmt.Printf("Domain ID: %s\n", domainInfo.Domain.ID)
fmt.Printf("Extension: %s\n", domainInfo.Domain.Extension)
fmt.Printf("Whois Server: %s\n", domainInfo.Domain.WhoisServer)
fmt.Printf("Status: %v\n", domainInfo.Domain.Status)
fmt.Printf("Created Date: %s\n", domainInfo.Domain.CreatedDate)
fmt.Printf("Updated Date: %s\n", domainInfo.Domain.UpdatedDate)
fmt.Printf("Expiration Date: %s\n", domainInfo.Domain.ExpirationDate)

fmt.Println("\nRegistrar Information:")
fmt.Printf("Registrar Name: %s\n", domainInfo.Registrar.Name)
fmt.Printf("Registrar Phone: %s\n", domainInfo.Registrar.Phone)
fmt.Printf("Registrar Email: %s\n", domainInfo.Registrar.Email)

fmt.Println("\nRegistrant Information:")
fmt.Printf("Registrant Name: %s\n", domainInfo.Registrant.Name)
fmt.Printf("Registrant Organization: %s\n", domainInfo.Registrant.Organization)
fmt.Printf("Registrant Country: %s\n", domainInfo.Registrant.Country)
fmt.Printf("Registrant Email: %s\n", domainInfo.Registrant.Email)
}

var whoisCmd = &cobra.Command{
Use: "whois [domain]",
Short: "Perform a WHOIS lookup on a domain",
Long: `Queries the who-dat.as93.net API to perform a WHOIS lookup on the specified domain.`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
target := args[0]

whoisData := apis.GetWhoisData(target)
displayData(*whoisData)
},
}

func init() {
rootCmd.AddCommand(whoisCmd)
}
78 changes: 78 additions & 0 deletions internal/apis/whodat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright © 2024 Alessandro Riva
Licensed under the MIT License.
See the LICENSE file for details.
*/
package apis

import (
"fmt"
"log"
"soc-cli/internal/util"
)

const whodatAPIURL = "https://who-dat.as93.net/%s"

// Domain represents the domain information
type Domain struct {
ID string `json:"id"`
Domain string `json:"domain"`
Punycode string `json:"punycode"`
Name string `json:"name"`
Extension string `json:"extension"`
WhoisServer string `json:"whois_server"`
Status []string `json:"status"`
NameServers []string `json:"name_servers"`
CreatedDate string `json:"created_date"`
CreatedDateInTime string `json:"created_date_in_time"`
UpdatedDate string `json:"updated_date"`
UpdatedDateInTime string `json:"updated_date_in_time"`
ExpirationDate string `json:"expiration_date"`
ExpirationDateInTime string `json:"expiration_date_in_time"`
}

// Registrar represents the registrar information
type Registrar struct {
ID string `json:"id"`
Name string `json:"name"`
Phone string `json:"phone"`
Email string `json:"email"`
ReferralURL string `json:"referral_url"`
}

// Contact represents the contact information (registrant, administrative, technical)
type Contact struct {
ID string `json:"id"`
Name string `json:"name"`
Organization string `json:"organization"`
Street string `json:"street"`
City string `json:"city"`
Province string `json:"province"`
PostalCode string `json:"postal_code"`
Country string `json:"country"`
Phone string `json:"phone"`
Email string `json:"email"`
}

// DomainInfo represents the entire structure of the JSON
type DomainInfo struct {
Domain Domain `json:"domain"`
Registrar Registrar `json:"registrar"`
Registrant Contact `json:"registrant"`
Administrative Contact `json:"administrative"`
Technical Contact `json:"technical"`
}

func GetWhoisData(domain string) *DomainInfo {
apiUrl := fmt.Sprintf(whodatAPIURL, domain)

var whois DomainInfo

err := util.MakeAPIRequest(apiUrl, nil, &whois)
if err != nil {
log.Fatalf("Error fetching whodat API: %v", err)
}

return &whois
}

0 comments on commit 6d2f472

Please sign in to comment.