-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
dialer.go
58 lines (50 loc) · 1.08 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
49
50
51
52
53
54
55
56
57
58
package main
import (
"errors"
"net"
"github.com/riobard/go-shadowsocks2/core"
"github.com/riobard/go-shadowsocks2/socks"
"github.com/riobard/go-shadowsocks2/speeddial"
)
type Dialer interface {
Dial(network, address string) (net.Conn, error)
}
type dialer struct {
*speeddial.Dialer
}
func (d dialer) Dial(network, address string) (net.Conn, error) {
if network != "tcp" {
return nil, errors.New("only TCP network is supported")
}
c, err := d.Dialer.Dial()
if err != nil {
return c, err
}
_, err = c.Write(socks.ParseAddr(address))
if err != nil {
c.Close()
}
return c, err
}
func fastdialer(u ...string) (*dialer, error) {
rs := make([]speeddial.Dial, len(u))
for i := range u {
addr, cipher, password, err := parseURL(u[i])
if err != nil {
return nil, err
}
ciph, err := core.PickCipher(cipher, nil, password)
if err != nil {
return nil, err
}
rs[i] = func() (net.Conn, error) {
c, err := net.Dial("tcp", addr)
if err != nil {
return c, err
}
c = ciph.StreamConn(c)
return c, nil
}
}
return &dialer{speeddial.New(rs...)}, nil
}