diff options
author | terminaldweller <devi@terminaldweller.com> | 2024-11-04 23:40:55 +0000 |
---|---|---|
committer | terminaldweller <devi@terminaldweller.com> | 2024-11-04 23:40:55 +0000 |
commit | d8f15ff06238dd96c62d96b5bfc4f4c3752fdeee (patch) | |
tree | 7da5f61390978b47fedac3b6d68e35f107b05afa /iana_whois.go | |
parent | fixed the wrong condition (diff) | |
download | milla-d8f15ff06238dd96c62d96b5bfc4f4c3752fdeee.tar.gz milla-d8f15ff06238dd96c62d96b5bfc4f4c3752fdeee.zip |
Diffstat (limited to 'iana_whois.go')
-rw-r--r-- | iana_whois.go | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/iana_whois.go b/iana_whois.go new file mode 100644 index 0000000..ed80c6e --- /dev/null +++ b/iana_whois.go @@ -0,0 +1,82 @@ +package main + +import ( + "log" + "net" + "net/http" + "net/url" + "time" + + "golang.org/x/net/html" + "golang.org/x/net/proxy" +) + +func IANAWhoisGet(query string, appConfig *TomlConfig) string { + var httpClient http.Client + + var dialer proxy.Dialer + + if appConfig.GeneralProxy != "" { + proxyURL, err := url.Parse(appConfig.GeneralProxy) + if err != nil { + log.Fatal(err.Error()) + + return "" + } + + dialer, err = proxy.FromURL(proxyURL, &net.Dialer{Timeout: time.Duration(appConfig.RequestTimeout) * time.Second}) + if err != nil { + log.Fatal(err.Error()) + + return "" + } + + httpClient = http.Client{ + Transport: &http.Transport{ + Dial: dialer.Dial, + }, + } + } + + resp, err := httpClient.Get("https://www.iana.org/whois?q=" + query) + if err != nil { + log.Println(err) + + return "" + } + + defer resp.Body.Close() + + doc, err := html.Parse(resp.Body) + if err != nil { + log.Println(err) + + return "" + } + + var getContent func(*html.Node) string + + getContent = func(n *html.Node) string { + if n.Type == html.ElementNode && n.Data == "pre" { + var content string + for c := n.FirstChild; c != nil; c = c.NextSibling { + if c.Type == html.TextNode { + content += c.Data + } + } + return content + } + for c := n.FirstChild; c != nil; c = c.NextSibling { + result := getContent(c) + if result != "" { + return result + } + } + return "" + } + + preContent := getContent(doc) + log.Println(preContent) + + return preContent +} |