-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathdialer.go
48 lines (40 loc) · 1.05 KB
/
dialer.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
package main
import (
"net"
"net/http"
"net/url"
"time"
"golang.org/x/net/proxy"
)
type UpstreamDialer struct {
dialer proxy.Dialer
}
func NewUpstreamDialer(socksAddr string, timeout time.Duration) (*UpstreamDialer, error) {
var dialer proxy.Dialer
if socksAddr != "" {
parsedURL, err := url.Parse(socksAddr)
user := parsedURL.User.Username()
password, _ := parsedURL.User.Password()
socksDialer, err := proxy.SOCKS5(
"tcp", parsedURL.Host,
&proxy.Auth{User: user, Password: password},
proxy.Direct,
)
if err != nil {
return nil, err
}
dialer = socksDialer
// set upstream proxy for http connections
defaultTransport := http.DefaultTransport.(*http.Transport).Clone()
defaultTransport.Proxy = func(req *http.Request) (*url.URL, error) {
return parsedURL, nil
}
http.DefaultTransport = defaultTransport
} else {
dialer = &net.Dialer{Timeout: timeout}
}
return &UpstreamDialer{dialer: dialer}, nil
}
func (u *UpstreamDialer) Dial(network, addr string) (net.Conn, error) {
return u.dialer.Dial(network, addr)
}