chore(publicip): internal/publicip/ipinfo package

This commit is contained in:
Quentin McGaw
2022-06-12 00:53:39 +00:00
parent 83b4a3fe55
commit 89277828ac
15 changed files with 41 additions and 46 deletions

View File

@@ -0,0 +1,74 @@
package ipinfo
import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"strings"
"github.com/qdm12/gluetun/internal/constants"
)
type Fetch struct {
client *http.Client
}
func New(client *http.Client) *Fetch {
return &Fetch{
client: client,
}
}
var (
ErrTooManyRequests = errors.New("too many requests sent for this month")
ErrBadHTTPStatus = errors.New("bad HTTP status received")
)
// 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 Response, err error) {
const baseURL = "https://ipinfo.io/"
url := baseURL
if ip != nil {
url += ip.String()
}
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return result, err
}
response, err := f.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 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)
if err := decoder.Decode(&result); err != nil {
return result, fmt.Errorf("cannot decode response: %w", err)
}
countryCode := strings.ToLower(result.Country)
country, ok := constants.CountryCodes()[countryCode]
if ok {
result.Country = country
}
return result, nil
}

View File

@@ -0,0 +1,22 @@
package ipinfo
import "net"
type Response struct {
IP net.IP `json:"ip,omitempty"`
Region string `json:"region,omitempty"`
Country string `json:"country,omitempty"`
City string `json:"city,omitempty"`
Hostname string `json:"hostname,omitempty"`
Loc string `json:"loc,omitempty"`
Org string `json:"org,omitempty"`
Postal string `json:"postal,omitempty"`
Timezone string `json:"timezone,omitempty"`
}
func (r Response) Copy() (copied Response) {
copied = r
copied.IP = make(net.IP, len(r.IP))
copy(copied.IP, r.IP)
return copied
}

View File

@@ -0,0 +1,54 @@
package ipinfo
import (
"context"
"net"
)
// 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 (f *Fetch) FetchMultiInfo(ctx context.Context, ips []net.IP) (
results []Response, err error) {
ctx, cancel := context.WithCancel(ctx)
type asyncResult struct {
index int
result Response
err error
}
resultsCh := make(chan asyncResult)
for i, ip := range ips {
go func(index int, ip net.IP) {
aResult := asyncResult{
index: index,
}
aResult.result, aResult.err = f.FetchInfo(ctx, ip)
resultsCh <- aResult
}(i, ip)
}
results = make([]Response, len(ips))
for i := 0; i < len(ips); i++ {
aResult := <-resultsCh
if aResult.err != nil {
if err == nil {
// Cancel on the first error encountered
err = aResult.err
cancel()
}
continue // ignore errors after the first one
}
results[aResult.index] = aResult.result
}
close(resultsCh)
cancel()
return results, err
}