diff --git a/internal/computing/cp_service.go b/internal/computing/cp_service.go index c5dd1771..18467b8f 100644 --- a/internal/computing/cp_service.go +++ b/internal/computing/cp_service.go @@ -1184,8 +1184,11 @@ func getLocation() (string, error) { if err != nil { regionCache, err = getRegionByIpApi() if err != nil { - logs.GetLogger().Errorf("get region info failed, error: %+v", err) - return "", err + regionCache, err = getRegionByIp2LocationIo() + if err != nil { + logs.GetLogger().Errorf("get region info failed, error: %+v", err) + return "", err + } } } return regionCache, nil @@ -1267,6 +1270,38 @@ func getRegionByIpInfo() (string, error) { return region, nil } +func getRegionByIp2LocationIo() (string, error) { + req, err := http.NewRequest("GET", "https://api.ip2location.io", nil) + if err != nil { + return "", err + } + req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36") + + client := http.DefaultClient + resp, err := client.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + + ipBytes, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + + var ipInfo struct { + Ip string `json:"ip"` + City string `json:"city_name"` + Region string `json:"region_name"` + CountryCode string `json:"country_code"` + } + if err = json.Unmarshal(ipBytes, &ipInfo); err != nil { + return "", err + } + region := strings.TrimSpace(ipInfo.Region) + "-" + ipInfo.CountryCode + return region, nil +} + func verifySignature(pubKStr, data, signature string) (bool, error) { sb, err := hexutil.Decode(signature) if err != nil {