From 529224be2d3bffa098e90df3a559cb8b058d9757 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sat, 20 Aug 2022 00:19:13 +0800 Subject: [PATCH] Fix read android packages --- go.mod | 1 + go.sum | 2 ++ packages_android.go | 25 ++++++++++++++++++------- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index e7d678f..4bd37c3 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.18 require ( github.com/fsnotify/fsnotify v1.5.4 + github.com/sagernet/abx-go v0.0.0-20220819185957-dba1257d738e github.com/sagernet/go-tun2socks v1.16.12-0.20220818015926-16cb67876a61 github.com/sagernet/netlink v0.0.0-20220816152750-7a75378bd31a github.com/sagernet/sing v0.0.0-20220819003212-2424b1e2fac1 diff --git a/go.sum b/go.sum index 628c11d..3d31fed 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwV github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= github.com/google/btree v1.0.1 h1:gK4Kx5IaGY9CD5sPJ36FHiBJ6ZXl0kilRiiCj+jdYp4= github.com/google/btree v1.0.1/go.mod h1:xXMiIv4Fb/0kKde4SpL7qlzvu5cMJDRkFDxJfI9uaxA= +github.com/sagernet/abx-go v0.0.0-20220819185957-dba1257d738e h1:5CFRo8FJbCuf5s/eTBdZpmMbn8Fe2eSMLNAYfKanA34= +github.com/sagernet/abx-go v0.0.0-20220819185957-dba1257d738e/go.mod h1:qbt0dWObotCfcjAJJ9AxtFPNSDUfZF+6dCpgKEOBn/g= github.com/sagernet/go-tun2socks v1.16.12-0.20220818015926-16cb67876a61 h1:5+m7c6AkmAylhauulqN/c5dnh8/KssrE9c93TQrXldA= github.com/sagernet/go-tun2socks v1.16.12-0.20220818015926-16cb67876a61/go.mod h1:QUQ4RRHD6hGGHdFMEtR8T2P6GS6R3D/CXKdaYHKKXms= github.com/sagernet/netlink v0.0.0-20220816152750-7a75378bd31a h1:iNtsfGMenajBUGZ/1yAzl1v3p+t/7IJ/ilQXq9haRZ8= diff --git a/packages_android.go b/packages_android.go index 94f4fe4..2206123 100644 --- a/packages_android.go +++ b/packages_android.go @@ -1,6 +1,7 @@ package tun import ( + "bytes" "context" "encoding/xml" "io" @@ -11,6 +12,7 @@ import ( E "github.com/sagernet/sing/common/exceptions" "github.com/fsnotify/fsnotify" + "github.com/sagernet/abx-go" ) type packageManager struct { @@ -97,18 +99,27 @@ func (m *packageManager) SharedPackageByID(id uint32) (string, bool) { } func (m *packageManager) updatePackages() error { + packagesData, err := os.ReadFile("/data/system/packages.xml") + if err != nil { + return err + } + var decoder *xml.Decoder + reader, ok := abx.NewReader(bytes.NewReader(packagesData)) + if ok { + decoder = xml.NewTokenDecoder(reader) + } else { + decoder = xml.NewDecoder(bytes.NewReader(packagesData)) + } + return m.decodePackages(decoder) +} + +func (m *packageManager) decodePackages(decoder *xml.Decoder) error { idByPackage := make(map[string][]uint32) sharedByPackage := make(map[string]uint32) packageById := make(map[uint32]string) sharedById := make(map[uint32]string) - packagesData, err := os.Open("/data/system/packages.xml") - if err != nil { - return err - } - decoder := xml.NewDecoder(packagesData) - var token xml.Token for { - token, err = decoder.Token() + token, err := decoder.Token() if err == io.EOF { break } else if err != nil {