chore(publicip): internal/publicip/ipinfo package
This commit is contained in:
@@ -33,6 +33,7 @@ import (
|
||||
"github.com/qdm12/gluetun/internal/pprof"
|
||||
"github.com/qdm12/gluetun/internal/provider"
|
||||
"github.com/qdm12/gluetun/internal/publicip"
|
||||
"github.com/qdm12/gluetun/internal/publicip/ipinfo"
|
||||
"github.com/qdm12/gluetun/internal/routing"
|
||||
"github.com/qdm12/gluetun/internal/server"
|
||||
"github.com/qdm12/gluetun/internal/shadowsocks"
|
||||
@@ -364,7 +365,7 @@ func _main(ctx context.Context, buildInfo models.BuildInformation,
|
||||
go unboundLooper.RunRestartTicker(dnsTickerCtx, dnsTickerDone)
|
||||
controlGroupHandler.Add(dnsTickerHandler)
|
||||
|
||||
ipFetcher := publicip.NewFetch(httpClient)
|
||||
ipFetcher := ipinfo.New(httpClient)
|
||||
publicIPLooper := publicip.NewLoop(ipFetcher,
|
||||
logger.New(log.SetComponent("ip getter")),
|
||||
allSettings.PublicIP, puid, pgid)
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"github.com/qdm12/gluetun/internal/configuration/sources"
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"github.com/qdm12/gluetun/internal/provider"
|
||||
publicipmodels "github.com/qdm12/gluetun/internal/publicip/models"
|
||||
"github.com/qdm12/gluetun/internal/publicip/ipinfo"
|
||||
"github.com/qdm12/gluetun/internal/storage"
|
||||
"github.com/qdm12/gluetun/internal/updater/resolver"
|
||||
)
|
||||
@@ -32,7 +32,7 @@ type ParallelResolver interface {
|
||||
}
|
||||
|
||||
type IPFetcher interface {
|
||||
FetchMultiInfo(ctx context.Context, ips []net.IP) (data []publicipmodels.IPInfoData, err error)
|
||||
FetchMultiInfo(ctx context.Context, ips []net.IP) (data []ipinfo.Response, err error)
|
||||
}
|
||||
|
||||
func (c *CLI) OpenvpnConfig(logger OpenvpnConfigLogger, source sources.Source) error {
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"github.com/qdm12/gluetun/internal/constants/providers"
|
||||
"github.com/qdm12/gluetun/internal/provider"
|
||||
"github.com/qdm12/gluetun/internal/publicip"
|
||||
"github.com/qdm12/gluetun/internal/publicip/ipinfo"
|
||||
"github.com/qdm12/gluetun/internal/storage"
|
||||
"github.com/qdm12/gluetun/internal/updater"
|
||||
"github.com/qdm12/gluetun/internal/updater/resolver"
|
||||
@@ -75,7 +75,7 @@ func (c *CLI) Update(ctx context.Context, args []string, logger UpdaterLogger) e
|
||||
httpClient := &http.Client{Timeout: clientTimeout}
|
||||
unzipper := unzip.New(httpClient)
|
||||
parallelResolver := resolver.NewParallelResolver(options.DNSAddress)
|
||||
ipFetcher := publicip.NewFetch(httpClient)
|
||||
ipFetcher := ipinfo.New(httpClient)
|
||||
|
||||
providers := provider.NewProviders(storage, time.Now, logger, httpClient,
|
||||
unzipper, parallelResolver, ipFetcher)
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"net"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
publicipmodels "github.com/qdm12/gluetun/internal/publicip/models"
|
||||
"github.com/qdm12/gluetun/internal/publicip/ipinfo"
|
||||
"github.com/qdm12/gluetun/internal/updater/resolver"
|
||||
)
|
||||
|
||||
@@ -31,5 +31,5 @@ type Warner interface {
|
||||
}
|
||||
|
||||
type IPFetcher interface {
|
||||
FetchMultiInfo(ctx context.Context, ips []net.IP) (data []publicipmodels.IPInfoData, err error)
|
||||
FetchMultiInfo(ctx context.Context, ips []net.IP) (data []ipinfo.Response, err error)
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
|
||||
"github.com/qdm12/gluetun/internal/configuration/settings"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
publicipmodels "github.com/qdm12/gluetun/internal/publicip/models"
|
||||
"github.com/qdm12/gluetun/internal/publicip/ipinfo"
|
||||
)
|
||||
|
||||
type statusManager interface {
|
||||
@@ -17,13 +17,13 @@ type statusManager interface {
|
||||
}
|
||||
|
||||
type stateManager interface {
|
||||
GetData() (data publicipmodels.IPInfoData)
|
||||
SetData(data publicipmodels.IPInfoData)
|
||||
GetData() (data ipinfo.Response)
|
||||
SetData(data ipinfo.Response)
|
||||
GetSettings() (settings settings.PublicIP)
|
||||
SetSettings(ctx context.Context, settings settings.PublicIP) (outcome string)
|
||||
}
|
||||
|
||||
type Fetcher interface {
|
||||
FetchInfo(ctx context.Context, ip net.IP) (
|
||||
result publicipmodels.IPInfoData, err error)
|
||||
result ipinfo.Response, err error)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
// Package publicip defines interfaces to get your public IP address.
|
||||
package publicip
|
||||
package ipinfo
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -11,14 +10,13 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"github.com/qdm12/gluetun/internal/publicip/models"
|
||||
)
|
||||
|
||||
type Fetch struct {
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func NewFetch(client *http.Client) *Fetch {
|
||||
func New(client *http.Client) *Fetch {
|
||||
return &Fetch{
|
||||
client: client,
|
||||
}
|
||||
@@ -33,7 +31,7 @@ var (
|
||||
// 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) {
|
||||
result Response, err error) {
|
||||
const baseURL = "https://ipinfo.io/"
|
||||
url := baseURL
|
||||
if ip != nil {
|
||||
@@ -1,8 +1,8 @@
|
||||
package models
|
||||
package ipinfo
|
||||
|
||||
import "net"
|
||||
|
||||
type IPInfoData struct {
|
||||
type Response struct {
|
||||
IP net.IP `json:"ip,omitempty"`
|
||||
Region string `json:"region,omitempty"`
|
||||
Country string `json:"country,omitempty"`
|
||||
@@ -14,9 +14,9 @@ type IPInfoData struct {
|
||||
Timezone string `json:"timezone,omitempty"`
|
||||
}
|
||||
|
||||
func (i IPInfoData) Copy() (copied IPInfoData) {
|
||||
copied = i
|
||||
copied.IP = make(net.IP, len(i.IP))
|
||||
copy(copied.IP, i.IP)
|
||||
func (r Response) Copy() (copied Response) {
|
||||
copied = r
|
||||
copied.IP = make(net.IP, len(r.IP))
|
||||
copy(copied.IP, r.IP)
|
||||
return copied
|
||||
}
|
||||
@@ -1,10 +1,8 @@
|
||||
package publicip
|
||||
package ipinfo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/publicip/models"
|
||||
)
|
||||
|
||||
// FetchMultiInfo obtains the public IP address information for every IP
|
||||
@@ -14,12 +12,12 @@ import (
|
||||
// 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 []models.IPInfoData, err error) {
|
||||
results []Response, err error) {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
|
||||
type asyncResult struct {
|
||||
index int
|
||||
result models.IPInfoData
|
||||
result Response
|
||||
err error
|
||||
}
|
||||
resultsCh := make(chan asyncResult)
|
||||
@@ -34,7 +32,7 @@ func (f *Fetch) FetchMultiInfo(ctx context.Context, ips []net.IP) (
|
||||
}(i, ip)
|
||||
}
|
||||
|
||||
results = make([]models.IPInfoData, len(ips))
|
||||
results = make([]Response, len(ips))
|
||||
for i := 0; i < len(ips); i++ {
|
||||
aResult := <-resultsCh
|
||||
if aResult.err != nil {
|
||||
@@ -1,13 +1,11 @@
|
||||
package publicip
|
||||
|
||||
import (
|
||||
"github.com/qdm12/gluetun/internal/publicip/models"
|
||||
)
|
||||
import "github.com/qdm12/gluetun/internal/publicip/ipinfo"
|
||||
|
||||
func (l *Loop) GetData() (data models.IPInfoData) {
|
||||
func (l *Loop) GetData() (data ipinfo.Response) {
|
||||
return l.state.GetData()
|
||||
}
|
||||
|
||||
func (l *Loop) SetData(data models.IPInfoData) {
|
||||
func (l *Loop) SetData(data ipinfo.Response) {
|
||||
l.state.SetData(data)
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"github.com/qdm12/gluetun/internal/publicip/models"
|
||||
"github.com/qdm12/gluetun/internal/publicip/ipinfo"
|
||||
)
|
||||
|
||||
func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
|
||||
@@ -21,7 +21,7 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
|
||||
getCtx, getCancel := context.WithCancel(ctx)
|
||||
defer getCancel()
|
||||
|
||||
resultCh := make(chan models.IPInfoData)
|
||||
resultCh := make(chan ipinfo.Response)
|
||||
errorCh := make(chan error)
|
||||
go func() {
|
||||
result, err := l.fetcher.FetchInfo(getCtx, nil)
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"github.com/qdm12/gluetun/internal/publicip/models"
|
||||
"github.com/qdm12/gluetun/internal/publicip/ipinfo"
|
||||
)
|
||||
|
||||
func (s *State) GetData() (data models.IPInfoData) {
|
||||
func (s *State) GetData() (data ipinfo.Response) {
|
||||
s.ipDataMu.RLock()
|
||||
defer s.ipDataMu.RUnlock()
|
||||
return s.ipData.Copy()
|
||||
}
|
||||
|
||||
func (s *State) SetData(data models.IPInfoData) {
|
||||
func (s *State) SetData(data ipinfo.Response) {
|
||||
s.ipDataMu.Lock()
|
||||
defer s.ipDataMu.Unlock()
|
||||
s.ipData = data.Copy()
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
|
||||
"github.com/qdm12/gluetun/internal/configuration/settings"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
publicipmodels "github.com/qdm12/gluetun/internal/publicip/models"
|
||||
"github.com/qdm12/gluetun/internal/publicip/ipinfo"
|
||||
)
|
||||
|
||||
func New(statusApplier StatusApplier,
|
||||
@@ -25,7 +25,7 @@ type State struct {
|
||||
settings settings.PublicIP
|
||||
settingsMu sync.RWMutex
|
||||
|
||||
ipData publicipmodels.IPInfoData
|
||||
ipData ipinfo.Response
|
||||
ipDataMu sync.RWMutex
|
||||
|
||||
updateTicker chan<- struct{}
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
|
||||
"github.com/qdm12/gluetun/internal/configuration/settings"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
publicipmodels "github.com/qdm12/gluetun/internal/publicip/models"
|
||||
"github.com/qdm12/gluetun/internal/publicip/ipinfo"
|
||||
)
|
||||
|
||||
type VPNLooper interface {
|
||||
@@ -26,5 +26,5 @@ type PortForwardedGetter interface {
|
||||
}
|
||||
|
||||
type PublicIPLoop interface {
|
||||
GetData() (data publicipmodels.IPInfoData)
|
||||
GetData() (data ipinfo.Response)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/publicip/models"
|
||||
"github.com/qdm12/gluetun/internal/publicip/ipinfo"
|
||||
)
|
||||
|
||||
func (l *Loop) cleanup(ctx context.Context, pfEnabled bool) {
|
||||
@@ -15,7 +15,7 @@ func (l *Loop) cleanup(ctx context.Context, pfEnabled bool) {
|
||||
}
|
||||
}
|
||||
|
||||
l.publicip.SetData(models.IPInfoData{}) // clear public IP address data
|
||||
l.publicip.SetData(ipinfo.Response{}) // clear public IP address data
|
||||
|
||||
if pfEnabled {
|
||||
const pfTimeout = 100 * time.Millisecond
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"github.com/qdm12/gluetun/internal/netlink"
|
||||
"github.com/qdm12/gluetun/internal/portforward"
|
||||
"github.com/qdm12/gluetun/internal/provider"
|
||||
publicipmodels "github.com/qdm12/gluetun/internal/publicip/models"
|
||||
"github.com/qdm12/gluetun/internal/publicip/ipinfo"
|
||||
)
|
||||
|
||||
type Firewall interface {
|
||||
@@ -71,7 +71,7 @@ type DNSLoop interface {
|
||||
type PublicIPLoop interface {
|
||||
ApplyStatus(ctx context.Context, status models.LoopStatus) (
|
||||
outcome string, err error)
|
||||
SetData(data publicipmodels.IPInfoData)
|
||||
SetData(data ipinfo.Response)
|
||||
}
|
||||
|
||||
type statusManager interface {
|
||||
|
||||
Reference in New Issue
Block a user