Servers updater (#232)
* Support for all VPN providers * Update all VPN providers servers information * Remove old tooling binaries
This commit is contained in:
@@ -1,81 +1,112 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/gluetun/internal/storage"
|
||||
)
|
||||
|
||||
type Updater interface {
|
||||
UpdateServers(options Options) error
|
||||
UpdateServers(ctx context.Context) error
|
||||
}
|
||||
|
||||
type updater struct {
|
||||
// configuration
|
||||
options Options
|
||||
storage storage.Storage
|
||||
timeNow func() time.Time
|
||||
println func(s string)
|
||||
httpGet func(url string) (resp *http.Response, err error)
|
||||
|
||||
// state
|
||||
servers models.AllServers
|
||||
|
||||
// Functions for tests
|
||||
timeNow func() time.Time
|
||||
println func(s string)
|
||||
httpGet httpGetFunc
|
||||
lookupIP lookupIPFunc
|
||||
}
|
||||
|
||||
func New(storage storage.Storage, httpClient *http.Client) Updater {
|
||||
func New(options Options, storage storage.Storage, httpClient *http.Client) Updater {
|
||||
if len(options.DNSAddress) == 0 {
|
||||
options.DNSAddress = "1.1.1.1"
|
||||
}
|
||||
resolver := newResolver(options.DNSAddress)
|
||||
return &updater{
|
||||
storage: storage,
|
||||
timeNow: time.Now,
|
||||
println: func(s string) { fmt.Println(s) },
|
||||
httpGet: httpClient.Get,
|
||||
storage: storage,
|
||||
timeNow: time.Now,
|
||||
println: func(s string) { fmt.Println(s) },
|
||||
httpGet: httpClient.Get,
|
||||
lookupIP: newLookupIP(resolver),
|
||||
options: options,
|
||||
}
|
||||
}
|
||||
|
||||
func (u *updater) UpdateServers(options Options) error {
|
||||
// TODO parallelize DNS resolution
|
||||
func (u *updater) UpdateServers(ctx context.Context) (err error) {
|
||||
const writeSync = false
|
||||
allServers, err := u.storage.SyncServers(constants.GetAllServers(), writeSync)
|
||||
u.servers, err = u.storage.SyncServers(constants.GetAllServers(), writeSync)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot update servers: %w", err)
|
||||
}
|
||||
|
||||
if options.PIA {
|
||||
const newServers = true
|
||||
servers, err := findPIAServers(newServers)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot update PIA servers: %w", err)
|
||||
}
|
||||
if options.Stdout {
|
||||
u.println(stringifyPIAServers(servers))
|
||||
}
|
||||
allServers.Pia.Timestamp = u.timeNow().Unix()
|
||||
allServers.Pia.Servers = servers
|
||||
if u.options.Cyberghost {
|
||||
u.updateCyberghost(ctx)
|
||||
}
|
||||
|
||||
if options.PIAold {
|
||||
const newServers = false
|
||||
servers, err := findPIAServers(newServers)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot update PIA old servers: %w", err)
|
||||
if u.options.Mullvad {
|
||||
if err := u.updateMullvad(); err != nil {
|
||||
return err
|
||||
}
|
||||
if options.Stdout {
|
||||
u.println(stringifyPIAOldServers(servers))
|
||||
}
|
||||
allServers.PiaOld.Timestamp = u.timeNow().Unix()
|
||||
allServers.PiaOld.Servers = servers
|
||||
}
|
||||
|
||||
if options.Mullvad {
|
||||
servers, err := u.findMullvadServers()
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot update Mullvad servers: %w", err)
|
||||
if u.options.Nordvpn {
|
||||
// TODO support servers offering only TCP or only UDP
|
||||
if err := u.updateNordvpn(); err != nil {
|
||||
return err
|
||||
}
|
||||
if options.Stdout {
|
||||
u.println(stringifyMullvadServers(servers))
|
||||
}
|
||||
allServers.Mullvad.Timestamp = u.timeNow().Unix()
|
||||
allServers.Mullvad.Servers = servers
|
||||
}
|
||||
|
||||
if options.File {
|
||||
if err := u.storage.FlushToFile(allServers); err != nil {
|
||||
if u.options.PIA {
|
||||
if err := u.updatePIA(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if u.options.PIAold {
|
||||
if err := u.updatePIAOld(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if u.options.Purevpn {
|
||||
// TODO support servers offering only TCP or only UDP
|
||||
if err := u.updatePurevpn(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if u.options.Surfshark {
|
||||
if err := u.updateSurfshark(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if u.options.Vyprvpn {
|
||||
if err := u.updateVyprvpn(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if u.options.Windscribe {
|
||||
u.updateWindscribe(ctx)
|
||||
}
|
||||
|
||||
if u.options.File {
|
||||
if err := u.storage.FlushToFile(u.servers); err != nil {
|
||||
return fmt.Errorf("cannot update servers: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user