Skip to content

Commit

Permalink
feat(publicip): PUBLICIP_API variable supporting ipinfo and `ip2l…
Browse files Browse the repository at this point in the history
…ocation`
  • Loading branch information
qdm12 committed Feb 14, 2024
1 parent cfca026 commit 423a5c3
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 3 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ ENV VPN_SERVICE_PROVIDER=pia \
# Public IP
PUBLICIP_FILE="/tmp/gluetun/ip" \
PUBLICIP_PERIOD=12h \
PUBLICIP_API=ipinfo \
PUBLICIP_API_TOKEN= \
# Pprof
PPROF_ENABLED=no \
Expand Down
3 changes: 2 additions & 1 deletion cmd/gluetun/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ func _main(ctx context.Context, buildInfo models.BuildInformation,
go unboundLooper.RunRestartTicker(dnsTickerCtx, dnsTickerDone)
controlGroupHandler.Add(dnsTickerHandler)

ipFetcher, err := pubipapi.New(pubipapi.IPInfo, httpClient, *allSettings.PublicIP.APIToken)
publicipAPI, _ := pubipapi.ParseProvider(allSettings.PublicIP.API)
ipFetcher, err := pubipapi.New(publicipAPI, httpClient, *allSettings.PublicIP.APIToken)
if err != nil {
return fmt.Errorf("creating public IP API client: %w", err)
}
Expand Down
15 changes: 15 additions & 0 deletions internal/configuration/settings/publicip.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"
"time"

"github.com/qdm12/gluetun/internal/publicip/api"
"github.com/qdm12/gosettings"
"github.com/qdm12/gotree"
)
Expand All @@ -21,6 +22,9 @@ type PublicIP struct {
// to write to a file. It cannot be nil for the
// internal state
IPFilepath *string
// API is the API name to use to fetch public IP information.
// It can be ipinfo or ip2location. It defaults to ipinfo.
API string
// APIToken is the token to use for the IP data service
// such as ipinfo.io. It can be the empty string to
// indicate not to use a token. It cannot be nil for the
Expand Down Expand Up @@ -56,33 +60,42 @@ func (p PublicIP) validate() (err error) {
}
}

_, err = api.ParseProvider(p.API)
if err != nil {
return fmt.Errorf("API name: %w", err)
}

return nil
}

func (p *PublicIP) copy() (copied PublicIP) {
return PublicIP{
Period: gosettings.CopyPointer(p.Period),
IPFilepath: gosettings.CopyPointer(p.IPFilepath),
API: p.API,
APIToken: gosettings.CopyPointer(p.APIToken),
}
}

func (p *PublicIP) mergeWith(other PublicIP) {
p.Period = gosettings.MergeWithPointer(p.Period, other.Period)
p.IPFilepath = gosettings.MergeWithPointer(p.IPFilepath, other.IPFilepath)
p.API = gosettings.MergeWithString(p.API, other.API)
p.APIToken = gosettings.MergeWithPointer(p.APIToken, other.APIToken)
}

func (p *PublicIP) overrideWith(other PublicIP) {
p.Period = gosettings.OverrideWithPointer(p.Period, other.Period)
p.IPFilepath = gosettings.OverrideWithPointer(p.IPFilepath, other.IPFilepath)
p.API = gosettings.OverrideWithString(p.API, other.API)
p.APIToken = gosettings.OverrideWithPointer(p.APIToken, other.APIToken)
}

func (p *PublicIP) setDefaults() {
const defaultPeriod = 12 * time.Hour
p.Period = gosettings.DefaultPointer(p.Period, defaultPeriod)
p.IPFilepath = gosettings.DefaultPointer(p.IPFilepath, "/tmp/gluetun/ip")
p.API = gosettings.DefaultString(p.API, "ipinfo")
p.APIToken = gosettings.DefaultPointer(p.APIToken, "")
}

Expand All @@ -108,6 +121,8 @@ func (p PublicIP) toLinesNode() (node *gotree.Node) {
node.Appendf("IP file path: %s", *p.IPFilepath)
}

node.Appendf("Public IP data API: %s", p.API)

