diff --git a/component/geodata/init.go b/component/geodata/init.go index f7dd7a9e..acae1a34 100644 --- a/component/geodata/init.go +++ b/component/geodata/init.go @@ -1,13 +1,17 @@ package geodata import ( + "context" "fmt" - "github.com/Dreamacro/clash/component/mmdb" - C "github.com/Dreamacro/clash/constant" - "github.com/Dreamacro/clash/log" "io" "net/http" "os" + "time" + + clashHttp "github.com/Dreamacro/clash/component/http" + "github.com/Dreamacro/clash/component/mmdb" + C "github.com/Dreamacro/clash/constant" + "github.com/Dreamacro/clash/log" ) var initGeoSite bool @@ -38,7 +42,9 @@ func InitGeoSite() error { } func downloadGeoSite(path string) (err error) { - resp, err := http.Get(C.GeoSiteUrl) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*90) + defer cancel() + resp, err := clashHttp.HttpRequest(ctx, C.GeoSiteUrl, http.MethodGet, http.Header{"User-Agent": {"clash"}}, nil) if err != nil { return } @@ -55,7 +61,9 @@ func downloadGeoSite(path string) (err error) { } func downloadGeoIP(path string) (err error) { - resp, err := http.Get(C.GeoIpUrl) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*90) + defer cancel() + resp, err := clashHttp.HttpRequest(ctx, C.GeoIpUrl, http.MethodGet, http.Header{"User-Agent": {"clash"}}, nil) if err != nil { return } diff --git a/component/mmdb/mmdb.go b/component/mmdb/mmdb.go index 8f28d486..14f6f997 100644 --- a/component/mmdb/mmdb.go +++ b/component/mmdb/mmdb.go @@ -1,14 +1,18 @@ package mmdb import ( - "github.com/oschwald/geoip2-golang" + "context" "io" "net/http" "os" "sync" + "time" + clashHttp "github.com/Dreamacro/clash/component/http" C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/log" + + "github.com/oschwald/geoip2-golang" ) var ( @@ -47,7 +51,9 @@ func Instance() *geoip2.Reader { } func DownloadMMDB(path string) (err error) { - resp, err := http.Get(C.MmdbUrl) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*90) + defer cancel() + resp, err := clashHttp.HttpRequest(ctx, C.MmdbUrl, http.MethodGet, http.Header{"User-Agent": {"clash"}}, nil) if err != nil { return } diff --git a/config/updateGeo.go b/config/updateGeo.go index 698bd52d..e76301ba 100644 --- a/config/updateGeo.go +++ b/config/updateGeo.go @@ -1,15 +1,20 @@ package config import ( + "context" "fmt" - "github.com/Dreamacro/clash/component/geodata" - _ "github.com/Dreamacro/clash/component/geodata/standard" - C "github.com/Dreamacro/clash/constant" - "github.com/oschwald/geoip2-golang" "io" "net/http" "os" "runtime" + "time" + + "github.com/Dreamacro/clash/component/geodata" + _ "github.com/Dreamacro/clash/component/geodata/standard" + clashHttp "github.com/Dreamacro/clash/component/http" + C "github.com/Dreamacro/clash/constant" + + "github.com/oschwald/geoip2-golang" ) func UpdateGeoDatabases() error { @@ -69,7 +74,9 @@ func UpdateGeoDatabases() error { } func downloadForBytes(url string) ([]byte, error) { - resp, err := http.Get(url) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*90) + defer cancel() + resp, err := clashHttp.HttpRequest(ctx, url, http.MethodGet, http.Header{"User-Agent": {"clash"}}, nil) if err != nil { return nil, err }