aboutsummaryrefslogtreecommitdiffstats
path: root/iana_whois.go
blob: ed80c6ecc1175987ef4758a4dbbe8fec98087573 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
}