Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added IP2Location.io API #120

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions internal/computing/cp_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down