From fbead56ec97ae93f904f4476df1741af718c9c2a Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Wed, 27 Nov 2024 09:28:38 +0800 Subject: [PATCH] feat: add `size-limit` for provider https://github.com/MetaCubeX/mihomo/issues/1645 --- adapter/provider/parser.go | 3 ++- component/resource/vehicle.go | 32 +++++++++++++++++++------------- component/updater/update_geo.go | 8 ++++---- docs/config.yaml | 2 ++ rules/provider/parse.go | 17 +++++++++-------- 5 files changed, 36 insertions(+), 26 deletions(-) diff --git a/adapter/provider/parser.go b/adapter/provider/parser.go index b0db5db3..b305df7f 100644 --- a/adapter/provider/parser.go +++ b/adapter/provider/parser.go @@ -66,6 +66,7 @@ type proxyProviderSchema struct { ExcludeFilter string `provider:"exclude-filter,omitempty"` ExcludeType string `provider:"exclude-type,omitempty"` DialerProxy string `provider:"dialer-proxy,omitempty"` + SizeLimit int64 `provider:"size-limit,omitempty"` HealthCheck healthCheckSchema `provider:"health-check,omitempty"` Override OverrideSchema `provider:"override,omitempty"` @@ -111,7 +112,7 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide return nil, fmt.Errorf("%w: %s", errSubPath, path) } } - vehicle = resource.NewHTTPVehicle(schema.URL, path, schema.Proxy, schema.Header, resource.DefaultHttpTimeout) + vehicle = resource.NewHTTPVehicle(schema.URL, path, schema.Proxy, schema.Header, resource.DefaultHttpTimeout, schema.SizeLimit) default: return nil, fmt.Errorf("%w: %s", errVehicleType, schema.Type) } diff --git a/component/resource/vehicle.go b/component/resource/vehicle.go index 7c3cb1c2..18cebf00 100644 --- a/component/resource/vehicle.go +++ b/component/resource/vehicle.go @@ -84,12 +84,13 @@ func NewFileVehicle(path string) *FileVehicle { } type HTTPVehicle struct { - url string - path string - proxy string - header http.Header - timeout time.Duration - provider types.ProxyProvider + url string + path string + proxy string + header http.Header + timeout time.Duration + sizeLimit int64 + provider types.ProxyProvider } func (h *HTTPVehicle) Url() string { @@ -151,7 +152,11 @@ func (h *HTTPVehicle) Read(ctx context.Context, oldHash utils.HashType) (buf []b err = errors.New(resp.Status) return } - buf, err = io.ReadAll(resp.Body) + var reader io.Reader = resp.Body + if h.sizeLimit > 0 { + reader = io.LimitReader(reader, h.sizeLimit) + } + buf, err = io.ReadAll(reader) if err != nil { return } @@ -166,12 +171,13 @@ func (h *HTTPVehicle) Read(ctx context.Context, oldHash utils.HashType) (buf []b return } -func NewHTTPVehicle(url string, path string, proxy string, header http.Header, timeout time.Duration) *HTTPVehicle { +func NewHTTPVehicle(url string, path string, proxy string, header http.Header, timeout time.Duration, sizeLimit int64) *HTTPVehicle { return &HTTPVehicle{ - url: url, - path: path, - proxy: proxy, - header: header, - timeout: timeout, + url: url, + path: path, + proxy: proxy, + header: header, + timeout: timeout, + sizeLimit: sizeLimit, } } diff --git a/component/updater/update_geo.go b/component/updater/update_geo.go index bba0dabd..719a5215 100644 --- a/component/updater/update_geo.go +++ b/component/updater/update_geo.go @@ -45,7 +45,7 @@ func SetGeoUpdateInterval(newGeoUpdateInterval int) { } func UpdateMMDB() (err error) { - vehicle := resource.NewHTTPVehicle(geodata.MmdbUrl(), C.Path.MMDB(), "", nil, defaultHttpTimeout) + vehicle := resource.NewHTTPVehicle(geodata.MmdbUrl(), C.Path.MMDB(), "", nil, defaultHttpTimeout, 0) var oldHash utils.HashType if buf, err := os.ReadFile(vehicle.Path()); err == nil { oldHash = utils.MakeHash(buf) @@ -76,7 +76,7 @@ func UpdateMMDB() (err error) { } func UpdateASN() (err error) { - vehicle := resource.NewHTTPVehicle(geodata.ASNUrl(), C.Path.ASN(), "", nil, defaultHttpTimeout) + vehicle := resource.NewHTTPVehicle(geodata.ASNUrl(), C.Path.ASN(), "", nil, defaultHttpTimeout, 0) var oldHash utils.HashType if buf, err := os.ReadFile(vehicle.Path()); err == nil { oldHash = utils.MakeHash(buf) @@ -109,7 +109,7 @@ func UpdateASN() (err error) { func UpdateGeoIp() (err error) { geoLoader, err := geodata.GetGeoDataLoader("standard") - vehicle := resource.NewHTTPVehicle(geodata.GeoIpUrl(), C.Path.GeoIP(), "", nil, defaultHttpTimeout) + vehicle := resource.NewHTTPVehicle(geodata.GeoIpUrl(), C.Path.GeoIP(), "", nil, defaultHttpTimeout, 0) var oldHash utils.HashType if buf, err := os.ReadFile(vehicle.Path()); err == nil { oldHash = utils.MakeHash(buf) @@ -139,7 +139,7 @@ func UpdateGeoIp() (err error) { func UpdateGeoSite() (err error) { geoLoader, err := geodata.GetGeoDataLoader("standard") - vehicle := resource.NewHTTPVehicle(geodata.GeoSiteUrl(), C.Path.GeoSite(), "", nil, defaultHttpTimeout) + vehicle := resource.NewHTTPVehicle(geodata.GeoSiteUrl(), C.Path.GeoSite(), "", nil, defaultHttpTimeout, 0) var oldHash utils.HashType if buf, err := os.ReadFile(vehicle.Path()); err == nil { oldHash = utils.MakeHash(buf) diff --git a/docs/config.yaml b/docs/config.yaml index e75e5bd5..5e83eea3 100644 --- a/docs/config.yaml +++ b/docs/config.yaml @@ -930,6 +930,7 @@ proxy-providers: interval: 3600 path: ./provider1.yaml # 默认只允许存储在 mihomo 的 Home Dir,如果想存储到任意位置,添加环境变量 SKIP_SAFE_PATH_CHECK=1 proxy: DIRECT + # size-limit: 10240 # 限制下载文件最大为10kb,默认为0即不限制文件大小 header: User-Agent: - "Clash/v1.18.0" @@ -977,6 +978,7 @@ rule-providers: type: http # http 的 path 可空置,默认储存路径为 homedir 的 rules 文件夹,文件名为 url 的 md5 url: "url" proxy: DIRECT + # size-limit: 10240 # 限制下载文件最大为10kb,默认为0即不限制文件大小 rule2: behavior: classical interval: 259200 diff --git a/rules/provider/parse.go b/rules/provider/parse.go index 3bd8f54c..b04096fb 100644 --- a/rules/provider/parse.go +++ b/rules/provider/parse.go @@ -16,13 +16,14 @@ var ( ) 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"` + 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"` } func ParseRuleProvider(name string, mapping map[string]interface{}, parse func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (parsed C.Rule, parseErr error)) (P.RuleProvider, error) { @@ -53,7 +54,7 @@ func ParseRuleProvider(name string, mapping map[string]interface{}, parse func(t return nil, fmt.Errorf("%w: %s", errSubPath, path) } } - vehicle = resource.NewHTTPVehicle(schema.URL, path, schema.Proxy, nil, resource.DefaultHttpTimeout) + vehicle = resource.NewHTTPVehicle(schema.URL, path, schema.Proxy, nil, resource.DefaultHttpTimeout, schema.SizeLimit) default: return nil, fmt.Errorf("unsupported vehicle type: %s", schema.Type) }