Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit fbead56

Browse files
committed
feat: add size-limit for provider
MetaCubeX/mihomo#1645
1 parent 1fff34d commit fbead56

File tree

5 files changed

+36
-26
lines changed

5 files changed

+36
-26
lines changed

adapter/provider/parser.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ type proxyProviderSchema struct {
6666
ExcludeFilter string `provider:"exclude-filter,omitempty"`
6767
ExcludeType string `provider:"exclude-type,omitempty"`
6868
DialerProxy string `provider:"dialer-proxy,omitempty"`
69+
SizeLimit int64 `provider:"size-limit,omitempty"`
6970

7071
HealthCheck healthCheckSchema `provider:"health-check,omitempty"`
7172
Override OverrideSchema `provider:"override,omitempty"`
@@ -111,7 +112,7 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide
111112
return nil, fmt.Errorf("%w: %s", errSubPath, path)
112113
}
113114
}
114-
vehicle = resource.NewHTTPVehicle(schema.URL, path, schema.Proxy, schema.Header, resource.DefaultHttpTimeout)
115+
vehicle = resource.NewHTTPVehicle(schema.URL, path, schema.Proxy, schema.Header, resource.DefaultHttpTimeout, schema.SizeLimit)
115116
default:
116117
return nil, fmt.Errorf("%w: %s", errVehicleType, schema.Type)
117118
}

component/resource/vehicle.go

+19-13
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,13 @@ func NewFileVehicle(path string) *FileVehicle {
8484
}
8585

8686
type HTTPVehicle struct {
87-
url string
88-
path string
89-
proxy string
90-
header http.Header
91-
timeout time.Duration
92-
provider types.ProxyProvider
87+
url string
88+
path string
89+
proxy string
90+
header http.Header
91+
timeout time.Duration
92+
sizeLimit int64
93+
provider types.ProxyProvider
9394
}
9495

