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:
Quentin McGaw
2022-06-12 00:09:01 +00:00
parent 45c9e780c0
commit 83b4a3fe55
19 changed files with 101 additions and 155 deletions

View File

@@ -6,6 +6,7 @@ import (
"net"
"github.com/qdm12/gluetun/internal/models"
publicipmodels "github.com/qdm12/gluetun/internal/publicip/models"
"github.com/qdm12/gluetun/internal/updater/resolver"
)
@@ -28,3 +29,7 @@ type Unzipper interface {
type Warner interface {
Warn(s string)
}
type IPFetcher interface {
FetchMultiInfo(ctx context.Context, ips []net.IP) (data []publicipmodels.IPInfoData, err error)
}

View File

@@ -2,7 +2,6 @@ package privado
import (
"math/rand"
"net/http"
"github.com/qdm12/gluetun/internal/constants/providers"
"github.com/qdm12/gluetun/internal/provider/common"
@@ -18,14 +17,14 @@ type Provider struct {
}
func New(storage common.Storage, randSource rand.Source,
client *http.Client, unzipper common.Unzipper,
ipFetcher common.IPFetcher, unzipper common.Unzipper,
updaterWarner common.Warner,
parallelResolver common.ParallelResolver) *Provider {
return &Provider{
storage: storage,
randSource: randSource,
NoPortForwarder: utils.NewNoPortForwarding(providers.Privado),
Fetcher: updater.New(client, unzipper, updaterWarner, parallelResolver),
Fetcher: updater.New(ipFetcher, unzipper, updaterWarner, parallelResolver),
}
}

View File

@@ -3,19 +3,18 @@ package updater
import (
"context"
"net"
"net/http"
"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
ipsToGetInfo := make([]net.IP, 0, len(servers))
for _, server := range servers {
ipsToGetInfo = append(ipsToGetInfo, server.IPs...)
}
ipsInfo, err := publicip.MultiInfo(ctx, client, ipsToGetInfo)
ipsInfo, err := fetcher.FetchMultiInfo(ctx, ipsToGetInfo)
if err != nil {
return err
}

View File

@@ -68,7 +68,7 @@ func (u *Updater) FetchServers(ctx context.Context, minServers int) (
servers = hts.toServersSlice()
if err := setLocationInfo(ctx, u.client, servers); err != nil {
if err := setLocationInfo(ctx, u.ipFetcher, servers); err != nil {
return nil, err
}

View File

@@ -1,22 +1,20 @@
package updater
import (
"net/http"
"github.com/qdm12/gluetun/internal/provider/common"
)
type Updater struct {
client *http.Client
ipFetcher common.IPFetcher
unzipper common.Unzipper
parallelResolver common.ParallelResolver
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 {
return &Updater{
client: client,
ipFetcher: ipFetcher,
unzipper: unzipper,
parallelResolver: parallelResolver,
warner: warner,

View File

@@ -45,7 +45,7 @@ type Storage interface {
func NewProviders(storage Storage, timeNow func() time.Time,
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())
//nolint:lll
@@ -60,11 +60,11 @@ func NewProviders(storage Storage, timeNow func() time.Time,
providers.Mullvad: mullvad.New(storage, randSource, client),
providers.Nordvpn: nordvpn.New(storage, randSource, client, 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.Privatevpn: privatevpn.New(storage, randSource, unzipper, 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.Torguard: torguard.New(storage, randSource, unzipper, updaterWarner, parallelResolver),
providers.VPNUnlimited: vpnunlimited.New(storage, randSource, unzipper, updaterWarner, parallelResolver),

View File

@@ -2,7 +2,6 @@ package purevpn
import (
"math/rand"
"net/http"
"github.com/qdm12/gluetun/internal/constants/providers"
"github.com/qdm12/gluetun/internal/provider/common"
@@ -18,13 +17,13 @@ type Provider struct {
}
func New(storage common.Storage, randSource rand.Source,
client *http.Client, unzipper common.Unzipper, updaterWarner common.Warner,
parallelResolver common.ParallelResolver) *Provider {
ipFetcher common.IPFetcher, unzipper common.Unzipper,
updaterWarner common.Warner, parallelResolver common.ParallelResolver) *Provider {
return &Provider{
storage: storage,
randSource: randSource,
NoPortForwarder: utils.NewNoPortForwarding(providers.Purevpn),
Fetcher: updater.New(client, unzipper, updaterWarner, parallelResolver),
Fetcher: updater.New(ipFetcher, unzipper, updaterWarner, parallelResolver),
}
}

View File

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

View File

@@ -1,22 +1,20 @@
package updater
import (
"net/http"
"github.com/qdm12/gluetun/internal/provider/common"
)
type Updater struct {
client *http.Client
ipFetcher common.IPFetcher
unzipper common.Unzipper
parallelResolver common.ParallelResolver
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 {
return &Updater{
client: client,
ipFetcher: ipFetcher,
unzipper: unzipper,
parallelResolver: parallelResolver,
warner: warner,