From ff14220e08da9b2ed38231ec589e15b6f6c45dd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sun, 30 Jul 2023 20:26:09 +0800 Subject: [PATCH] platform: Update profile binary format --- experimental/libbox/command_server.go | 2 +- experimental/libbox/profile_import.go | 60 ++++++++++++++++++--------- test/go.mod | 22 +++++----- test/go.sum | 11 +++++ 4 files changed, 64 insertions(+), 31 deletions(-) diff --git a/experimental/libbox/command_server.go b/experimental/libbox/command_server.go index dabb6f87..1aeb12af 100644 --- a/experimental/libbox/command_server.go +++ b/experimental/libbox/command_server.go @@ -83,7 +83,7 @@ func (s *CommandServer) listenUNIX() error { Net: "unix", }) if err != nil { - return E.Cause(err, "listen") + return E.Cause(err, "listen ", sockPath) } if sUserID > 0 { err = os.Chown(sockPath, sUserID, sGroupID) diff --git a/experimental/libbox/profile_import.go b/experimental/libbox/profile_import.go index 7b2b7cb6..cbe72907 100644 --- a/experimental/libbox/profile_import.go +++ b/experimental/libbox/profile_import.go @@ -2,7 +2,9 @@ package libbox import ( "bytes" + "compress/gzip" "encoding/binary" + "io" E "github.com/sagernet/sing/common/exceptions" "github.com/sagernet/sing/common/rw" @@ -170,19 +172,25 @@ type ProfileContent struct { } func (c *ProfileContent) Encode() []byte { - var buffer bytes.Buffer + buffer := new(bytes.Buffer) buffer.WriteByte(MessageTypeProfileContent) - rw.WriteVString(&buffer, c.Name) - binary.Write(&buffer, binary.BigEndian, c.Type) - rw.WriteVString(&buffer, c.Config) - rw.WriteVString(&buffer, c.RemotePath) - binary.Write(&buffer, binary.BigEndian, c.AutoUpdate) - binary.Write(&buffer, binary.BigEndian, c.LastUpdated) + buffer.WriteByte(0) + writer := gzip.NewWriter(buffer) + rw.WriteVString(writer, c.Name) + binary.Write(writer, binary.BigEndian, c.Type) + rw.WriteVString(writer, c.Config) + if c.Type != ProfileTypeLocal { + rw.WriteVString(writer, c.RemotePath) + binary.Write(writer, binary.BigEndian, c.AutoUpdate) + binary.Write(writer, binary.BigEndian, c.LastUpdated) + } + writer.Flush() + writer.Close() return buffer.Bytes() } func DecodeProfileContent(data []byte) (*ProfileContent, error) { - reader := bytes.NewReader(data) + var reader io.Reader = bytes.NewReader(data) messageType, err := rw.ReadByte(reader) if err != nil { return nil, err @@ -190,6 +198,18 @@ func DecodeProfileContent(data []byte) (*ProfileContent, error) { if messageType != MessageTypeProfileContent { return nil, E.New("invalid message") } + version, err := rw.ReadByte(reader) + if err != nil { + return nil, err + } + if version == 0 { + reader, err = gzip.NewReader(reader) + if err != nil { + return nil, E.Cause(err, "unsupported profile") + } + } else { + return nil, E.Cause(err, "unsupported profile") + } var content ProfileContent content.Name, err = rw.ReadVString(reader) if err != nil { @@ -203,17 +223,19 @@ func DecodeProfileContent(data []byte) (*ProfileContent, error) { if err != nil { return nil, err } - content.RemotePath, err = rw.ReadVString(reader) - if err != nil { - return nil, err - } - err = binary.Read(reader, binary.BigEndian, &content.AutoUpdate) - if err != nil { - return nil, err - } - err = binary.Read(reader, binary.BigEndian, &content.LastUpdated) - if err != nil { - return nil, err + if content.Type != ProfileTypeLocal { + content.RemotePath, err = rw.ReadVString(reader) + if err != nil { + return nil, err + } + err = binary.Read(reader, binary.BigEndian, &content.AutoUpdate) + if err != nil { + return nil, err + } + err = binary.Read(reader, binary.BigEndian, &content.LastUpdated) + if err != nil { + return nil, err + } } return &content, nil } diff --git a/test/go.mod b/test/go.mod index 81d1cf02..6144a3ad 100644 --- a/test/go.mod +++ b/test/go.mod @@ -10,9 +10,9 @@ require ( github.com/docker/docker v20.10.18+incompatible github.com/docker/go-connections v0.4.0 github.com/gofrs/uuid/v5 v5.0.0 - github.com/sagernet/sing v0.2.8-0.20230707055657-7c9b4d624da7 - github.com/sagernet/sing-shadowsocks v0.2.3-0.20230703131347-b044960bd355 - github.com/sagernet/sing-shadowsocks2 v0.1.2-0.20230703131506-ca0c6adde968 + github.com/sagernet/sing v0.2.9 + github.com/sagernet/sing-shadowsocks v0.2.4 + github.com/sagernet/sing-shadowsocks2 v0.1.3 github.com/spyzhov/ajson v0.7.1 github.com/stretchr/testify v1.8.4 go.uber.org/goleak v1.2.0 @@ -26,7 +26,7 @@ require ( github.com/Microsoft/go-winio v0.6.0 // indirect github.com/ajg/form v1.5.1 // indirect github.com/andybalholm/brotli v1.0.5 // indirect - github.com/caddyserver/certmagic v0.18.2 // indirect + github.com/caddyserver/certmagic v0.19.0 // indirect github.com/cloudflare/circl v1.2.1-0.20221019164342-6ab4dfed8f3c // indirect github.com/cretz/bine v0.2.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect @@ -34,9 +34,9 @@ require ( github.com/docker/go-units v0.4.0 // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect - github.com/go-chi/chi/v5 v5.0.8 // indirect + github.com/go-chi/chi/v5 v5.0.10 // indirect github.com/go-chi/cors v1.2.1 // indirect - github.com/go-chi/render v1.0.2 // indirect + github.com/go-chi/render v1.0.3 // indirect github.com/go-ole/go-ole v1.2.6 // indirect github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect github.com/gogo/protobuf v1.3.2 // indirect @@ -73,11 +73,11 @@ require ( github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97 // indirect github.com/sagernet/quic-go v0.0.0-20230615020047-10f05c797c02 // indirect github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691 // indirect - github.com/sagernet/sing-dns v0.1.7-0.20230703131656-fd65b6178bf9 // indirect - github.com/sagernet/sing-mux v0.1.1-0.20230703132253-2cedde0fbc90 // indirect - github.com/sagernet/sing-shadowtls v0.1.3-0.20230703132509-93bbad3057e4 // indirect - github.com/sagernet/sing-tun v0.1.9-0.20230703134424-fd850d00e5cd // indirect - github.com/sagernet/sing-vmess v0.1.7-0.20230711074224-7d2a9a318162 // indirect + github.com/sagernet/sing-dns v0.1.8 // indirect + github.com/sagernet/sing-mux v0.1.2 // indirect + github.com/sagernet/sing-shadowtls v0.1.4 // indirect + github.com/sagernet/sing-tun v0.1.11 // indirect + github.com/sagernet/sing-vmess v0.1.7 // indirect github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37 // indirect github.com/sagernet/tfo-go v0.0.0-20230303015439-ffcfd8c41cf9 // indirect github.com/sagernet/utls v0.0.0-20230309024959-6732c2ab36f2 // indirect diff --git a/test/go.sum b/test/go.sum index 98ee8193..63328a2a 100644 --- a/test/go.sum +++ b/test/go.sum @@ -14,6 +14,7 @@ github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHG github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/caddyserver/certmagic v0.18.2 h1:Nj2+M+A2Ho9IF6n1wUSbra4mX1X6ALzWpul9HooprHA= github.com/caddyserver/certmagic v0.18.2/go.mod h1:cLsgYXecH1iVUPjDXw15/1SKjZk/TK+aFfQk5FnugGQ= +github.com/caddyserver/certmagic v0.19.0/go.mod h1:fsL01NomQ6N+kE2j37ZCnig2MFosG+MIO4ztnmG/zz8= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= @@ -39,10 +40,12 @@ github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4 github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0= github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4= github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58= github.com/go-chi/render v1.0.2 h1:4ER/udB0+fMWB2Jlf15RV3F4A2FDuYi/9f+lFttR/Lg= github.com/go-chi/render v1.0.2/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0= +github.com/go-chi/render v1.0.3/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0= github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I= @@ -132,20 +135,28 @@ github.com/sagernet/sing v0.0.0-20220817130738-ce854cda8522/go.mod h1:QVsS5L/ZA2 github.com/sagernet/sing v0.1.8/go.mod h1:jt1w2u7lJQFFSGLiRrRIs5YWmx4kAPfWuOejuDW9qMk= github.com/sagernet/sing v0.2.8-0.20230707055657-7c9b4d624da7 h1:sKQu4Gc6vMfzleoFK3h8ROfn/TP2U36qQME3/iYaoqA= github.com/sagernet/sing v0.2.8-0.20230707055657-7c9b4d624da7/go.mod h1:Ta8nHnDLAwqySzKhGoKk4ZIB+vJ3GTKj7UPrWYvM+4w= +github.com/sagernet/sing v0.2.9/go.mod h1:Ta8nHnDLAwqySzKhGoKk4ZIB+vJ3GTKj7UPrWYvM+4w= github.com/sagernet/sing-dns v0.1.7-0.20230703131656-fd65b6178bf9 h1:35qe74ygIKj5uQkDDD0Xtv+iWOspQsS/Lqhs2XiY0Ak= github.com/sagernet/sing-dns v0.1.7-0.20230703131656-fd65b6178bf9/go.mod h1:wgmbh0yruJXRO8tmfwPx6hOl6pyReWRoeHdkRehWkmw= +github.com/sagernet/sing-dns v0.1.8/go.mod h1:lHv8WMl9GKfMV8Wt1AJTtjVTF/h5/owpGY2YutYZF6g= github.com/sagernet/sing-mux v0.1.1-0.20230703132253-2cedde0fbc90 h1:aEe2HrRc9OTS7IZ8RHyh224OhltnwRQs4/y89UsHPo8= github.com/sagernet/sing-mux v0.1.1-0.20230703132253-2cedde0fbc90/go.mod h1:sm126rB5EUi9HLf4jCSHTqo+XRPbh4BoEVeLbr2WRbE= +github.com/sagernet/sing-mux v0.1.2/go.mod h1:r2V8AlOzXaRCHXK7fILCUGzuI2iILweTaG8C5xlpHxo= github.com/sagernet/sing-shadowsocks v0.2.3-0.20230703131347-b044960bd355 h1:XOgNZYnkDrx5qtNS4kqIOHMhjZuc7mJ2pY/x3EyZX8Q= github.com/sagernet/sing-shadowsocks v0.2.3-0.20230703131347-b044960bd355/go.mod h1:atEATsxqPo8qCPcFt8Rw7TFEJ70egCoMR7PziX4jmjI= +github.com/sagernet/sing-shadowsocks v0.2.4/go.mod h1:80fNKP0wnqlu85GZXV1H1vDPC/2t+dQbFggOw4XuFUM= github.com/sagernet/sing-shadowsocks2 v0.1.2-0.20230703131506-ca0c6adde968 h1:UctXygnZfqsFR+2hZXfpWK3pSYKLbBQMuli9GDE6QU0= github.com/sagernet/sing-shadowsocks2 v0.1.2-0.20230703131506-ca0c6adde968/go.mod h1:xFxUGbtnqRLxtQftCILFeKf43GE6S83f0I6CsO9BxGE= +github.com/sagernet/sing-shadowsocks2 v0.1.3/go.mod h1:DOhJc/cLeqRv0wuePrQso+iUmDxOnWF4eT/oMcRzYFw= github.com/sagernet/sing-shadowtls v0.1.3-0.20230703132509-93bbad3057e4 h1:ZjLyCkEENqXzGp4PRZbQGk5wPzEq0Rg+/2jK82lmy3Q= github.com/sagernet/sing-shadowtls v0.1.3-0.20230703132509-93bbad3057e4/go.mod h1:8ZSSHJSNOG7cUCUYJemZNH873EsKdFqABykTypoS/2M= +github.com/sagernet/sing-shadowtls v0.1.4/go.mod h1:F8NBgsY5YN2beQavdgdm1DPlhaKQlaL6lpDdcBglGK4= github.com/sagernet/sing-tun v0.1.9-0.20230703134424-fd850d00e5cd h1:lkJA/P1L2XE5lNDnzA2fygx6DZIks3Sx87GN2OE0jNY= github.com/sagernet/sing-tun v0.1.9-0.20230703134424-fd850d00e5cd/go.mod h1:XNQoXtvsmeva+dADmo/57KktLNgm5ubOyR67Niahqj8= +github.com/sagernet/sing-tun v0.1.11/go.mod h1:XsyIVKd/Qp+2SdLZWGbavHtcpE7J7XU3S1zJmcoj9Ck= github.com/sagernet/sing-vmess v0.1.7-0.20230711074224-7d2a9a318162 h1:EvEbqAdZJalFqLwKSHIixH+TRed0iY2f8KatI7mtt1Q= github.com/sagernet/sing-vmess v0.1.7-0.20230711074224-7d2a9a318162/go.mod h1:FHEBboQRvfBDbZd/fAKBpgR4w7QbYyzNL/MLd22/iTk= +github.com/sagernet/sing-vmess v0.1.7/go.mod h1:1qkC1L1T2sxnS/NuO6HU72S8TkltV+EXoKGR29m/Yss= github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37 h1:HuE6xSwco/Xed8ajZ+coeYLmioq0Qp1/Z2zczFaV8as= github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37/go.mod h1:3skNSftZDJWTGVtVaM2jfbce8qHnmH/AGDRe62iNOg0= github.com/sagernet/tfo-go v0.0.0-20230303015439-ffcfd8c41cf9 h1:2ItpW1nMNkPzmBTxV0/eClCklHrFSQMnUGcpUmJxVeE=