chore(publicip): refactoring
- Exported `Fetcher` interface - Inject `Fetcher` to publicip loop and updaters - Get public IP and information at the same time - Only query ipinfo.io - Make `MultiInfo` part of the `Fetch` object
This commit is contained in:
@@ -364,7 +364,8 @@ func _main(ctx context.Context, buildInfo models.BuildInformation,
|
|||||||
go unboundLooper.RunRestartTicker(dnsTickerCtx, dnsTickerDone)
|
go unboundLooper.RunRestartTicker(dnsTickerCtx, dnsTickerDone)
|
||||||
controlGroupHandler.Add(dnsTickerHandler)
|
controlGroupHandler.Add(dnsTickerHandler)
|
||||||
|
|
||||||
publicIPLooper := publicip.NewLoop(httpClient,
|
ipFetcher := publicip.NewFetch(httpClient)
|
||||||
|
publicIPLooper := publicip.NewLoop(ipFetcher,
|
||||||
logger.New(log.SetComponent("ip getter")),
|
logger.New(log.SetComponent("ip getter")),
|
||||||
allSettings.PublicIP, puid, pgid)
|
allSettings.PublicIP, puid, pgid)
|
||||||
pubIPHandler, pubIPCtx, pubIPDone := goshutdown.NewGoRoutineHandler(
|
pubIPHandler, pubIPCtx, pubIPDone := goshutdown.NewGoRoutineHandler(
|
||||||
@@ -382,7 +383,7 @@ func _main(ctx context.Context, buildInfo models.BuildInformation,
|
|||||||
unzipper := unzip.New(httpClient)
|
unzipper := unzip.New(httpClient)
|
||||||
parallelResolver := resolver.NewParallelResolver(allSettings.Updater.DNSAddress)
|
parallelResolver := resolver.NewParallelResolver(allSettings.Updater.DNSAddress)
|
||||||
providers := provider.NewProviders(storage, time.Now,
|
providers := provider.NewProviders(storage, time.Now,
|
||||||
updaterLogger, httpClient, unzipper, parallelResolver)
|
updaterLogger, httpClient, unzipper, parallelResolver, ipFetcher)
|
||||||
|
|
||||||
vpnLogger := logger.New(log.SetComponent("vpn"))
|
vpnLogger := logger.New(log.SetComponent("vpn"))
|
||||||
vpnLooper := vpn.NewLoop(allSettings.VPN, allSettings.Firewall.VPNInputPorts,
|
vpnLooper := vpn.NewLoop(allSettings.VPN, allSettings.Firewall.VPNInputPorts,
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/qdm12/gluetun/internal/configuration/sources"
|
"github.com/qdm12/gluetun/internal/configuration/sources"
|
||||||
"github.com/qdm12/gluetun/internal/constants"
|
"github.com/qdm12/gluetun/internal/constants"
|
||||||
"github.com/qdm12/gluetun/internal/provider"
|
"github.com/qdm12/gluetun/internal/provider"
|
||||||
|
publicipmodels "github.com/qdm12/gluetun/internal/publicip/models"
|
||||||
"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"
|
||||||
)
|
)
|
||||||
@@ -30,6 +31,10 @@ type ParallelResolver interface {
|
|||||||
hostToIPs map[string][]net.IP, warnings []string, err error)
|
hostToIPs map[string][]net.IP, warnings []string, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type IPFetcher interface {
|
||||||
|
FetchMultiInfo(ctx context.Context, ips []net.IP) (data []publicipmodels.IPInfoData, err error)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *CLI) OpenvpnConfig(logger OpenvpnConfigLogger, source sources.Source) error {
|
func (c *CLI) OpenvpnConfig(logger OpenvpnConfigLogger, source sources.Source) error {
|
||||||
storage, err := storage.New(logger, constants.ServersData)
|
storage, err := storage.New(logger, constants.ServersData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -50,8 +55,10 @@ func (c *CLI) OpenvpnConfig(logger OpenvpnConfigLogger, source sources.Source) e
|
|||||||
client := (*http.Client)(nil)
|
client := (*http.Client)(nil)
|
||||||
warner := (Warner)(nil)
|
warner := (Warner)(nil)
|
||||||
parallelResolver := (ParallelResolver)(nil)
|
parallelResolver := (ParallelResolver)(nil)
|
||||||
|
ipFetcher := (IPFetcher)(nil)
|
||||||
|
|
||||||
providers := provider.NewProviders(storage, time.Now, warner, client, unzipper, parallelResolver)
|
providers := provider.NewProviders(storage, time.Now, warner, client,
|
||||||
|
unzipper, parallelResolver, ipFetcher)
|
||||||
providerConf := providers.Get(*allSettings.VPN.Provider.Name)
|
providerConf := providers.Get(*allSettings.VPN.Provider.Name)
|
||||||
connection, err := providerConf.GetConnection(allSettings.VPN.Provider.ServerSelection)
|
connection, err := providerConf.GetConnection(allSettings.VPN.Provider.ServerSelection)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
"github.com/qdm12/gluetun/internal/constants"
|
"github.com/qdm12/gluetun/internal/constants"
|
||||||
"github.com/qdm12/gluetun/internal/constants/providers"
|
"github.com/qdm12/gluetun/internal/constants/providers"
|
||||||
"github.com/qdm12/gluetun/internal/provider"
|
"github.com/qdm12/gluetun/internal/provider"
|
||||||
|
"github.com/qdm12/gluetun/internal/publicip"
|
||||||
"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"
|
||||||
@@ -74,8 +75,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 := publicip.NewFetch(httpClient)
|
||||||
|
|
||||||
providers := provider.NewProviders(storage, time.Now, logger, httpClient, unzipper, parallelResolver)
|
providers := provider.NewProviders(storage, time.Now, logger, httpClient,
|
||||||
|
unzipper, parallelResolver, ipFetcher)
|
||||||
|
|
||||||
updater := updater.New(httpClient, storage, providers, logger)
|
updater := updater.New(httpClient, storage, providers, logger)
|
||||||
err = updater.UpdateServers(ctx, options.Providers)
|
err = updater.UpdateServers(ctx, options.Providers)
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/models"
|
"github.com/qdm12/gluetun/internal/models"
|
||||||
|
publicipmodels "github.com/qdm12/gluetun/internal/publicip/models"
|
||||||
"github.com/qdm12/gluetun/internal/updater/resolver"
|
"github.com/qdm12/gluetun/internal/updater/resolver"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -28,3 +29,7 @@ type Unzipper interface {
|
|||||||
type Warner interface {
|
type Warner interface {
|
||||||
Warn(s string)
|
Warn(s string)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type IPFetcher interface {
|
||||||
|
FetchMultiInfo(ctx context.Context, ips []net.IP) (data []publicipmodels.IPInfoData, err error)
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package privado
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/constants/providers"
|
"github.com/qdm12/gluetun/internal/constants/providers"
|
||||||
"github.com/qdm12/gluetun/internal/provider/common"
|
"github.com/qdm12/gluetun/internal/provider/common"
|
||||||
@@ -18,14 +17,14 @@ type Provider struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func New(storage common.Storage, randSource rand.Source,
|
func New(storage common.Storage, randSource rand.Source,
|
||||||
client *http.Client, unzipper common.Unzipper,
|
ipFetcher common.IPFetcher, unzipper common.Unzipper,
|
||||||
updaterWarner common.Warner,
|
updaterWarner common.Warner,
|
||||||
parallelResolver common.ParallelResolver) *Provider {
|
parallelResolver common.ParallelResolver) *Provider {
|
||||||
return &Provider{
|
return &Provider{
|
||||||
storage: storage,
|
storage: storage,
|
||||||
randSource: randSource,
|
randSource: randSource,
|
||||||
NoPortForwarder: utils.NewNoPortForwarding(providers.Privado),
|
NoPortForwarder: utils.NewNoPortForwarding(providers.Privado),
|
||||||
Fetcher: updater.New(client, unzipper, updaterWarner, parallelResolver),
|
Fetcher: updater.New(ipFetcher, unzipper, updaterWarner, parallelResolver),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,19 +3,18 @@ package updater
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/models"
|
"github.com/qdm12/gluetun/internal/models"
|
||||||
"github.com/qdm12/gluetun/internal/publicip"
|
"github.com/qdm12/gluetun/internal/provider/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
func setLocationInfo(ctx context.Context, client *http.Client, servers []models.Server) (err error) {
|
func setLocationInfo(ctx context.Context, fetcher common.IPFetcher, servers []models.Server) (err error) {
|
||||||
// Get public IP address information
|
// Get public IP address information
|
||||||
ipsToGetInfo := make([]net.IP, 0, len(servers))
|
ipsToGetInfo := make([]net.IP, 0, len(servers))
|
||||||
for _, server := range servers {
|
for _, server := range servers {
|
||||||
ipsToGetInfo = append(ipsToGetInfo, server.IPs...)
|
ipsToGetInfo = append(ipsToGetInfo, server.IPs...)
|
||||||
}
|
}
|
||||||
ipsInfo, err := publicip.MultiInfo(ctx, client, ipsToGetInfo)
|
ipsInfo, err := fetcher.FetchMultiInfo(ctx, ipsToGetInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ func (u *Updater) FetchServers(ctx context.Context, minServers int) (
|
|||||||
|
|
||||||
servers = hts.toServersSlice()
|
servers = hts.toServersSlice()
|
||||||
|
|
||||||
if err := setLocationInfo(ctx, u.client, servers); err != nil {
|
if err := setLocationInfo(ctx, u.ipFetcher, servers); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,22 +1,20 @@
|
|||||||
package updater
|
package updater
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/provider/common"
|
"github.com/qdm12/gluetun/internal/provider/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Updater struct {
|
type Updater struct {
|
||||||
client *http.Client
|
ipFetcher common.IPFetcher
|
||||||
unzipper common.Unzipper
|
unzipper common.Unzipper
|
||||||
parallelResolver common.ParallelResolver
|
parallelResolver common.ParallelResolver
|
||||||
warner common.Warner
|
warner common.Warner
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(client *http.Client, unzipper common.Unzipper,
|
func New(ipFetcher common.IPFetcher, unzipper common.Unzipper,
|
||||||
warner common.Warner, parallelResolver common.ParallelResolver) *Updater {
|
warner common.Warner, parallelResolver common.ParallelResolver) *Updater {
|
||||||
return &Updater{
|
return &Updater{
|
||||||
client: client,
|
ipFetcher: ipFetcher,
|
||||||
unzipper: unzipper,
|
unzipper: unzipper,
|
||||||
parallelResolver: parallelResolver,
|
parallelResolver: parallelResolver,
|
||||||
warner: warner,
|
warner: warner,
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ type Storage interface {
|
|||||||
|
|
||||||
func NewProviders(storage Storage, timeNow func() time.Time,
|
func NewProviders(storage Storage, timeNow func() time.Time,
|
||||||
updaterWarner common.Warner, client *http.Client, unzipper common.Unzipper,
|
updaterWarner common.Warner, client *http.Client, unzipper common.Unzipper,
|
||||||
parallelResolver common.ParallelResolver) *Providers {
|
parallelResolver common.ParallelResolver, ipFetcher common.IPFetcher) *Providers {
|
||||||
randSource := rand.NewSource(timeNow().UnixNano())
|
randSource := rand.NewSource(timeNow().UnixNano())
|
||||||
|
|
||||||
//nolint:lll
|
//nolint:lll
|
||||||
@@ -60,11 +60,11 @@ func NewProviders(storage Storage, timeNow func() time.Time,
|
|||||||
providers.Mullvad: mullvad.New(storage, randSource, client),
|
providers.Mullvad: mullvad.New(storage, randSource, client),
|
||||||
providers.Nordvpn: nordvpn.New(storage, randSource, client, updaterWarner),
|
providers.Nordvpn: nordvpn.New(storage, randSource, client, updaterWarner),
|
||||||
providers.Perfectprivacy: perfectprivacy.New(storage, randSource, unzipper, updaterWarner),
|
providers.Perfectprivacy: perfectprivacy.New(storage, randSource, unzipper, updaterWarner),
|
||||||
providers.Privado: privado.New(storage, randSource, client, unzipper, updaterWarner, parallelResolver),
|
providers.Privado: privado.New(storage, randSource, ipFetcher, unzipper, updaterWarner, parallelResolver),
|
||||||
providers.PrivateInternetAccess: privateinternetaccess.New(storage, randSource, timeNow, client),
|
providers.PrivateInternetAccess: privateinternetaccess.New(storage, randSource, timeNow, client),
|
||||||
providers.Privatevpn: privatevpn.New(storage, randSource, unzipper, updaterWarner, parallelResolver),
|
providers.Privatevpn: privatevpn.New(storage, randSource, unzipper, updaterWarner, parallelResolver),
|
||||||
providers.Protonvpn: protonvpn.New(storage, randSource, client, updaterWarner, parallelResolver),
|
providers.Protonvpn: protonvpn.New(storage, randSource, client, updaterWarner, parallelResolver),
|
||||||
providers.Purevpn: purevpn.New(storage, randSource, client, unzipper, updaterWarner, parallelResolver),
|
providers.Purevpn: purevpn.New(storage, randSource, ipFetcher, unzipper, updaterWarner, parallelResolver),
|
||||||
providers.Surfshark: surfshark.New(storage, randSource, client, unzipper, updaterWarner, parallelResolver),
|
providers.Surfshark: surfshark.New(storage, randSource, client, unzipper, updaterWarner, parallelResolver),
|
||||||
providers.Torguard: torguard.New(storage, randSource, unzipper, updaterWarner, parallelResolver),
|
providers.Torguard: torguard.New(storage, randSource, unzipper, updaterWarner, parallelResolver),
|
||||||
providers.VPNUnlimited: vpnunlimited.New(storage, randSource, unzipper, updaterWarner, parallelResolver),
|
providers.VPNUnlimited: vpnunlimited.New(storage, randSource, unzipper, updaterWarner, parallelResolver),
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package purevpn
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/constants/providers"
|
"github.com/qdm12/gluetun/internal/constants/providers"
|
||||||
"github.com/qdm12/gluetun/internal/provider/common"
|
"github.com/qdm12/gluetun/internal/provider/common"
|
||||||
@@ -18,13 +17,13 @@ type Provider struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func New(storage common.Storage, randSource rand.Source,
|
func New(storage common.Storage, randSource rand.Source,
|
||||||
client *http.Client, unzipper common.Unzipper, updaterWarner common.Warner,
|
ipFetcher common.IPFetcher, unzipper common.Unzipper,
|
||||||
parallelResolver common.ParallelResolver) *Provider {
|
updaterWarner common.Warner, parallelResolver common.ParallelResolver) *Provider {
|
||||||
return &Provider{
|
return &Provider{
|
||||||
storage: storage,
|
storage: storage,
|
||||||
randSource: randSource,
|
randSource: randSource,
|
||||||
NoPortForwarder: utils.NewNoPortForwarding(providers.Purevpn),
|
NoPortForwarder: utils.NewNoPortForwarding(providers.Purevpn),
|
||||||
Fetcher: updater.New(client, unzipper, updaterWarner, parallelResolver),
|
Fetcher: updater.New(ipFetcher, unzipper, updaterWarner, parallelResolver),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ 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"
|
|
||||||
"github.com/qdm12/gluetun/internal/updater/openvpn"
|
"github.com/qdm12/gluetun/internal/updater/openvpn"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -83,7 +82,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 := publicip.MultiInfo(ctx, u.client, ipsToGetInfo)
|
ipsInfo, err := u.ipFetcher.FetchMultiInfo(ctx, ipsToGetInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,20 @@
|
|||||||
package updater
|
package updater
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/provider/common"
|
"github.com/qdm12/gluetun/internal/provider/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Updater struct {
|
type Updater struct {
|
||||||
client *http.Client
|
ipFetcher common.IPFetcher
|
||||||
unzipper common.Unzipper
|
unzipper common.Unzipper
|
||||||
parallelResolver common.ParallelResolver
|
parallelResolver common.ParallelResolver
|
||||||
warner common.Warner
|
warner common.Warner
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(client *http.Client, unzipper common.Unzipper,
|
func New(ipFetcher common.IPFetcher, unzipper common.Unzipper,
|
||||||
warner common.Warner, parallelResolver common.ParallelResolver) *Updater {
|
warner common.Warner, parallelResolver common.ParallelResolver) *Updater {
|
||||||
return &Updater{
|
return &Updater{
|
||||||
client: client,
|
ipFetcher: ipFetcher,
|
||||||
unzipper: unzipper,
|
unzipper: unzipper,
|
||||||
parallelResolver: parallelResolver,
|
parallelResolver: parallelResolver,
|
||||||
warner: warner,
|
warner: warner,
|
||||||
|
|||||||
@@ -3,66 +3,74 @@ package publicip
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"math/rand"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/qdm12/gluetun/internal/constants"
|
||||||
|
"github.com/qdm12/gluetun/internal/publicip/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Fetch struct {
|
type Fetch struct {
|
||||||
client *http.Client
|
client *http.Client
|
||||||
randIntn func(n int) int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFetch(client *http.Client) *Fetch {
|
func NewFetch(client *http.Client) *Fetch {
|
||||||
return &Fetch{
|
return &Fetch{
|
||||||
client: client,
|
client: client,
|
||||||
randIntn: rand.Intn,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var ErrParseIP = errors.New("cannot parse IP address")
|
var (
|
||||||
|
ErrTooManyRequests = errors.New("too many requests sent for this month")
|
||||||
|
ErrBadHTTPStatus = errors.New("bad HTTP status received")
|
||||||
|
)
|
||||||
|
|
||||||
func (f *Fetch) FetchPublicIP(ctx context.Context) (ip net.IP, err error) {
|
// FetchInfo obtains information on the ip address provided
|
||||||
urls := []string{
|
// using the ipinfo.io API. If the ip is nil, the public IP address
|
||||||
"https://ifconfig.me/ip",
|
// of the machine is used as the IP.
|
||||||
"http://ip1.dynupdate.no-ip.com:8245",
|
func (f *Fetch) FetchInfo(ctx context.Context, ip net.IP) (
|
||||||
"http://ip1.dynupdate.no-ip.com",
|
result models.IPInfoData, err error) {
|
||||||
"https://api.ipify.org",
|
const baseURL = "https://ipinfo.io/"
|
||||||
"https://domains.google.com/checkip",
|
url := baseURL
|
||||||
"https://ifconfig.io/ip",
|
if ip != nil {
|
||||||
"https://ipinfo.io/ip",
|
url += ip.String()
|
||||||
}
|
|
||||||
url := urls[f.randIntn(len(urls))]
|
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
response, err := f.client.Do(req)
|
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return result, err
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := f.client.Do(request)
|
||||||
|
if err != nil {
|
||||||
|
return result, err
|
||||||
}
|
}
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
|
|
||||||
if response.StatusCode != http.StatusOK {
|
switch response.StatusCode {
|
||||||
return nil, fmt.Errorf("%w from %s: %d %s", ErrBadStatusCode,
|
case http.StatusOK:
|
||||||
url, response.StatusCode, response.Status)
|
case http.StatusTooManyRequests:
|
||||||
|
return result, fmt.Errorf("%w from %s: %d %s",
|
||||||
|
ErrTooManyRequests, url, response.StatusCode, response.Status)
|
||||||
|
default:
|
||||||
|
return result, fmt.Errorf("%w from %s: %d %s",
|
||||||
|
ErrBadHTTPStatus, url, response.StatusCode, response.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err := io.ReadAll(response.Body)
|
decoder := json.NewDecoder(response.Body)
|
||||||
if err != nil {
|
if err := decoder.Decode(&result); err != nil {
|
||||||
return nil, fmt.Errorf("cannot ready response body: %w", err)
|
return result, fmt.Errorf("cannot decode response: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
s := strings.ReplaceAll(string(content), "\n", "")
|
countryCode := strings.ToLower(result.Country)
|
||||||
ip = net.ParseIP(s)
|
country, ok := constants.CountryCodes()[countryCode]
|
||||||
if ip == nil {
|
if ok {
|
||||||
return nil, fmt.Errorf("%w: %s", ErrParseIP, s)
|
result.Country = country
|
||||||
}
|
}
|
||||||
return ip, nil
|
|
||||||
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,56 +0,0 @@
|
|||||||
package publicip
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/constants"
|
|
||||||
"github.com/qdm12/gluetun/internal/publicip/models"
|
|
||||||
)
|
|
||||||
|
|
||||||
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
|
|
||||||
result models.IPInfoData, err error) {
|
|
||||||
const baseURL = "https://ipinfo.io/"
|
|
||||||
url := baseURL + ip.String()
|
|
||||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
||||||
if err != nil {
|
|
||||||
return result, err
|
|
||||||
}
|
|
||||||
|
|
||||||
response, err := 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: %s", ErrTooManyRequests, baseURL)
|
|
||||||
default:
|
|
||||||
return result, fmt.Errorf("%w: %d %s", ErrBadHTTPStatus,
|
|
||||||
response.StatusCode, response.Status)
|
|
||||||
}
|
|
||||||
|
|
||||||
decoder := json.NewDecoder(response.Body)
|
|
||||||
if err := decoder.Decode(&result); err != nil {
|
|
||||||
return result, err
|
|
||||||
}
|
|
||||||
|
|
||||||
countryCode := strings.ToLower(result.Country)
|
|
||||||
country, ok := constants.CountryCodes()[countryCode]
|
|
||||||
if ok {
|
|
||||||
result.Country = country
|
|
||||||
}
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
@@ -23,6 +23,7 @@ type stateManager interface {
|
|||||||
SetSettings(ctx context.Context, settings settings.PublicIP) (outcome string)
|
SetSettings(ctx context.Context, settings settings.PublicIP) (outcome string)
|
||||||
}
|
}
|
||||||
|
|
||||||
type fetcher interface {
|
type Fetcher interface {
|
||||||
FetchPublicIP(ctx context.Context) (ip net.IP, err error)
|
FetchInfo(ctx context.Context, ip net.IP) (
|
||||||
|
result publicipmodels.IPInfoData, err error)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package publicip
|
package publicip
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/configuration/settings"
|
"github.com/qdm12/gluetun/internal/configuration/settings"
|
||||||
@@ -15,8 +14,7 @@ type Loop struct {
|
|||||||
statusManager statusManager
|
statusManager statusManager
|
||||||
state stateManager
|
state stateManager
|
||||||
// Objects
|
// Objects
|
||||||
fetcher fetcher
|
fetcher Fetcher
|
||||||
client *http.Client
|
|
||||||
logger Logger
|
logger Logger
|
||||||
// Fixed settings
|
// Fixed settings
|
||||||
puid int
|
puid int
|
||||||
@@ -35,7 +33,7 @@ type Loop struct {
|
|||||||
|
|
||||||
const defaultBackoffTime = 5 * time.Second
|
const defaultBackoffTime = 5 * time.Second
|
||||||
|
|
||||||
func NewLoop(client *http.Client, logger Logger,
|
func NewLoop(fetcher Fetcher, logger Logger,
|
||||||
settings settings.PublicIP, puid, pgid int) *Loop {
|
settings settings.PublicIP, puid, pgid int) *Loop {
|
||||||
start := make(chan struct{})
|
start := make(chan struct{})
|
||||||
running := make(chan models.LoopStatus)
|
running := make(chan models.LoopStatus)
|
||||||
@@ -50,8 +48,7 @@ func NewLoop(client *http.Client, logger Logger,
|
|||||||
statusManager: statusManager,
|
statusManager: statusManager,
|
||||||
state: state,
|
state: state,
|
||||||
// Objects
|
// Objects
|
||||||
client: client,
|
fetcher: fetcher,
|
||||||
fetcher: NewFetch(client),
|
|
||||||
logger: logger,
|
logger: logger,
|
||||||
puid: puid,
|
puid: puid,
|
||||||
pgid: pgid,
|
pgid: pgid,
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package models
|
|||||||
import "net"
|
import "net"
|
||||||
|
|
||||||
type IPInfoData struct {
|
type IPInfoData struct {
|
||||||
IP net.IP `json:"public_ip"`
|
IP net.IP `json:"ip,omitempty"`
|
||||||
Region string `json:"region,omitempty"`
|
Region string `json:"region,omitempty"`
|
||||||
Country string `json:"country,omitempty"`
|
Country string `json:"country,omitempty"`
|
||||||
City string `json:"city,omitempty"`
|
City string `json:"city,omitempty"`
|
||||||
@@ -20,8 +20,3 @@ func (i IPInfoData) Copy() (copied IPInfoData) {
|
|||||||
copy(copied.IP, i.IP)
|
copy(copied.IP, i.IP)
|
||||||
return copied
|
return copied
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IPInfoData) SetIP(ip net.IP) {
|
|
||||||
i.IP = make(net.IP, len(ip))
|
|
||||||
copy(i.IP, ip)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -3,18 +3,17 @@ package publicip
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/publicip/models"
|
"github.com/qdm12/gluetun/internal/publicip/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MultiInfo obtains the public IP address information for every IP
|
// FetchMultiInfo obtains the public IP address information for every IP
|
||||||
// addresses provided and returns a slice of results with the corresponding
|
// addresses provided and returns a slice of results with the corresponding
|
||||||
// order as to the IP addresses slice order.
|
// order as to the IP addresses slice order.
|
||||||
// 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 MultiInfo(ctx context.Context, client *http.Client, ips []net.IP) (
|
func (f *Fetch) FetchMultiInfo(ctx context.Context, ips []net.IP) (
|
||||||
results []models.IPInfoData, err error) {
|
results []models.IPInfoData, err error) {
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
|
||||||
@@ -30,7 +29,7 @@ func MultiInfo(ctx context.Context, client *http.Client, ips []net.IP) (
|
|||||||
aResult := asyncResult{
|
aResult := asyncResult{
|
||||||
index: index,
|
index: index,
|
||||||
}
|
}
|
||||||
aResult.result, aResult.err = Info(ctx, client, ip)
|
aResult.result, aResult.err = f.FetchInfo(ctx, ip)
|
||||||
resultsCh <- aResult
|
resultsCh <- aResult
|
||||||
}(i, ip)
|
}(i, ip)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ package publicip
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/constants"
|
"github.com/qdm12/gluetun/internal/constants"
|
||||||
|
"github.com/qdm12/gluetun/internal/publicip/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
|
func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
|
||||||
@@ -21,17 +21,17 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
|
|||||||
getCtx, getCancel := context.WithCancel(ctx)
|
getCtx, getCancel := context.WithCancel(ctx)
|
||||||
defer getCancel()
|
defer getCancel()
|
||||||
|
|
||||||
ipCh := make(chan net.IP)
|
resultCh := make(chan models.IPInfoData)
|
||||||
errorCh := make(chan error)
|
errorCh := make(chan error)
|
||||||
go func() {
|
go func() {
|
||||||
ip, err := l.fetcher.FetchPublicIP(getCtx)
|
result, err := l.fetcher.FetchInfo(getCtx, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if getCtx.Err() == nil {
|
if getCtx.Err() == nil {
|
||||||
errorCh <- err
|
errorCh <- err
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ipCh <- ip
|
resultCh <- result
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if l.userTrigger {
|
if l.userTrigger {
|
||||||
@@ -64,30 +64,24 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
|
|||||||
getCancel()
|
getCancel()
|
||||||
<-errorCh
|
<-errorCh
|
||||||
l.stopped <- struct{}{}
|
l.stopped <- struct{}{}
|
||||||
case ip := <-ipCh:
|
case result := <-resultCh:
|
||||||
getCancel()
|
getCancel()
|
||||||
|
|
||||||
message := "Public IP address is " + ip.String()
|
message := "Public IP address is " + result.IP.String()
|
||||||
result, err := Info(ctx, l.client, ip)
|
|
||||||
if err != nil {
|
|
||||||
l.logger.Warn(err.Error())
|
|
||||||
} else {
|
|
||||||
message += " (" + result.Country + ", " + result.Region + ", " + result.City + ")"
|
message += " (" + result.Country + ", " + result.Region + ", " + result.City + ")"
|
||||||
}
|
|
||||||
l.logger.Info(message)
|
l.logger.Info(message)
|
||||||
|
|
||||||
result.SetIP(ip)
|
|
||||||
l.state.SetData(result)
|
l.state.SetData(result)
|
||||||
|
|
||||||
filepath := *l.state.GetSettings().IPFilepath
|
filepath := *l.state.GetSettings().IPFilepath
|
||||||
err = persistPublicIP(filepath, ip.String(), l.puid, l.pgid)
|
err := persistPublicIP(filepath, result.IP.String(), l.puid, l.pgid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.logger.Error(err.Error())
|
l.logger.Error(err.Error())
|
||||||
}
|
}
|
||||||
l.statusManager.SetStatus(constants.Completed)
|
l.statusManager.SetStatus(constants.Completed)
|
||||||
case err := <-errorCh:
|
case err := <-errorCh:
|
||||||
getCancel()
|
getCancel()
|
||||||
close(ipCh)
|
close(resultCh)
|
||||||
l.statusManager.SetStatus(constants.Crashed)
|
l.statusManager.SetStatus(constants.Crashed)
|
||||||
l.logAndWait(ctx, err)
|
l.logAndWait(ctx, err)
|
||||||
stayHere = false
|
stayHere = false
|
||||||
|
|||||||
Reference in New Issue
Block a user