Repository navigation
Expand file tree
/
Copy pathupstream.go
More file actions
154 lines (133 loc) · 3.98 KB
/
Copy pathupstream.go
File metadata and controls
154 lines (133 loc) · 3.98 KB
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
package proxify
import (
"bufio"
"encoding/base64"
"fmt"
"net"
"net/http"
"net/url"
rbtransport "github.com/projectdiscovery/roundrobin/transport"
"golang.org/x/net/proxy"
)
type httpProxyDialer struct {
proxyURL *url.URL
forward proxy.Dialer
}
// Dial connects to the address using the HTTP proxy.
func (d *httpProxyDialer) Dial(_, addr string) (net.Conn, error) {
conn, err := d.forward.Dial("tcp", d.proxyURL.Host)
if err != nil {
return nil, err
}
connectReq := &http.Request{
Method: "CONNECT",
URL: &url.URL{Opaque: addr},
Host: addr,
Header: make(http.Header),
}
if d.proxyURL.User != nil {
encodedUserinfo := base64.StdEncoding.EncodeToString([]byte(d.proxyURL.User.String()))
connectReq.Header.Set("Proxy-Authorization", "Basic "+encodedUserinfo)
}
if err := connectReq.Write(conn); err != nil {
_ = conn.Close()
return nil, err
}
br := bufio.NewReader(conn)
resp, err := http.ReadResponse(br, connectReq)
if err != nil {
_ = conn.Close()
return nil, err
}
if resp.StatusCode != http.StatusOK {
_ = conn.Close()
return nil, fmt.Errorf("unexpected response from proxy: %s", resp.Status)
}
return conn, nil
}
type httpProxyRoundRobinDialer struct {
proxyDialers map[string]httpProxyDialer
transport *rbtransport.RoundTransport
}
// Dial connects to the address on the named network via one of the HTTP proxies using round-robin scheduling.
func (d *httpProxyRoundRobinDialer) Dial(network, addr string) (net.Conn, error) {
nextProxyURL := d.transport.Next()
dialer, ok := d.proxyDialers[nextProxyURL]
if !ok {
return nil, fmt.Errorf("no matching proxy dialer found")
}
return dialer.Dial(network, addr)
}
func newHTTPProxyRoundRobinDialer(upstreamProxies []string) (proxy.Dialer, error) {
if len(upstreamProxies) == 0 {
return nil, fmt.Errorf("proxy URLs cannot be empty")
}
proxyURLs := make([]*url.URL, 0, len(upstreamProxies))
dialers := make(map[string]httpProxyDialer)
for _, proxyAddr := range upstreamProxies {
proxyURL, err := url.Parse(proxyAddr)
if err != nil {
return nil, err
}
proxyURLs = append(proxyURLs, proxyURL)
dialer := httpProxyDialer{proxyURL: proxyURL, forward: proxy.Direct}
dialers[proxyURL.String()] = dialer
}
robin, err := rbtransport.NewWithOptions(1, toStringSlice(proxyURLs)...)
if err != nil {
return nil, err
}
return &httpProxyRoundRobinDialer{proxyDialers: dialers, transport: robin}, nil
}
type socks5ProxyRoundRobinDialer struct {
proxyDialers map[string]proxy.Dialer
robin *rbtransport.RoundTransport
}
// Dial connects to the address on the named network via one of the SOCKS5 proxies using round-robin scheduling.
func (d *socks5ProxyRoundRobinDialer) Dial(network, addr string) (net.Conn, error) {
nextProxyURL := d.robin.Next()
dialer, ok := d.proxyDialers[nextProxyURL]
if !ok {
return nil, fmt.Errorf("no matching proxy dialer found")
}
return dialer.Dial(network, addr)
}
func newSOCKS5ProxyRoundRobinDialer(upstreamProxies []string) (proxy.Dialer, error) {
if len(upstreamProxies) == 0 {
return nil, fmt.Errorf("proxy URLs cannot be empty")
}
proxyURLs := make([]*url.URL, 0, len(upstreamProxies))
dialers := make(map[string]proxy.Dialer)
for _, proxyAddr := range upstreamProxies {
proxyURL, err := url.Parse(proxyAddr)
if err != nil {
return nil, err
}
proxyURLs = append(proxyURLs, proxyURL)
var auth *proxy.Auth
if proxyURL.User != nil {
password, _ := proxyURL.User.Password()
auth = &proxy.Auth{
User: proxyURL.User.Username(),
Password: password,
}
}
dialer, err := proxy.SOCKS5("tcp", proxyURL.Host, auth, proxy.Direct)
if err != nil {
return nil, err
}
dialers[proxyAddr] = dialer
}
robin, err := rbtransport.NewWithOptions(1, toStringSlice(proxyURLs)...)
if err != nil {
return nil, err
}
return &socks5ProxyRoundRobinDialer{proxyDialers: dialers, robin: robin}, nil
}
func toStringSlice(urls []*url.URL) []string {
s := make([]string, len(urls))
for i, u := range urls {
s[i] = u.String()
}
return s
}