diff --git a/service/common/httpClient/httpClient.go b/service/common/httpClient/httpClient.go index 3c424ea5..2fcf571d 100644 --- a/service/common/httpClient/httpClient.go +++ b/service/common/httpClient/httpClient.go @@ -60,18 +60,25 @@ func GetHttpClientWithv2rayAPac() (client *http.Client, err error) { return GetHttpClientWithProxy("http://" + net.JoinHostPort(host, strconv.Itoa(configure.GetPortsNotNil().HttpWithPac))) } -func GetHttpClientAutomatically() (c *http.Client, err error) { +func GetHttpClientAutomatically() (c *http.Client) { if !v2ray.ProcessManager.Running() || configure.GetConnectedServers() == nil || v2ray.IsTransparentOn() { - return http.DefaultClient, nil + return http.DefaultClient } setting := configure.GetSettingNotNil() + var err error switch setting.ProxyModeWhenSubscribe { case configure.ProxyModePac: c, err = GetHttpClientWithv2rayAPac() + if err != nil { + return http.DefaultClient + } case configure.ProxyModeProxy: c, err = GetHttpClientWithv2rayAProxy() + if err != nil { + return http.DefaultClient + } default: c = http.DefaultClient } - return + return c } diff --git a/service/core/v2ray/asset/asset.go b/service/core/v2ray/asset/asset.go index 0a8a6168..1a529ce2 100644 --- a/service/core/v2ray/asset/asset.go +++ b/service/core/v2ray/asset/asset.go @@ -2,12 +2,15 @@ package asset import ( "errors" + "fmt" "github.com/adrg/xdg" "github.com/muhammadmuzzammil1998/jsonc" "github.com/v2rayA/v2rayA/common/files" + "github.com/v2rayA/v2rayA/common/httpClient" "github.com/v2rayA/v2rayA/conf" "github.com/v2rayA/v2rayA/core/v2ray/where" "github.com/v2rayA/v2rayA/pkg/util/log" + "io" "io/fs" "os" "path" @@ -136,3 +139,20 @@ func GetV2rayConfigPath() (p string) { func GetV2rayConfigDirPath() (p string) { return conf.GetEnvironmentConfig().V2rayConfigDirectory } + +func Download(url string, to string) (err error) { + resp, err := httpClient.GetHttpClientAutomatically().Get(url) + if err != nil || resp.StatusCode != 200 { + if err == nil { + defer resp.Body.Close() + err = fmt.Errorf("code: %v %v", resp.StatusCode, resp.Status) + } + return err + } + defer resp.Body.Close() + b, err := io.ReadAll(resp.Body) + if err != nil { + return err + } + return os.WriteFile(to, b, 0644) +} diff --git a/service/core/v2ray/asset/dat/geoip.go b/service/core/v2ray/asset/dat/geoip.go index 804cccab..bb3b01a3 100644 --- a/service/core/v2ray/asset/dat/geoip.go +++ b/service/core/v2ray/asset/dat/geoip.go @@ -3,7 +3,6 @@ package dat import ( "fmt" "github.com/v2rayA/v2rayA/core/v2ray/asset" - gopeed2 "github.com/v2rayA/v2rayA/pkg/util/gopeed" "github.com/v2rayA/v2rayA/pkg/util/log" "os" "strings" @@ -14,10 +13,7 @@ func UpdateLocalGeoIP() (err error) { if err != nil { return err } - if err = gopeed2.Down(&gopeed2.Request{ - Method: "GET", - URL: "https://hubmirror.v2raya.org/v2fly/geoip/releases/latest/download/geoip.dat", - }, pathSiteDat+".new"); err != nil { + if err = asset.Download("https://hubmirror.v2raya.org/v2fly/geoip/releases/latest/download/geoip.dat", pathSiteDat+".new"); err != nil { log.Warn("UpdateLocalGeoIP: %v", err) return } diff --git a/service/core/v2ray/asset/dat/geosite.go b/service/core/v2ray/asset/dat/geosite.go index 606e7fb6..bb3d9425 100644 --- a/service/core/v2ray/asset/dat/geosite.go +++ b/service/core/v2ray/asset/dat/geosite.go @@ -3,7 +3,6 @@ package dat import ( "fmt" "github.com/v2rayA/v2rayA/core/v2ray/asset" - gopeed2 "github.com/v2rayA/v2rayA/pkg/util/gopeed" "github.com/v2rayA/v2rayA/pkg/util/log" "os" "strings" @@ -14,10 +13,8 @@ func UpdateLocalGeoSite() (err error) { if err != nil { return err } - if err = gopeed2.Down(&gopeed2.Request{ - Method: "GET", - URL: "https://hubmirror.v2raya.org/v2fly/domain-list-community/releases/latest/download/dlc.dat", - }, pathSiteDat+".new"); err != nil { + + if err = asset.Download("https://hubmirror.v2raya.org/v2fly/domain-list-community/releases/latest/download/dlc.dat", pathSiteDat+".new"); err != nil { log.Warn("UpdateLocalGeoSite: %v", err) return } diff --git a/service/core/v2ray/asset/dat/gfwlist.go b/service/core/v2ray/asset/dat/gfwlist.go index 0b292ca6..011f2d95 100644 --- a/service/core/v2ray/asset/dat/gfwlist.go +++ b/service/core/v2ray/asset/dat/gfwlist.go @@ -10,7 +10,6 @@ import ( "github.com/v2rayA/v2rayA/core/v2ray" "github.com/v2rayA/v2rayA/core/v2ray/asset" "github.com/v2rayA/v2rayA/db/configure" - gopeed2 "github.com/v2rayA/v2rayA/pkg/util/gopeed" "github.com/v2rayA/v2rayA/pkg/util/log" "io" "net/http" @@ -100,7 +99,7 @@ var ( ) func httpGet(url string) (data string, err error) { - resp, err := http.Get(url) + resp, err := httpClient.GetHttpClientAutomatically().Get(url) if err != nil { return } @@ -121,10 +120,7 @@ func UpdateLocalGFWList() (localGFWListVersionAfterUpdate string, err error) { return "", err } u := fmt.Sprintf(`https://hubmirror.v2raya.org/v2rayA/dist-v2ray-rules-dat/raw/%v/geosite.dat`, gfwlist.Tag) - if err = gopeed2.Down(&gopeed2.Request{ - Method: "GET", - URL: u, - }, pathSiteDat+".new"); err != nil { + if err = asset.Download(u, pathSiteDat+".new"); err != nil { log.Warn("UpdateLocalGFWList: %v", err) return } diff --git a/service/server/service/import.go b/service/server/service/import.go index 746bec80..75f31524 100644 --- a/service/server/service/import.go +++ b/service/server/service/import.go @@ -91,10 +91,7 @@ func Import(url string, which *configure.Which) (err error) { } else { // maybe it is a OOCv1 token } - c, err := httpClient.GetHttpClientAutomatically() - if err != nil { - return err - } + c := httpClient.GetHttpClientAutomatically() c.Timeout = 90 * time.Second infos, status, err := ResolveSubscriptionWithClient(source, c) if err != nil { diff --git a/service/server/service/subscription.go b/service/server/service/subscription.go index db0bb95a..864a3b33 100644 --- a/service/server/service/subscription.go +++ b/service/server/service/subscription.go @@ -254,11 +254,7 @@ func getDataUsageStatus(bytesUsed, bytesRemaining uint64) (status string) { func UpdateSubscription(index int, disconnectIfNecessary bool) (err error) { subscriptions := configure.GetSubscriptionsV2() addr := subscriptions[index].Address - c, err := httpClient.GetHttpClientAutomatically() - if err != nil { - reason := "failed to get proxy" - return fmt.Errorf("UpdateSubscription: %v", reason) - } + c := httpClient.GetHttpClientAutomatically() resolv.CheckResolvConf() subscriptionInfos, status, err := ResolveSubscriptionWithClient(addr, c) if err != nil {