Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for .vn domain #244

Merged
merged 2 commits into from
Jan 25, 2024
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
67 changes: 67 additions & 0 deletions internal/whois/adapter_vn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package whois

import (
"net/url"
"strings"

"github.com/domainr/whois"
)

// Adapter for whois.net.vn
type vnAdapter struct{}

func (a *vnAdapter) Prepare(req *whois.Request) error {
req.URL = generateVnWhoisRequestUrl(req.Query)
// Override request body to avoid any conflict.
req.Body = nil

return nil
}

func (a *vnAdapter) Text(res *whois.Response) ([]byte, error) {
return parseVnWhoisResponseBody(res.Body), nil
}

func init() {
whois.BindAdapter(
&vnAdapter{},
"whois.net.vn",
)
}

// Generate URL for .vn whois request.
// Query sent to whois.net.vn should go to `/whois.php` route with `act` & `domain` parameters.
func generateVnWhoisRequestUrl(query string) string {
whoisEndpoint := "https://whois.net.vn/whois.php?"

whoisQueryParams := url.Values{}
whoisQueryParams.Set("act", "getwhois")
whoisQueryParams.Set("domain", query)

return whoisEndpoint + whoisQueryParams.Encode()
}

// Remove HTML Tags, tab characters, exceeding spaces, correct some keys and generate a registar field
// to avoid parsing errors in later step.
func parseVnWhoisResponseBody(bodyContent []byte) []byte {
htmlTags := []string{"<br/>", "<div>", "</div>"}

resBodyString := string(bodyContent)

// Remove HTML tag (<br>, <div>).
for _, htmlTag := range htmlTags {
resBodyString = strings.ReplaceAll(resBodyString, htmlTag, "")
}

// Remove tab characters.
resBodyString = strings.ReplaceAll(resBodyString, "\t", "")

// Remove exceeding spaces & correct some keys in response.
resBodyString = strings.ReplaceAll(resBodyString, "Expired Date : ", "Expire Date:")
resBodyString = strings.ReplaceAll(resBodyString, "Issue Date : ", "Issue Date:")

// Generate Registrar field.
resBodyString = "Registrar WHOIS Server: Not available" + resBodyString

return []byte(resBodyString)
}
7 changes: 6 additions & 1 deletion internal/whois/whois.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,12 @@ func (c whoisClient) request(ctx context.Context, domain, host string) (string,
if err != nil {
return "", fmt.Errorf("failed to fetch whois request: %w", err)
}
body := string(resp.Body)
respText, err := resp.Text()
if err != nil {
return "", fmt.Errorf("failed to parse response body into text: %w", err)
}

body := string(respText)

if host == "" {
// do not recurse
Expand Down
3 changes: 2 additions & 1 deletion internal/whois/whois_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ func TestWhoisParsing(t *testing.T) {
{domain: "GOOGLE.RS", host: "", err: ""},
{domain: "google.co.th", host: "", err: ""},
{domain: "google.fi", host: "", err: ""},
{domain: "google.vn", host: "whois.net.vn", err: ""},
} {
tt := tt
t.Run(tt.domain, func(t *testing.T) {
t.Parallel()
is := is.New(t)

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
t.Cleanup(cancel)

expiry, err := NewClient().ExpireTime(ctx, tt.domain, tt.host)
Expand Down
Loading