mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-03-02 10:50:24 +00:00
feat: add size-limit for provider
https://github.com/MetaCubeX/mihomo/issues/1645
This commit is contained in:
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user