-
Notifications
You must be signed in to change notification settings - Fork 12
/
transport.proxy.go
120 lines (108 loc) · 4.36 KB
/
transport.proxy.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
// 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 (
"net"
"net/http"
"net/url"
"time"
"github.com/searKing/golang/go/net/http/httpproxy"
"github.com/searKing/golang/go/net/resolver"
)
// RequestWithProxyTarget returns a shallow copy of r with its context changed
// to ctx, TargetUrl and Host inside. The provided ctx must be non-nil.
// proxyUrl is proxy's url, like socks5://127.0.0.1:8080
// proxyTarget is as like gRPC Naming for proxy service discovery, with Host in TargetUrl replaced if not empty.
func RequestWithProxyTarget(req *http.Request, proxy *httpproxy.Proxy) *http.Request {
if proxy == nil {
return req
}
return req.WithContext(httpproxy.WithProxy(req.Context(), proxy))
}
// ProxyFuncFromContextOrEnvironment builds a proxy function from the given string, which should
// represent a Target that can be used as a proxy. It performs basic
// sanitization of the Target retrieved in context of Request, and returns any error encountered.
func ProxyFuncFromContextOrEnvironment(req *http.Request) (*url.URL, error) {
proxy := httpproxy.ContextProxy(req.Context())
// load proxy from environment if proxy not set
if proxy == nil || proxy.ProxyUrl == "" {
return http.ProxyFromEnvironment(req)
}
proxyUrl, err := httpproxy.ParseProxyUrl(proxy.ProxyUrl)
if err != nil {
return nil, err
}
if proxyUrl == nil {
return nil, nil
}
if proxy.ProxyTarget == "" {
return proxyUrl, nil
}
// replace host of proxy if target of proxy if resolved
address, err := resolver.ResolveOneAddr(req.Context(), proxy.ProxyTarget)
if err != nil {
return nil, err
}
if address.Addr != "" {
proxyUrl.Host = address.Addr
}
proxy.ProxyAddrResolved = address
return proxyUrl, nil
}
// DefaultTransportWithDynamicProxy is the default implementation of Transport and is
// used by DefaultClientWithDynamicProxy. It establishes network connections as needed
// and caches them for reuse by subsequent calls. It uses HTTP proxies
// as directed by the ProxyFuncFromContextOrEnvironment, $HTTP_PROXY and $NO_PROXY (or $http_proxy and
// $no_proxy) environment variables.
var DefaultTransportWithDynamicProxy http.RoundTripper = &http.Transport{
Proxy: ProxyFuncFromContextOrEnvironment,
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,
}
// DefaultClientWithDynamicProxy is the default Client with DefaultTransportWithDynamicProxy.
var DefaultClientWithDynamicProxy = &http.Client{
Transport: DefaultTransportWithDynamicProxy,
}
// ProxyFuncWithTargetOrDefault builds a proxy function from the given string, which should
// represent a Target that can be used as a proxy. It performs basic
// sanitization of the Target and returns any error encountered.
// 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 ProxyFuncWithTargetOrDefault(fixedProxyUrl string, fixedProxyTarget string, def func(req *http.Request) (*url.URL, error)) func(req *http.Request) (*url.URL, error) {
if fixedProxyUrl == "" {
return def
}
proxy, err := httpproxy.ParseProxyUrl(fixedProxyUrl)
if err != nil {
return func(req *http.Request) (*url.URL, error) {
return nil, err
}
}
if proxy == nil || fixedProxyTarget == "" {
return func(req *http.Request) (*url.URL, error) {
return proxy, nil
}
}
return func(req *http.Request) (*url.URL, error) {
req2 := RequestWithProxyTarget(req, &httpproxy.Proxy{
ProxyUrl: fixedProxyUrl,
ProxyTarget: fixedProxyTarget,
})
return ProxyFuncFromContextOrEnvironment(req2)
}
}
// TransportWithProxyTarget wraps http.RoundTripper with request url replaced by Target resolved by resolver.
// 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 TransportWithProxyTarget(t *http.Transport, fixedProxyUrl string, fixedProxyTarget string) *http.Transport {
t.Proxy = ProxyFuncWithTargetOrDefault(fixedProxyUrl, fixedProxyTarget, t.Proxy)
return t
}