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
|
|
|
// Client structure
|
2020-01-30 07:45:49 +01:00
|
|
|
type Client struct {
|
2020-02-25 07:56:04 +01:00
|
|
|
Id string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
Enable bool `json:"enable"`
|
|
|
|
IgnorePersistentKeepalive bool `json:"ignorePersistentKeepalive"`
|
2020-03-18 07:57:15 +01:00
|
|
|
PresharedKey string `json:"presharedKey"`
|
2020-02-25 07:56:04 +01:00
|
|
|
AllowedIPs []string `json:"allowedIPs"`
|
|
|
|
Address []string `json:"address"`
|
2020-06-10 09:44:15 +02:00
|
|
|
Tags []string `json:"tags"`
|
2020-02-25 07:56:04 +01:00
|
|
|
PrivateKey string `json:"privateKey"`
|
|
|
|
PublicKey string `json:"publicKey"`
|
2020-04-28 13:11:49 +02:00
|
|
|
CreatedBy string `json:"createdBy"`
|
|
|
|
UpdatedBy string `json:"updatedBy"`
|
2020-02-25 07:56:04 +01:00
|
|
|
Created time.Time `json:"created"`
|
|
|
|
Updated time.Time `json:"updated"`
|
2020-02-06 05:30:36 +01:00
|
|
|
}
|
|
|
|
|
2020-04-19 07:13:44 +02:00
|
|
|
// IsValid check if model is valid
|
2020-02-06 05:30:36 +01:00
|
|
|
func (a Client) IsValid() []error {
|
|
|
|
errs := make([]error, 0)
|
|
|
|
|
|
|
|
// check if the name empty
|
|
|
|
if a.Name == "" {
|
|
|
|
errs = append(errs, fmt.Errorf("name is required"))
|
|
|
|
}
|
|
|
|
// check the name field is between 3 to 40 chars
|
|
|
|
if len(a.Name) < 2 || len(a.Name) > 40 {
|
|
|
|
errs = append(errs, fmt.Errorf("name field must be between 2-40 chars"))
|
|
|
|
}
|
2020-02-25 07:56:04 +01:00
|
|
|
// email is not required, but if provided must match regex
|
|
|
|
if a.Email != "" {
|
|
|
|
if !util.RegexpEmail.MatchString(a.Email) {
|
|
|
|
errs = append(errs, fmt.Errorf("email %s is invalid", a.Email))
|
|
|
|
}
|
2020-02-06 05:30:36 +01:00
|
|
|
}
|
|
|
|
// check if the allowedIPs empty
|
2020-02-19 08:19:16 +01:00
|
|
|
if len(a.AllowedIPs) == 0 {
|
2020-02-06 05:30:36 +01:00
|
|
|
errs = append(errs, fmt.Errorf("allowedIPs field is required"))
|
|
|
|
}
|
|
|
|
// check if the allowedIPs are valid
|
2020-02-19 08:19:16 +01:00
|
|
|
for _, allowedIP := range a.AllowedIPs {
|
|
|
|
if !util.IsValidCidr(allowedIP) {
|
2020-02-06 05:30:36 +01:00
|
|
|
errs = append(errs, fmt.Errorf("allowedIP %s is invalid", allowedIP))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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 field 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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs
|
2020-01-30 07:45:49 +01:00
|
|
|
}
|