From 2388e0550b0808085eab9de72d1362d0912c0423 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Fri, 11 Oct 2024 21:55:03 +0000 Subject: [PATCH] hotfix(publicip): return an error if trying to use cloudflare as ip provider for updating servers data --- internal/cli/openvpnconfig.go | 2 ++ internal/provider/common/updater.go | 7 +++++-- internal/provider/privado/updater/servers.go | 4 ++++ internal/provider/purevpn/updater/servers.go | 4 ++++ internal/publicip/api/api.go | 2 ++ internal/publicip/api/cloudflare.go | 8 ++++++++ internal/publicip/api/ifconfigco.go | 8 ++++++++ internal/publicip/api/ip2location.go | 8 ++++++++ internal/publicip/api/ipinfo.go | 8 ++++++++ internal/publicip/interfaces.go | 1 + 10 files changed, 50 insertions(+), 2 deletions(-) diff --git a/internal/cli/openvpnconfig.go b/internal/cli/openvpnconfig.go index aacfa1b88..520f824d9 100644 --- a/internal/cli/openvpnconfig.go +++ b/internal/cli/openvpnconfig.go @@ -34,6 +34,8 @@ type ParallelResolver interface { } type IPFetcher interface { + String() string + CanFetchAnyIP() bool FetchInfo(ctx context.Context, ip netip.Addr) (data models.PublicIP, err error) } diff --git a/internal/provider/common/updater.go b/internal/provider/common/updater.go index f72bcaacb..5a11112e0 100644 --- a/internal/provider/common/updater.go +++ b/internal/provider/common/updater.go @@ -10,8 +10,9 @@ import ( ) var ( - ErrNotEnoughServers = errors.New("not enough servers found") - ErrHTTPStatusCodeNotOK = errors.New("HTTP status code not OK") + ErrNotEnoughServers = errors.New("not enough servers found") + ErrHTTPStatusCodeNotOK = errors.New("HTTP status code not OK") + ErrIPFetcherUnsupported = errors.New("IP fetcher not supported") ) type Fetcher interface { @@ -33,5 +34,7 @@ type Warner interface { } type IPFetcher interface { + String() string + CanFetchAnyIP() bool FetchInfo(ctx context.Context, ip netip.Addr) (result models.PublicIP, err error) } diff --git a/internal/provider/privado/updater/servers.go b/internal/provider/privado/updater/servers.go index 2d15f1ec1..9d031fcb1 100644 --- a/internal/provider/privado/updater/servers.go +++ b/internal/provider/privado/updater/servers.go @@ -14,6 +14,10 @@ import ( func (u *Updater) FetchServers(ctx context.Context, minServers int) ( servers []models.Server, err error, ) { + if !u.ipFetcher.CanFetchAnyIP() { + return nil, fmt.Errorf("%w: %s", common.ErrIPFetcherUnsupported, u.ipFetcher.String()) + } + const url = "https://privadovpn.com/apps/ovpn_configs.zip" contents, err := u.unzipper.FetchAndExtract(ctx, url) if err != nil { diff --git a/internal/provider/purevpn/updater/servers.go b/internal/provider/purevpn/updater/servers.go index 01249650d..6748968eb 100644 --- a/internal/provider/purevpn/updater/servers.go +++ b/internal/provider/purevpn/updater/servers.go @@ -16,6 +16,10 @@ import ( func (u *Updater) FetchServers(ctx context.Context, minServers int) ( servers []models.Server, err error, ) { + if !u.ipFetcher.CanFetchAnyIP() { + return nil, fmt.Errorf("%w: %s", common.ErrIPFetcherUnsupported, u.ipFetcher.String()) + } + const url = "https://d11a57lttb2ffq.cloudfront.net/heartbleed/router/Recommended-CA2.zip" contents, err := u.unzipper.FetchAndExtract(ctx, url) if err != nil { diff --git a/internal/publicip/api/api.go b/internal/publicip/api/api.go index 4d415519d..623b1c6ce 100644 --- a/internal/publicip/api/api.go +++ b/internal/publicip/api/api.go @@ -12,6 +12,8 @@ import ( ) type API interface { + String() string + CanFetchAnyIP() bool FetchInfo(ctx context.Context, ip netip.Addr) ( result models.PublicIP, err error) } diff --git a/internal/publicip/api/cloudflare.go b/internal/publicip/api/cloudflare.go index d79f3cef6..56a5f03a5 100644 --- a/internal/publicip/api/cloudflare.go +++ b/internal/publicip/api/cloudflare.go @@ -22,6 +22,14 @@ func newCloudflare(client *http.Client) *cloudflare { } } +func (c *cloudflare) String() string { + return string(Cloudflare) +} + +func (c *cloudflare) CanFetchAnyIP() bool { + return false +} + // FetchInfo obtains information on the public IP address of the machine, // and returns an error if the `ip` argument is set since the Cloudflare API // can only be used to provide details about the current machine public IP. diff --git a/internal/publicip/api/ifconfigco.go b/internal/publicip/api/ifconfigco.go index 5c278096f..8da079963 100644 --- a/internal/publicip/api/ifconfigco.go +++ b/internal/publicip/api/ifconfigco.go @@ -20,6 +20,14 @@ func newIfConfigCo(client *http.Client) *ifConfigCo { } } +func (i *ifConfigCo) String() string { + return string(IfConfigCo) +} + +func (i *ifConfigCo) CanFetchAnyIP() bool { + return true +} + // FetchInfo obtains information on the ip address provided // using the ifconfig.co/json API. If the ip is the zero value, // the public IP address of the machine is used as the IP. diff --git a/internal/publicip/api/ip2location.go b/internal/publicip/api/ip2location.go index d8fb9fcec..7a3174e7c 100644 --- a/internal/publicip/api/ip2location.go +++ b/internal/publicip/api/ip2location.go @@ -23,6 +23,14 @@ func newIP2Location(client *http.Client, token string) *ip2Location { } } +func (i *ip2Location) String() string { + return string(IP2Location) +} + +func (i *ip2Location) CanFetchAnyIP() bool { + return true +} + // 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. diff --git a/internal/publicip/api/ipinfo.go b/internal/publicip/api/ipinfo.go index 9212a13f6..4ff787853 100644 --- a/internal/publicip/api/ipinfo.go +++ b/internal/publicip/api/ipinfo.go @@ -24,6 +24,14 @@ func newIPInfo(client *http.Client, token string) *ipInfo { } } +func (i *ipInfo) String() string { + return string(IPInfo) +} + +func (i *ipInfo) CanFetchAnyIP() bool { + return true +} + // 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. diff --git a/internal/publicip/interfaces.go b/internal/publicip/interfaces.go index db14f1f5b..f0c0d4a98 100644 --- a/internal/publicip/interfaces.go +++ b/internal/publicip/interfaces.go @@ -8,6 +8,7 @@ import ( ) type Fetcher interface { + String() string FetchInfo(ctx context.Context, ip netip.Addr) ( result models.PublicIP, err error) }