-
Notifications
You must be signed in to change notification settings - Fork 113
/
gout_options.go
121 lines (92 loc) · 1.96 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
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) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
opts.hc.Transport = tr
}
// 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))
}