Skip to content

Commit

Permalink
feat: add support for .vn domain (#244)
Browse files Browse the repository at this point in the history
* Add adapter for .vn domain

* [internal/whois] impl adapter_vn
  • Loading branch information
vucong2409 authored Jan 25, 2024
1 parent 523d38b commit 8fd3c3e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
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

0 comments on commit 8fd3c3e

Please sign in to comment.