chore(publicip): less coupling with ipinfo.io

This commit is contained in:
Quentin McGaw
2024-02-13 11:11:10 +00:00
parent 6a6337b98f
commit cfca026621
14 changed files with 136 additions and 75 deletions

View File

@@ -34,7 +34,7 @@ import (
"github.com/qdm12/gluetun/internal/pprof" "github.com/qdm12/gluetun/internal/pprof"
"github.com/qdm12/gluetun/internal/provider" "github.com/qdm12/gluetun/internal/provider"
"github.com/qdm12/gluetun/internal/publicip" "github.com/qdm12/gluetun/internal/publicip"
"github.com/qdm12/gluetun/internal/publicip/ipinfo" pubipapi "github.com/qdm12/gluetun/internal/publicip/api"
"github.com/qdm12/gluetun/internal/routing" "github.com/qdm12/gluetun/internal/routing"
"github.com/qdm12/gluetun/internal/server" "github.com/qdm12/gluetun/internal/server"
"github.com/qdm12/gluetun/internal/shadowsocks" "github.com/qdm12/gluetun/internal/shadowsocks"
@@ -396,7 +396,10 @@ func _main(ctx context.Context, buildInfo models.BuildInformation,
go unboundLooper.RunRestartTicker(dnsTickerCtx, dnsTickerDone) go unboundLooper.RunRestartTicker(dnsTickerCtx, dnsTickerDone)
controlGroupHandler.Add(dnsTickerHandler) controlGroupHandler.Add(dnsTickerHandler)
ipFetcher := ipinfo.New(httpClient, *allSettings.PublicIP.APIToken) ipFetcher, err := pubipapi.New(pubipapi.IPInfo, httpClient, *allSettings.PublicIP.APIToken)
if err != nil {
return fmt.Errorf("creating public IP API client: %w", err)
}
publicIPLooper := publicip.NewLoop(ipFetcher, publicIPLooper := publicip.NewLoop(ipFetcher,
logger.New(log.SetComponent("ip getter")), logger.New(log.SetComponent("ip getter")),
allSettings.PublicIP, puid, pgid) allSettings.PublicIP, puid, pgid)

View File

@@ -9,9 +9,9 @@ import (
"time" "time"
"github.com/qdm12/gluetun/internal/constants" "github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/openvpn/extract" "github.com/qdm12/gluetun/internal/openvpn/extract"
"github.com/qdm12/gluetun/internal/provider" "github.com/qdm12/gluetun/internal/provider"
"github.com/qdm12/gluetun/internal/publicip/ipinfo"
"github.com/qdm12/gluetun/internal/storage" "github.com/qdm12/gluetun/internal/storage"
"github.com/qdm12/gluetun/internal/updater/resolver" "github.com/qdm12/gluetun/internal/updater/resolver"
) )
@@ -32,7 +32,7 @@ type ParallelResolver interface {
} }
type IPFetcher interface { type IPFetcher interface {
FetchMultiInfo(ctx context.Context, ips []netip.Addr) (data []ipinfo.Response, err error) FetchInfo(ctx context.Context, ip netip.Addr) (data models.PublicIP, err error)
} }
type IPv6Checker interface { type IPv6Checker interface {

View File

@@ -14,7 +14,7 @@ import (
"github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/constants/providers"
"github.com/qdm12/gluetun/internal/openvpn/extract" "github.com/qdm12/gluetun/internal/openvpn/extract"
"github.com/qdm12/gluetun/internal/provider" "github.com/qdm12/gluetun/internal/provider"
"github.com/qdm12/gluetun/internal/publicip/ipinfo" "github.com/qdm12/gluetun/internal/publicip/api"
"github.com/qdm12/gluetun/internal/storage" "github.com/qdm12/gluetun/internal/storage"
"github.com/qdm12/gluetun/internal/updater" "github.com/qdm12/gluetun/internal/updater"
"github.com/qdm12/gluetun/internal/updater/resolver" "github.com/qdm12/gluetun/internal/updater/resolver"
@@ -80,7 +80,10 @@ func (c *CLI) Update(ctx context.Context, args []string, logger UpdaterLogger) e
httpClient := &http.Client{Timeout: clientTimeout} httpClient := &http.Client{Timeout: clientTimeout}
unzipper := unzip.New(httpClient) unzipper := unzip.New(httpClient)
parallelResolver := resolver.NewParallelResolver(options.DNSAddress) parallelResolver := resolver.NewParallelResolver(options.DNSAddress)
ipFetcher := ipinfo.New(httpClient, ipToken) ipFetcher, err := api.New(api.IPInfo, httpClient, ipToken)
if err != nil {
return fmt.Errorf("creating public IP API client: %w", err)
}
openvpnFileExtractor := extract.New() openvpnFileExtractor := extract.New()
providers := provider.NewProviders(storage, time.Now, logger, httpClient, providers := provider.NewProviders(storage, time.Now, logger, httpClient,

View File

@@ -6,7 +6,6 @@ import (
"net/netip" "net/netip"
"github.com/qdm12/gluetun/internal/models" "github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/publicip/ipinfo"
"github.com/qdm12/gluetun/internal/updater/resolver" "github.com/qdm12/gluetun/internal/updater/resolver"
) )
@@ -34,5 +33,5 @@ type Warner interface {
} }
type IPFetcher interface { type IPFetcher interface {
FetchMultiInfo(ctx context.Context, ips []netip.Addr) (data []ipinfo.Response, err error) FetchInfo(ctx context.Context, ip netip.Addr) (result models.PublicIP, err error)
} }

View File

@@ -6,6 +6,7 @@ import (
"github.com/qdm12/gluetun/internal/models" "github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/provider/common" "github.com/qdm12/gluetun/internal/provider/common"
"github.com/qdm12/gluetun/internal/publicip/api"
) )
func setLocationInfo(ctx context.Context, fetcher common.IPFetcher, servers []models.Server) (err error) { func setLocationInfo(ctx context.Context, fetcher common.IPFetcher, servers []models.Server) (err error) {
@@ -14,7 +15,7 @@ func setLocationInfo(ctx context.Context, fetcher common.IPFetcher, servers []mo
for _, server := range servers { for _, server := range servers {
ipsToGetInfo = append(ipsToGetInfo, server.IPs...) ipsToGetInfo = append(ipsToGetInfo, server.IPs...)
} }
ipsInfo, err := fetcher.FetchMultiInfo(ctx, ipsToGetInfo) ipsInfo, err := api.FetchMultiInfo(ctx, fetcher, ipsToGetInfo)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -9,6 +9,7 @@ import (
"github.com/qdm12/gluetun/internal/models" "github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/provider/common" "github.com/qdm12/gluetun/internal/provider/common"
"github.com/qdm12/gluetun/internal/publicip/api"
"github.com/qdm12/gluetun/internal/updater/openvpn" "github.com/qdm12/gluetun/internal/updater/openvpn"
) )
@@ -80,7 +81,7 @@ func (u *Updater) FetchServers(ctx context.Context, minServers int) (
for i := range servers { for i := range servers {
ipsToGetInfo[i] = servers[i].IPs[0] ipsToGetInfo[i] = servers[i].IPs[0]
} }
ipsInfo, err := u.ipFetcher.FetchMultiInfo(ctx, ipsToGetInfo) ipsInfo, err := api.FetchMultiInfo(ctx, u.ipFetcher, ipsToGetInfo)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -0,0 +1,47 @@
package api
import (
"context"
"errors"
"fmt"
"net/http"
"net/netip"
"strings"
"github.com/qdm12/gluetun/internal/models"
)
type API interface {
FetchInfo(ctx context.Context, ip netip.Addr) (
result models.PublicIP, err error)
}
type Provider string
const (
IPInfo Provider = "ipinfo"
)
func New(provider Provider, client *http.Client, token string) ( //nolint:ireturn
a API, err error) {
switch provider {
case IPInfo:
return newIPInfo(client, token), nil
default:
panic("provider not valid: " + provider)
}
}
var (
ErrProviderNotValid = errors.New("API name is not valid")
)
func ParseProvider(s string) (provider Provider, err error) {
switch strings.ToLower(s) {
case "ipinfo":
return IPInfo, nil
default:
return "", fmt.Errorf(`%w: %q can only be "ipinfo" or "ip2location"`,
ErrProviderNotValid, s)
}
}

View File

@@ -0,0 +1,9 @@
package api
import "errors"
var (
ErrTokenNotValid = errors.New("token is not valid")
ErrTooManyRequests = errors.New("too many requests sent for this month")
ErrBadHTTPStatus = errors.New("bad HTTP status received")
)

View File

@@ -0,0 +1,13 @@
package api
import (
"context"
"net/netip"
"github.com/qdm12/gluetun/internal/models"
)
type Fetcher interface {
FetchInfo(ctx context.Context, ip netip.Addr) (
result models.PublicIP, err error)
}

View File

@@ -1,40 +1,34 @@
package ipinfo package api
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"net/netip" "net/netip"
"strings" "strings"
"github.com/qdm12/gluetun/internal/constants" "github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/models"
) )
type Fetch struct { type ipInfo struct {
client *http.Client client *http.Client
token string token string
} }
func New(client *http.Client, token string) *Fetch { func newIPInfo(client *http.Client, token string) *ipInfo {
return &Fetch{ return &ipInfo{
client: client, client: client,
token: token, token: token,
} }
} }
var (
ErrTokenNotValid = errors.New("token is not valid")
ErrTooManyRequests = errors.New("too many requests sent for this month")
ErrBadHTTPStatus = errors.New("bad HTTP status received")
)
// FetchInfo obtains information on the ip address provided // FetchInfo obtains information on the ip address provided
// using the ipinfo.io API. If the ip is the zero value, the public IP address // using the ipinfo.io API. If the ip is the zero value, the public IP address
// of the machine is used as the IP. // of the machine is used as the IP.
func (f *Fetch) FetchInfo(ctx context.Context, ip netip.Addr) ( func (i *ipInfo) FetchInfo(ctx context.Context, ip netip.Addr) (
result Response, err error) { result models.PublicIP, err error) {
url := "https://ipinfo.io/" url := "https://ipinfo.io/"
switch { switch {
case ip.Is6(): case ip.Is6():
@@ -47,15 +41,15 @@ func (f *Fetch) FetchInfo(ctx context.Context, ip netip.Addr) (
if err != nil { if err != nil {
return result, err return result, err
} }
request.Header.Set("Authorization", "Bearer "+f.token) request.Header.Set("Authorization", "Bearer "+i.token)
response, err := f.client.Do(request) response, err := i.client.Do(request)
if err != nil { if err != nil {
return result, err return result, err
} }
defer response.Body.Close() defer response.Body.Close()
if f.token != "" && response.StatusCode == http.StatusUnauthorized { if i.token != "" && response.StatusCode == http.StatusUnauthorized {
return result, fmt.Errorf("%w: %s", ErrTokenNotValid, response.Status) return result, fmt.Errorf("%w: %s", ErrTokenNotValid, response.Status)
} }
@@ -70,15 +64,37 @@ func (f *Fetch) FetchInfo(ctx context.Context, ip netip.Addr) (
} }
decoder := json.NewDecoder(response.Body) decoder := json.NewDecoder(response.Body)
if err := decoder.Decode(&result); err != nil { 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) return result, fmt.Errorf("decoding response: %w", err)
} }
countryCode := strings.ToLower(result.Country) countryCode := strings.ToLower(data.Country)
country, ok := constants.CountryCodes()[countryCode] country, ok := constants.CountryCodes()[countryCode]
if ok { if ok {
result.Country = country 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 return result, nil
} }

View File

@@ -1,8 +1,10 @@
package ipinfo package api
import ( import (
"context" "context"
"net/netip" "net/netip"
"github.com/qdm12/gluetun/internal/models"
) )
// FetchMultiInfo obtains the public IP address information for every IP // FetchMultiInfo obtains the public IP address information for every IP
@@ -11,13 +13,13 @@ import (
// If an error is encountered, all the operations are canceled and // If an error is encountered, all the operations are canceled and
// an error is returned, so the results returned should be considered // an error is returned, so the results returned should be considered
// incomplete in this case. // incomplete in this case.
func (f *Fetch) FetchMultiInfo(ctx context.Context, ips []netip.Addr) ( func FetchMultiInfo(ctx context.Context, fetcher Fetcher, ips []netip.Addr) (
results []Response, err error) { results []models.PublicIP, err error) {
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
type asyncResult struct { type asyncResult struct {
index int index int
result Response result models.PublicIP
err error err error
} }
resultsCh := make(chan asyncResult) resultsCh := make(chan asyncResult)
@@ -27,12 +29,12 @@ func (f *Fetch) FetchMultiInfo(ctx context.Context, ips []netip.Addr) (
aResult := asyncResult{ aResult := asyncResult{
index: index, index: index,
} }
aResult.result, aResult.err = f.FetchInfo(ctx, ip) aResult.result, aResult.err = fetcher.FetchInfo(ctx, ip)
resultsCh <- aResult resultsCh <- aResult
}(i, ip) }(i, ip)
} }
results = make([]Response, len(ips)) results = make([]models.PublicIP, len(ips))
for i := 0; i < len(ips); i++ { for i := 0; i < len(ips); i++ {
aResult := <-resultsCh aResult := <-resultsCh
if aResult.err != nil { if aResult.err != nil {

View File

@@ -4,12 +4,12 @@ import (
"context" "context"
"net/netip" "net/netip"
"github.com/qdm12/gluetun/internal/publicip/ipinfo" "github.com/qdm12/gluetun/internal/models"
) )
type Fetcher interface { type Fetcher interface {
FetchInfo(ctx context.Context, ip netip.Addr) ( FetchInfo(ctx context.Context, ip netip.Addr) (
result ipinfo.Response, err error) result models.PublicIP, err error)
} }
type Logger interface { type Logger interface {

View File

@@ -1,33 +0,0 @@
package ipinfo
import (
"net/netip"
"github.com/qdm12/gluetun/internal/models"
)
type Response 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"`
}
func (r *Response) ToPublicIPModel() (model models.PublicIP) {
return models.PublicIP{
IP: r.IP,
Region: r.Region,
Country: r.Country,
City: r.City,
Hostname: r.Hostname,
Location: r.Loc,
Organization: r.Org,
PostalCode: r.Postal,
Timezone: r.Timezone,
}
}

View File

@@ -10,7 +10,7 @@ import (
"github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/models" "github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/publicip/ipinfo" "github.com/qdm12/gluetun/internal/publicip/api"
) )
type Loop struct { type Loop struct {
@@ -103,7 +103,7 @@ func (l *Loop) run(runCtx context.Context, runDone chan<- struct{},
lastFetch = l.timeNow() lastFetch = l.timeNow()
timerIsReadyToReset = l.updateTimer(*l.settings.Period, lastFetch, timer, timerIsReadyToReset) timerIsReadyToReset = l.updateTimer(*l.settings.Period, lastFetch, timer, timerIsReadyToReset)
if errors.Is(err, ipinfo.ErrTooManyRequests) { if errors.Is(err, api.ErrTooManyRequests) {
continue continue
} }
@@ -112,7 +112,7 @@ func (l *Loop) run(runCtx context.Context, runDone chan<- struct{},
l.logger.Info(message) l.logger.Info(message)
l.ipDataMutex.Lock() l.ipDataMutex.Lock()
l.ipData = result.ToPublicIPModel() l.ipData = result
l.ipDataMutex.Unlock() l.ipDataMutex.Unlock()
filepath := *l.settings.IPFilepath filepath := *l.settings.IPFilepath
@@ -123,7 +123,7 @@ func (l *Loop) run(runCtx context.Context, runDone chan<- struct{},
} }
} }
func (l *Loop) fetchIPData(ctx context.Context) (result ipinfo.Response, err error) { func (l *Loop) fetchIPData(ctx context.Context) (result models.PublicIP, err error) {
// keep retrying since settings updates won't change the // keep retrying since settings updates won't change the
// behavior of the following code. // behavior of the following code.
const defaultBackoffTime = 5 * time.Second const defaultBackoffTime = 5 * time.Second
@@ -135,7 +135,7 @@ func (l *Loop) fetchIPData(ctx context.Context) (result ipinfo.Response, err err
return result, nil return result, nil
case ctx.Err() != nil: case ctx.Err() != nil:
return result, err return result, err
case errors.Is(err, ipinfo.ErrTooManyRequests): case errors.Is(err, api.ErrTooManyRequests):
l.logger.Warn(err.Error() + "; not retrying.") l.logger.Warn(err.Error() + "; not retrying.")
return result, err return result, err
} }