From e928ef59c1aaa5b7db01a3a01f2c97730f0d0d16 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Fri, 5 Jun 2026 12:07:23 +0800 Subject: [PATCH] feat: add `path-in-bundle` to rule-providers to support extract from BundleMRS.7z when the local file does not exist --- adapter/provider/provider.go | 2 +- component/resource/fetcher.go | 48 +++++++++++++++++++++++++++++------ config/config.go | 3 ++- constant/path.go | 26 ++++++++++++++++--- docs/config.yaml | 1 + go.mod | 10 ++++++-- go.sum | 27 +++++++++++++++++--- rules/bundle/bundle.go | 46 +++++++++++++++++++++++++++++++++ rules/provider/parse.go | 25 +++++++++--------- rules/provider/provider.go | 4 +-- 10 files changed, 160 insertions(+), 32 deletions(-) create mode 100644 rules/bundle/bundle.go diff --git a/adapter/provider/provider.go b/adapter/provider/provider.go index dc3f8878..fa7d56bc 100644 --- a/adapter/provider/provider.go +++ b/adapter/provider/provider.go @@ -205,7 +205,7 @@ func NewProxySetProvider(name string, interval time.Duration, payload []map[stri hc.setProxies(proxies) } - fetcher := resource.NewFetcher[[]C.Proxy](name, interval, vehicle, parser, pd.setProxies) + fetcher := resource.NewFetcher[[]C.Proxy](name, interval, vehicle, nil, parser, pd.setProxies) pd.Fetcher = fetcher if httpVehicle, ok := vehicle.(*resource.HTTPVehicle); ok { httpVehicle.SetInRead(func(resp *http.Response) { diff --git a/component/resource/fetcher.go b/component/resource/fetcher.go index a48e7bc8..464af2e7 100644 --- a/component/resource/fetcher.go +++ b/component/resource/fetcher.go @@ -2,6 +2,8 @@ package resource import ( "context" + "io" + "io/fs" "os" "sync" "time" @@ -16,6 +18,7 @@ import ( ) type Parser[V any] func([]byte) (V, error) +type BundleFile func() (fs.File, error) type Fetcher[V any] struct { ctx context.Context @@ -23,6 +26,7 @@ type Fetcher[V any] struct { resourceType string name string vehicle P.Vehicle + bundleFile BundleFile updatedAt time.Time hash utils.HashType parser Parser[V] @@ -66,6 +70,33 @@ func (f *Fetcher[V]) Initial() (V, error) { } } + // parse local file error, fallback to bundle file + if f.bundleFile != nil { + // bundle file exists, use it first + if file, fErr := f.bundleFile(); fErr == nil { + defer file.Close() + buf, err := io.ReadAll(file) + var modTime time.Time + if stat, sErr := file.Stat(); sErr == nil { + modTime = stat.ModTime() + } + contents, _, err := f.loadBuf(buf, utils.MakeHash(buf), true) + f.updatedAt = modTime // reset updatedAt to file's modTime + + if err == nil { + log.Infoln("[Provider] %s extract successful from bundle file", f.Name()) + err = f.startPullLoop(time.Since(modTime) > f.interval) + if err != nil { + return lo.Empty[V](), err + } + return contents, nil + } + log.Warnln("[Provider] %s read bundle file error: %s", f.Name(), err.Error()) + } else { + log.Warnln("[Provider] %s read bundle file error: %s", f.Name(), fErr.Error()) + } + } + // parse local file error, fallback to remote contents, _, updateErr := f.Update() @@ -218,20 +249,21 @@ func (f *Fetcher[V]) updateWithLog() { return } -func NewFetcher[V any](name string, interval time.Duration, vehicle P.Vehicle, parser Parser[V], onUpdate func(V)) *Fetcher[V] { +func NewFetcher[V any](name string, interval time.Duration, vehicle P.Vehicle, bundleFile BundleFile, parser Parser[V], onUpdate func(V)) *Fetcher[V] { ctx, cancel := context.WithCancel(context.Background()) minBackoff := 10 * time.Second if interval < minBackoff { minBackoff = interval } return &Fetcher[V]{ - ctx: ctx, - ctxCancel: cancel, - name: name, - vehicle: vehicle, - parser: parser, - onUpdate: onUpdate, - interval: interval, + ctx: ctx, + ctxCancel: cancel, + name: name, + bundleFile: bundleFile, + vehicle: vehicle, + parser: parser, + onUpdate: onUpdate, + interval: interval, backoff: slowdown.Backoff{ Factor: 2, Jitter: false, diff --git a/config/config.go b/config/config.go index 354f064c..9cb1acf8 100644 --- a/config/config.go +++ b/config/config.go @@ -35,6 +35,7 @@ import ( LC "github.com/metacubex/mihomo/listener/config" "github.com/metacubex/mihomo/log" R "github.com/metacubex/mihomo/rules" + RB "github.com/metacubex/mihomo/rules/bundle" RC "github.com/metacubex/mihomo/rules/common" RP "github.com/metacubex/mihomo/rules/provider" RW "github.com/metacubex/mihomo/rules/wrapper" @@ -1003,7 +1004,7 @@ func parseRuleProviders(cfg *RawConfig) (ruleProviders map[string]P.RuleProvider ruleProviders = map[string]P.RuleProvider{} // parse rule provider for name, mapping := range cfg.RuleProvider { - rp, err := RP.ParseRuleProvider(name, mapping, R.ParseRule) + rp, err := RP.ParseRuleProvider(name, mapping, R.ParseRule, RB.MakeBundleFile) if err != nil { return nil, err } diff --git a/constant/path.go b/constant/path.go index a7f180b5..f2463a4a 100644 --- a/constant/path.go +++ b/constant/path.go @@ -15,9 +15,10 @@ import ( const Name = "mihomo" var ( - GeositeName = "GeoSite.dat" - GeoipName = "GeoIP.dat" - ASNName = "ASN.mmdb" + GeositeName = "GeoSite.dat" + GeoipName = "GeoIP.dat" + ASNName = "ASN.mmdb" + BundleMRSName = "BundleMRS.7z" ) // Path is used to get the configuration path @@ -162,6 +163,25 @@ func (p *path) ASN() string { return P.Join(p.homeDir, ASNName) } +func (p *path) BundleMRS() string { + files, err := os.ReadDir(p.homeDir) + if err != nil { + return "" + } + for _, fi := range files { + if fi.IsDir() { + // 目录则直接跳过 + continue + } else { + if strings.EqualFold(fi.Name(), "BundleMRS.7z") { + BundleMRSName = fi.Name() + return P.Join(p.homeDir, fi.Name()) + } + } + } + return P.Join(p.homeDir, BundleMRSName) +} + func (p *path) OldCache() string { return P.Join(p.homeDir, ".cache") } diff --git a/docs/config.yaml b/docs/config.yaml index afa5b36a..d5fe478f 100644 --- a/docs/config.yaml +++ b/docs/config.yaml @@ -1570,6 +1570,7 @@ rule-providers: format: mrs behavior: domain path: /path/to/save/file.mrs + # path-in-bundle: "geo/geosite/cn.mrs" # 当设置该选项时,如果本地文件不存在,会优先从 Home Dir的 BundleMRS.7z 中解压该文件,此项的值用于指定在 BundleMRS.7z 中的路径 rule4: type: inline behavior: domain # classical / ipcidr diff --git a/go.mod b/go.mod index b1dc6474..7f9604e2 100644 --- a/go.mod +++ b/go.mod @@ -29,6 +29,7 @@ require ( github.com/metacubex/quic-go v0.59.1-0.20260520020949-fcd18c7b6ace github.com/metacubex/randv2 v0.2.0 github.com/metacubex/restls-client-go v0.1.7 + github.com/metacubex/sevenzip v1.6.4 github.com/metacubex/sing v0.5.7 github.com/metacubex/sing-mux v0.3.9 github.com/metacubex/sing-quic v0.0.0-20260527143057-68e10a6afdc3 @@ -80,7 +81,9 @@ require ( github.com/ajg/form v1.5.1 // indirect github.com/akutz/memconn v0.1.0 // indirect github.com/alexbrainman/sspi v0.0.0-20231016080023-1a75b4708caa // indirect - github.com/andybalholm/brotli v1.0.6 // indirect + github.com/andybalholm/brotli v1.1.1 // indirect + github.com/bodgit/plumbing v1.3.0 // indirect + github.com/bodgit/windows v1.0.1 // indirect github.com/coder/websocket v1.8.12 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/dunglas/httpsfv v1.0.2 // indirect @@ -99,6 +102,7 @@ require ( github.com/google/btree v1.1.3 // indirect github.com/google/go-cmp v0.6.0 // indirect github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 // indirect + github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect github.com/huin/goupnp v1.3.0 // indirect github.com/josharian/native v1.1.0 // indirect github.com/jsimonetti/rtnetlink v1.4.0 // indirect @@ -117,7 +121,7 @@ require ( github.com/metacubex/yamux v0.0.0-20250918083631-dd5f17c0be49 // indirect github.com/mitchellh/go-ps v1.0.0 // indirect github.com/oasisprotocol/deoxysii v0.0.0-20220228165953-2091330c22b7 // indirect - github.com/pierrec/lz4/v4 v4.1.14 // indirect + github.com/pierrec/lz4/v4 v4.1.27 // indirect github.com/pires/go-proxyproto v0.8.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/safchain/ethtool v0.3.0 // indirect @@ -125,11 +129,13 @@ require ( github.com/sina-ghaderi/poly1305 v0.0.0-20220724002748-c5926b03988b // indirect github.com/sina-ghaderi/rabaead v0.0.0-20220730151906-ab6e06b96e8c // indirect github.com/sina-ghaderi/rabbitio v0.0.0-20220730151941-9ce26f4f872e // indirect + github.com/stangelandcl/ppmd v0.1.1 // indirect github.com/tailscale/certstore v0.1.1-0.20260409135935-3638fb84b77d // indirect github.com/tailscale/go-winio v0.0.0-20231025203758-c4f33415bf55 // indirect github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a // indirect github.com/tailscale/peercred v0.0.0-20250107143737-35a0c7bd7edc // indirect github.com/u-root/uio v0.0.0-20230220225925-ffce2a382923 // indirect + github.com/ulikunitz/xz v0.5.15 // indirect github.com/vishvananda/netns v0.0.5 // indirect github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect github.com/x448/float16 v0.8.4 // indirect diff --git a/go.sum b/go.sum index de20d6de..2939c689 100644 --- a/go.sum +++ b/go.sum @@ -9,11 +9,15 @@ github.com/akutz/memconn v0.1.0 h1:NawI0TORU4hcOMsMr11g7vwlCdkYeLKXBcxWu2W/P8A= github.com/akutz/memconn v0.1.0/go.mod h1:Jo8rI7m0NieZyLI5e2CDlRdRqRRB4S7Xp77ukDjH+Fw= github.com/alexbrainman/sspi v0.0.0-20231016080023-1a75b4708caa h1:LHTHcTQiSGT7VVbI0o4wBRNQIgn917usHWOd6VAffYI= github.com/alexbrainman/sspi v0.0.0-20231016080023-1a75b4708caa/go.mod h1:cEWa1LVoE5KvSD9ONXsZrj0z6KqySlCCNKHlLzbqAt4= -github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI= -github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA= +github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA= github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk= github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg= github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk= +github.com/bodgit/plumbing v1.3.0 h1:pf9Itz1JOQgn7vEOE7v7nlEfBykYqvUYioC61TwWCFU= +github.com/bodgit/plumbing v1.3.0/go.mod h1:JOTb4XiRu5xfnmdnDJo6GmSbSbtSyufrsyZFByMtKEs= +github.com/bodgit/windows v1.0.1 h1:tF7K6KOluPYygXa3Z2594zxlkbKPAOvqr97etrGNIz4= +github.com/bodgit/windows v1.0.1/go.mod h1:a6JLwrB4KrTR5hBpp8FI9/9W9jJfeQ2h4XDXU74ZCdM= github.com/cilium/ebpf v0.12.3 h1:8ht6F9MquybnY97at+VDZb3eQQr8ev79RueWeVaEcG4= github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NAo= github.com/coder/websocket v1.8.12/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs= @@ -69,6 +73,8 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 h1:FKHo8hFI3A+7w0aUQuYXQ+6EN5stWmeY/AZqtM8xk9k= github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8/go.mod h1:K1liHPHnj73Fdn/EKuT8nrFqBihUSKXoLYU0BuatOYo= github.com/google/tink/go v1.6.1 h1:t7JHqO8Ath2w2ig5vjwQYJzhGEZymedQc90lQXUBa4I= +github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= +github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= github.com/huin/goupnp v1.3.0 h1:UvLUlWDNpoUdYzb2TCn+MuTWtcjXKSza2n6CBdQ0xXc= github.com/huin/goupnp v1.3.0/go.mod h1:gnGPsThkYa7bFi/KWmEysQRf48l2dvR5bxr2OFckNX8= github.com/insomniacslk/dhcp v0.0.0-20250109001534-8abf58130905 h1:q3OEI9RaN/wwcx+qgGo6ZaoJkCiDYe/gjDLfq7lQQF4= @@ -145,6 +151,8 @@ github.com/metacubex/randv2 v0.2.0 h1:uP38uBvV2SxYfLj53kuvAjbND4RUDfFJjwr4UigMiL github.com/metacubex/randv2 v0.2.0/go.mod h1:kFi2SzrQ5WuneuoLLCMkABtiBu6VRrMrWFqSPyj2cxY= github.com/metacubex/restls-client-go v0.1.7 h1:eCwiXCTQb5WJu9IlgYvDBA1OgrINv58dEe7hcN5H15k= github.com/metacubex/restls-client-go v0.1.7/go.mod h1:BN/U52vPw7j8VTSh2vleD/MnmVKCov84mS5VcjVHH4g= +github.com/metacubex/sevenzip v1.6.4 h1:OIL+DeOeSAbKNsjqxcYUMiarRmX6Kaxakb0GT7E9Oik= +github.com/metacubex/sevenzip v1.6.4/go.mod h1:FP3X9bzFKj9wPxifGN9B3w2fIEicMjzKYIGIhnu+1pw= github.com/metacubex/sing v0.5.7 h1:8OC+fhKFSv/l9ehEhJRaZZAOuthfZo68SteBVLe8QqM= github.com/metacubex/sing v0.5.7/go.mod h1:ypf0mjwlZm0sKdQSY+yQvmsbWa0hNPtkeqyRMGgoN+w= github.com/metacubex/sing-mux v0.3.9 h1:/aoBD2+sK2qsXDlNDe3hkR0GZuFDtwIZhOeGUx9W0Yk= @@ -196,8 +204,9 @@ github.com/openacid/must v0.1.3/go.mod h1:luPiXCuJlEo3UUFQngVQokV0MPGryeYvtCbQPs github.com/openacid/testkeys v0.1.6/go.mod h1:MfA7cACzBpbiwekivj8StqX0WIRmqlMsci1c37CA3Do= github.com/oschwald/maxminddb-golang v1.12.0 h1:9FnTOD0YOhP7DGxGsq4glzpGy5+w7pq50AS6wALUMYs= github.com/oschwald/maxminddb-golang v1.12.0/go.mod h1:q0Nob5lTCqyQ8WT6FYgS1L7PXKVVbgiymefNwIjPzgY= -github.com/pierrec/lz4/v4 v4.1.14 h1:+fL8AQEZtz/ijeNnpduH0bROTu0O3NZAlPjQxGn8LwE= github.com/pierrec/lz4/v4 v4.1.14/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= +github.com/pierrec/lz4/v4 v4.1.27 h1:+PhzhWDrjRj89TH2sw43nE3+4+W8lSxIuQadEHZyjUk= +github.com/pierrec/lz4/v4 v4.1.27/go.mod h1:EoQMVJgeeEOMsCqCzqFm2O0cJvljX2nGZjcRIPL34O4= github.com/pires/go-proxyproto v0.8.0 h1:5unRmEAPbHXHuLjDg01CxJWf91cw3lKHc/0xzKpXEe0= github.com/pires/go-proxyproto v0.8.0/go.mod h1:iknsfgnH8EkjrMeMyvfKByp9TiBZCKZM0jx2xmKqnVY= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -220,10 +229,18 @@ github.com/sina-ghaderi/rabbitio v0.0.0-20220730151941-9ce26f4f872e h1:ur8uMsPIF github.com/sina-ghaderi/rabbitio v0.0.0-20220730151941-9ce26f4f872e/go.mod h1:+e5fBW3bpPyo+3uLo513gIUblc03egGjMM0+5GKbzK8= github.com/sirupsen/logrus v1.9.4 h1:TsZE7l11zFCLZnZ+teH4Umoq5BhEIfIzfRDZ1Uzql2w= github.com/sirupsen/logrus v1.9.4/go.mod h1:ftWc9WdOfJ0a92nsE2jF5u5ZwH8Bv2zdeOC42RjbV2g= +github.com/stangelandcl/ppmd v0.1.1 h1:c25QazhlWUn5nmR1QOzafKhQxBicAr7GGCKER2aJ8H8= +github.com/stangelandcl/ppmd v0.1.1/go.mod h1:Rrv7M+/2P5jYr/GMLhBl7Ug3uJ1bUiVzr5LbbaV6xgY= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= github.com/tailscale/certstore v0.1.1-0.20260409135935-3638fb84b77d h1:JcGKBZAL7ePLwOhUdN8qGQZlP5GueEiIZwY7R62pejE= @@ -236,6 +253,8 @@ github.com/tailscale/peercred v0.0.0-20250107143737-35a0c7bd7edc h1:24heQPtnFR+y github.com/tailscale/peercred v0.0.0-20250107143737-35a0c7bd7edc/go.mod h1:f93CXfllFsO9ZQVq+Zocb1Gp4G5Fz0b0rXHLOzt/Djc= github.com/u-root/uio v0.0.0-20230220225925-ffce2a382923 h1:tHNk7XK9GkmKUR6Gh8gVBKXc2MVSZ4G/NnWLtzw4gNA= github.com/u-root/uio v0.0.0-20230220225925-ffce2a382923/go.mod h1:eLL9Nub3yfAho7qB0MzZizFhTU2QkLeoVsWdHtDW264= +github.com/ulikunitz/xz v0.5.15 h1:9DNdB5s+SgV3bQ2ApL10xRc35ck0DuIX/isZvIk+ubY= +github.com/ulikunitz/xz v0.5.15/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE= github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU= github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0= @@ -248,6 +267,8 @@ github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= github.com/xtaci/lossyconn v0.0.0-20190602105132-8df528c0c9ae h1:J0GxkO96kL4WF+AIT3M4mfUVinOCPgf2uUWYFUzN0sM= +github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU= +github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E= github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4= github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4= gitlab.com/go-extension/aes-ccm v0.0.0-20230221065045-e58665ef23c7 h1:UNrDfkQqiEYzdMlNsVvBYOAJWZjdktqFE9tQh5BT2+4= diff --git a/rules/bundle/bundle.go b/rules/bundle/bundle.go new file mode 100644 index 00000000..a956ecde --- /dev/null +++ b/rules/bundle/bundle.go @@ -0,0 +1,46 @@ +package bundle + +import ( + "errors" + "fmt" + "io" + "io/fs" + "os" + + "github.com/metacubex/mihomo/component/resource" + C "github.com/metacubex/mihomo/constant" + + "github.com/metacubex/sevenzip" +) + +func MakeBundleFile(path string) resource.BundleFile { + if path == "" { + return nil + } + return func() (fs.File, error) { + if _, err := os.Stat(C.Path.BundleMRS()); os.IsNotExist(err) { + return nil, fmt.Errorf("bundle file not exist: %s", C.Path.BundleMRS()) + } + r, err := sevenzip.OpenReader(C.Path.BundleMRS()) + if err != nil { + return nil, fmt.Errorf("open bundle file error: %w", err) + } + f, err := r.Open(path) + if err != nil { + _ = r.Close() + return nil, fmt.Errorf("open path in bundle file error: %w", err) + } + return file{f, r}, nil + } +} + +type file struct { + fs.File + closer io.Closer +} + +func (f file) Close() error { + err1 := f.File.Close() + err2 := f.closer.Close() + return errors.Join(err1, err2) +} diff --git a/rules/provider/parse.go b/rules/provider/parse.go index 8f951612..3ddfab8d 100644 --- a/rules/provider/parse.go +++ b/rules/provider/parse.go @@ -12,19 +12,20 @@ import ( ) type ruleProviderSchema struct { - Type string `provider:"type"` - Behavior string `provider:"behavior"` - Path string `provider:"path,omitempty"` - URL string `provider:"url,omitempty"` - Proxy string `provider:"proxy,omitempty"` - Format string `provider:"format,omitempty"` - Interval int `provider:"interval,omitempty"` - SizeLimit int64 `provider:"size-limit,omitempty"` - Payload []string `provider:"payload,omitempty"` - Header map[string][]string `provider:"header,omitempty"` + Type string `provider:"type"` + Behavior string `provider:"behavior"` + Path string `provider:"path,omitempty"` + URL string `provider:"url,omitempty"` + Proxy string `provider:"proxy,omitempty"` + Format string `provider:"format,omitempty"` + Interval int `provider:"interval,omitempty"` + SizeLimit int64 `provider:"size-limit,omitempty"` + Payload []string `provider:"payload,omitempty"` + Header map[string][]string `provider:"header,omitempty"` + PathInBundle string `provider:"path-in-bundle,omitempty"` } -func ParseRuleProvider(name string, mapping map[string]any, parse common.ParseRuleFunc) (P.RuleProvider, error) { +func ParseRuleProvider(name string, mapping map[string]any, parse common.ParseRuleFunc, makeBundleFile func(pathInBundle string) resource.BundleFile) (P.RuleProvider, error) { schema := &ruleProviderSchema{} decoder := structure.NewDecoder(structure.Option{TagName: "provider", WeaklyTypedInput: true}) if err := decoder.Decode(mapping, schema); err != nil { @@ -64,5 +65,5 @@ func ParseRuleProvider(name string, mapping map[string]any, parse common.ParseRu interval := time.Duration(uint(schema.Interval)) * time.Second - return NewRuleSetProvider(name, behavior, format, interval, vehicle, schema.Payload, parse), nil + return NewRuleSetProvider(name, behavior, format, interval, vehicle, schema.Payload, makeBundleFile(schema.PathInBundle), parse), nil } diff --git a/rules/provider/provider.go b/rules/provider/provider.go index 0534a838..2e7829ae 100644 --- a/rules/provider/provider.go +++ b/rules/provider/provider.go @@ -122,7 +122,7 @@ func (rp *RuleSetProvider) Close() error { return rp.ruleSetProvider.Close() } -func NewRuleSetProvider(name string, behavior P.RuleBehavior, format P.RuleFormat, interval time.Duration, vehicle P.Vehicle, payload []string, parse common.ParseRuleFunc) P.RuleProvider { +func NewRuleSetProvider(name string, behavior P.RuleBehavior, format P.RuleFormat, interval time.Duration, vehicle P.Vehicle, payload []string, bundleFile resource.BundleFile, parse common.ParseRuleFunc) P.RuleProvider { rp := &ruleSetProvider{ baseProvider: baseProvider{ behavior: behavior, @@ -139,7 +139,7 @@ func NewRuleSetProvider(name string, behavior P.RuleBehavior, format P.RuleForma if len(payload) > 0 { // using as fallback rules rp.strategy = rulesParseInline(payload, rp.strategy) } - rp.Fetcher = resource.NewFetcher(name, interval, vehicle, func(bytes []byte) (ruleStrategy, error) { + rp.Fetcher = resource.NewFetcher(name, interval, vehicle, bundleFile, func(bytes []byte) (ruleStrategy, error) { return rulesParse(bytes, newStrategy(behavior, parse), format) }, onUpdate)