chore(updater): check servers have minimal information
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants/vpn"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
@@ -31,6 +34,31 @@ type Server struct {
|
||||
IPs []net.IP `json:"ips,omitempty"`
|
||||
}
|
||||
|
||||
var (
|
||||
ErrVPNFieldEmpty = errors.New("vpn field is empty")
|
||||
ErrHostnameFieldEmpty = errors.New("hostname field is empty")
|
||||
ErrIPsFieldEmpty = errors.New("ips field is empty")
|
||||
ErrNoNetworkProtocol = errors.New("both TCP and UDP fields are false")
|
||||
ErrWireguardPublicKeyEmpty = errors.New("wireguard public key field is empty")
|
||||
)
|
||||
|
||||
func (s *Server) HasMinimumInformation() (err error) {
|
||||
switch {
|
||||
case s.VPN == "":
|
||||
return ErrVPNFieldEmpty
|
||||
case s.Hostname == "":
|
||||
return ErrHostnameFieldEmpty
|
||||
case len(s.IPs) == 0:
|
||||
return ErrIPsFieldEmpty
|
||||
case !s.TCP && !s.UDP:
|
||||
return ErrNoNetworkProtocol
|
||||
case s.VPN == vpn.Wireguard && s.WgPubKey == "":
|
||||
return ErrWireguardPublicKeyEmpty
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) Equal(other Server) (equal bool) {
|
||||
if !ipsAreEqual(s.IPs, other.IPs) {
|
||||
return false
|
||||
|
||||
@@ -2,6 +2,8 @@ package updater
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
@@ -12,6 +14,8 @@ type Provider interface {
|
||||
FetchServers(ctx context.Context, minServers int) (servers []models.Server, err error)
|
||||
}
|
||||
|
||||
var ErrServerHasNotEnoughInformation = errors.New("server has not enough information")
|
||||
|
||||
func (u *Updater) updateProvider(ctx context.Context, provider Provider,
|
||||
minRatio float64) (err error) {
|
||||
providerName := provider.Name()
|
||||
@@ -22,6 +26,17 @@ func (u *Updater) updateProvider(ctx context.Context, provider Provider,
|
||||
return fmt.Errorf("cannot get servers: %w", err)
|
||||
}
|
||||
|
||||
for _, server := range servers {
|
||||
err := server.HasMinimumInformation()
|
||||
if err != nil {
|
||||
serverJSON, err := json.Marshal(server)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return fmt.Errorf("server %s has not enough information: %w", serverJSON, err)
|
||||
}
|
||||
}
|
||||
|
||||
if u.storage.ServersAreEqual(providerName, servers) {
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user