9596
func (h *HTTPVehicle) Url() string {
@@ -151,7 +152,11 @@ func (h *HTTPVehicle) Read(ctx context.Context, oldHash utils.HashType) (buf []b
151152
err = errors.New(resp.Status)
152153
return
153154
}
154-
buf, err = io.ReadAll(resp.Body)
155+
var reader io.Reader = resp.Body
156+
if h.sizeLimit > 0 {
157+
reader = io.LimitReader(reader, h.sizeLimit)
158+
}
159+
buf, err = io.ReadAll(reader)
155160
if err != nil {
156161
return
157162
}
@@ -166,12 +171,13 @@ func (h *HTTPVehicle) Read(ctx context.Context, oldHash utils.HashType) (buf []b
166171
return
167172
}
168173

169-
func NewHTTPVehicle(url string, path string, proxy string, header http.Header, timeout time.Duration) *HTTPVehicle {
174+
func NewHTTPVehicle(url string, path string, proxy string, header http.Header, timeout time.Duration, sizeLimit int64) *HTTPVehicle {
170175
return &HTTPVehicle{
171-
url: url,
172-
path: path,
173-
proxy: proxy,
174-
header: header,
175-
timeout: timeout,
176+
url: url,
177+
path: path,
178+
proxy: proxy,
179+
header: header,
180+
timeout: timeout,
181+
sizeLimit: sizeLimit,
176182
}
177183
}

component/updater/update_geo.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func SetGeoUpdateInterval(newGeoUpdateInterval int) {
4545
}
4646

4747
func UpdateMMDB() (err error) {
48-
vehicle := resource.NewHTTPVehicle(geodata.MmdbUrl(), C.Path.MMDB(), "", nil, defaultHttpTimeout)
48+
vehicle := resource.NewHTTPVehicle(geodata.MmdbUrl(), C.Path.MMDB(), "", nil, defaultHttpTimeout, 0)
4949
var oldHash utils.HashType
5050
if buf, err := os.ReadFile(vehicle.Path()); err == nil {
5151
oldHash = utils.MakeHash(buf)
@@ -76,7 +76,7 @@ func UpdateMMDB() (err error) {
7676
}
7777

7878
func UpdateASN() (err error) {
79-
vehicle := resource.NewHTTPVehicle(geodata.ASNUrl(), C.Path.ASN(), "", nil, defaultHttpTimeout)
79+
vehicle := resource.NewHTTPVehicle(geodata.ASNUrl(), C.Path.ASN(), "", nil, defaultHttpTimeout, 0)
8080
var oldHash utils.HashType
8181
if buf, err := os.ReadFile(vehicle.Path()); err == nil {
8282
oldHash = utils.MakeHash(buf)
@@ -109,7 +109,7 @@ func UpdateASN() (err error) {
109109
func UpdateGeoIp() (err error) {
110110
geoLoader, err := geodata.GetGeoDataLoader("standard")
111111

112-
vehicle := resource.NewHTTPVehicle(geodata.GeoIpUrl(), C.Path.GeoIP(), "", nil, defaultHttpTimeout)
112+
vehicle := resource.NewHTTPVehicle(geodata.GeoIpUrl(), C.Path.GeoIP(), "", nil, defaultHttpTimeout, 0)
113113
var oldHash utils.HashType
114114
if buf, err := os.ReadFile(vehicle.Path()); err == nil {
115115
oldHash = utils.MakeHash(buf)
@@ -139,7 +139,7 @@ func UpdateGeoIp() (err error) {
139139
func UpdateGeoSite() (err error) {
140140
geoLoader, err := geodata.GetGeoDataLoader("standard")
141141

142-
vehicle := resource.NewHTTPVehicle(geodata.GeoSiteUrl(), C.Path.GeoSite(), "", nil, defaultHttpTimeout)
142+
vehicle := resource.NewHTTPVehicle(geodata.GeoSiteUrl(), C.Path.GeoSite(), "", nil, defaultHttpTimeout, 0)
143143
var oldHash utils.HashType
144144
if buf, err := os.ReadFile(vehicle.Path()); err == nil {
145145
oldHash = utils.MakeHash(buf)

docs/config.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,7 @@ proxy-providers:
930930
interval: 3600
931931
path: ./provider1.yaml # 默认只允许存储在 mihomo 的 Home Dir,如果想存储到任意位置,添加环境变量 SKIP_SAFE_PATH_CHECK=1
932932
proxy: DIRECT
933+
# size-limit: 10240 # 限制下载文件最大为10kb,默认为0即不限制文件大小
933934
header:
934935
User-Agent:
935936
- "Clash/v1.18.0"
@@ -977,6 +978,7 @@ rule-providers:
977978
type: http # http 的 path 可空置,默认储存路径为 homedir 的 rules 文件夹,文件名为 url 的 md5
978979
url: "url"
979980
proxy: DIRECT
981+
# size-limit: 10240 # 限制下载文件最大为10kb,默认为0即不限制文件大小
980982
rule2:
981983
behavior: classical
982984
interval: 259200

rules/provider/parse.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ var (
1616
)
1717

1818
type ruleProviderSchema struct {
19-
Type string `provider:"type"`
20-
Behavior string `provider:"behavior"`
21-
Path string `provider:"path,omitempty"`
22-
URL string `provider:"url,omitempty"`
23-
Proxy string `provider:"proxy,omitempty"`
24-
Format string `provider:"format,omitempty"`
25-
Interval int `provider:"interval,omitempty"`
19+
Type string `provider:"type"`
20+
Behavior string `provider:"behavior"`
21+
Path string `provider:"path,omitempty"`
22+
URL string `provider:"url,omitempty"`
23+
Proxy string `provider:"proxy,omitempty"`
24+
Format string `provider:"format,omitempty"`
25+
Interval int `provider:"interval,omitempty"`
26+
SizeLimit int64 `provider:"size-limit,omitempty"`
2627
}
2728

2829
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
5354
return nil, fmt.Errorf("%w: %s", errSubPath, path)
5455
}
5556
}
56-
vehicle = resource.NewHTTPVehicle(schema.URL, path, schema.Proxy, nil, resource.DefaultHttpTimeout)
57+
vehicle = resource.NewHTTPVehicle(schema.URL, path, schema.Proxy, nil, resource.DefaultHttpTimeout, schema.SizeLimit)
5758
default:
5859
return nil, fmt.Errorf("unsupported vehicle type: %s", schema.Type)
5960
}

0 commit comments

Comments
 (0)