2020-01-30 07:45:49 +01:00
|
|
|
package model
|
|
|
|
|
2020-02-06 05:30:36 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
2021-04-06 14:23:23 +02:00
|
|
|
"github.com/vx3r/wg-gen-web/util"
|
2020-02-06 05:30:36 +01:00
|
|
|
"time"
|
|
|
|
)
|
2020-01-30 07:45:49 +01:00
|
|
|
|
2020-01-30 08:17:29 +01:00
|
|
|
// Server structure
|
2020-01-30 07:45:49 +01:00
|
|
|
type Server struct {
|
2020-02-19 08:19:16 +01:00
|
|
|
Address []string `json:"address"`
|
2020-01-30 08:12:26 +01:00
|
|
|
ListenPort int `json:"listenPort"`
|
2020-02-19 08:19:16 +01:00
|
|
|
Mtu int `json:"mtu"`
|
2020-01-30 08:12:26 +01:00
|
|
|
PrivateKey string `json:"privateKey"`
|
|
|
|
PublicKey string `json:"publicKey"`
|
|
|
|
Endpoint string `json:"endpoint"`
|
|
|
|
PersistentKeepalive int `json:"persistentKeepalive"`
|
2020-02-19 08:19:16 +01:00
|
|
|
Dns []string `json:"dns"`
|
2020-04-14 18:47:56 +02:00
|
|
|
AllowedIPs []string `json:"allowedips"`
|
2020-02-05 03:57:43 +01:00
|
|
|
PreUp string `json:"preUp"`
|
|
|
|
PostUp string `json:"postUp"`
|
|
|
|
PreDown string `json:"preDown"`
|
|
|
|
PostDown string `json:"postDown"`
|
2020-04-28 13:11:49 +02:00
|
|
|
UpdatedBy string `json:"updatedBy"`
|
2020-02-06 05:30:36 +01:00
|
|
|
Created time.Time `json:"created"`
|
|
|
|
Updated time.Time `json:"updated"`
|
|
|
|
}
|
|
|
|
|
2020-04-19 07:13:44 +02:00
|
|
|
// IsValid check if model is valid
|
2020-02-06 05:30:36 +01:00
|
|
|
func (a Server) IsValid() []error {
|
|
|
|
errs := make([]error, 0)
|
|
|
|
|
|
|
|
// check if the address empty
|
2020-02-19 08:19:16 +01:00
|
|
|
if len(a.Address) == 0 {
|
2020-02-06 05:30:36 +01:00
|
|
|
errs = append(errs, fmt.Errorf("address is required"))
|
|
|
|
}
|
|
|
|
// check if the address are valid
|
2020-02-19 08:19:16 +01:00
|
|
|
for _, address := range a.Address {
|
|
|
|
if !util.IsValidCidr(address) {
|
2020-02-06 05:30:36 +01: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 08:19:16 +01: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 05:30:36 +01:00
|
|
|
}
|
|
|
|
// check if the address are valid
|
2020-02-19 08:19:16 +01:00
|
|
|
for _, dns := range a.Dns {
|
|
|
|
if !util.IsValidIp(dns) {
|
2020-02-06 05:30:36 +01:00
|
|
|
errs = append(errs, fmt.Errorf("dns %s is invalid", dns))
|
|
|
|
}
|
|
|
|
}
|
2020-04-14 18:47:56 +02:00
|
|
|
// check if the allowedIPs are valid
|
|
|
|
for _, allowedIP := range a.AllowedIPs {
|
|
|
|
if !util.IsValidCidr(allowedIP) {
|
|
|
|
errs = append(errs, fmt.Errorf("allowedIP %s is invalid", allowedIP))
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 05:30:36 +01:00
|
|
|
|
|
|
|
return errs
|
2020-01-30 07:45:49 +01:00
|
|
|
}
|