-
Notifications
You must be signed in to change notification settings - Fork 12
/
client.go
172 lines (153 loc) · 4.94 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
164
165
166
167
168
169
170
171
172
// Copyright 2022 The searKing Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package http
import (
"context"
"io"
"net"
"net/http"
"net/url"
"strings"
"time"
)
type Client struct {
http.Client
target string // resolver.Target, will replace Host in url.Url
replaceHostInRequest bool // resolve target to proxy and replace host if target resolved
}
// Use adds middleware handlers to the transport.
func (c *Client) Use(d ...RoundTripDecorator) *Client {
if len(d) == 0 {
return c
}
var rts RoundTripDecorators
rts = append(rts, d...)
c.Transport = rts.WrapRoundTrip(c.Transport)
// for chained call
return c
}
// parseURL is just url.Parse. It exists only so that url.Parse can be called
// in places where url is shadowed for godoc. See https://golang.org/cl/49930.
var parseURL = url.Parse
// NewClient returns a http client wrapper behaves like http.Client
// u is the original url to send HTTP request
// target is the resolver to resolve Host to send HTTP request,
// that is replacing host in url(NOT HOST in http header) by address resolved by target
// proxyUrl is proxy's url, like sock5://127.0.0.1:8080
// proxyTarget is proxy's addr, replace the HOST in proxyUrl if not empty
func NewClient(u, target string, proxyUrl string, proxyTarget string) (*Client, error) {
tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
if proxyUrl != "" && proxyTarget != "" {
tr.Proxy = ProxyFuncWithTargetOrDefault(proxyUrl, proxyTarget, tr.Proxy)
}
if len(u) > 0 {
urlParsed, err := parseURL(u)
if err != nil {
return nil, err
}
hostname := urlParsed.Hostname()
if strings.Index(hostname, "unix:") == 0 {
tr = &http.Transport{
DisableCompression: true,
DialContext: func(ctx context.Context, network, addr string) (conn net.Conn, e error) {
return net.Dial("unix", urlParsed.Host)
},
}
}
}
client := http.Client{Transport: tr}
return &Client{
Client: client,
target: target,
replaceHostInRequest: false,
}, nil
}
// NewClientWithTarget returns a Client with http.Client and host replaced by resolver.Target
// target is the resolver to resolve Host to send HTTP request,
// that is replacing host in url(NOT HOST in http header) by address resolved by target
func NewClientWithTarget(target string) *Client {
cli, _ := NewClient("", target, "", "")
return cli
}
// NewClientWithProxy returns a Client with http.Client with proxy set by resolver.Target
// proxyUrl is proxy's url, like sock5://127.0.0.1:8080
// proxyTarget is proxy's addr, replace the HOST in proxyUrl if not empty
func NewClientWithProxy(proxyUrl string, proxyTarget string) *Client {
cli, _ := NewClient("", "", proxyUrl, proxyTarget)
return cli
}
func NewClientWithUnixDisableCompression(u string) (*Client, error) {
return NewClient(u, "", "", "")
}
func (c *Client) Do(req *http.Request) (*http.Response, error) {
err := RequestWithTarget(req, c.target, c.replaceHostInRequest)
if err != nil {
return nil, err
}
return c.Client.Do(req)
}
func (c *Client) Head(url string) (resp *http.Response, err error) {
req, err := http.NewRequest(http.MethodHead, url, nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
func (c *Client) Get(url string) (resp *http.Response, err error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
func (c *Client) Post(url string, contentType string, body io.Reader) (resp *http.Response, err error) {
req, err := http.NewRequest(http.MethodPost, url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
return c.Do(req)
}
func (c *Client) PostForm(url string, data url.Values) (resp *http.Response, err error) {
return c.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
}
func Head(url string) (resp *http.Response, err error) {
client, err := NewClientWithUnixDisableCompression(url)
if err != nil {
return nil, err
}
return client.Head(url)
}
func Get(url string) (resp *http.Response, err error) {
client, err := NewClientWithUnixDisableCompression(url)
if err != nil {
return nil, err
}
return client.Get(url)
}
func Post(url, contentType string, body io.Reader) (resp *http.Response, err error) {
client, err := NewClientWithUnixDisableCompression(url)
if err != nil {
return nil, err
}
return client.Post(url, contentType, body)
}
func PostForm(url string, data url.Values) (resp *http.Response, err error) {
client, err := NewClientWithUnixDisableCompression(url)
if err != nil {
return nil, err
}
return client.PostForm(url, data)
}