mihomo/adapter/outbound/shadowsocks.go

231 lines
6.6 KiB
Go
Raw Normal View History

2019-12-08 12:17:24 +08:00
package outbound
2018-06-10 22:50:03 +08:00
import (
"context"
"errors"
2018-06-10 22:50:03 +08:00
"fmt"
"net"
"strconv"
2019-02-11 15:25:10 +08:00
"github.com/Dreamacro/clash/common/structure"
2020-02-09 17:02:48 +08:00
"github.com/Dreamacro/clash/component/dialer"
2018-06-10 22:50:03 +08:00
C "github.com/Dreamacro/clash/constant"
2021-05-13 22:18:49 +08:00
obfs "github.com/Dreamacro/clash/transport/simple-obfs"
"github.com/Dreamacro/clash/transport/socks5"
v2rayObfs "github.com/Dreamacro/clash/transport/v2ray-plugin"
2022-12-05 11:03:28 +08:00
"github.com/metacubex/sing-shadowsocks"
"github.com/metacubex/sing-shadowsocks/shadowimpl"
"github.com/sagernet/sing/common/bufio"
M "github.com/sagernet/sing/common/metadata"
2022-06-18 10:50:18 +08:00
"github.com/sagernet/sing/common/uot"
2018-06-10 22:50:03 +08:00
)
type ShadowSocks struct {
2018-12-22 23:56:42 +08:00
*Base
method shadowsocks.Method
2019-02-11 15:25:10 +08:00
2022-06-18 10:50:18 +08:00
option *ShadowSocksOption
2019-02-11 15:25:10 +08:00
// obfs
obfsMode string
obfsOption *simpleObfsOption
v2rayOption *v2rayObfs.Option
2018-06-10 22:50:03 +08:00
}
type ShadowSocksOption struct {
BasicOption
2022-03-16 12:10:13 +08: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"`
2022-06-18 10:50:18 +08:00
UDPOverTCP bool `proxy:"udp-over-tcp,omitempty"`
}
2019-02-11 15:25:10 +08:00
type simpleObfsOption struct {
Mode string `obfs:"mode,omitempty"`
2019-02-11 15:25:10 +08:00
Host string `obfs:"host,omitempty"`
}
type v2rayObfsOption struct {
Mode string `obfs:"mode"`
Host string `obfs:"host,omitempty"`
Path string `obfs:"path,omitempty"`
TLS bool `obfs:"tls,omitempty"`
2022-07-11 13:42:28 +08:00
Fingerprint string `obfs:"fingerprint,omitempty"`
Headers map[string]string `obfs:"headers,omitempty"`
SkipCertVerify bool `obfs:"skip-cert-verify,omitempty"`
Mux bool `obfs:"mux,omitempty"`
2019-02-11 15:25:10 +08:00
}
2021-04-29 11:23:14 +08:00
// StreamConn implements C.ProxyAdapter
func (ss *ShadowSocks) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
2019-02-11 15:25:10 +08:00
switch ss.obfsMode {
case "tls":
2019-02-11 15:25:10 +08:00
c = obfs.NewTLSObfs(c, ss.obfsOption.Host)
case "http":
_, port, _ := net.SplitHostPort(ss.addr)
2019-02-11 15:25:10 +08:00
c = obfs.NewHTTPObfs(c, ss.obfsOption.Host, port)
case "websocket":
var err error
c, err = v2rayObfs.NewV2rayObfs(c, ss.v2rayOption)
2019-02-11 15:25:10 +08:00
if err != nil {
return nil, fmt.Errorf("%s connect error: %w", ss.addr, err)
2019-02-11 15:25:10 +08:00
}
}
2022-06-18 16:38:44 +08:00
if metadata.NetWork == C.UDP && ss.option.UDPOverTCP {
2022-09-19 17:37:16 +08:00
return ss.method.DialConn(c, M.ParseSocksaddr(uot.UOTMagicAddress+":443"))
2022-06-18 16:38:44 +08:00
}
return ss.method.DialConn(c, M.ParseSocksaddr(metadata.RemoteAddress()))
}
2021-04-29 11:23:14 +08:00
// DialContext implements C.ProxyAdapter
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...)...)
if err != nil {
return nil, fmt.Errorf("%s connect error: %w", ss.addr, err)
}
tcpKeepAlive(c)
defer safeConnClose(c, err)
c, err = ss.StreamConn(c, metadata)
return NewConn(c, ss), err
2018-06-10 22:50:03 +08:00
}
// ListenPacketContext implements C.ProxyAdapter
func (ss *ShadowSocks) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
2022-06-18 10:50:18 +08:00
if ss.option.UDPOverTCP {
tcpConn, err := ss.DialContext(ctx, metadata, opts...)
if err != nil {
return nil, err
}
return newPacketConn(uot.NewClientConn(tcpConn), ss), nil
}
addr, err := resolveUDPAddrWithPrefer(ctx, "udp", ss.addr, ss.prefer)
2019-04-23 23:29:36 +08:00
if err != nil {
2020-01-31 14:43:54 +08:00
return nil, err
2019-04-23 23:29:36 +08:00
}
pc, err := dialer.ListenPacket(ctx, dialer.ParseNetwork("udp", addr.AddrPort().Addr()), "", ss.Base.DialOptions(opts...)...)
2019-04-24 10:29:29 +08:00
if err != nil {
2020-01-31 14:43:54 +08:00
return nil, err
2019-04-24 10:29:29 +08:00
}
pc = ss.method.DialPacketConn(&bufio.BindPacketConn{PacketConn: pc, Addr: addr})
return newPacketConn(pc, ss), nil
2019-04-23 23:29:36 +08:00
}
2022-06-18 16:38:44 +08:00
// ListenPacketOnStreamConn implements C.ProxyAdapter
func (ss *ShadowSocks) ListenPacketOnStreamConn(c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
if ss.option.UDPOverTCP {
return newPacketConn(uot.NewClientConn(c), ss), nil
}
return nil, errors.New("no support")
}
// SupportUOT implements C.ProxyAdapter
func (ss *ShadowSocks) SupportUOT() bool {
return ss.option.UDPOverTCP
}
func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
method, err := shadowimpl.FetchMethod(option.Cipher, option.Password)
if err != nil {
return nil, fmt.Errorf("ss %s initialize error: %w", addr, err)
2018-06-10 22:50:03 +08:00
}
var v2rayOption *v2rayObfs.Option
2019-02-11 15:25:10 +08: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 {
return nil, fmt.Errorf("ss %s initialize obfs error: %w", addr, err)
2019-02-11 15:25:10 +08:00
}
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", addr, opts.Mode)
2019-06-18 20:37:53 +08:00
}
2019-02-11 15:25:10 +08:00
obfsMode = opts.Mode
obfsOption = &opts
} else if option.Plugin == "v2ray-plugin" {
opts := v2rayObfsOption{Host: "bing.com", Mux: true}
2019-02-11 15:25:10 +08:00
if err := decoder.Decode(option.PluginOpts, &opts); err != nil {
return nil, fmt.Errorf("ss %s initialize v2ray-plugin error: %w", addr, err)
2019-02-11 15:25:10 +08:00
}
2019-06-18 20:37:53 +08:00
if opts.Mode != "websocket" {
return nil, fmt.Errorf("ss %s obfs mode error: %s", addr, opts.Mode)
2019-06-18 20:37:53 +08:00
}
2019-02-11 15:25:10 +08:00
obfsMode = opts.Mode
v2rayOption = &v2rayObfs.Option{
Host: opts.Host,
Path: opts.Path,
Headers: opts.Headers,
Mux: opts.Mux,
}
2019-06-18 20:37:53 +08:00
2019-02-11 15:25:10 +08:00
if opts.TLS {
v2rayOption.TLS = true
v2rayOption.SkipCertVerify = opts.SkipCertVerify
2019-02-11 15:25:10 +08:00
}
}
return &ShadowSocks{
2018-12-22 23:56:42 +08:00
Base: &Base{
2022-08-28 13:41:19 +08:00
name: option.Name,
addr: addr,
tp: C.Shadowsocks,
udp: option.UDP,
iface: option.Interface,
rmark: option.RoutingMark,
prefer: C.NewDNSPrefer(option.IPVersion),
2018-12-22 23:56:42 +08:00
},
method: method,
2019-02-11 15:25:10 +08:00
2022-06-18 10:50:18 +08:00
option: &option,
obfsMode: obfsMode,
v2rayOption: v2rayOption,
obfsOption: obfsOption,
}, nil
2018-06-10 22:50:03 +08:00
}
2020-02-17 17:34:19 +08:00
type ssPacketConn struct {
2019-04-23 23:29:36 +08:00
net.PacketConn
2020-01-31 14:43:54 +08:00
rAddr net.Addr
2019-04-23 23:29:36 +08:00
}
2020-02-17 17:34:19 +08:00
func (spc *ssPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
2020-01-31 14:43:54 +08:00
packet, err := socks5.EncodeUDPPacket(socks5.ParseAddrToSocksAddr(addr), b)
2019-10-11 20:11:18 +08:00
if err != nil {
return
}
2020-02-17 17:34:19 +08:00
return spc.PacketConn.WriteTo(packet[3:], spc.rAddr)
2019-04-23 23:29:36 +08:00
}
2020-02-17 17:34:19 +08:00
func (spc *ssPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
n, _, e := spc.PacketConn.ReadFrom(b)
2020-02-18 16:05:12 +08:00
if e != nil {
return 0, nil, e
}
2020-02-18 16:05:12 +08:00
addr := socks5.SplitAddr(b[:n])
if addr == nil {
return 0, nil, errors.New("parse addr error")
}
2020-03-02 23:47:23 +08:00
udpAddr := addr.UDPAddr()
if udpAddr == nil {
return 0, nil, errors.New("parse addr error")
}
2019-04-23 23:29:36 +08:00
copy(b, b[len(addr):])
2020-03-02 23:47:23 +08:00
return n - len(addr), udpAddr, e
2019-04-23 23:29:36 +08:00
}