Files
gluetun/internal/publicip/api/ifconfigco.go
Quentin McGaw a61302f135 feat(publicip): resilient public ip fetcher (#2518)
- `PUBLICIP_API` accepts a comma separated list of ip data sources, where the first one is the base default one, and sources after it are backup sources used if we are rate limited.
- `PUBLICIP_API` defaults to `ipinfo,ifconfigco,ip2location,cloudflare` such that it now has `ifconfigco,ip2location,cloudflare` as backup ip data sources.
- `PUBLICIP_API_TOKEN` accepts a comma separated list of ip data source tokens, each corresponding by position to the APIs listed in `PUBLICIP_API`.
- logs ip data source when logging public ip information
- assume a rate limiting error is for 30 days (no persistence)
- ready for future live settings updates
  - consider an ip data source no longer banned if the token changes
  - keeps track of ban times when updating the list of fetchers
2024-10-19 15:21:14 +02:00

99 lines
2.4 KiB
Go

package api
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/netip"
"github.com/qdm12/gluetun/internal/models"
)
type ifConfigCo struct {
client *http.Client
}
func newIfConfigCo(client *http.Client) *ifConfigCo {
return &ifConfigCo{
client: client,
}
}
func (i *ifConfigCo) String() string {
return string(IfConfigCo)
}
func (i *ifConfigCo) CanFetchAnyIP() bool {
return true
}
func (i *ifConfigCo) Token() string {
return ""
}
// FetchInfo obtains information on the ip address provided
// using the ifconfig.co/json API. If the ip is the zero value,
// the public IP address of the machine is used as the IP.
func (i *ifConfigCo) FetchInfo(ctx context.Context, ip netip.Addr) (
result models.PublicIP, err error,
) {
url := "https://ifconfig.co/json"
if ip.IsValid() {
url += "?ip=" + ip.String()
}
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return result, err
}
response, err := i.client.Do(request)
if err != nil {
return result, err
}
defer response.Body.Close()
switch response.StatusCode {
case http.StatusOK:
case http.StatusTooManyRequests:
return result, fmt.Errorf("%w from %s: %s",
ErrTooManyRequests, url, response.Status)
default:
return result, fmt.Errorf("%w from %s: %s",
ErrBadHTTPStatus, url, response.Status)
}
decoder := json.NewDecoder(response.Body)
var data struct {
IP netip.Addr `json:"ip,omitempty"`
Country string `json:"country,omitempty"`
RegionName string `json:"region_name,omitempty"`
ZipCode string `json:"zip_code,omitempty"`
City string `json:"city,omitempty"`
Latitude float32 `json:"latitude,omitempty"`
Longitude float32 `json:"longitude,omitempty"`
Hostname string `json:"hostname,omitempty"`
// Timezone in the form America/Montreal
Timezone string `json:"time_zone,omitempty"`
AsnOrg string `json:"asn_org,omitempty"`
}
err = decoder.Decode(&data)
if err != nil {
return result, fmt.Errorf("decoding response: %w", err)
}
result = models.PublicIP{
IP: data.IP,
Region: data.RegionName,
Country: data.Country,
City: data.City,
Hostname: data.Hostname,
Location: fmt.Sprintf("%f,%f", data.Latitude, data.Longitude),
Organization: data.AsnOrg,
PostalCode: data.ZipCode,
Timezone: data.Timezone,
}
return result, nil
}