2019-12-08 12:17:24 +08:00
|
|
|
package outbound
|
2018-06-10 22:50:03 +08:00
|
|
|
|
|
|
|
import (
|
2019-10-12 23:55:39 +08:00
|
|
|
"context"
|
2020-03-01 01:46:02 +08:00
|
|
|
"errors"
|
2018-06-10 22:50:03 +08:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
|
2023-02-27 00:26:49 +08:00
|
|
|
N "github.com/Dreamacro/clash/common/net"
|
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"
|
2023-04-10 11:20:28 +08:00
|
|
|
"github.com/Dreamacro/clash/component/proxydialer"
|
2023-04-12 21:49:22 +08:00
|
|
|
"github.com/Dreamacro/clash/component/resolver"
|
2018-06-10 22:50:03 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2023-03-14 13:33:24 +08:00
|
|
|
"github.com/Dreamacro/clash/transport/restls"
|
2021-05-13 22:18:49 +08:00
|
|
|
obfs "github.com/Dreamacro/clash/transport/simple-obfs"
|
2023-02-21 21:58:37 +08:00
|
|
|
shadowtls "github.com/Dreamacro/clash/transport/sing-shadowtls"
|
2021-05-13 22:18:49 +08:00
|
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
|
|
|
v2rayObfs "github.com/Dreamacro/clash/transport/v2ray-plugin"
|
2022-11-12 20:43:48 +08:00
|
|
|
|
2023-03-14 13:33:24 +08:00
|
|
|
restlsC "github.com/3andne/restls-client-go"
|
2023-05-10 07:23:49 +08:00
|
|
|
"github.com/metacubex/sing-shadowsocks2"
|
2022-06-06 19:52:02 +08:00
|
|
|
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
|
2022-06-06 19:52:02 +08:00
|
|
|
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
|
2023-01-11 00:10:39 +08:00
|
|
|
obfsMode string
|
|
|
|
obfsOption *simpleObfsOption
|
|
|
|
v2rayOption *v2rayObfs.Option
|
2023-02-21 21:58:37 +08:00
|
|
|
shadowTLSOption *shadowtls.ShadowTLSOption
|
2023-03-14 13:33:24 +08:00
|
|
|
restlsConfig *restlsC.Config
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
2018-10-02 15:26:36 +08:00
|
|
|
type ShadowSocksOption struct {
|
2021-11-07 16:48:51 +08:00
|
|
|
BasicOption
|
2023-03-14 13:33:24 +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"`
|
|
|
|
UDPOverTCP bool `proxy:"udp-over-tcp,omitempty"`
|
2023-03-15 14:08:52 +08:00
|
|
|
UDPOverTCPVersion int `proxy:"udp-over-tcp-version,omitempty"`
|
2023-03-14 13:33:24 +08:00
|
|
|
ClientFingerprint string `proxy:"client-fingerprint,omitempty"`
|
2018-10-02 15:26:36 +08:00
|
|
|
}
|
|
|
|
|
2019-02-11 15:25:10 +08:00
|
|
|
type simpleObfsOption struct {
|
2020-12-24 13:47:56 +08:00
|
|
|
Mode string `obfs:"mode,omitempty"`
|
2019-02-11 15:25:10 +08:00
|
|
|
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"`
|
2022-07-11 13:42:28 +08:00
|
|
|
Fingerprint string `obfs:"fingerprint,omitempty"`
|
2019-03-15 09:43:46 +08:00
|
|
|
Headers map[string]string `obfs:"headers,omitempty"`
|
|
|
|
SkipCertVerify bool `obfs:"skip-cert-verify,omitempty"`
|
2019-09-21 23:49:00 +08:00
|
|
|
Mux bool `obfs:"mux,omitempty"`
|
2019-02-11 15:25:10 +08:00
|
|
|
}
|
|
|
|
|
2023-01-11 00:10:39 +08:00
|
|
|
type shadowTLSOption struct {
|
2023-03-14 13:33:24 +08:00
|
|
|
Password string `obfs:"password"`
|
|
|
|
Host string `obfs:"host"`
|
|
|
|
Fingerprint string `obfs:"fingerprint,omitempty"`
|
|
|
|
SkipCertVerify bool `obfs:"skip-cert-verify,omitempty"`
|
|
|
|
Version int `obfs:"version,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type restlsOption struct {
|
|
|
|
Password string `obfs:"password"`
|
|
|
|
Host string `obfs:"host"`
|
|
|
|
VersionHint string `obfs:"version-hint"`
|
|
|
|
RestlsScript string `obfs:"restls-script,omitempty"`
|
2023-01-11 00:10:39 +08:00
|
|
|
}
|
|
|
|
|
2023-05-18 13:15:08 +08:00
|
|
|
// StreamConnContext implements C.ProxyAdapter
|
2023-03-14 16:50:27 +08:00
|
|
|
func (ss *ShadowSocks) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
|
|
|
useEarly := false
|
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":
|
2020-03-21 23:46:49 +08:00
|
|
|
_, 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
|
2023-05-18 13:15:08 +08:00
|
|
|
c, err = v2rayObfs.NewV2rayObfs(ctx, c, ss.v2rayOption)
|
2019-02-11 15:25:10 +08:00
|
|
|
if err != nil {
|
2020-03-21 23:46:49 +08:00
|
|
|
return nil, fmt.Errorf("%s connect error: %w", ss.addr, err)
|
2019-02-11 15:25:10 +08:00
|
|
|
}
|
2023-03-14 16:50:27 +08:00
|
|
|
case shadowtls.Mode:
|
|
|
|
var err error
|
|
|
|
c, err = shadowtls.NewShadowTLS(ctx, c, ss.shadowTLSOption)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
useEarly = true
|
2023-03-14 13:33:24 +08:00
|
|
|
case restls.Mode:
|
|
|
|
var err error
|
2023-03-14 16:50:27 +08:00
|
|
|
c, err = restls.NewRestls(ctx, c, ss.restlsConfig)
|
2023-03-14 13:33:24 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s (restls) connect error: %w", ss.addr, err)
|
|
|
|
}
|
2023-03-14 16:50:27 +08:00
|
|
|
useEarly = true
|
2018-09-16 23:02:32 +08:00
|
|
|
}
|
2023-03-14 16:50:27 +08:00
|
|
|
useEarly = useEarly || N.NeedHandshake(c)
|
2022-06-18 16:38:44 +08:00
|
|
|
if metadata.NetWork == C.UDP && ss.option.UDPOverTCP {
|
2023-03-17 13:23:37 +08:00
|
|
|
uotDestination := uot.RequestDestination(uint8(ss.option.UDPOverTCPVersion))
|
2023-03-14 16:50:27 +08:00
|
|
|
if useEarly {
|
2023-03-15 14:08:52 +08:00
|
|
|
return ss.method.DialEarlyConn(c, uotDestination), nil
|
2023-02-27 00:26:49 +08:00
|
|
|
} else {
|
2023-03-15 14:08:52 +08:00
|
|
|
return ss.method.DialConn(c, uotDestination)
|
2023-02-27 00:26:49 +08:00
|
|
|
}
|
|
|
|
}
|
2023-03-14 16:50:27 +08:00
|
|
|
if useEarly {
|
2023-02-27 00:26:49 +08:00
|
|
|
return ss.method.DialEarlyConn(c, M.ParseSocksaddr(metadata.RemoteAddress())), nil
|
|
|
|
} else {
|
|
|
|
return ss.method.DialConn(c, M.ParseSocksaddr(metadata.RemoteAddress()))
|
2022-06-18 16:38:44 +08:00
|
|
|
}
|
2020-03-21 23:46:49 +08:00
|
|
|
}
|
|
|
|
|
2021-04-29 11:23:14 +08:00
|
|
|
// DialContext implements C.ProxyAdapter
|
2021-11-07 16:48:51 +08:00
|
|
|
func (ss *ShadowSocks) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
2022-12-20 00:11:02 +08:00
|
|
|
return ss.DialContextWithDialer(ctx, dialer.NewDialer(ss.Base.DialOptions(opts...)...), metadata)
|
2022-12-19 21:34:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// DialContextWithDialer implements C.ProxyAdapter
|
|
|
|
func (ss *ShadowSocks) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
2023-04-10 11:20:28 +08:00
|
|
|
if len(ss.option.DialerProxy) > 0 {
|
|
|
|
dialer, err = proxydialer.NewByName(ss.option.DialerProxy, dialer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2022-12-19 21:34:07 +08:00
|
|
|
c, err := dialer.DialContext(ctx, "tcp", ss.addr)
|
2020-03-21 23:46:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error: %w", ss.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
|
|
|
|
2023-03-14 16:50:27 +08:00
|
|
|
c, err = ss.StreamConnContext(ctx, c, metadata)
|
2020-03-21 23:46:49 +08:00
|
|
|
return NewConn(c, ss), err
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
2021-10-15 21:44:53 +08:00
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
2021-11-07 16:48:51 +08:00
|
|
|
func (ss *ShadowSocks) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
2022-12-20 00:11:02 +08:00
|
|
|
return ss.ListenPacketWithDialer(ctx, dialer.NewDialer(ss.Base.DialOptions(opts...)...), metadata)
|
2022-12-19 21:34:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListenPacketWithDialer implements C.ProxyAdapter
|
|
|
|
func (ss *ShadowSocks) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
2023-04-10 11:20:28 +08:00
|
|
|
if len(ss.option.DialerProxy) > 0 {
|
|
|
|
dialer, err = proxydialer.NewByName(ss.option.DialerProxy, dialer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2022-06-18 10:50:18 +08:00
|
|
|
if ss.option.UDPOverTCP {
|
2022-12-19 21:34:07 +08:00
|
|
|
tcpConn, err := ss.DialContextWithDialer(ctx, dialer, metadata)
|
2022-06-18 10:50:18 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-12 21:49:22 +08:00
|
|
|
return ss.ListenPacketOnStreamConn(ctx, tcpConn, metadata)
|
2022-06-18 10:50:18 +08:00
|
|
|
}
|
2022-12-13 11:18:32 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-12-19 21:34:07 +08:00
|
|
|
pc, err := dialer.ListenPacket(ctx, "udp", "", addr.AddrPort())
|
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
|
|
|
}
|
2023-05-12 09:14:27 +08:00
|
|
|
pc = ss.method.DialPacketConn(N.NewBindPacketConn(pc, addr))
|
2022-06-06 19:52:02 +08:00
|
|
|
return newPacketConn(pc, ss), nil
|
2019-04-23 23:29:36 +08:00
|
|
|
}
|
|
|
|
|
2022-12-19 21:34:07 +08:00
|
|
|
// SupportWithDialer implements C.ProxyAdapter
|
2023-04-11 12:51:24 +08:00
|
|
|
func (ss *ShadowSocks) SupportWithDialer() C.NetWork {
|
|
|
|
return C.ALLNet
|
2022-12-19 17:02:04 +08:00
|
|
|
}
|
|
|
|
|
2022-06-18 16:38:44 +08:00
|
|
|
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
2023-04-12 21:49:22 +08:00
|
|
|
func (ss *ShadowSocks) ListenPacketOnStreamConn(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
2022-06-18 16:38:44 +08:00
|
|
|
if ss.option.UDPOverTCP {
|
2023-04-12 21:49:22 +08:00
|
|
|
// ss uot use stream-oriented udp with a special address, so we need a net.UDPAddr
|
|
|
|
if !metadata.Resolved() {
|
|
|
|
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("can't resolve ip")
|
|
|
|
}
|
|
|
|
metadata.DstIP = ip
|
|
|
|
}
|
|
|
|
|
|
|
|
destination := M.SocksaddrFromNet(metadata.UDPAddr())
|
2023-03-17 13:23:37 +08:00
|
|
|
if ss.option.UDPOverTCPVersion == uot.LegacyVersion {
|
|
|
|
return newPacketConn(uot.NewConn(c, uot.Request{Destination: destination}), ss), nil
|
2023-03-15 14:08:52 +08:00
|
|
|
} else {
|
|
|
|
return newPacketConn(uot.NewLazyConn(c, uot.Request{Destination: destination}), ss), nil
|
|
|
|
}
|
2022-06-18 16:38:44 +08:00
|
|
|
}
|
2023-04-11 12:51:24 +08:00
|
|
|
return nil, C.ErrNotSupport
|
2022-06-18 16:38:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SupportUOT implements C.ProxyAdapter
|
|
|
|
func (ss *ShadowSocks) SupportUOT() bool {
|
|
|
|
return ss.option.UDPOverTCP
|
|
|
|
}
|
|
|
|
|
2018-10-02 15:26:36 +08:00
|
|
|
func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
2020-03-21 23:46:49 +08:00
|
|
|
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
2023-04-30 18:57:16 +08:00
|
|
|
method, err := shadowsocks.CreateMethod(context.Background(), option.Cipher, shadowsocks.MethodOptions{
|
|
|
|
Password: option.Password,
|
|
|
|
})
|
2018-06-16 15:41:26 +08:00
|
|
|
if err != nil {
|
2020-03-21 23:46:49 +08:00
|
|
|
return nil, fmt.Errorf("ss %s initialize error: %w", addr, err)
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
2018-09-16 23:02:32 +08:00
|
|
|
|
2019-09-21 23:49:00 +08:00
|
|
|
var v2rayOption *v2rayObfs.Option
|
2019-02-11 15:25:10 +08:00
|
|
|
var obfsOption *simpleObfsOption
|
2023-02-21 21:58:37 +08:00
|
|
|
var shadowTLSOpt *shadowtls.ShadowTLSOption
|
2023-03-14 13:33:24 +08:00
|
|
|
var restlsConfig *restlsC.Config
|
2019-02-11 15:25:10 +08:00
|
|
|
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 23:46:49 +08:00
|
|
|
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" {
|
2020-03-21 23:46:49 +08:00
|
|
|
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" {
|
2019-09-21 23:49:00 +08:00
|
|
|
opts := v2rayObfsOption{Host: "bing.com", Mux: true}
|
2019-02-11 15:25:10 +08:00
|
|
|
if err := decoder.Decode(option.PluginOpts, &opts); err != nil {
|
2020-03-21 23:46:49 +08:00
|
|
|
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" {
|
2020-03-21 23:46:49 +08:00
|
|
|
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
|
2020-03-31 16:07:21 +08:00
|
|
|
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 {
|
2020-03-31 16:07:21 +08:00
|
|
|
v2rayOption.TLS = true
|
|
|
|
v2rayOption.SkipCertVerify = opts.SkipCertVerify
|
2019-02-11 15:25:10 +08:00
|
|
|
}
|
2023-01-11 00:10:39 +08:00
|
|
|
} else if option.Plugin == shadowtls.Mode {
|
|
|
|
obfsMode = shadowtls.Mode
|
2023-02-21 21:58:37 +08:00
|
|
|
opt := &shadowTLSOption{
|
|
|
|
Version: 2,
|
2023-01-11 00:10:39 +08:00
|
|
|
}
|
2023-02-21 21:58:37 +08:00
|
|
|
if err := decoder.Decode(option.PluginOpts, opt); err != nil {
|
|
|
|
return nil, fmt.Errorf("ss %s initialize shadow-tls-plugin error: %w", addr, err)
|
2023-01-11 10:19:30 +08:00
|
|
|
}
|
|
|
|
|
2023-02-21 21:58:37 +08:00
|
|
|
shadowTLSOpt = &shadowtls.ShadowTLSOption{
|
|
|
|
Password: opt.Password,
|
|
|
|
Host: opt.Host,
|
|
|
|
Fingerprint: opt.Fingerprint,
|
2023-03-14 13:33:24 +08:00
|
|
|
ClientFingerprint: option.ClientFingerprint,
|
2023-02-21 21:58:37 +08:00
|
|
|
SkipCertVerify: opt.SkipCertVerify,
|
|
|
|
Version: opt.Version,
|
2023-01-11 10:19:30 +08:00
|
|
|
}
|
2023-03-14 13:33:24 +08:00
|
|
|
} else if option.Plugin == restls.Mode {
|
|
|
|
obfsMode = restls.Mode
|
|
|
|
restlsOpt := &restlsOption{}
|
|
|
|
if err := decoder.Decode(option.PluginOpts, restlsOpt); err != nil {
|
|
|
|
return nil, fmt.Errorf("ss %s initialize restls-plugin error: %w", addr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
restlsConfig, err = restlsC.NewRestlsConfig(restlsOpt.Host, restlsOpt.Password, restlsOpt.VersionHint, restlsOpt.RestlsScript, option.ClientFingerprint)
|
|
|
|
restlsConfig.SessionTicketsDisabled = true
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("ss %s initialize restls-plugin error: %w", addr, err)
|
|
|
|
}
|
|
|
|
|
2018-09-16 23:02:32 +08:00
|
|
|
}
|
2023-03-15 14:08:52 +08:00
|
|
|
switch option.UDPOverTCPVersion {
|
|
|
|
case uot.Version, uot.LegacyVersion:
|
|
|
|
case 0:
|
|
|
|
option.UDPOverTCPVersion = uot.Version
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("ss %s unknown udp over tcp protocol version: %d", addr, option.UDPOverTCPVersion)
|
|
|
|
}
|
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{
|
2022-08-28 13:41:19 +08:00
|
|
|
name: option.Name,
|
|
|
|
addr: addr,
|
|
|
|
tp: C.Shadowsocks,
|
|
|
|
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),
|
2018-12-22 23:56:42 +08:00
|
|
|
},
|
2022-06-06 19:52:02 +08:00
|
|
|
method: method,
|
2019-02-11 15:25:10 +08:00
|
|
|
|
2023-01-11 00:10:39 +08:00
|
|
|
option: &option,
|
|
|
|
obfsMode: obfsMode,
|
|
|
|
v2rayOption: v2rayOption,
|
|
|
|
obfsOption: obfsOption,
|
|
|
|
shadowTLSOption: shadowTLSOpt,
|
2023-03-14 13:33:24 +08:00
|
|
|
restlsConfig: restlsConfig,
|
2018-06-16 15:41:26 +08:00
|
|
|
}, 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
|
2019-12-28 18:44:01 +08:00
|
|
|
}
|
2020-03-01 01:46:02 +08:00
|
|
|
|
2020-02-18 16:05:12 +08:00
|
|
|
addr := socks5.SplitAddr(b[:n])
|
2020-03-01 01:46:02 +08:00
|
|
|
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
|
|
|
}
|