/
ss_dialer.go
47 lines (40 loc) · 908 Bytes
/
ss_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
package main
import (
"errors"
"net"
"net/url"
"github.com/hyqhyq3/through/common"
ss "github.com/shadowsocks/shadowsocks-go/shadowsocks"
)
type SSDialer struct {
name string
Host string
Method string
Password string
}
func NewSSDialer(name string, u *url.URL) (common.Dialer, error) {
host := u.Host
if u.User == nil {
return nil, errors.New("Must have encrypt method and password")
}
method := u.User.Username()
password, _ := u.User.Password()
return &SSDialer{name, host, method, password}, nil
}
func init() {
RegisterProxyType("ss", NewSSDialer)
}
func (s *SSDialer) Dial(network, addr string) (c net.Conn, err error) {
cipher, err := ss.NewCipher(s.Method, s.Password)
if err != nil {
return nil, err
}
ssConn, err := ss.Dial(addr, s.Host, cipher)
if err != nil {
return nil, err
}
return ssConn, nil
}
func (s *SSDialer) Name() string {
return s.name
}