if *p.APIToken != "" {
node.Appendf("API token: %s", gosettings.ObfuscateKey(*p.APIToken))
}
Expand Down
3 changes: 2 additions & 1 deletion internal/configuration/settings/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ func Test_Settings_String(t *testing.T) {
| └── Process GID: 1000
├── Public IP settings:
| ├── Fetching: every 12h0m0s
| └── IP file path: /tmp/gluetun/ip
| ├── IP file path: /tmp/gluetun/ip
| └── Public IP data API: ipinfo
└── Version settings:
└── Enabled: yes`,
},
Expand Down
2 changes: 2 additions & 0 deletions internal/configuration/sources/env/publicip.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func (s *Source) readPublicIP() (publicIP settings.PublicIP, err error) {
publicIP.IPFilepath = s.env.Get("PUBLICIP_FILE",
env.ForceLowercase(false), env.RetroKeys("IP_STATUS_FILE"))

publicIP.API = s.env.String("PUBLICIP_API")

publicIP.APIToken = s.env.Get("PUBLICIP_API_TOKEN")

return publicIP, nil
Expand Down
7 changes: 6 additions & 1 deletion internal/publicip/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ type API interface {
type Provider string

const (
IPInfo Provider = "ipinfo"
IPInfo Provider = "ipinfo"
IP2Location Provider = "ip2location"
)

func New(provider Provider, client *http.Client, token string) ( //nolint:ireturn
a API, err error) {
switch provider {
case IPInfo:
return newIPInfo(client, token), nil
case IP2Location:
return newIP2Location(client, token), nil
default:
panic("provider not valid: " + provider)
}
Expand All @@ -40,6 +43,8 @@ func ParseProvider(s string) (provider Provider, err error) {
switch strings.ToLower(s) {
case "ipinfo":
return IPInfo, nil
case "ip2location":
return IP2Location, nil
default:
return "", fmt.Errorf(`%w: %q can only be "ipinfo" or "ip2location"`,
ErrProviderNotValid, s)
Expand Down
97 changes: 97 additions & 0 deletions internal/publicip/api/ip2location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package api

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/netip"
"strings"

"github.com/qdm12/gluetun/internal/models"
)

type ip2Location struct {
client *http.Client
token string
}

func newIP2Location(client *http.Client, token string) *ip2Location {
return &ip2Location{
client: client,
token: token,
}
}

// 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.
func (i *ip2Location) FetchInfo(ctx context.Context, ip netip.Addr) (
result models.PublicIP, err error) {
url := "https://api.ip2location.io/"
if ip.IsValid() {
url += "?ip=" + ip.String()
}

if i.token != "" {
if !strings.Contains(url, "?") {
url += "?"
}
url += "&key=" + i.token
}

request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return result, err
}

response, err := i.client.Do(request)
if err != nil {
return result, err
}
defer response.Body.Close()

if i.token != "" && response.StatusCode == http.StatusUnauthorized {
return result, fmt.Errorf("%w: %s", ErrTokenNotValid, response.Status)
}

switch response.StatusCode {
case http.StatusOK:
case http.StatusTooManyRequests, http.StatusForbidden:
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)
}

decoder := json.NewDecoder(response.Body)
var data struct {
IP netip.Addr `json:"ip,omitempty"`
CountryName string `json:"country_name,omitempty"`
RegionName string `json:"region_name,omitempty"`
CityName string `json:"city_name,omitempty"`
Latitude string `json:"latitude,omitempty"`
Longitude string `json:"longitude,omitempty"`
ZipCode string `json:"zip_code,omitempty"`
// Timezone in the form -07:00
Timezone string `json:"time_zone,omitempty"`
As string `json:"as,omitempty"`
}
if err := decoder.Decode(&data); err != nil {
return result, fmt.Errorf("decoding response: %w", err)
}

result = models.PublicIP{
IP: data.IP,
Region: data.RegionName,
Country: data.CountryName,
City: data.CityName,
Hostname: "", // no hostname
Location: fmt.Sprintf("%s,%s", data.Latitude, data.Longitude),
Organization: data.As,
PostalCode: data.ZipCode,
Timezone: data.Timezone,
}
return result, nil
}

0 comments on commit 423a5c3

Please sign in to comment.