Files
gluetun/gluetun-servers/pkg/publicip/api/ipinfo.go
T
Quentin McGaw d9cc7dcffb refactor(storage): new storage file structure
- new directory structure containing manifest.json and one json file per provider, by default.
- the manifest.json file can specify a filepath for each vpn provider
- each vpn provider json data file can contain the `"preferred": true` field to enforce it is used even if outdated, unless there is a version mismatch
- `STORAGE_SERVERS_DIRECTORY_PATH` replaces `STORAGE_FILEPATH` (which is now a migration source only). It sets the directory where server manifest and per-provider JSON files are stored (default: `/gluetun/servers/`).
- First-run migration: On startup, gluetun checks for the old /gluetun/servers.json file; if found and no new manifest exists, it automatically migrates all data to /gluetun/servers/ directory structure
- Silent fallback: If legacy file isn't found, uses the new directory path normally
- Legacy cleanup: After successful migration, attempts to remove the old fat JSON file (logs warning only if removal fails, e.g., read-only bind mounts)
2026-05-11 04:53:04 +00:00

121 lines
3.0 KiB
Go

package api
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/netip"
"strings"
"time"
"github.com/qdm12/gluetun-servers/pkg/constants"
"github.com/qdm12/gluetun-servers/pkg/publicip/models"
)
type ipInfo struct {
client *http.Client
token string
}
func newIPInfo(client *http.Client, token string) *ipInfo {
return &ipInfo{
client: client,
token: token,
}
}
func (i *ipInfo) String() string {
return string(IPInfo)
}
func (i *ipInfo) CanFetchAnyIP() bool {
return true
}
func (i *ipInfo) Token() string {
return i.token
}
// FetchInfo obtains information on the ip address provided
// using the ipinfo.io API. If the ip is the zero value, the public IP address
// of the machine is used as the IP.
func (i *ipInfo) FetchInfo(ctx context.Context, ip netip.Addr) (
result models.PublicIP, err error,
) {
// Define a timeout since the default client has a large timeout and we don't
// want to wait too long.
const timeout = 15 * time.Second
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
url := "https://ipinfo.io/"
switch {
case ip.Is6():
url = "https://v6.ipinfo.io/" + ip.String()
case ip.Is4():
url = "https://ipinfo.io/" + ip.String()
}
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return result, err
}
request.Header.Set("Authorization", "Bearer "+i.token)
response, err := i.client.Do(request)
if err != nil {
return result, err
}
defer response.Body.Close()
if i.token != "" && response.StatusCode == http.StatusUnauthorized {
return result, fmt.Errorf("token is not valid: %s", response.Status)
}
switch response.StatusCode {
case http.StatusOK:
case http.StatusTooManyRequests, http.StatusForbidden:
return result, fmt.Errorf("%w from %s: %d %s",
ErrTooManyRequests, url, response.StatusCode, response.Status)
default:
return result, fmt.Errorf("bad HTTP status received from %s: %d %s",
url, response.StatusCode, response.Status)
}
decoder := json.NewDecoder(response.Body)
var data struct {
IP netip.Addr `json:"ip,omitempty"`
Region string `json:"region,omitempty"`
Country string `json:"country,omitempty"`
City string `json:"city,omitempty"`
Hostname string `json:"hostname,omitempty"`
Loc string `json:"loc,omitempty"`
Org string `json:"org,omitempty"`
Postal string `json:"postal,omitempty"`
Timezone string `json:"timezone,omitempty"`
}
if err := decoder.Decode(&data); err != nil {
return result, fmt.Errorf("decoding response: %w", err)
}
countryCode := strings.ToLower(data.Country)
country, ok := constants.CountryCodes()[countryCode]
if ok {
data.Country = country
}
result = models.PublicIP{
IP: data.IP,
Region: data.Region,
Country: data.Country,
City: data.City,
Hostname: data.Hostname,
Location: data.Loc,
Organization: data.Org,
PostalCode: data.Postal,
Timezone: data.Timezone,
}
return result, nil
}