-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxychain.go
executable file
·40 lines (35 loc) · 930 Bytes
/
proxychain.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
package proxychain
import (
"context"
"golang.org/x/net/proxy"
"net"
"net/http"
)
type ProxyConfig struct {
Address string
Auth *proxy.Auth
}
func GetClient(chain []ProxyConfig) *http.Client {
client := http.DefaultClient
client.Transport = getTransport(chain)
return client
}
func getTransport(chain []ProxyConfig) *http.Transport {
dialContext := func(ctx context.Context, network, address string) (net.Conn, error) {
return getProxyDialer(chain).Dial(network, address)
}
tr := &http.Transport{
DialContext: dialContext,
}
return tr
}
// dialer1 -> dialer2 -> dialer... -> url
func getProxyDialer(chain []ProxyConfig) (dialer proxy.Dialer) {
firstProxy := chain[0]
dialer, _ = proxy.SOCKS5("tcp", firstProxy.Address, firstProxy.Auth, proxy.Direct)
for i := 1; i < len(chain); i++ {
chainProxy := chain[i]
dialer, _ = proxy.SOCKS5("tcp", chainProxy.Address, chainProxy.Auth, dialer)
}
return
}