chore(updater): check servers have minimal information

This commit is contained in:
Quentin McGaw
2022-07-02 00:12:01 +00:00
parent 6826b05d58
commit 6d2f9b9508
2 changed files with 43 additions and 0 deletions

View File

@@ -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