-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
19 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package domainer | |
|
||
import ( | ||
"golang.org/x/net/publicsuffix" | ||
"net" | ||
"strconv" | ||
"strings" | ||
) | ||
|
@@ -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"` | ||
|
||
|
@@ -62,6 +67,10 @@ type URL struct { | |
// Password represents the password used to access the domain. | ||
// Example: "pass" in "https://user:[email protected]: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. | ||
|
@@ -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:], ".") | ||
|
@@ -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 | ||
} |