From aa76324ac6f2bc439afcb0e8cbd163c3c645eb41 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Mon, 6 Jun 2022 19:52:02 +0800 Subject: [PATCH] feat: add support for shadowsocks 2022 ciphers --- adapter/outbound/shadowsocks.go | 28 ++++++++++++++++------------ common/pool/alloc.go | 2 +- common/pool/pool.go | 4 ++-- go.mod | 6 +++++- go.sum | 13 +++++++++++-- 5 files changed, 35 insertions(+), 18 deletions(-) diff --git a/adapter/outbound/shadowsocks.go b/adapter/outbound/shadowsocks.go index 6ed78050..5f53edae 100644 --- a/adapter/outbound/shadowsocks.go +++ b/adapter/outbound/shadowsocks.go @@ -7,18 +7,27 @@ import ( "net" "strconv" + "github.com/Dreamacro/clash/common/pool" "github.com/Dreamacro/clash/common/structure" "github.com/Dreamacro/clash/component/dialer" C "github.com/Dreamacro/clash/constant" - "github.com/Dreamacro/clash/transport/shadowsocks/core" obfs "github.com/Dreamacro/clash/transport/simple-obfs" "github.com/Dreamacro/clash/transport/socks5" v2rayObfs "github.com/Dreamacro/clash/transport/v2ray-plugin" + "github.com/sagernet/sing-shadowsocks" + "github.com/sagernet/sing-shadowsocks/shadowimpl" + "github.com/sagernet/sing/common/buf" + "github.com/sagernet/sing/common/bufio" + M "github.com/sagernet/sing/common/metadata" ) +func init() { + buf.DefaultAllocator = pool.DefaultAllocator +} + type ShadowSocks struct { *Base - cipher core.Cipher + method shadowsocks.Method // obfs obfsMode string @@ -68,9 +77,7 @@ func (ss *ShadowSocks) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, e return nil, fmt.Errorf("%s connect error: %w", ss.addr, err) } } - c = ss.cipher.StreamConn(c) - _, err := c.Write(serializesSocksAddr(metadata)) - return c, err + return ss.method.DialConn(c, M.ParseSocksaddr(metadata.RemoteAddress())) } // DialContext implements C.ProxyAdapter @@ -99,16 +106,13 @@ func (ss *ShadowSocks) ListenPacketContext(ctx context.Context, metadata *C.Meta pc.Close() return nil, err } - - pc = ss.cipher.PacketConn(pc) - return newPacketConn(&ssPacketConn{PacketConn: pc, rAddr: addr}, ss), nil + pc = ss.method.DialPacketConn(&bufio.BindPacketConn{PacketConn: pc, Addr: addr}) + return newPacketConn(pc, ss), nil } func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) { addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port)) - cipher := option.Cipher - password := option.Password - ciph, err := core.PickCipher(cipher, nil, password) + method, err := shadowimpl.FetchMethod(option.Cipher, option.Password) if err != nil { return nil, fmt.Errorf("ss %s initialize error: %w", addr, err) } @@ -161,7 +165,7 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) { iface: option.Interface, rmark: option.RoutingMark, }, - cipher: ciph, + method: method, obfsMode: obfsMode, v2rayOption: v2rayOption, diff --git a/common/pool/alloc.go b/common/pool/alloc.go index efc19123..4c063bb8 100644 --- a/common/pool/alloc.go +++ b/common/pool/alloc.go @@ -8,7 +8,7 @@ import ( "sync" ) -var defaultAllocator = NewAllocator() +var DefaultAllocator = NewAllocator() // Allocator for incoming frames, optimized to prevent overwriting after zeroing type Allocator struct { diff --git a/common/pool/pool.go b/common/pool/pool.go index bee4887f..a2beb082 100644 --- a/common/pool/pool.go +++ b/common/pool/pool.go @@ -13,9 +13,9 @@ const ( ) func Get(size int) []byte { - return defaultAllocator.Get(size) + return DefaultAllocator.Get(size) } func Put(buf []byte) error { - return defaultAllocator.Put(buf) + return DefaultAllocator.Put(buf) } diff --git a/go.mod b/go.mod index 0117be57..7001acf4 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,8 @@ require ( github.com/lucas-clemente/quic-go v0.27.0 github.com/miekg/dns v1.1.49 github.com/oschwald/geoip2-golang v1.7.0 + github.com/sagernet/sing v0.0.0-20220606113732-5efacc1c7db5 + github.com/sagernet/sing-shadowsocks v0.0.0-20220606072955-efe895ec59b5 github.com/sirupsen/logrus v1.8.1 github.com/stretchr/testify v1.7.1 github.com/vishvananda/netlink v1.2.0-beta.0.20220404152918-5e915e014938 @@ -30,6 +32,7 @@ require ( golang.zx2c4.com/wireguard/windows v0.5.4-0.20220328111914-004c22c5647e google.golang.org/protobuf v1.28.0 gopkg.in/yaml.v2 v2.4.0 + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b gvisor.dev/gvisor v0.0.0-20220527053002-8ab279227ac8 ) @@ -39,6 +42,7 @@ require ( github.com/fsnotify/fsnotify v1.5.4 // indirect github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect github.com/google/btree v1.0.1 // indirect + github.com/klauspost/cpuid/v2 v2.0.12 // indirect github.com/kr/pretty v0.2.1 // indirect github.com/marten-seemann/qtls-go1-16 v0.1.5 // indirect github.com/marten-seemann/qtls-go1-17 v0.1.1 // indirect @@ -55,7 +59,7 @@ require ( golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect golang.zx2c4.com/wintun v0.0.0-20211104114900-415007cec224 // indirect gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect + lukechampine.com/blake3 v1.1.7 // indirect ) replace github.com/vishvananda/netlink v1.2.0-beta.0.20220404152918-5e915e014938 => github.com/MetaCubeX/netlink v1.2.0-beta.0.20220529072258-d6853f887820 diff --git a/go.sum b/go.sum index 8b215254..1f8d8316 100644 --- a/go.sum +++ b/go.sum @@ -97,6 +97,9 @@ github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCV github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.0.12 h1:p9dKCg8i4gmOxtv35DvrYoWqYzQrvEVdjQ762Y0OqZE= +github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= @@ -158,6 +161,10 @@ github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1: github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= +github.com/sagernet/sing v0.0.0-20220606113732-5efacc1c7db5 h1:jE4puDTAniQRtt/TmmHVIdgqo3RDoi+ztPa2oQXpG28= +github.com/sagernet/sing v0.0.0-20220606113732-5efacc1c7db5/go.mod h1:w2HnJzXKHpD6F5Z/9XlSD4qbcpHY2RSZuQnFzqgELMg= +github.com/sagernet/sing-shadowsocks v0.0.0-20220606072955-efe895ec59b5 h1:yA6MV01pgWaLaFk9sdb1M8HSPasRj55NcYsAQVuNq0A= +github.com/sagernet/sing-shadowsocks v0.0.0-20220606072955-efe895ec59b5/go.mod h1:4Wb0SmPgT8ZBQeY2kf4LFI6jl4T9mHJ2uDPdxG5ky38= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY= github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM= @@ -388,13 +395,15 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= grpc.go4.org v0.0.0-20170609214715-11d0a25b4919/go.mod h1:77eQGdRu53HpSqPFJFmuJdjuHRquDANNeA4x7B8WQ9o= gvisor.dev/gvisor v0.0.0-20220527053002-8ab279227ac8 h1:K6RgHqNR+9t3sKVsfRFsvXryRL5kL6wtBPU5aPt1jLY= gvisor.dev/gvisor v0.0.0-20220527053002-8ab279227ac8/go.mod h1:TIvkJD0sxe8pIob3p6T8IzxXunlp6yfgktvTNp+DGNM= honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +lukechampine.com/blake3 v1.1.7 h1:GgRMhmdsuK8+ii6UZFDL8Nb+VyMwadAgcJyfYHxG6n0= +lukechampine.com/blake3 v1.1.7/go.mod h1:tkKEOtDkNtklkXtLNEOGNq5tcV90tJiA1vAA12R78LA= sourcegraph.com/sourcegraph/go-diff v0.5.0/go.mod h1:kuch7UrkMzY0X+p9CRK03kfuPQ2zzQcaEFbx8wA8rck= sourcegraph.com/sqs/pbtypes v0.0.0-20180604144634-d3ebe8f20ae4/go.mod h1:ketZ/q3QxT9HOBeFhu6RdvsftgpsbFHBF5Cas6cDKZ0=