-
Notifications
You must be signed in to change notification settings - Fork 115
/
gout_options.go
127 lines (97 loc) · 2.09 KB
/
gout_options.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
package gout
import (
"crypto/tls"
"net/http"
"time"
"github.com/guonaihong/gout/hcutil"
"github.com/guonaihong/gout/setting"
)
type options struct {
hc *http.Client
setting.Setting
err error
}
type Option interface {
apply(*options)
}
// 1.start
type insecureSkipVerifyOption bool
func (i insecureSkipVerifyOption) apply(opts *options) {
if opts.hc.Transport == nil {
opts.hc.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
return
}
opts.hc.Transport.(*http.Transport).TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
}
// 1.忽略ssl验证
func WithInsecureSkipVerify() Option {
b := true
return insecureSkipVerifyOption(b)
}
// 2. start
type client http.Client
func (c *client) apply(opts *options) {
opts.hc = (*http.Client)(c)
}
// 2.自定义http.Client
func WithClient(c *http.Client) Option {
return (*client)(c)
}
// 3.start
type close3xx struct{}
func (c close3xx) apply(opts *options) {
opts.hc.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}
// 3.关闭3xx自动跳转
func WithClose3xxJump() Option {
return close3xx{}
}
// 4.timeout
type timeout time.Duration
func WithTimeout(t time.Duration) Option {
return (*timeout)(&t)
}
func (t *timeout) apply(opts *options) {
opts.SetTimeout(time.Duration(*t))
}
// 5. 设置代理
type proxy string
func WithProxy(p string) Option {
return (*proxy)(&p)
}
func (p *proxy) apply(opts *options) {
if opts.hc == nil {
opts.hc = &http.Client{}
}
opts.err = hcutil.SetProxy(opts.hc, string(*p))
}
// 6. 设置socks5代理
type socks5 string
func WithSocks5(s string) Option {
return (*socks5)(&s)
}
func (s *socks5) apply(opts *options) {
if opts.hc == nil {
opts.hc = &http.Client{}
}
opts.err = hcutil.SetSOCKS5(opts.hc, string(*s))
}
// 7. 设置unix socket
type unixSocket string
func WithUnixSocket(u string) Option {
return (*unixSocket)(&u)
}
func (u *unixSocket) apply(opts *options) {
if opts.hc == nil {
opts.hc = &http.Client{}
}
opts.err = hcutil.UnixSocket(opts.hc, string(*u))
}