forked from domainr/whois
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
163 lines (147 loc) · 4.52 KB
/
client.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package whois
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"time"
)
const (
// DefaultTimeout sets the maximum lifetime of whois requests.
DefaultTimeout = 30 * time.Second
// DefaultReadLimit sets the maximum bytes a client will attempt to read from a connection.
DefaultReadLimit = 1 << 20 // 1 MB
)
// Client represents a whois client. It contains an http.Client, for executing
// some whois Requests.
type Client struct {
Dial func(string, string) (net.Conn, error) // Deprecated, use DialContext instead
DialContext func(context.Context, string, string) (net.Conn, error) // Only used for port 43 (whois) requests, not HTTP(S)
HTTPClient *http.Client // If nil, http.DefaultClient will be used
Timeout time.Duration // Deprecated (use a Context instead)
}
// DefaultClient represents a shared whois client with a default timeout, HTTP
// transport, and dialer.
var DefaultClient = NewClient(DefaultTimeout)
// NewClient creates and initializes a new Client with the specified timeout.
func NewClient(timeout time.Duration) *Client {
return &Client{
Timeout: timeout,
}
}
func (c *Client) dialContext(ctx context.Context, network, address string) (net.Conn, error) {
var conn net.Conn
var err error
switch {
case c.DialContext != nil:
conn, err = c.DialContext(ctx, network, address)
case c.Dial != nil:
conn, err = c.Dial(network, address)
default:
conn, err = defaultDialer.DialContext(ctx, network, address)
}
if err != nil {
return nil, err
}
if deadline, ok := ctx.Deadline(); ok {
err = conn.SetDeadline(deadline)
}
return conn, err
}
var defaultDialer = &net.Dialer{}
// FetchError reports the underlying error and includes the target host of the fetch operation.
type FetchError struct {
Err error
Host string
}
// Error implements the error interface.
func (f *FetchError) Error() string {
return f.Err.Error()
}
// Fetch sends the Request to a whois server.
func (c *Client) Fetch(req *Request) (*Response, error) {
return c.FetchContext(context.Background(), req)
}
// FetchContext sends the Request to a whois server.
// If ctx cancels or times out before the request completes, it will return an error.
func (c *Client) FetchContext(ctx context.Context, req *Request) (*Response, error) {
if c.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, c.Timeout)
defer cancel()
}
if req.URL != "" {
return c.fetchHTTP(ctx, req)
}
return c.fetchWhois(ctx, req)
}
func (c *Client) fetchWhois(ctx context.Context, req *Request) (*Response, error) {
if req.Host == "" {
return nil, &FetchError{fmt.Errorf("no request host for %s", req.Query), "unknown"}
}
conn, err := c.dialContext(ctx, "tcp", req.Host+":43")
if err != nil {
return nil, &FetchError{err, req.Host}
}
defer conn.Close()
if _, err = conn.Write(req.Body); err != nil {
logError(err)
return nil, &FetchError{err, req.Host}
}
res := NewResponse(req.Query, req.Host)
if res.Body, err = ioutil.ReadAll(io.LimitReader(conn, DefaultReadLimit)); err != nil {
logError(err)
return nil, &FetchError{err, req.Host}
}
res.DetectContentType("")
return res, nil
}
func (c *Client) fetchHTTP(ctx context.Context, req *Request) (*Response, error) {
hreq, err := httpRequest(ctx, req)
if err != nil {
return nil, &FetchError{err, req.Host}
}
hc := c.HTTPClient
if hc == nil {
hc = http.DefaultClient
}
hres, err := hc.Do(hreq)
if err != nil {
return nil, &FetchError{err, req.Host}
}
res := NewResponse(req.Query, req.Host)
if res.Body, err = ioutil.ReadAll(io.LimitReader(hres.Body, DefaultReadLimit)); err != nil {
logError(err)
return nil, &FetchError{err, req.Host}
}
res.DetectContentType(hres.Header.Get("Content-Type"))
return res, nil
}
func httpRequest(ctx context.Context, req *Request) (*http.Request, error) {
var hreq *http.Request
var err error
// POST if non-zero Request.Body
if len(req.Body) > 0 {
hreq, err = http.NewRequest("POST", req.URL, bytes.NewReader(req.Body))
} else {
hreq, err = http.NewRequest("GET", req.URL, nil)
}
if err != nil {
return nil, err
}
// Some web whois servers require a Referer header
hreq.Header.Add("Referer", req.URL)
return hreq.WithContext(ctx), nil
}
func logError(err error) {
switch t := err.(type) {
case net.Error:
fmt.Fprintf(os.Stderr, "net.Error timeout=%t, temp=%t: %s\n", t.Timeout(), t.Temporary(), err.Error())
default:
fmt.Fprintf(os.Stderr, "Unknown error %v: %s\n", t, err.Error())
}
}