2019-12-08 04:17:24 +00:00
|
|
|
package outbound
|
2018-06-10 14:50:03 +00:00
|
|
|
|
|
|
|
import (
|
2019-10-12 15:55:39 +00:00
|
|
|
"context"
|
2020-02-29 17:46:02 +00:00
|
|
|
"errors"
|
2018-06-10 14:50:03 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
|
2019-02-11 07:25:10 +00:00
|
|
|
"github.com/Dreamacro/clash/common/structure"
|
2020-02-09 09:02:48 +00:00
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
2018-06-10 14:50:03 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-05-13 14:18:49 +00:00
|
|
|
obfs "github.com/Dreamacro/clash/transport/simple-obfs"
|
|
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
|
|
|
v2rayObfs "github.com/Dreamacro/clash/transport/v2ray-plugin"
|
2018-06-10 14:50:03 +00:00
|
|
|
|
2018-09-21 07:27:51 +00:00
|
|
|
"github.com/Dreamacro/go-shadowsocks2/core"
|
2018-06-10 14:50:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ShadowSocks struct {
|
2018-12-22 15:56:42 +00:00
|
|
|
*Base
|
2019-02-11 07:25:10 +00:00
|
|
|
cipher core.Cipher
|
|
|
|
|
|
|
|
// obfs
|
2019-09-21 15:49:00 +00:00
|
|
|
obfsMode string
|
|
|
|
obfsOption *simpleObfsOption
|
|
|
|
v2rayOption *v2rayObfs.Option
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 07:26:36 +00:00
|
|
|
type ShadowSocksOption struct {
|
2021-11-07 08:48:51 +00:00
|
|
|
BasicOption
|
2022-03-16 04:10:13 +00:00
|
|
|
Name string `proxy:"name"`
|
|
|
|
Server string `proxy:"server"`
|
|
|
|
Port int `proxy:"port"`
|
|
|
|
Password string `proxy:"password"`
|
|
|
|
Cipher string `proxy:"cipher"`
|
|
|
|
UDP bool `proxy:"udp,omitempty"`
|
|
|
|
Plugin string `proxy:"plugin,omitempty"`
|
|
|
|
PluginOpts map[string]any `proxy:"plugin-opts,omitempty"`
|
2018-10-02 07:26:36 +00:00
|
|
|
}
|
|
|
|
|
2019-02-11 07:25:10 +00:00
|
|
|
type simpleObfsOption struct {
|
2020-12-24 05:47:56 +00:00
|
|
|
Mode string `obfs:"mode,omitempty"`
|
2019-02-11 07:25:10 +00:00
|
|
|
Host string `obfs:"host,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type v2rayObfsOption struct {
|
2019-03-15 01:43:46 +00: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-09-21 15:49:00 +00:00
|
|
|
Mux bool `obfs:"mux,omitempty"`
|
2019-02-11 07:25:10 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 09:27:55 +00:00
|
|
|
func (ss *ShadowSocks) IsProxyGroup() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:23:14 +00:00
|
|
|
// StreamConn implements C.ProxyAdapter
|
2020-03-21 15:46:49 +00:00
|
|
|
func (ss *ShadowSocks) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
2019-02-11 07:25:10 +00:00
|
|
|
switch ss.obfsMode {
|
2018-09-16 15:02:32 +00:00
|
|
|
case "tls":
|
2019-02-11 07:25:10 +00:00
|
|
|
c = obfs.NewTLSObfs(c, ss.obfsOption.Host)
|
2018-09-16 15:02:32 +00:00
|
|
|
case "http":
|
2020-03-21 15:46:49 +00:00
|
|
|
_, port, _ := net.SplitHostPort(ss.addr)
|
2019-02-11 07:25:10 +00:00
|
|
|
c = obfs.NewHTTPObfs(c, ss.obfsOption.Host, port)
|
|
|
|
case "websocket":
|
|
|
|
var err error
|
2019-09-21 15:49:00 +00:00
|
|
|
c, err = v2rayObfs.NewV2rayObfs(c, ss.v2rayOption)
|
2019-02-11 07:25:10 +00:00
|
|
|
if err != nil {
|
2020-03-21 15:46:49 +00:00
|
|
|
return nil, fmt.Errorf("%s connect error: %w", ss.addr, err)
|
2019-02-11 07:25:10 +00:00
|
|
|
}
|
2018-09-16 15:02:32 +00:00
|
|
|
}
|
2018-06-16 07:41:26 +00:00
|
|
|
c = ss.cipher.StreamConn(c)
|
2020-03-21 15:46:49 +00:00
|
|
|
_, err := c.Write(serializesSocksAddr(metadata))
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:23:14 +00:00
|
|
|
// DialContext implements C.ProxyAdapter
|
2021-11-07 08:48:51 +00:00
|
|
|
func (ss *ShadowSocks) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
|
|
|
c, err := dialer.DialContext(ctx, "tcp", ss.addr, ss.Base.DialOptions(opts...)...)
|
2020-03-21 15:46:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error: %w", ss.addr, err)
|
|
|
|
}
|
|
|
|
tcpKeepAlive(c)
|
|
|
|
|
2021-03-22 15:26:20 +00:00
|
|
|
defer safeConnClose(c, err)
|
|
|
|
|
2020-03-21 15:46:49 +00:00
|
|
|
c, err = ss.StreamConn(c, metadata)
|
|
|
|
return NewConn(c, ss), err
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 13:44:53 +00:00
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
2021-11-07 08:48:51 +00:00
|
|
|
func (ss *ShadowSocks) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
|
|
|
pc, err := dialer.ListenPacket(ctx, "udp", "", ss.Base.DialOptions(opts...)...)
|
2019-04-23 15:29:36 +00:00
|
|
|
if err != nil {
|
2020-01-31 06:43:54 +00:00
|
|
|
return nil, err
|
2019-04-23 15:29:36 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 15:46:49 +00:00
|
|
|
addr, err := resolveUDPAddr("udp", ss.addr)
|
2019-04-24 02:29:29 +00:00
|
|
|
if err != nil {
|
2021-03-22 15:26:20 +00:00
|
|
|
pc.Close()
|
2020-01-31 06:43:54 +00:00
|
|
|
return nil, err
|
2019-04-24 02:29:29 +00:00
|
|
|
}
|
2019-04-23 15:29:36 +00:00
|
|
|
|
|
|
|
pc = ss.cipher.PacketConn(pc)
|
2020-02-17 09:34:19 +00:00
|
|
|
return newPacketConn(&ssPacketConn{PacketConn: pc, rAddr: addr}, ss), nil
|
2019-04-23 15:29:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 07:26:36 +00:00
|
|
|
func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
2020-03-21 15:46:49 +00:00
|
|
|
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
2018-10-02 07:26:36 +00:00
|
|
|
cipher := option.Cipher
|
|
|
|
password := option.Password
|
2018-09-21 03:33:29 +00:00
|
|
|
ciph, err := core.PickCipher(cipher, nil, password)
|
2018-06-16 07:41:26 +00:00
|
|
|
if err != nil {
|
2020-03-21 15:46:49 +00:00
|
|
|
return nil, fmt.Errorf("ss %s initialize error: %w", addr, err)
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
2018-09-16 15:02:32 +00:00
|
|
|
|
2019-09-21 15:49:00 +00:00
|
|
|
var v2rayOption *v2rayObfs.Option
|
2019-02-11 07:25:10 +00:00
|
|
|
var obfsOption *simpleObfsOption
|
|
|
|
obfsMode := ""
|
|
|
|
|
|
|
|
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 {
|
2020-03-21 15:46:49 +00:00
|
|
|
return nil, fmt.Errorf("ss %s initialize obfs error: %w", addr, err)
|
2019-02-11 07:25:10 +00:00
|
|
|
}
|
2019-06-18 12:37:53 +00:00
|
|
|
|
|
|
|
if opts.Mode != "tls" && opts.Mode != "http" {
|
2020-03-21 15:46:49 +00:00
|
|
|
return nil, fmt.Errorf("ss %s obfs mode error: %s", addr, opts.Mode)
|
2019-06-18 12:37:53 +00:00
|
|
|
}
|
2019-02-11 07:25:10 +00:00
|
|
|
obfsMode = opts.Mode
|
|
|
|
obfsOption = &opts
|
|
|
|
} else if option.Plugin == "v2ray-plugin" {
|
2019-09-21 15:49:00 +00:00
|
|
|
opts := v2rayObfsOption{Host: "bing.com", Mux: true}
|
2019-02-11 07:25:10 +00:00
|
|
|
if err := decoder.Decode(option.PluginOpts, &opts); err != nil {
|
2020-03-21 15:46:49 +00:00
|
|
|
return nil, fmt.Errorf("ss %s initialize v2ray-plugin error: %w", addr, err)
|
2019-02-11 07:25:10 +00:00
|
|
|
}
|
2019-06-18 12:37:53 +00:00
|
|
|
|
|
|
|
if opts.Mode != "websocket" {
|
2020-03-21 15:46:49 +00:00
|
|
|
return nil, fmt.Errorf("ss %s obfs mode error: %s", addr, opts.Mode)
|
2019-06-18 12:37:53 +00:00
|
|
|
}
|
2019-02-11 07:25:10 +00:00
|
|
|
obfsMode = opts.Mode
|
2020-03-31 08:07:21 +00:00
|
|
|
v2rayOption = &v2rayObfs.Option{
|
|
|
|
Host: opts.Host,
|
|
|
|
Path: opts.Path,
|
|
|
|
Headers: opts.Headers,
|
|
|
|
Mux: opts.Mux,
|
|
|
|
}
|
2019-06-18 12:37:53 +00:00
|
|
|
|
2019-02-11 07:25:10 +00:00
|
|
|
if opts.TLS {
|
2020-03-31 08:07:21 +00:00
|
|
|
v2rayOption.TLS = true
|
|
|
|
v2rayOption.SkipCertVerify = opts.SkipCertVerify
|
2019-02-11 07:25:10 +00:00
|
|
|
}
|
2018-09-16 15:02:32 +00:00
|
|
|
}
|
|
|
|
|
2018-06-16 07:41:26 +00:00
|
|
|
return &ShadowSocks{
|
2018-12-22 15:56:42 +00:00
|
|
|
Base: &Base{
|
2021-11-07 08:48:51 +00:00
|
|
|
name: option.Name,
|
|
|
|
addr: addr,
|
|
|
|
tp: C.Shadowsocks,
|
|
|
|
udp: option.UDP,
|
|
|
|
iface: option.Interface,
|
2022-03-19 05:29:30 +00:00
|
|
|
rmark: option.RoutingMark,
|
2018-12-22 15:56:42 +00:00
|
|
|
},
|
2019-02-11 07:25:10 +00:00
|
|
|
cipher: ciph,
|
|
|
|
|
2019-09-21 15:49:00 +00:00
|
|
|
obfsMode: obfsMode,
|
|
|
|
v2rayOption: v2rayOption,
|
|
|
|
obfsOption: obfsOption,
|
2018-06-16 07:41:26 +00:00
|
|
|
}, nil
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 09:34:19 +00:00
|
|
|
type ssPacketConn struct {
|
2019-04-23 15:29:36 +00:00
|
|
|
net.PacketConn
|
2020-01-31 06:43:54 +00:00
|
|
|
rAddr net.Addr
|
2019-04-23 15:29:36 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 09:34:19 +00:00
|
|
|
func (spc *ssPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
2020-01-31 06:43:54 +00:00
|
|
|
packet, err := socks5.EncodeUDPPacket(socks5.ParseAddrToSocksAddr(addr), b)
|
2019-10-11 12:11:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-02-17 09:34:19 +00:00
|
|
|
return spc.PacketConn.WriteTo(packet[3:], spc.rAddr)
|
2019-04-23 15:29:36 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 09:34:19 +00:00
|
|
|
func (spc *ssPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
|
|
|
n, _, e := spc.PacketConn.ReadFrom(b)
|
2020-02-18 08:05:12 +00:00
|
|
|
if e != nil {
|
|
|
|
return 0, nil, e
|
2019-12-28 10:44:01 +00:00
|
|
|
}
|
2020-02-29 17:46:02 +00:00
|
|
|
|
2020-02-18 08:05:12 +00:00
|
|
|
addr := socks5.SplitAddr(b[:n])
|
2020-02-29 17:46:02 +00:00
|
|
|
if addr == nil {
|
|
|
|
return 0, nil, errors.New("parse addr error")
|
|
|
|
}
|
|
|
|
|
2020-03-02 15:47:23 +00:00
|
|
|
udpAddr := addr.UDPAddr()
|
|
|
|
if udpAddr == nil {
|
|
|
|
return 0, nil, errors.New("parse addr error")
|
|
|
|
}
|
|
|
|
|
2019-04-23 15:29:36 +00:00
|
|
|
copy(b, b[len(addr):])
|
2020-03-02 15:47:23 +00:00
|
|
|
return n - len(addr), udpAddr, e
|
2019-04-23 15:29:36 +00:00
|
|
|
}
|