-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
93 lines (80 loc) · 2.26 KB
/
client.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 proxyclient
import (
"context"
"errors"
"net"
"net/url"
"strings"
)
type Dial func(network, address string) (net.Conn, error)
type DialFactory func(*url.URL, Dial) (Dial, error)
var schemes = map[string]DialFactory{
"DIRECT": newDirectProxyClient,
"REJECT": newRejectProxyClient,
"BLACKHOLE": newBlackholeProxyClient,
"SOCKS": newSocksProxyClient,
"SOCKS4": newSocksProxyClient,
"SOCKS4A": newSocksProxyClient,
"SOCKS5": newSocksProxyClient,
"SOCKS5+TLS": newSocksProxyClient,
"HTTP": newHTTPProxyClient,
"HTTPS": newHTTPProxyClient,
"SS": newShadowsocksProxyClient,
"SSH": newSSHAgentProxyClient,
}
func NewClient(proxy *url.URL) (Dial, error) {
return NewClientWithDial(proxy, net.Dial)
}
func NewClientChain(proxies []*url.URL) (Dial, error) {
return NewClientChainWithDial(proxies, net.Dial)
}
func NewClientWithDial(proxy *url.URL, upstreamDial Dial) (_ Dial, err error) {
if proxy == nil {
err = errors.New("proxy url is nil")
return
}
if upstreamDial == nil {
err = errors.New("upstream dial is nil")
return
}
proxy = normalizeLink(*proxy)
if _, ok := schemes[proxy.Scheme]; !ok {
err = errors.New("unsupported proxy client.")
return
}
return schemes[proxy.Scheme](proxy, upstreamDial)
}
func NewClientChainWithDial(proxies []*url.URL, upstreamDial Dial) (dial Dial, err error) {
dial = upstreamDial
for _, proxyURL := range proxies {
dial, err = NewClientWithDial(proxyURL, dial)
if err != nil {
return
}
}
return
}
func RegisterScheme(schemeName string, factory DialFactory) {
schemes[strings.ToUpper(schemeName)] = factory
}
func SupportedSchemes() []string {
schemeNames := make([]string, 0, len(schemes))
for schemeName := range schemes {
schemeNames = append(schemeNames, schemeName)
}
return schemeNames
}
func (dial Dial) Context(ctx context.Context, network, address string) (net.Conn, error) {
return dial(network, address)
}
func (dial Dial) TCPOnly(network, address string) (net.Conn, error) {
switch strings.ToUpper(network) {
case "TCP", "TCP4", "TCP6":
return dial(network, address)
default:
return nil, errors.New("unsupported network type.")
}
}
func (dial Dial) Dial(network, address string) (net.Conn, error) {
return dial(network, address)
}