2018-06-10 22:50:03 +08:00
|
|
|
package adapters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
|
2018-09-30 12:25:52 +08:00
|
|
|
"github.com/Dreamacro/clash/component/simple-obfs"
|
2018-06-10 22:50:03 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
|
2018-09-21 15:27:51 +08:00
|
|
|
"github.com/Dreamacro/go-shadowsocks2/core"
|
|
|
|
"github.com/Dreamacro/go-shadowsocks2/socks"
|
2018-06-10 22:50:03 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// ShadowsocksAdapter is a shadowsocks adapter
|
|
|
|
type ShadowsocksAdapter struct {
|
|
|
|
conn net.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close is used to close connection
|
|
|
|
func (ss *ShadowsocksAdapter) Close() {
|
|
|
|
ss.conn.Close()
|
|
|
|
}
|
|
|
|
|
2018-06-14 01:00:58 +08:00
|
|
|
func (ss *ShadowsocksAdapter) Conn() net.Conn {
|
|
|
|
return ss.conn
|
|
|
|
}
|
|
|
|
|
2018-06-10 22:50:03 +08:00
|
|
|
type ShadowSocks struct {
|
2018-09-16 23:02:32 +08:00
|
|
|
server string
|
|
|
|
name string
|
|
|
|
obfs string
|
|
|
|
obfsHost string
|
|
|
|
cipher core.Cipher
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:34:13 +08:00
|
|
|
func (ss *ShadowSocks) Name() string {
|
|
|
|
return ss.name
|
|
|
|
}
|
|
|
|
|
2018-07-12 23:28:38 +08:00
|
|
|
func (ss *ShadowSocks) Type() C.AdapterType {
|
|
|
|
return C.Shadowsocks
|
|
|
|
}
|
|
|
|
|
2018-09-30 12:25:52 +08:00
|
|
|
func (ss *ShadowSocks) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) {
|
2018-06-10 22:50:03 +08:00
|
|
|
c, err := net.Dial("tcp", ss.server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error", ss.server)
|
|
|
|
}
|
2018-08-29 15:00:12 +08:00
|
|
|
tcpKeepAlive(c)
|
2018-09-16 23:02:32 +08:00
|
|
|
switch ss.obfs {
|
|
|
|
case "tls":
|
|
|
|
c = obfs.NewTLSObfs(c, ss.obfsHost)
|
|
|
|
case "http":
|
|
|
|
_, port, _ := net.SplitHostPort(ss.server)
|
|
|
|
c = obfs.NewHTTPObfs(c, ss.obfsHost, port)
|
|
|
|
}
|
2018-06-16 15:41:26 +08:00
|
|
|
c = ss.cipher.StreamConn(c)
|
2018-09-30 12:25:52 +08:00
|
|
|
_, err = c.Write(serializesSocksAddr(metadata))
|
2018-07-26 00:04:59 +08:00
|
|
|
return &ShadowsocksAdapter{conn: c}, err
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
2018-09-16 23:02:32 +08:00
|
|
|
func NewShadowSocks(name string, ssURL string, option map[string]string) (*ShadowSocks, error) {
|
2018-06-10 22:50:03 +08:00
|
|
|
server, cipher, password, _ := parseURL(ssURL)
|
2018-09-21 11:33:29 +08:00
|
|
|
ciph, err := core.PickCipher(cipher, nil, password)
|
2018-06-16 15:41:26 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("ss %s initialize error: %s", server, err.Error())
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
2018-09-16 23:02:32 +08:00
|
|
|
|
|
|
|
obfs := ""
|
|
|
|
obfsHost := "bing.com"
|
|
|
|
if value, ok := option["obfs"]; ok {
|
|
|
|
obfs = value
|
|
|
|
}
|
|
|
|
|
|
|
|
if value, ok := option["obfs-host"]; ok {
|
|
|
|
obfsHost = value
|
|
|
|
}
|
|
|
|
|
2018-06-16 15:41:26 +08:00
|
|
|
return &ShadowSocks{
|
2018-09-16 23:02:32 +08:00
|
|
|
server: server,
|
|
|
|
name: name,
|
|
|
|
cipher: ciph,
|
|
|
|
obfs: obfs,
|
|
|
|
obfsHost: obfsHost,
|
2018-06-16 15:41:26 +08:00
|
|
|
}, nil
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseURL(s string) (addr, cipher, password string, err error) {
|
|
|
|
u, err := url.Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = u.Host
|
|
|
|
if u.User != nil {
|
|
|
|
cipher = u.User.Username()
|
|
|
|
password, _ = u.User.Password()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-30 12:25:52 +08:00
|
|
|
func serializesSocksAddr(metadata *C.Metadata) []byte {
|
2018-06-10 22:50:03 +08:00
|
|
|
var buf [][]byte
|
2018-09-30 12:25:52 +08:00
|
|
|
aType := uint8(metadata.AddrType)
|
|
|
|
p, _ := strconv.Atoi(metadata.Port)
|
2018-06-10 22:50:03 +08:00
|
|
|
port := []byte{uint8(p >> 8), uint8(p & 0xff)}
|
2018-09-30 12:25:52 +08:00
|
|
|
switch metadata.AddrType {
|
2018-06-10 22:50:03 +08:00
|
|
|
case socks.AtypDomainName:
|
2018-09-30 12:25:52 +08:00
|
|
|
len := uint8(len(metadata.Host))
|
|
|
|
host := []byte(metadata.Host)
|
2018-06-22 23:56:41 +08:00
|
|
|
buf = [][]byte{{aType, len}, host, port}
|
2018-06-10 22:50:03 +08:00
|
|
|
case socks.AtypIPv4:
|
2018-09-30 12:25:52 +08:00
|
|
|
host := metadata.IP.To4()
|
2018-06-22 23:56:41 +08:00
|
|
|
buf = [][]byte{{aType}, host, port}
|
2018-06-10 22:50:03 +08:00
|
|
|
case socks.AtypIPv6:
|
2018-09-30 12:25:52 +08:00
|
|
|
host := metadata.IP.To16()
|
2018-06-22 23:56:41 +08:00
|
|
|
buf = [][]byte{{aType}, host, port}
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
2018-09-21 11:33:29 +08:00
|
|
|
return bytes.Join(buf, nil)
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|