Skip to content

Commit

Permalink
Added IP address and hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
boatware committed Mar 16, 2023
1 parent 659e73c commit b6b76c5
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package domainer

import (
"golang.org/x/net/publicsuffix"
"net"
"strconv"
"strings"
)
Expand Down Expand Up @@ -31,7 +32,11 @@ type URL struct {
// Example: "www" in "https://www.example.com:443/search?q=hello+world#test"
Subdomain string `json:"subdomain"`

// Domain represents the domain name.
// Hostname represents the hostname of the domain.
// Example: "example.com" in "https://www.example.com:443/search?q=hello+world#test"
Hostname string `json:"hostname"`

// Domain represents the domain name (or second level domain).
// Example: "example" in "https://www.example.com:443/search?q=hello+world#test"
Domain string `json:"domain"`

Expand Down Expand Up @@ -62,6 +67,10 @@ type URL struct {
// Password represents the password used to access the domain.
// Example: "pass" in "https://user:pass@example.com:443/search?q=hello+world#test"
Password string `json:"password"`

// IPAddress represents the IP address the domain resolves to.
// Example: "127.0.0.1" (obviously not a real server IP address)
IPAddress string `json:"ip_address"`
}

// FromString parses a given domain name and returns a URL struct.
Expand Down Expand Up @@ -197,6 +206,8 @@ func FromString(url string) (*URL, error) {
return nil, err
}

u.Hostname = tldPlusOne

// Split the tldPlusOne into url and tld
tldPlusOneParts := strings.Split(tldPlusOne, ".")
tld := strings.Join(tldPlusOneParts[1:], ".")
Expand All @@ -217,5 +228,12 @@ func FromString(url string) (*URL, error) {
// The rest of the url is the subdomain
u.Subdomain = strings.Join(domainParts[:len(domainParts)-1], ".")

// Get the IP address
ip, err := net.LookupIP(u.Hostname)
if err != nil {
return nil, err
}
u.IPAddress = ip[0].String()

return u, nil
}

0 comments on commit b6b76c5

Please sign in to comment.