2020-01-30 06:45:49 +00:00
|
|
|
package model
|
|
|
|
|
2020-02-06 04:30:36 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/util"
|
|
|
|
"time"
|
|
|
|
)
|
2020-01-30 06:45:49 +00:00
|
|
|
|
2020-01-30 07:17:29 +00:00
|
|
|
// Server structure
|
2020-01-30 06:45:49 +00:00
|
|
|
type Server struct {
|
2020-02-19 07:19:16 +00:00
|
|
|
Address []string `json:"address"`
|
2020-01-30 07:12:26 +00:00
|
|
|
ListenPort int `json:"listenPort"`
|
2020-02-19 07:19:16 +00:00
|
|
|
Mtu int `json:"mtu"`
|
2020-01-30 07:12:26 +00:00
|
|
|
PrivateKey string `json:"privateKey"`
|
|
|
|
PublicKey string `json:"publicKey"`
|
|
|
|
PresharedKey string `json:"presharedKey"`
|
|
|
|
Endpoint string `json:"endpoint"`
|
|
|
|
PersistentKeepalive int `json:"persistentKeepalive"`
|
2020-02-19 07:19:16 +00:00
|
|
|
Dns []string `json:"dns"`
|
2020-02-05 02:57:43 +00:00
|
|
|
PreUp string `json:"preUp"`
|
|
|
|
PostUp string `json:"postUp"`
|
|
|
|
PreDown string `json:"preDown"`
|
|
|
|
PostDown string `json:"postDown"`
|
2020-02-06 04:30:36 +00:00
|
|
|
Created time.Time `json:"created"`
|
|
|
|
Updated time.Time `json:"updated"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a Server) IsValid() []error {
|
|
|
|
errs := make([]error, 0)
|
|
|
|
|
|
|
|
// check if the address empty
|
2020-02-19 07:19:16 +00:00
|
|
|
if len(a.Address) == 0 {
|
2020-02-06 04:30:36 +00:00
|
|
|
errs = append(errs, fmt.Errorf("address is required"))
|
|
|
|
}
|
|
|
|
// check if the address are valid
|
2020-02-19 07:19:16 +00:00
|
|
|
for _, address := range a.Address {
|
|
|
|
if !util.IsValidCidr(address) {
|
2020-02-06 04:30:36 +00:00
|
|
|
errs = append(errs, fmt.Errorf("address %s is invalid", address))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// check if the listenPort is valid
|
|
|
|
if a.ListenPort < 0 || a.ListenPort > 65535 {
|
|
|
|
errs = append(errs, fmt.Errorf("listenPort %s is invalid", a.ListenPort))
|
|
|
|
}
|
|
|
|
// check if the endpoint empty
|
|
|
|
if a.Endpoint == "" {
|
|
|
|
errs = append(errs, fmt.Errorf("endpoint is required"))
|
|
|
|
}
|
|
|
|
// check if the persistentKeepalive is valid
|
|
|
|
if a.PersistentKeepalive < 0 {
|
|
|
|
errs = append(errs, fmt.Errorf("persistentKeepalive %d is invalid", a.PersistentKeepalive))
|
|
|
|
}
|
2020-02-19 07:19:16 +00:00
|
|
|
// check if the mtu is valid
|
|
|
|
if a.Mtu < 0 {
|
|
|
|
errs = append(errs, fmt.Errorf("MTU %d is invalid", a.PersistentKeepalive))
|
2020-02-06 04:30:36 +00:00
|
|
|
}
|
|
|
|
// check if the address are valid
|
2020-02-19 07:19:16 +00:00
|
|
|
for _, dns := range a.Dns {
|
|
|
|
if !util.IsValidIp(dns) {
|
2020-02-06 04:30:36 +00:00
|
|
|
errs = append(errs, fmt.Errorf("dns %s is invalid", dns))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs
|
2020-01-30 06:45:49 +00:00
|
|
|
}
|