-
Notifications
You must be signed in to change notification settings - Fork 41
/
proxy.go
125 lines (107 loc) · 2.83 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
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
package antch
import (
"bufio"
"context"
"encoding/base64"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"
"golang.org/x/net/proxy"
)
// ProxyKey is a key for the proxy URL that used by Crawler.
type ProxyKey struct{}
func proxyHandler(f func(*http.Request) (*url.URL, error), next HttpMessageHandler) HttpMessageHandler {
// Registers proxy protocol(HTTP,HTTPS,SOCKS5).
proxy.RegisterDialerType("http", httpProxy)
proxy.RegisterDialerType("https", httpProxy)
return HttpMessageHandlerFunc(func(req *http.Request) (*http.Response, error) {
proxyURL, err := f(req)
if err != nil {
return nil, err
}
ctx := context.WithValue(req.Context(), ProxyKey{}, proxyURL)
return next.Send(req.WithContext(ctx))
})
}
var zeroDialer net.Dialer
func proxyDialContext(ctx context.Context, network, address string) (net.Conn, error) {
if v := ctx.Value(ProxyKey{}); v != nil {
dialer, err := proxy.FromURL(v.(*url.URL), proxy.Direct)
if err != nil {
return nil, err
}
return dialer.Dial(network, address)
}
return zeroDialer.DialContext(ctx, network, address)
}
func httpProxy(u *url.URL, forward proxy.Dialer) (proxy.Dialer, error) {
h := &httpDialer{
host: u.Host,
forward: forward,
}
if u.User != nil {
h.shouldAuth = true
h.username = u.User.Username()
h.password, _ = u.User.Password()
}
return h, nil
}
type httpDialer struct {
host string
shouldAuth bool
username, password string
forward proxy.Dialer
}
func (d *httpDialer) auth() string {
if d.shouldAuth {
auth := d.username + ":" + d.password
return "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
}
return ""
}
func (d *httpDialer) Dial(network, addr string) (net.Conn, error) {
conn, err := d.forward.Dial("tcp", d.host)
if err != nil {
return nil, err
}
connectReq := &http.Request{
Method: "CONNECT",
URL: &url.URL{Opaque: addr},
Host: addr,
Header: make(http.Header),
Close: false,
}
if pa := d.auth(); pa != "" {
connectReq.Header.Set("Proxy-Authorization", pa)
}
connectReq.Write(conn)
br := bufio.NewReader(conn)
resp, err := http.ReadResponse(br, connectReq)
if err != nil {
conn.Close()
return nil, err
}
defer resp.Body.Close()
io.Copy(ioutil.Discard, resp.Body)
if resp.StatusCode != 200 {
conn.Close()
f := strings.SplitN(resp.Status, " ", 2)
return nil, fmt.Errorf("proxy: %v", errors.New(f[1]))
}
return conn, nil
}
// ProxyMiddleware is an HTTP proxy middleware to take HTTP Request
// use the HTTP proxy to access remote sites.
//
// ProxyMiddleware supports HTTP/HTTPS,SOCKS5 protocol list.
// etc http://127.0.0.1:8080 or https://127.0.0.1:8080 or socks5://127.0.0.1:1080
func ProxyMiddleware(f func(*http.Request) (*url.URL, error)) Middleware {
return func(next HttpMessageHandler) HttpMessageHandler {
return proxyHandler(f, next)
}
}