mirror of
https://github.com/qdm12/gluetun.git
synced 2026-07-24 19:36:24 +02:00
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)
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
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 ip2Location struct {
|
||||
client *http.Client
|
||||
token string
|
||||
countryCodes map[string]string
|
||||
}
|
||||
|
||||
func newIP2Location(client *http.Client, token string) *ip2Location {
|
||||
return &ip2Location{
|
||||
client: client,
|
||||
token: token,
|
||||
countryCodes: constants.CountryCodes(),
|
||||
}
|
||||
}
|
||||
|
||||
func (i *ip2Location) String() string {
|
||||
return string(IP2Location)
|
||||
}
|
||||
|
||||
func (i *ip2Location) CanFetchAnyIP() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (i *ip2Location) Token() string {
|
||||
return i.token
|
||||
}
|
||||
|
||||
// FetchInfo obtains information on the ip address provided
|
||||
// using the api.ip2location.io API. If the ip is the zero value,
|
||||
// the public IP address of the machine is used as the IP.
|
||||
func (i *ip2Location) 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://api.ip2location.io/"
|
||||
if ip.IsValid() {
|
||||
url += "?ip=" + ip.String()
|
||||
}
|
||||
|
||||
if i.token != "" {
|
||||
if !strings.Contains(url, "?") {
|
||||
url += "?"
|
||||
}
|
||||
url += "&key=" + i.token
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
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"`
|
||||
CountryName string `json:"country_name,omitempty"`
|
||||
RegionName string `json:"region_name,omitempty"`
|
||||
CityName string `json:"city_name,omitempty"`
|
||||
Latitude float32 `json:"latitude,omitempty"`
|
||||
Longitude float32 `json:"longitude,omitempty"`
|
||||
ZipCode string `json:"zip_code,omitempty"`
|
||||
// Timezone in the form -07:00
|
||||
Timezone string `json:"time_zone,omitempty"`
|
||||
As string `json:"as,omitempty"`
|
||||
}
|
||||
if err := decoder.Decode(&data); err != nil {
|
||||
return result, fmt.Errorf("decoding response: %w", err)
|
||||
}
|
||||
|
||||
// Remove parentheses from country name
|
||||
idx := strings.Index(data.CountryName, " (")
|
||||
if idx != -1 {
|
||||
data.CountryName = data.CountryName[:idx]
|
||||
}
|
||||
|
||||
// Rename country to match country string obtained from other IP data sources
|
||||
countryRenames := map[string]string{
|
||||
"Netherlands": i.countryCodes["nl"],
|
||||
"United States of America": i.countryCodes["us"],
|
||||
"United Kingdom of Great Britain and Northern Ireland": i.countryCodes["gb"],
|
||||
"Czechia": i.countryCodes["cz"],
|
||||
"Korea": i.countryCodes["kr"],
|
||||
}
|
||||
if newName, ok := countryRenames[data.CountryName]; ok {
|
||||
data.CountryName = newName
|
||||
}
|
||||
|
||||
result = models.PublicIP{
|
||||
IP: data.IP,
|
||||
Region: data.RegionName,
|
||||
Country: data.CountryName,
|
||||
City: data.CityName,
|
||||
Hostname: "", // no hostname
|
||||
Location: fmt.Sprintf("%f,%f", data.Latitude, data.Longitude),
|
||||
Organization: data.As,
|
||||
PostalCode: data.ZipCode,
|
||||
Timezone: data.Timezone,
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user