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

@@ -3,66 +3,74 @@ package publicip
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"strings"
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/publicip/models"
)
type Fetch struct {
client *http.Client
randIntn func(n int) int
client *http.Client
}
func NewFetch(client *http.Client) *Fetch {
return &Fetch{
client: client,
randIntn: rand.Intn,
client: client,
}
}
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) {
urls := []string{
"https://ifconfig.me/ip",
"http://ip1.dynupdate.no-ip.com:8245",
"http://ip1.dynupdate.no-ip.com",
"https://api.ipify.org",
"https://domains.google.com/checkip",
"https://ifconfig.io/ip",
"https://ipinfo.io/ip",
}
url := urls[f.randIntn(len(urls))]
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
// FetchInfo obtains information on the ip address provided
// using the ipinfo.io API. If the ip is nil, the public IP address
// of the machine is used as the IP.
func (f *Fetch) FetchInfo(ctx context.Context, ip net.IP) (
result models.IPInfoData, err error) {
const baseURL = "https://ipinfo.io/"
url := baseURL
if ip != nil {
url += ip.String()
}
response, err := f.client.Do(req)
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, 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()
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%w from %s: %d %s", ErrBadStatusCode,
url, response.StatusCode, response.Status)
switch response.StatusCode {
case http.StatusOK:
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)
if err != nil {
return nil, fmt.Errorf("cannot ready response body: %w", err)
decoder := json.NewDecoder(response.Body)
if err := decoder.Decode(&result); err != nil {
return result, fmt.Errorf("cannot decode response: %w", err)
}
s := strings.ReplaceAll(string(content), "\n", "")
ip = net.ParseIP(s)
if ip == nil {
return nil, fmt.Errorf("%w: %s", ErrParseIP, s)
countryCode := strings.ToLower(result.Country)
country, ok := constants.CountryCodes()[countryCode]
if ok {
result.Country = country
}
return ip, nil
return result, nil
}

View File

@@ -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
}

View File

@@ -23,6 +23,7 @@ type stateManager interface {
SetSettings(ctx context.Context, settings settings.PublicIP) (outcome string)
}
type fetcher interface {
FetchPublicIP(ctx context.Context) (ip net.IP, err error)
type Fetcher interface {
FetchInfo(ctx context.Context, ip net.IP) (
result publicipmodels.IPInfoData, err error)
}

View File

@@ -1,7 +1,6 @@
package publicip
import (
"net/http"
"time"
"github.com/qdm12/gluetun/internal/configuration/settings"
@@ -15,8 +14,7 @@ type Loop struct {
statusManager statusManager
state stateManager
// Objects
fetcher fetcher
client *http.Client
fetcher Fetcher
logger Logger
// Fixed settings
puid int
@@ -35,7 +33,7 @@ type Loop struct {
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 {
start := make(chan struct{})
running := make(chan models.LoopStatus)
@@ -50,8 +48,7 @@ func NewLoop(client *http.Client, logger Logger,
statusManager: statusManager,
state: state,
// Objects
client: client,
fetcher: NewFetch(client),
fetcher: fetcher,
logger: logger,
puid: puid,
pgid: pgid,

View File

@@ -3,7 +3,7 @@ package models
import "net"
type IPInfoData struct {
IP net.IP `json:"public_ip"`
IP net.IP `json:"ip,omitempty"`
Region string `json:"region,omitempty"`
Country string `json:"country,omitempty"`
City string `json:"city,omitempty"`
@@ -20,8 +20,3 @@ func (i IPInfoData) Copy() (copied IPInfoData) {
copy(copied.IP, i.IP)
return copied
}
func (i *IPInfoData) SetIP(ip net.IP) {
i.IP = make(net.IP, len(ip))
copy(i.IP, ip)
}

View File

@@ -3,18 +3,17 @@ package publicip
import (
"context"
"net"
"net/http"
"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
// order as to the IP addresses slice order.
// If an error is encountered, all the operations are canceled and
// an error is returned, so the results returned should be considered
// 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) {
ctx, cancel := context.WithCancel(ctx)
@@ -30,7 +29,7 @@ func MultiInfo(ctx context.Context, client *http.Client, ips []net.IP) (
aResult := asyncResult{
index: index,
}
aResult.result, aResult.err = Info(ctx, client, ip)
aResult.result, aResult.err = f.FetchInfo(ctx, ip)
resultsCh <- aResult
}(i, ip)
}

View File

@@ -2,10 +2,10 @@ package publicip
import (
"context"
"net"
"os"
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/publicip/models"
)
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)
defer getCancel()
ipCh := make(chan net.IP)
resultCh := make(chan models.IPInfoData)
errorCh := make(chan error)
go func() {
ip, err := l.fetcher.FetchPublicIP(getCtx)
result, err := l.fetcher.FetchInfo(getCtx, nil)
if err != nil {
if getCtx.Err() == nil {
errorCh <- err
}
return
}
ipCh <- ip
resultCh <- result
}()
if l.userTrigger {
@@ -64,30 +64,24 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
getCancel()
<-errorCh
l.stopped <- struct{}{}
case ip := <-ipCh:
case result := <-resultCh:
getCancel()
message := "Public IP address is " + 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 := "Public IP address is " + result.IP.String()
message += " (" + result.Country + ", " + result.Region + ", " + result.City + ")"
l.logger.Info(message)
result.SetIP(ip)
l.state.SetData(result)
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 {
l.logger.Error(err.Error())
}
l.statusManager.SetStatus(constants.Completed)
case err := <-errorCh:
getCancel()
close(ipCh)
close(resultCh)
l.statusManager.SetStatus(constants.Crashed)
l.logAndWait(ctx, err)
stayHere = false