Maintenance: improve publicip with Result struct

This commit is contained in:
Quentin McGaw
2021-05-08 23:30:29 +00:00
parent 248cc0d3d3
commit d9a70fd094
3 changed files with 24 additions and 18 deletions

View File

@@ -12,43 +12,49 @@ import (
"github.com/qdm12/gluetun/internal/constants"
)
type ipInfoData struct {
type Result struct {
Region string `json:"region"`
Country string `json:"country"`
City string `json:"city"`
}
var ErrBadHTTPStatus = errors.New("bad HTTP status received")
var (
ErrTooManyRequests = errors.New("too many requests sent for this month")
ErrBadHTTPStatus = errors.New("bad HTTP status received")
)
func Info(ctx context.Context, client *http.Client, ip net.IP) ( //nolint:interfacer
country, region, city string, err error) {
result Result, err error) {
const baseURL = "https://ipinfo.io/"
url := baseURL + ip.String()
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return "", "", "", err
return result, err
}
response, err := client.Do(request)
if err != nil {
return "", "", "", err
return result, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return "", "", "", fmt.Errorf("%w: %d", ErrBadHTTPStatus, response.StatusCode)
switch response.StatusCode {
case http.StatusOK:
case http.StatusTooManyRequests:
return result, fmt.Errorf("%w: %s", ErrTooManyRequests, baseURL)
default:
return result, fmt.Errorf("%w: %d", ErrBadHTTPStatus, response.StatusCode)
}
decoder := json.NewDecoder(response.Body)
var data ipInfoData
if err := decoder.Decode(&data); err != nil {
return "", "", "", err
if err := decoder.Decode(&result); err != nil {
return result, err
}
countryCode := strings.ToLower(data.Country)
countryCode := strings.ToLower(result.Country)
country, ok := constants.CountryCodes()[countryCode]
if !ok {
country = data.Country
if ok {
result.Country = country
}
return country, data.Region, data.City, nil
return result, nil
}

View File

@@ -154,11 +154,11 @@ func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup) {
l.state.setPublicIP(ip)
message := "Public IP address is " + ip.String()
country, region, city, err := Info(ctx, l.client, ip)
result, err := Info(ctx, l.client, ip)
if err != nil {
l.logger.Warn(err)
} else {
message += " (" + country + ", " + region + ", " + city + ")"
message += " (" + result.Country + ", " + result.Region + ", " + result.City + ")"
}
l.logger.Info(message)

View File

@@ -76,13 +76,13 @@ func GetServers(ctx context.Context, client *http.Client,
// Dedup by location
lts := make(locationToServer)
for _, server := range servers {
country, region, city, err := publicip.Info(ctx, client, server.IPs[0])
ipInfo, err := publicip.Info(ctx, client, server.IPs[0])
if err != nil {
return nil, warnings, err
}
// TODO split servers by host
lts.add(country, region, city, server.IPs)
lts.add(ipInfo.Country, ipInfo.Region, ipInfo.City, server.IPs)
}
if len(servers) < minServers {