-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
40 lines (37 loc) · 956 Bytes
/
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
package restyclient
import (
"context"
"net"
"net/http"
"net/url"
"golang.org/x/net/proxy"
)
// SetProxy Proxy client
func SetProxy(client *http.Client, proxyString string) error {
proxyURL, err := url.Parse(proxyString)
if err != nil {
return err
}
if client.Transport == nil {
client.Transport = &http.Transport{}
}
switch proxyURL.Scheme {
case "http", "https":
client.Transport.(*http.Transport).Proxy = http.ProxyURL(proxyURL)
case "socks5", "socks5h":
fallthrough
default: // proxy.RegisterDialerType(``)
dialer, err := proxy.FromURL(proxyURL, proxy.Direct)
if err != nil {
return err
}
if dial, ok := dialer.(proxy.ContextDialer); ok {
client.Transport.(*http.Transport).DialContext = dial.DialContext
} else {
client.Transport.(*http.Transport).DialContext = func(ctx context.Context, network string, address string) (net.Conn, error) {
return dialer.Dial(network, address)
}
}
}
return nil
}