-
Notifications
You must be signed in to change notification settings - Fork 2
/
proxy.go
61 lines (55 loc) · 1.41 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
package proxyclient
import (
"errors"
"net"
"net/url"
"strconv"
"time"
httpProxy "github.com/missdeer/HTTPProxy"
socksProxy "github.com/missdeer/SOCKSProxy"
)
func newDirectProxyClient(proxy *url.URL, _ Dial) (dial Dial, err error) {
dial = net.Dial
if timeout, _ := time.ParseDuration(proxy.Query().Get("timeout")); timeout != 0 {
dial = DialWithTimeout(timeout)
}
return
}
func newRejectProxyClient(proxy *url.URL, _ Dial) (dial Dial, err error) {
dialErr := errors.New("reject dial")
dial = func(network, address string) (net.Conn, error) {
return nil, dialErr
}
if try, _ := strconv.ParseInt(proxy.Query().Get("try-to-blackhole"), 10, 8); try > 0 {
attempt := int64(0)
dial = func(network, address string) (net.Conn, error) {
attempt++
if attempt > try {
return blackholeConn{}, nil
}
return nil, dialErr
}
}
return
}
func newHTTPProxyClient(proxy *url.URL, upstreamDial Dial) (dial Dial, err error) {
client := httpProxy.Client{
Proxy: *proxy,
TLSConfig: tlsConfigByProxyURL(proxy),
UpstreamDial: upstreamDial,
}
dial = Dial(client.Dial).TCPOnly
return
}
func newSocksProxyClient(proxy *url.URL, upstreamDial Dial) (dial Dial, err error) {
conf := &socksProxy.SOCKSConf{
TLSConfig: tlsConfigByProxyURL(proxy),
Dial: upstreamDial,
}
client, err := socksProxy.NewClient(proxy, conf)
if err != nil {
return
}
dial = Dial(client.Dial).TCPOnly
return
}