chore(updater): common GetServers signature
- Log warnings when running outside of CLI mode - Remove updater CLI bool setting - Warnings are logged in updating functions
This commit is contained in:
@@ -11,21 +11,18 @@ import (
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/gluetun/internal/updater/openvpn"
|
||||
"github.com/qdm12/gluetun/internal/updater/resolver"
|
||||
"github.com/qdm12/gluetun/internal/updater/unzip"
|
||||
)
|
||||
|
||||
var ErrNotEnoughServers = errors.New("not enough servers found")
|
||||
|
||||
func GetServers(ctx context.Context, unzipper unzip.Unzipper,
|
||||
presolver resolver.Parallel, minServers int) (
|
||||
servers []models.Server, warnings []string, err error) {
|
||||
func (u *Updater) GetServers(ctx context.Context, minServers int) (
|
||||
servers []models.Server, err error) {
|
||||
const url = "https://privatevpn.com/client/PrivateVPN-TUN.zip"
|
||||
contents, err := unzipper.FetchAndExtract(ctx, url)
|
||||
contents, err := u.unzipper.FetchAndExtract(ctx, url)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
} else if len(contents) < minServers {
|
||||
return nil, nil, fmt.Errorf("%w: %d and expected at least %d",
|
||||
return nil, fmt.Errorf("%w: %d and expected at least %d",
|
||||
ErrNotEnoughServers, len(contents), minServers)
|
||||
}
|
||||
|
||||
@@ -41,18 +38,19 @@ func GetServers(ctx context.Context, unzipper unzip.Unzipper,
|
||||
|
||||
countryCode, city, err := parseFilename(fileName)
|
||||
if err != nil {
|
||||
warnings = append(warnings, err.Error())
|
||||
// treat error as warning and go to next file
|
||||
u.warner.Warn(err.Error() + " in " + fileName)
|
||||
continue
|
||||
}
|
||||
|
||||
country, warning := codeToCountry(countryCode, countryCodes)
|
||||
if warning != "" {
|
||||
warnings = append(warnings, warning)
|
||||
u.warner.Warn(warning)
|
||||
}
|
||||
|
||||
host, warning, err := openvpn.ExtractHost(content)
|
||||
if warning != "" {
|
||||
warnings = append(warnings, warning)
|
||||
u.warner.Warn(warning)
|
||||
}
|
||||
if err == nil { // found host
|
||||
hts.add(host, country, city)
|
||||
@@ -61,12 +59,11 @@ func GetServers(ctx context.Context, unzipper unzip.Unzipper,
|
||||
|
||||
ips, extractIPErr := openvpn.ExtractIPs(content)
|
||||
if warning != "" {
|
||||
warnings = append(warnings, warning)
|
||||
u.warner.Warn(warning)
|
||||
}
|
||||
if extractIPErr != nil {
|
||||
// treat extract host error as warning and go to next file
|
||||
warning := err.Error() + " in " + fileName
|
||||
warnings = append(warnings, warning)
|
||||
u.warner.Warn(extractIPErr.Error() + " in " + fileName)
|
||||
continue
|
||||
}
|
||||
server := models.Server{
|
||||
@@ -80,16 +77,18 @@ func GetServers(ctx context.Context, unzipper unzip.Unzipper,
|
||||
}
|
||||
|
||||
if len(noHostnameServers)+len(hts) < minServers {
|
||||
return nil, warnings, fmt.Errorf("%w: %d and expected at least %d",
|
||||
return nil, fmt.Errorf("%w: %d and expected at least %d",
|
||||
ErrNotEnoughServers, len(servers)+len(hts), minServers)
|
||||
}
|
||||
|
||||
hosts := hts.toHostsSlice()
|
||||
|
||||
hostToIPs, newWarnings, err := resolveHosts(ctx, presolver, hosts, minServers)
|
||||
warnings = append(warnings, newWarnings...)
|
||||
hostToIPs, warnings, err := resolveHosts(ctx, u.presolver, hosts, minServers)
|
||||
for _, warning := range warnings {
|
||||
u.warner.Warn(warning)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, warnings, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hts.adaptWithIPs(hostToIPs)
|
||||
@@ -99,5 +98,5 @@ func GetServers(ctx context.Context, unzipper unzip.Unzipper,
|
||||
|
||||
sortServers(servers)
|
||||
|
||||
return servers, warnings, nil
|
||||
return servers, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user