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