Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion whois.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package whois

import (
"errors"
"io"
"net"
"strings"
Expand All @@ -11,6 +12,8 @@ const (
ianaWHOISServerAddress = "whois.iana.org:43"
)

var tldWithoutExpirationDate = []string{"at","be","ch","co.at","com.br","or.at","de","fr","me","mx","nl"}

type Client struct {
whoisServerAddress string

Expand Down Expand Up @@ -49,14 +52,27 @@ func (c *Client) WithReferralCache(enabled bool) *Client {
return c
}

func doesTLDHaveExpirationDate(e string) bool {
for _, a := range tldWithoutExpirationDate {
if a == e {
return true
}
}
return false
}

func (c *Client) Query(domain string) (string, error) {
parts := strings.Split(domain, ".")
domainExtension := parts[len(parts)-1]
if doesTLDHaveExpirationDate(domainExtension) {
return "", errors.New("Domain extension " + domainExtension + " does not have a grace period.")
}
if c.isCachingReferralWHOISServers {
if cachedWHOISServer, ok := c.referralWHOISServersCache[domain]; ok {
return c.query(cachedWHOISServer, domain)
}
}
output, err := c.query(c.whoisServerAddress, parts[len(parts)-1])
output, err := c.query(c.whoisServerAddress, domainExtension)
if err != nil {
return "", err
}
Expand Down
40 changes: 24 additions & 16 deletions whois_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func TestClient(t *testing.T) {
domain: "name.black",
wantErr: false,
},
{
domain: "name.de",
wantErr: true,
},
}
client := NewClient().WithReferralCache(true)
for _, scenario := range scenarios {
Expand All @@ -56,11 +60,13 @@ func TestClient(t *testing.T) {
t.Error("expected error, got none")
t.FailNow()
}
if !scenario.wantErr && err != nil {
t.Error("expected no error, got", err.Error())
}
if !strings.Contains(strings.ToLower(output), scenario.domain) {
t.Errorf("expected %s in output, got %s", scenario.domain, output)
if !scenario.wantErr {
if err != nil {
t.Error("expected no error, got", err.Error())
}
if !strings.Contains(strings.ToLower(output), scenario.domain) {
t.Errorf("expected %s in output, got %s", scenario.domain, output)
}
}
})
time.Sleep(50 * time.Millisecond) // Give the WHOIS servers some breathing room
Expand All @@ -70,17 +76,19 @@ func TestClient(t *testing.T) {
t.Error("expected error, got none")
t.FailNow()
}
if !scenario.wantErr && err != nil {
t.Error("expected no error, got", err.Error())
}
if response.ExpirationDate.Unix() == 0 {
t.Errorf("expected to have an expiry date")
}
if len(response.NameServers) == 0 {
t.Errorf("expected to have at least one name server")
}
if len(response.DomainStatuses) == 0 {
t.Errorf("expected to have at least one domain status")
if !scenario.wantErr {
if err != nil {
t.Error("expected no error, got", err.Error())
}
if response.ExpirationDate.Unix() == 0 {
t.Errorf("expected to have an expiry date")
}
if len(response.NameServers) == 0 {
t.Errorf("expected to have at least one name server")
}
if len(response.DomainStatuses) == 0 {
t.Errorf("expected to have at least one domain status")
}
}
})
time.Sleep(50 * time.Millisecond) // Give the WHOIS servers some breathing room
Expand Down