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 public IP endpoint #177

Open
wants to merge 1 commit into
base: master
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
30 changes: 21 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
_ "net/http/pprof"
Expand Down Expand Up @@ -295,21 +296,32 @@ func cmdRegisterEstuary(ctx *cli.Context) error {
}

// Get public IP address

publicIPRes, err := http.Get("http://ip-api.com/json")
if err != nil {
return fmt.Errorf("could not get public ip: %v", err)
}
defer publicIPRes.Body.Close()

var ipInfo struct {
Query string
}

if err := json.NewDecoder(publicIPRes.Body).Decode(&ipInfo); err != nil {
return fmt.Errorf("could not get public ip: %v", err)
publicIP, err := http.Get("https://ip2location.io/ip")
if err == nil {
defer publicIP.Body.Close()
bodyBytes, err := io.ReadAll(publicIP.Body)

if err == nil {
bodyStr := string(bodyBytes[:])
ipInfo.Query = bodyStr
}
}

if ipInfo.Query == "" {
publicIPRes, err := http.Get("http://ip-api.com/json")
if err != nil {
return fmt.Errorf("could not get public ip: %v", err)
}
defer publicIPRes.Body.Close()

if err := json.NewDecoder(publicIPRes.Body).Decode(&ipInfo); err != nil {
return fmt.Errorf("could not get public ip: %v", err)
}
}
fmt.Printf("Using IP: %s\n", ipInfo.Query)

// Get peer key
Expand Down