-
Notifications
You must be signed in to change notification settings - Fork 6
/
proxy.go
93 lines (84 loc) · 2.49 KB
/
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
package main
import (
"context"
"fmt"
"io"
"log"
"math/rand"
"net"
"net/http"
"net/url"
"strings"
"time"
)
var proxyPool map[string]string
// registerProxy register proxy into proxy pool
func registerProxy(name, addr string) error {
log.Printf("register new proxy: %v ipaddr: %v\n", name, addr)
temp := strings.Split(addr, ":")
if len(temp) != 2 {
return fmt.Errorf("invalid net addr:%v", addr)
}
proxyPool[name] = addr
return nil
}
// Request use http method to request the spec url
func Request(method, url string, body io.Reader) (resp *http.Response, err error) {
request, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
request.Header.Set("User-Agent", userAgent())
return proxyClient().Do(request)
}
func proxyClient() *http.Client {
if len(proxyPool) == 0 {
return http.DefaultClient
}
proxyAddr := getProxyAddr()
proxy, err := url.Parse(proxyAddr)
if err != nil {
log.Fatalf("parse url failed: %v", err)
}
netTransport := &http.Transport{
Proxy: http.ProxyURL(proxy),
DialContext: func(context context.Context, netw, addr string) (net.Conn, error) {
defer context.Done()
c, err := net.DialTimeout(netw, addr, time.Second*time.Duration(10))
if err != nil {
return nil, err
}
return c, nil
},
MaxIdleConnsPerHost: 10,
ResponseHeaderTimeout: time.Second * time.Duration(5),
}
return &http.Client{
Timeout: time.Second * 10,
Transport: netTransport,
}
}
func getProxyAddr() string {
// default for round robbin
res := ""
for _, v := range proxyPool {
res = v
}
return res
}
func userAgent() string {
agent := [...]string{
"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0",
"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11",
"Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)",
"Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)",
"User-Agent,Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
"User-Agent, Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)",
"User-Agent,Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
length := len(agent)
return agent[r.Intn(length)]
}