-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.go
170 lines (144 loc) · 3.87 KB
/
middleware.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
package youcrawl
import (
"fmt"
"math/rand"
"net/http"
"net/url"
"time"
)
type Middleware interface {
// before request call
Process(c *http.Client, r *http.Request, ctx *Context)
// after request call
RequestCallback(c *http.Client, r *http.Request, ctx *Context)
}
type ProxyMiddleware struct {
List []string
}
type ProxyMiddlewareOption struct {
// set proxy list,
// if both ProxyList and ProxyFilePath are provided,combine tow list
ProxyList []string
// read proxy from file,use `./proxy.txt` by default,
// if both ProxyList and ProxyFilePath are provided,combine tow list
ProxyFilePath string
}
func NewProxyMiddleware(option ProxyMiddlewareOption) (*ProxyMiddleware, error) {
proxyList := option.ProxyList
if proxyList == nil {
proxyList = make([]string, 0)
}
list, err := readProxyListFile(option.ProxyFilePath)
if err != nil {
return nil, err
}
proxyList = append(proxyList, list...)
middleware := &ProxyMiddleware{List: proxyList}
return middleware, nil
}
func readProxyListFile(proxyPath string) ([]string, error) {
if len(proxyPath) == 0 {
proxyPath = "./proxy.txt"
}
rawList, err := ReadListFile(proxyPath)
proxyList := make([]string, 0)
// drop not valid url
for _, rawUrl := range rawList {
_, err = url.Parse(rawUrl)
if err != nil {
continue
}
proxyList = append(proxyList, rawUrl)
}
if err != nil {
return nil, err
}
return proxyList, nil
}
func (p *ProxyMiddleware) RequestCallback(c *http.Client, r *http.Request, ctx *Context) {
}
func (p *ProxyMiddleware) Process(c *http.Client, r *http.Request, ctx *Context) {
proxyURL := p.GetProxy()
if len(proxyURL) > 0 {
useProxyUrl, err := url.Parse(proxyURL)
if err != nil {
fmt.Println(err)
}
c.Transport = &http.Transport{
Proxy: http.ProxyURL(useProxyUrl),
}
}
}
func (p *ProxyMiddleware) GetProxy() string {
if len(p.List) == 0 {
return ""
}
randomIndex := rand.Intn(len(p.List))
pick := p.List[randomIndex]
return pick
}
type UserAgentMiddleware struct {
List []string
}
type UserAgentMiddlewareOption struct {
// set user agent list,
// if both UserAgentList and UserAgentFilePath are provided,combine tow list
UserAgentList []string
// read useragent from file,use `./ua.txt` by default,
// if both UserAgentList and UserAgentFilePath are provided,combine tow list
UserAgentFilePath string
}
func NewUserAgentMiddleware(option UserAgentMiddlewareOption) (*UserAgentMiddleware, error) {
UserAgentList := option.UserAgentList
if UserAgentList == nil {
UserAgentList = make([]string, 0)
}
list, err := readUserAgentListFile(option.UserAgentFilePath)
if err != nil {
return nil, err
}
UserAgentList = append(UserAgentList, list...)
middleware := &UserAgentMiddleware{List: UserAgentList}
return middleware, nil
}
func readUserAgentListFile(UserAgentPath string) ([]string, error) {
if len(UserAgentPath) == 0 {
UserAgentPath = "./ua.txt"
}
rawList, err := ReadListFile(UserAgentPath)
if err != nil {
return nil, err
}
return rawList, nil
}
func (p *UserAgentMiddleware) RequestCallback(c *http.Client, r *http.Request, ctx *Context) {
}
func (p *UserAgentMiddleware) Process(c *http.Client, r *http.Request, ctx *Context) {
uaString := p.GetUserAgent()
if len(uaString) > 0 {
r.Header.Add("User-Agent", uaString)
}
}
func (p *UserAgentMiddleware) GetUserAgent() string {
if len(p.List) == 0 {
return ""
}
randomIndex := rand.Intn(len(p.List))
pick := p.List[randomIndex]
return pick
}
type DelayMiddleware struct {
Min int
Max int
Fixed int
}
func (d *DelayMiddleware) Process(_ *http.Client, r *http.Request, _ *Context) {
if d.Fixed != 0 {
<- time.After(time.Duration(d.Fixed) * time.Second)
}else if d.Min < d.Max{
randomValue := RandomIntRangeWithStringSeed(d.Min,d.Max,r.URL.String())
<- time.After(time.Duration(randomValue) * time.Second)
}
}
func (d *DelayMiddleware) RequestCallback(_ *http.Client, _ *http.Request, _ *Context) {
}