-
Notifications
You must be signed in to change notification settings - Fork 12
/
client.go
170 lines (149 loc) · 4.75 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
// 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"
"github.com/searKing/golang/go/net/http/httphost"
"github.com/searKing/golang/go/net/http/httpproxy"
)
type Client struct {
http.Client
Proxy *httpproxy.Proxy
Host *httphost.Host
}
// 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,
// sends HTTP Request to target by proxy url with Host replaced by proxyTarget
//
// u is the original url to send HTTP request, empty usually.
// 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 Host
// fixedProxyUrl is proxy's url, like socks5://127.0.0.1:8080
// fixedProxyTarget is as like gRPC Naming for proxy service discovery, with Host in TargetUrl replaced if not empty.
func NewClient(u, hostTarget string, proxyUrl string, proxyTarget string) (*Client, error) {
tr := DefaultTransportWithDynamicHostAndProxy
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}
c := &Client{
Client: client,
}
if hostTarget != "" {
c.Host = &httphost.Host{
HostTarget: hostTarget,
ReplaceHostInRequest: false,
}
}
if proxyUrl != "" {
c.Proxy = &httpproxy.Proxy{
ProxyUrl: proxyUrl,
ProxyTarget: proxyTarget,
}
}
return c, nil
}
// NewClientWithTarget returns a Client with http.Client and host replaced by resolver.Host
// 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 Host
func NewClientWithTarget(target string) *Client {
cli, _ := NewClient("", target, "", "")
return cli
}
// NewClientWithProxy returns a Client with http.Client with proxy set by resolver.Host
// TargetUrl is proxy's url, like socks5://127.0.0.1:8080
// Host is proxy's addr, replace the HOST in TargetUrl 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, err error) {
return c.Client.Do(RequestWithHostTarget(RequestWithProxyTarget(req, c.Proxy), c.Host))
}
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)
}