mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-01-03 16:13:30 +08:00
feat: add size-limit
for provider
https://github.com/MetaCubeX/mihomo/issues/1645
This commit is contained in:
parent
1fff34d30e
commit
fbead56ec9
@ -66,6 +66,7 @@ type proxyProviderSchema struct {
|
|||||||
ExcludeFilter string `provider:"exclude-filter,omitempty"`
|
ExcludeFilter string `provider:"exclude-filter,omitempty"`
|
||||||
ExcludeType string `provider:"exclude-type,omitempty"`
|
ExcludeType string `provider:"exclude-type,omitempty"`
|
||||||
DialerProxy string `provider:"dialer-proxy,omitempty"`
|
DialerProxy string `provider:"dialer-proxy,omitempty"`
|
||||||
|
SizeLimit int64 `provider:"size-limit,omitempty"`
|
||||||
|
|
||||||
HealthCheck healthCheckSchema `provider:"health-check,omitempty"`
|
HealthCheck healthCheckSchema `provider:"health-check,omitempty"`
|
||||||
Override OverrideSchema `provider:"override,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)
|
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:
|
default:
|
||||||
return nil, fmt.Errorf("%w: %s", errVehicleType, schema.Type)
|
return nil, fmt.Errorf("%w: %s", errVehicleType, schema.Type)
|
||||||
}
|
}
|
||||||
|
@ -89,6 +89,7 @@ type HTTPVehicle struct {
|
|||||||
proxy string
|
proxy string
|
||||||
header http.Header
|
header http.Header
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
|
sizeLimit int64
|
||||||
provider types.ProxyProvider
|
provider types.ProxyProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,7 +152,11 @@ func (h *HTTPVehicle) Read(ctx context.Context, oldHash utils.HashType) (buf []b
|
|||||||
err = errors.New(resp.Status)
|
err = errors.New(resp.Status)
|
||||||
return
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -166,12 +171,13 @@ func (h *HTTPVehicle) Read(ctx context.Context, oldHash utils.HashType) (buf []b
|
|||||||
return
|
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{
|
return &HTTPVehicle{
|
||||||
url: url,
|
url: url,
|
||||||
path: path,
|
path: path,
|
||||||
proxy: proxy,
|
proxy: proxy,
|
||||||
header: header,
|
header: header,
|
||||||
timeout: timeout,
|
timeout: timeout,
|
||||||
|
sizeLimit: sizeLimit,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ func SetGeoUpdateInterval(newGeoUpdateInterval int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func UpdateMMDB() (err error) {
|
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
|
var oldHash utils.HashType
|
||||||
if buf, err := os.ReadFile(vehicle.Path()); err == nil {
|
if buf, err := os.ReadFile(vehicle.Path()); err == nil {
|
||||||
oldHash = utils.MakeHash(buf)
|
oldHash = utils.MakeHash(buf)
|
||||||
@ -76,7 +76,7 @@ func UpdateMMDB() (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func UpdateASN() (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
|
var oldHash utils.HashType
|
||||||
if buf, err := os.ReadFile(vehicle.Path()); err == nil {
|
if buf, err := os.ReadFile(vehicle.Path()); err == nil {
|
||||||
oldHash = utils.MakeHash(buf)
|
oldHash = utils.MakeHash(buf)
|
||||||
@ -109,7 +109,7 @@ func UpdateASN() (err error) {
|
|||||||
func UpdateGeoIp() (err error) {
|
func UpdateGeoIp() (err error) {
|
||||||
geoLoader, err := geodata.GetGeoDataLoader("standard")
|
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
|
var oldHash utils.HashType
|
||||||
if buf, err := os.ReadFile(vehicle.Path()); err == nil {
|
if buf, err := os.ReadFile(vehicle.Path()); err == nil {
|
||||||
oldHash = utils.MakeHash(buf)
|
oldHash = utils.MakeHash(buf)
|
||||||
@ -139,7 +139,7 @@ func UpdateGeoIp() (err error) {
|
|||||||
func UpdateGeoSite() (err error) {
|
func UpdateGeoSite() (err error) {
|
||||||
geoLoader, err := geodata.GetGeoDataLoader("standard")
|
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
|
var oldHash utils.HashType
|
||||||
if buf, err := os.ReadFile(vehicle.Path()); err == nil {
|
if buf, err := os.ReadFile(vehicle.Path()); err == nil {
|
||||||
oldHash = utils.MakeHash(buf)
|
oldHash = utils.MakeHash(buf)
|
||||||
|
@ -930,6 +930,7 @@ proxy-providers:
|
|||||||
interval: 3600
|
interval: 3600
|
||||||
path: ./provider1.yaml # 默认只允许存储在 mihomo 的 Home Dir,如果想存储到任意位置,添加环境变量 SKIP_SAFE_PATH_CHECK=1
|
path: ./provider1.yaml # 默认只允许存储在 mihomo 的 Home Dir,如果想存储到任意位置,添加环境变量 SKIP_SAFE_PATH_CHECK=1
|
||||||
proxy: DIRECT
|
proxy: DIRECT
|
||||||
|
# size-limit: 10240 # 限制下载文件最大为10kb,默认为0即不限制文件大小
|
||||||
header:
|
header:
|
||||||
User-Agent:
|
User-Agent:
|
||||||
- "Clash/v1.18.0"
|
- "Clash/v1.18.0"
|
||||||
@ -977,6 +978,7 @@ rule-providers:
|
|||||||
type: http # http 的 path 可空置,默认储存路径为 homedir 的 rules 文件夹,文件名为 url 的 md5
|
type: http # http 的 path 可空置,默认储存路径为 homedir 的 rules 文件夹,文件名为 url 的 md5
|
||||||
url: "url"
|
url: "url"
|
||||||
proxy: DIRECT
|
proxy: DIRECT
|
||||||
|
# size-limit: 10240 # 限制下载文件最大为10kb,默认为0即不限制文件大小
|
||||||
rule2:
|
rule2:
|
||||||
behavior: classical
|
behavior: classical
|
||||||
interval: 259200
|
interval: 259200
|
||||||
|
@ -23,6 +23,7 @@ type ruleProviderSchema struct {
|
|||||||
Proxy string `provider:"proxy,omitempty"`
|
Proxy string `provider:"proxy,omitempty"`
|
||||||
Format string `provider:"format,omitempty"`
|
Format string `provider:"format,omitempty"`
|
||||||
Interval int `provider:"interval,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) {
|
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)
|
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:
|
default:
|
||||||
return nil, fmt.Errorf("unsupported vehicle type: %s", schema.Type)
|
return nil, fmt.Errorf("unsupported vehicle type: %s", schema.Type)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user