-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
69 lines (54 loc) · 1.16 KB
/
conn.go
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
package arin
import (
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"time"
)
func httpClient() *http.Client {
var d = &net.Dialer{
Timeout: 5 * time.Second,
}
var tr = &http.Transport{
Dial: d.Dial,
TLSHandshakeTimeout: 5 * time.Second,
}
return &http.Client{
Timeout: 10 * time.Second,
Transport: tr,
}
}
func makeRequest(resource, handle string) string {
url := fmt.Sprintf("http://whois.arin.net/rest/%s/%s", resource, handle)
return request(url)
}
func makeSubRequest(resource, handle, sub string) string {
url := fmt.Sprintf("http://whois.arin.net/rest/%s/%s/%s", resource, handle, sub)
return request(url)
}
func request(url string) string {
client := httpClient()
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Printf("Request Error: %v\n", err)
return ""
}
req.Header.Set("Accept", "text/plain")
resp, err := client.Do(req)
if err != nil {
log.Printf("Response Error: %v\n", err)
return ""
}
if resp.StatusCode == 404 {
return ""
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Parse Error: %v\n", err)
return ""
}
return string(data)
}