1
1
mirror of https://github.com/vx3r/wg-gen-web.git synced 2024-11-26 04:19:41 +01:00
wg-gen-web/model/server.go

74 lines
2.2 KiB
Go
Raw Normal View History

2020-01-30 07:45:49 +01:00
package model
import (
"fmt"
2021-04-06 14:23:23 +02:00
"github.com/vx3r/wg-gen-web/util"
"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 {
Address []string `json:"address"`
2020-01-30 08:12:26 +01:00
ListenPort int `json:"listenPort"`
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"`
Dns []string `json:"dns"`
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"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
}
// IsValid check if model is valid
func (a Server) IsValid() []error {
errs := make([]error, 0)
// check if the address empty
if len(a.Address) == 0 {
errs = append(errs, fmt.Errorf("address is required"))
}
// check if the address are valid
for _, address := range a.Address {
if !util.IsValidCidr(address) {
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))
}
// check if the mtu is valid
if a.Mtu < 0 {
errs = append(errs, fmt.Errorf("MTU %d is invalid", a.PersistentKeepalive))
}
// check if the address are valid
for _, dns := range a.Dns {
if !util.IsValidIp(dns) {
errs = append(errs, fmt.Errorf("dns %s is invalid", dns))
}
}
// 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))
}
}
return errs
2020-01-30 07:45:49 +01:00
}