-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxy_dialer.go
62 lines (54 loc) · 1.19 KB
/
proxy_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package tproxy2socks
import (
"errors"
"fmt"
"github.com/0990/socks5"
"net"
"net/url"
"strings"
)
type proxyDialer interface {
Dial(network, addr string) (net.Conn, error)
}
func newProxyDialer(proxy string, udpTimeout int32) (proxyDialer, error) {
protocol, addr, err := parseProxy(proxy)
if err != nil {
return nil, err
}
switch protocol {
case "socks5":
return socks5.NewSocks5Client(socks5.ClientCfg{
ServerAddr: addr,
UserName: "",
Password: "",
UDPTimout: int(udpTimeout),
TCPTimeout: 60,
}), nil
case "socks4":
return socks5.NewSocks4Client(socks5.ClientCfg{
ServerAddr: addr,
UserName: "",
Password: "",
UDPTimout: int(udpTimeout),
TCPTimeout: 60,
}), nil
default:
return nil, errors.New("not support proxy type")
}
}
func parseProxy(s string) (string, string, error) {
if !strings.Contains(s, "://") {
s = fmt.Sprintf("%s://%s", "socks5" /* default protocol */, s)
}
u, err := url.Parse(s)
if err != nil {
return "", "", err
}
protocol := strings.ToLower(u.Scheme)
switch protocol {
case "socks5", "socks4":
return protocol, u.Host, nil
default:
return "", "", fmt.Errorf("unsupported protocol: %s", protocol)
}
}