-
Notifications
You must be signed in to change notification settings - Fork 3
/
proxy.go
55 lines (48 loc) · 1.53 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package req
import (
"context"
"errors"
"fmt"
"net"
"net/url"
"golang.org/x/net/proxy"
)
const (
protocolHTTP = "http"
protocolHTTPS = "https"
protocolSOCKS = "socks5"
)
var ErrInvalidProxyURL = errors.New("invalid proxyURL format (It should be http[s]/socks5://[username:password@]host:port)")
// socks5Dialer is a dialer for socks5 proxy
// It uses a socks5 proxy to dial the network connection
// TODO: handle error when dialing
func socks5DialerContext(proxyURL string) func(ctx context.Context, network, addr string) (net.Conn, error) {
var proxyAuth *proxy.Auth
socksURL, err := url.Parse(proxyURL)
if err == nil {
proxyAuth = &proxy.Auth{}
proxyAuth.User = socksURL.User.Username()
proxyAuth.Password, _ = socksURL.User.Password()
}
proxyAuth = &proxy.Auth{}
proxyAuth.User = socksURL.User.Username()
proxyAuth.Password, _ = socksURL.User.Password()
dialer, proxyErr := proxy.SOCKS5("tcp", fmt.Sprintf("%s:%s", socksURL.Hostname(), socksURL.Port()), proxyAuth, proxy.Direct)
dc := dialer.(interface {
DialContext(ctx context.Context, network, addr string) (net.Conn, error)
})
if proxyErr == nil {
return dc.DialContext
}
return nil
}
func validateProxyURL(proxyURL string) (*url.URL, error) {
if u, err := url.Parse(proxyURL); err == nil && isSupportedProtocol(u.Scheme) {
return u, nil
}
return nil, ErrInvalidProxyURL
}
// isSupportedProtocol checks given protocols are supported
func isSupportedProtocol(value string) bool {
return value == protocolHTTP || value == protocolHTTPS || value == protocolSOCKS
}