Skip to content

Commit

Permalink
hotfix(publicip): return an error if trying to use cloudflare as ip p…
Browse files Browse the repository at this point in the history
…rovider for updating servers data
  • Loading branch information
qdm12 committed Oct 11, 2024
1 parent a7d70dd commit 2388e05
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 2 deletions.
2 changes: 2 additions & 0 deletions internal/cli/openvpnconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
7 changes: 5 additions & 2 deletions internal/provider/common/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
4 changes: 4 additions & 0 deletions internal/provider/privado/updater/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions internal/provider/purevpn/updater/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions internal/publicip/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
)

type API interface {
String() string
CanFetchAnyIP() bool
FetchInfo(ctx context.Context, ip netip.Addr) (
result models.PublicIP, err error)
}
Expand Down
8 changes: 8 additions & 0 deletions internal/publicip/api/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions internal/publicip/api/ifconfigco.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions internal/publicip/api/ip2location.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions internal/publicip/api/ipinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions internal/publicip/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

type Fetcher interface {
String() string
FetchInfo(ctx context.Context, ip netip.Addr) (
result models.PublicIP, err error)
}
Expand Down

0 comments on commit 2388e05

Please sign in to comment.