feat(publicip): PUBLICIP_API variable supporting ipinfo and ip2location
This commit is contained in:
97
internal/publicip/api/ip2location.go
Normal file
97
internal/publicip/api/ip2location.go
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user