2018-06-10 22:50:03 +08:00
|
|
|
package adapters
|
|
|
|
|
|
|
|
import (
|
2019-02-11 15:25:10 +08:00
|
|
|
"crypto/tls"
|
2018-11-21 13:47:46 +08:00
|
|
|
"encoding/json"
|
2018-06-10 22:50:03 +08:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
|
2019-04-23 23:29:36 +08:00
|
|
|
"github.com/Dreamacro/clash/common/pool"
|
2019-02-11 15:25:10 +08:00
|
|
|
"github.com/Dreamacro/clash/common/structure"
|
2018-12-22 23:56:42 +08:00
|
|
|
obfs "github.com/Dreamacro/clash/component/simple-obfs"
|
2019-04-25 13:48:47 +08:00
|
|
|
"github.com/Dreamacro/clash/component/socks5"
|
2019-02-11 15:25:10 +08:00
|
|
|
v2rayObfs "github.com/Dreamacro/clash/component/v2ray-plugin"
|
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"
|
2018-06-10 22:50:03 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type ShadowSocks struct {
|
2018-12-22 23:56:42 +08:00
|
|
|
*Base
|
2019-02-11 15:25:10 +08:00
|
|
|
server string
|
|
|
|
cipher core.Cipher
|
|
|
|
|
|
|
|
// obfs
|
|
|
|
obfsMode string
|
|
|
|
obfsOption *simpleObfsOption
|
|
|
|
wsOption *v2rayObfs.WebsocketOption
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
2018-10-02 15:26:36 +08:00
|
|
|
type ShadowSocksOption struct {
|
2019-02-11 15:25:10 +08:00
|
|
|
Name string `proxy:"name"`
|
|
|
|
Server string `proxy:"server"`
|
|
|
|
Port int `proxy:"port"`
|
|
|
|
Password string `proxy:"password"`
|
|
|
|
Cipher string `proxy:"cipher"`
|
2019-04-23 23:29:36 +08:00
|
|
|
UDP bool `proxy:"udp,omitempty"`
|
2019-02-11 15:25:10 +08:00
|
|
|
Plugin string `proxy:"plugin,omitempty"`
|
|
|
|
PluginOpts map[string]interface{} `proxy:"plugin-opts,omitempty"`
|
|
|
|
|
|
|
|
// deprecated when bump to 1.0
|
2018-10-02 15:26:36 +08:00
|
|
|
Obfs string `proxy:"obfs,omitempty"`
|
|
|
|
ObfsHost string `proxy:"obfs-host,omitempty"`
|
|
|
|
}
|
|
|
|
|
2019-02-11 15:25:10 +08:00
|
|
|
type simpleObfsOption struct {
|
|
|
|
Mode string `obfs:"mode"`
|
|
|
|
Host string `obfs:"host,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type v2rayObfsOption struct {
|
2019-03-15 09:43:46 +08:00
|
|
|
Mode string `obfs:"mode"`
|
|
|
|
Host string `obfs:"host,omitempty"`
|
|
|
|
Path string `obfs:"path,omitempty"`
|
|
|
|
TLS bool `obfs:"tls,omitempty"`
|
|
|
|
Headers map[string]string `obfs:"headers,omitempty"`
|
|
|
|
SkipCertVerify bool `obfs:"skip-cert-verify,omitempty"`
|
2019-02-11 15:25:10 +08:00
|
|
|
}
|
|
|
|
|
2019-08-09 01:28:37 +08:00
|
|
|
func (ss *ShadowSocks) Dial(metadata *C.Metadata) (C.Conn, error) {
|
2019-06-29 00:58:59 +08:00
|
|
|
c, err := dialTimeout("tcp", ss.server, tcpTimeout)
|
2018-06-10 22:50:03 +08:00
|
|
|
if err != nil {
|
2019-02-11 15:25:10 +08:00
|
|
|
return nil, fmt.Errorf("%s connect error: %s", ss.server, err.Error())
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
2018-08-29 15:00:12 +08:00
|
|
|
tcpKeepAlive(c)
|
2019-02-11 15:25:10 +08:00
|
|
|
switch ss.obfsMode {
|
2018-09-16 23:02:32 +08:00
|
|
|
case "tls":
|
2019-02-11 15:25:10 +08:00
|
|
|
c = obfs.NewTLSObfs(c, ss.obfsOption.Host)
|
2018-09-16 23:02:32 +08:00
|
|
|
case "http":
|
|
|
|
_, port, _ := net.SplitHostPort(ss.server)
|
2019-02-11 15:25:10 +08:00
|
|
|
c = obfs.NewHTTPObfs(c, ss.obfsOption.Host, port)
|
|
|
|
case "websocket":
|
|
|
|
var err error
|
|
|
|
c, err = v2rayObfs.NewWebsocketObfs(c, ss.wsOption)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error: %s", ss.server, err.Error())
|
|
|
|
}
|
2018-09-16 23:02:32 +08:00
|
|
|
}
|
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))
|
2019-08-09 01:28:37 +08:00
|
|
|
return newConn(c, ss), err
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
2019-08-09 01:28:37 +08:00
|
|
|
func (ss *ShadowSocks) DialUDP(metadata *C.Metadata) (C.PacketConn, net.Addr, error) {
|
2019-04-23 23:29:36 +08:00
|
|
|
pc, err := net.ListenPacket("udp", "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-29 00:58:59 +08:00
|
|
|
addr, err := resolveUDPAddr("udp", ss.server)
|
2019-04-24 10:29:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-25 17:47:39 +08:00
|
|
|
targetAddr, err := net.ResolveUDPAddr("udp", net.JoinHostPort(metadata.String(), metadata.DstPort))
|
2019-04-24 10:29:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-04-23 23:29:36 +08:00
|
|
|
|
|
|
|
pc = ss.cipher.PacketConn(pc)
|
2019-08-09 01:28:37 +08:00
|
|
|
return newPacketConn(&ssUDPConn{PacketConn: pc, rAddr: targetAddr}, ss), addr, nil
|
2019-04-23 23:29:36 +08:00
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
func (ss *ShadowSocks) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(map[string]string{
|
|
|
|
"type": ss.Type().String(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-02 15:26:36 +08:00
|
|
|
func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
2018-11-04 21:12:16 +08:00
|
|
|
server := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
2018-10-02 15:26:36 +08:00
|
|
|
cipher := option.Cipher
|
|
|
|
password := option.Password
|
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
|
|
|
|
2019-02-11 15:25:10 +08:00
|
|
|
var wsOption *v2rayObfs.WebsocketOption
|
|
|
|
var obfsOption *simpleObfsOption
|
|
|
|
obfsMode := ""
|
|
|
|
|
|
|
|
// forward compatibility before 1.0
|
|
|
|
if option.Obfs != "" {
|
|
|
|
obfsMode = option.Obfs
|
|
|
|
obfsOption = &simpleObfsOption{
|
|
|
|
Host: "bing.com",
|
|
|
|
}
|
|
|
|
if option.ObfsHost != "" {
|
|
|
|
obfsOption.Host = option.ObfsHost
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder := structure.NewDecoder(structure.Option{TagName: "obfs", WeaklyTypedInput: true})
|
|
|
|
if option.Plugin == "obfs" {
|
|
|
|
opts := simpleObfsOption{Host: "bing.com"}
|
|
|
|
if err := decoder.Decode(option.PluginOpts, &opts); err != nil {
|
|
|
|
return nil, fmt.Errorf("ss %s initialize obfs error: %s", server, err.Error())
|
|
|
|
}
|
2019-06-18 20:37:53 +08:00
|
|
|
|
|
|
|
if opts.Mode != "tls" && opts.Mode != "http" {
|
|
|
|
return nil, fmt.Errorf("ss %s obfs mode error: %s", server, opts.Mode)
|
|
|
|
}
|
2019-02-11 15:25:10 +08:00
|
|
|
obfsMode = opts.Mode
|
|
|
|
obfsOption = &opts
|
|
|
|
} else if option.Plugin == "v2ray-plugin" {
|
|
|
|
opts := v2rayObfsOption{Host: "bing.com"}
|
|
|
|
if err := decoder.Decode(option.PluginOpts, &opts); err != nil {
|
|
|
|
return nil, fmt.Errorf("ss %s initialize v2ray-plugin error: %s", server, err.Error())
|
|
|
|
}
|
2019-06-18 20:37:53 +08:00
|
|
|
|
|
|
|
if opts.Mode != "websocket" {
|
|
|
|
return nil, fmt.Errorf("ss %s obfs mode error: %s", server, opts.Mode)
|
|
|
|
}
|
2019-02-11 15:25:10 +08:00
|
|
|
obfsMode = opts.Mode
|
2019-06-18 20:37:53 +08:00
|
|
|
|
2019-02-11 15:25:10 +08:00
|
|
|
var tlsConfig *tls.Config
|
|
|
|
if opts.TLS {
|
|
|
|
tlsConfig = &tls.Config{
|
|
|
|
ServerName: opts.Host,
|
|
|
|
InsecureSkipVerify: opts.SkipCertVerify,
|
|
|
|
ClientSessionCache: getClientSessionCache(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wsOption = &v2rayObfs.WebsocketOption{
|
|
|
|
Host: opts.Host,
|
|
|
|
Path: opts.Path,
|
2019-03-15 09:43:46 +08:00
|
|
|
Headers: opts.Headers,
|
2019-02-11 15:25:10 +08:00
|
|
|
TLSConfig: tlsConfig,
|
|
|
|
}
|
2018-09-16 23:02:32 +08:00
|
|
|
}
|
|
|
|
|
2018-06-16 15:41:26 +08:00
|
|
|
return &ShadowSocks{
|
2018-12-22 23:56:42 +08:00
|
|
|
Base: &Base{
|
|
|
|
name: option.Name,
|
|
|
|
tp: C.Shadowsocks,
|
2019-04-23 23:29:36 +08:00
|
|
|
udp: option.UDP,
|
2018-12-22 23:56:42 +08:00
|
|
|
},
|
2019-02-11 15:25:10 +08:00
|
|
|
server: server,
|
|
|
|
cipher: ciph,
|
|
|
|
|
|
|
|
obfsMode: obfsMode,
|
|
|
|
wsOption: wsOption,
|
|
|
|
obfsOption: obfsOption,
|
2018-06-16 15:41:26 +08:00
|
|
|
}, nil
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
2019-04-23 23:29:36 +08:00
|
|
|
type ssUDPConn struct {
|
|
|
|
net.PacketConn
|
|
|
|
rAddr net.Addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (uc *ssUDPConn) WriteTo(b []byte, addr net.Addr) (int, error) {
|
|
|
|
buf := pool.BufPool.Get().([]byte)
|
|
|
|
defer pool.BufPool.Put(buf[:cap(buf)])
|
2019-04-25 13:48:47 +08:00
|
|
|
rAddr := socks5.ParseAddr(uc.rAddr.String())
|
2019-04-23 23:29:36 +08:00
|
|
|
copy(buf[len(rAddr):], b)
|
|
|
|
copy(buf, rAddr)
|
|
|
|
return uc.PacketConn.WriteTo(buf[:len(rAddr)+len(b)], addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (uc *ssUDPConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
|
|
|
n, a, e := uc.PacketConn.ReadFrom(b)
|
2019-04-25 13:48:47 +08:00
|
|
|
addr := socks5.SplitAddr(b[:n])
|
2019-04-23 23:29:36 +08:00
|
|
|
copy(b, b[len(addr):])
|
|
|
|
return n - len(addr), a, e
|
|
|
|
}
|