Maintenance: improve publicip with Result struct
This commit is contained in:
@@ -12,43 +12,49 @@ import (
|
|||||||
"github.com/qdm12/gluetun/internal/constants"
|
"github.com/qdm12/gluetun/internal/constants"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ipInfoData struct {
|
type Result struct {
|
||||||
Region string `json:"region"`
|
Region string `json:"region"`
|
||||||
Country string `json:"country"`
|
Country string `json:"country"`
|
||||||
City string `json:"city"`
|
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
|
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/"
|
const baseURL = "https://ipinfo.io/"
|
||||||
url := baseURL + ip.String()
|
url := baseURL + ip.String()
|
||||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", "", err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
response, err := client.Do(request)
|
response, err := client.Do(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", "", err
|
return result, err
|
||||||
}
|
}
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
|
|
||||||
if response.StatusCode != http.StatusOK {
|
switch response.StatusCode {
|
||||||
return "", "", "", fmt.Errorf("%w: %d", ErrBadHTTPStatus, 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)
|
decoder := json.NewDecoder(response.Body)
|
||||||
var data ipInfoData
|
if err := decoder.Decode(&result); err != nil {
|
||||||
if err := decoder.Decode(&data); err != nil {
|
return result, err
|
||||||
return "", "", "", err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
countryCode := strings.ToLower(data.Country)
|
countryCode := strings.ToLower(result.Country)
|
||||||
country, ok := constants.CountryCodes()[countryCode]
|
country, ok := constants.CountryCodes()[countryCode]
|
||||||
if !ok {
|
if ok {
|
||||||
country = data.Country
|
result.Country = country
|
||||||
}
|
}
|
||||||
return country, data.Region, data.City, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -154,11 +154,11 @@ func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup) {
|
|||||||
l.state.setPublicIP(ip)
|
l.state.setPublicIP(ip)
|
||||||
|
|
||||||
message := "Public IP address is " + ip.String()
|
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 {
|
if err != nil {
|
||||||
l.logger.Warn(err)
|
l.logger.Warn(err)
|
||||||
} else {
|
} else {
|
||||||
message += " (" + country + ", " + region + ", " + city + ")"
|
message += " (" + result.Country + ", " + result.Region + ", " + result.City + ")"
|
||||||
}
|
}
|
||||||
l.logger.Info(message)
|
l.logger.Info(message)
|
||||||
|
|
||||||
|
|||||||
@@ -76,13 +76,13 @@ func GetServers(ctx context.Context, client *http.Client,
|
|||||||
// Dedup by location
|
// Dedup by location
|
||||||
lts := make(locationToServer)
|
lts := make(locationToServer)
|
||||||
for _, server := range servers {
|
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 {
|
if err != nil {
|
||||||
return nil, warnings, err
|
return nil, warnings, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO split servers by host
|
// 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 {
|
if len(servers) < minServers {
|
||||||
|
|||||||
Reference in New Issue
Block a user