2020-07-22 23:02:15 +08:00
|
|
|
package outbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
2023-04-10 11:20:28 +08:00
|
|
|
"github.com/Dreamacro/clash/component/proxydialer"
|
2020-07-22 23:02:15 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2022-05-24 20:28:18 +08:00
|
|
|
"github.com/Dreamacro/clash/transport/shadowsocks/core"
|
|
|
|
"github.com/Dreamacro/clash/transport/shadowsocks/shadowaead"
|
|
|
|
"github.com/Dreamacro/clash/transport/shadowsocks/shadowstream"
|
2021-05-13 22:18:49 +08:00
|
|
|
"github.com/Dreamacro/clash/transport/ssr/obfs"
|
|
|
|
"github.com/Dreamacro/clash/transport/ssr/protocol"
|
2020-07-22 23:02:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type ShadowSocksR struct {
|
|
|
|
*Base
|
2023-04-10 11:20:28 +08:00
|
|
|
option *ShadowSocksROption
|
2021-02-15 14:32:03 +08:00
|
|
|
cipher core.Cipher
|
2020-07-22 23:02:15 +08:00
|
|
|
obfs obfs.Obfs
|
|
|
|
protocol protocol.Protocol
|
|
|
|
}
|
|
|
|
|
|
|
|
type ShadowSocksROption struct {
|
2021-11-07 16:48:51 +08:00
|
|
|
BasicOption
|
2020-07-22 23:02:15 +08:00
|
|
|
Name string `proxy:"name"`
|
|
|
|
Server string `proxy:"server"`
|
|
|
|
Port int `proxy:"port"`
|
|
|
|
Password string `proxy:"password"`
|
|
|
|
Cipher string `proxy:"cipher"`
|
|
|
|
Obfs string `proxy:"obfs"`
|
|
|
|
ObfsParam string `proxy:"obfs-param,omitempty"`
|
|
|
|
Protocol string `proxy:"protocol"`
|
|
|
|
ProtocolParam string `proxy:"protocol-param,omitempty"`
|
|
|
|
UDP bool `proxy:"udp,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-04-29 11:23:14 +08:00
|
|
|
// StreamConn implements C.ProxyAdapter
|
2020-07-22 23:02:15 +08:00
|
|
|
func (ssr *ShadowSocksR) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
2021-02-15 14:32:03 +08:00
|
|
|
c = ssr.obfs.StreamConn(c)
|
2020-07-22 23:02:15 +08:00
|
|
|
c = ssr.cipher.StreamConn(c)
|
2021-02-15 14:32:03 +08:00
|
|
|
var (
|
|
|
|
iv []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
switch conn := c.(type) {
|
|
|
|
case *shadowstream.Conn:
|
|
|
|
iv, err = conn.ObtainWriteIV()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case *shadowaead.Conn:
|
2020-07-22 23:02:15 +08:00
|
|
|
return nil, fmt.Errorf("invalid connection type")
|
|
|
|
}
|
2021-02-15 14:32:03 +08:00
|
|
|
c = ssr.protocol.StreamConn(c, iv)
|
2020-07-22 23:02:15 +08:00
|
|
|
_, err = c.Write(serializesSocksAddr(metadata))
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
2021-04-29 11:23:14 +08:00
|
|
|
// DialContext implements C.ProxyAdapter
|
2021-11-07 16:48:51 +08:00
|
|
|
func (ssr *ShadowSocksR) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
2022-12-20 00:11:02 +08:00
|
|
|
return ssr.DialContextWithDialer(ctx, dialer.NewDialer(ssr.Base.DialOptions(opts...)...), metadata)
|
2022-12-19 21:34:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// DialContextWithDialer implements C.ProxyAdapter
|
|
|
|
func (ssr *ShadowSocksR) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
2023-04-10 11:20:28 +08:00
|
|
|
if len(ssr.option.DialerProxy) > 0 {
|
|
|
|
dialer, err = proxydialer.NewByName(ssr.option.DialerProxy, dialer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2022-12-19 21:34:07 +08:00
|
|
|
c, err := dialer.DialContext(ctx, "tcp", ssr.addr)
|
2020-07-22 23:02:15 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error: %w", ssr.addr, err)
|
|
|
|
}
|
|
|
|
tcpKeepAlive(c)
|
|
|
|
|
2022-12-16 22:15:44 +08:00
|
|
|
defer func(c net.Conn) {
|
2022-12-13 13:20:40 +08:00
|
|
|
safeConnClose(c, err)
|
2022-12-16 22:15:44 +08:00
|
|
|
}(c)
|
2021-03-22 23:26:20 +08:00
|
|
|
|
2020-07-22 23:02:15 +08:00
|
|
|
c, err = ssr.StreamConn(c, metadata)
|
|
|
|
return NewConn(c, ssr), err
|
|
|
|
}
|
|
|
|
|
2021-10-15 21:44:53 +08:00
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
2021-11-07 16:48:51 +08:00
|
|
|
func (ssr *ShadowSocksR) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
2022-12-20 00:11:02 +08:00
|
|
|
return ssr.ListenPacketWithDialer(ctx, dialer.NewDialer(ssr.Base.DialOptions(opts...)...), metadata)
|
2022-12-19 21:34:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListenPacketWithDialer implements C.ProxyAdapter
|
|
|
|
func (ssr *ShadowSocksR) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
2023-04-10 11:20:28 +08:00
|
|
|
if len(ssr.option.DialerProxy) > 0 {
|
|
|
|
dialer, err = proxydialer.NewByName(ssr.option.DialerProxy, dialer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2022-12-13 11:18:32 +08:00
|
|
|
addr, err := resolveUDPAddrWithPrefer(ctx, "udp", ssr.addr, ssr.prefer)
|
2020-07-22 23:02:15 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-19 21:34:07 +08:00
|
|
|
pc, err := dialer.ListenPacket(ctx, "udp", "", addr.AddrPort())
|
2020-07-22 23:02:15 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pc = ssr.cipher.PacketConn(pc)
|
2021-02-15 14:32:03 +08:00
|
|
|
pc = ssr.protocol.PacketConn(pc)
|
2020-07-22 23:02:15 +08:00
|
|
|
return newPacketConn(&ssPacketConn{PacketConn: pc, rAddr: addr}, ssr), nil
|
|
|
|
}
|
|
|
|
|
2022-12-19 21:34:07 +08:00
|
|
|
// SupportWithDialer implements C.ProxyAdapter
|
|
|
|
func (ssr *ShadowSocksR) SupportWithDialer() bool {
|
2022-12-19 17:02:04 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-07-22 23:02:15 +08:00
|
|
|
func NewShadowSocksR(option ShadowSocksROption) (*ShadowSocksR, error) {
|
2022-04-13 10:06:06 +08:00
|
|
|
// SSR protocol compatibility
|
|
|
|
// https://github.com/Dreamacro/clash/pull/2056
|
|
|
|
if option.Cipher == "none" {
|
|
|
|
option.Cipher = "dummy"
|
|
|
|
}
|
|
|
|
|
2020-07-22 23:02:15 +08:00
|
|
|
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
|
|
|
cipher := option.Cipher
|
|
|
|
password := option.Password
|
|
|
|
coreCiph, err := core.PickCipher(cipher, nil, password)
|
|
|
|
if err != nil {
|
2021-02-15 14:32:03 +08:00
|
|
|
return nil, fmt.Errorf("ssr %s initialize error: %w", addr, err)
|
2020-07-22 23:02:15 +08:00
|
|
|
}
|
2021-02-15 14:32:03 +08:00
|
|
|
var (
|
|
|
|
ivSize int
|
|
|
|
key []byte
|
|
|
|
)
|
2022-04-13 10:06:06 +08:00
|
|
|
|
2021-02-15 14:32:03 +08:00
|
|
|
if option.Cipher == "dummy" {
|
|
|
|
ivSize = 0
|
|
|
|
key = core.Kdf(option.Password, 16)
|
|
|
|
} else {
|
|
|
|
ciph, ok := coreCiph.(*core.StreamCipher)
|
|
|
|
if !ok {
|
2022-04-13 10:06:06 +08:00
|
|
|
return nil, fmt.Errorf("%s is not none or a supported stream cipher in ssr", cipher)
|
2021-02-15 14:32:03 +08:00
|
|
|
}
|
|
|
|
ivSize = ciph.IVSize()
|
|
|
|
key = ciph.Key
|
2020-07-22 23:02:15 +08:00
|
|
|
}
|
|
|
|
|
2021-02-15 14:32:03 +08:00
|
|
|
obfs, obfsOverhead, err := obfs.PickObfs(option.Obfs, &obfs.Base{
|
|
|
|
Host: option.Server,
|
|
|
|
Port: option.Port,
|
|
|
|
Key: key,
|
|
|
|
IVSize: ivSize,
|
|
|
|
Param: option.ObfsParam,
|
2020-07-22 23:02:15 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("ssr %s initialize obfs error: %w", addr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol, err := protocol.PickProtocol(option.Protocol, &protocol.Base{
|
2021-02-15 14:32:03 +08:00
|
|
|
Key: key,
|
|
|
|
Overhead: obfsOverhead,
|
|
|
|
Param: option.ProtocolParam,
|
2020-07-22 23:02:15 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("ssr %s initialize protocol error: %w", addr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ShadowSocksR{
|
|
|
|
Base: &Base{
|
2022-08-28 13:41:19 +08:00
|
|
|
name: option.Name,
|
|
|
|
addr: addr,
|
|
|
|
tp: C.ShadowsocksR,
|
|
|
|
udp: option.UDP,
|
2023-02-24 13:53:44 +08:00
|
|
|
tfo: option.TFO,
|
2022-08-28 13:41:19 +08:00
|
|
|
iface: option.Interface,
|
|
|
|
rmark: option.RoutingMark,
|
|
|
|
prefer: C.NewDNSPrefer(option.IPVersion),
|
2020-07-22 23:02:15 +08:00
|
|
|
},
|
2023-04-10 11:20:28 +08:00
|
|
|
option: &option,
|
2021-02-15 14:32:03 +08:00
|
|
|
cipher: coreCiph,
|
2020-07-22 23:02:15 +08:00
|
|
|
obfs: obfs,
|
|
|
|
protocol: protocol,
|
|
|
|
}, nil
|
|
|
|
}
|