0
0
mirror of https://github.com/vx3r/wg-gen-web.git synced 2025-09-11 12:24:27 +00:00

specify ip address / proper error validation in backend / refactor

This commit is contained in:
vx3r
2020-02-06 13:30:36 +09:00
parent 4edd5cb44e
commit b7c88b747b
8 changed files with 268 additions and 35 deletions

View File

@ -1,6 +1,11 @@
package model
import "time"
import (
"fmt"
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/util"
"strings"
"time"
)
// Client structure
type Client struct {
@ -8,10 +13,53 @@ type Client struct {
Name string `json:"name"`
Email string `json:"email"`
Enable bool `json:"enable"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
AllowedIPs string `json:"allowedIPs"`
Address string `json:"address"`
PrivateKey string `json:"privateKey"`
PublicKey string `json:"publicKey"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
}
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"))
}
// check if the email empty
if a.Email == "" {
errs = append(errs, fmt.Errorf("email field is required"))
}
// check if email valid
if !util.RegexpEmail.MatchString(a.Email) {
errs = append(errs, fmt.Errorf("email %s is invalid", a.Email))
}
// check if the allowedIPs empty
if a.AllowedIPs == "" {
errs = append(errs, fmt.Errorf("allowedIPs field is required"))
}
// check if the allowedIPs are valid
for _, allowedIP := range strings.Split(a.AllowedIPs, ",") {
if !util.IsValidCidr(strings.TrimSpace(allowedIP)) {
errs = append(errs, fmt.Errorf("allowedIP %s is invalid", allowedIP))
}
}
// check if the address empty
if a.Address == "" {
errs = append(errs, fmt.Errorf("address field is required"))
}
// check if the address are valid
for _, address := range strings.Split(a.Address, ",") {
if !util.IsValidCidr(strings.TrimSpace(address)) {
errs = append(errs, fmt.Errorf("address %s is invalid", address))
}
}
return errs
}

View File

@ -1,12 +1,15 @@
package model
import "time"
import (
"fmt"
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/util"
"strings"
"time"
)
// Server structure
type Server struct {
Name string `json:"name"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
Address string `json:"address"`
ListenPort int `json:"listenPort"`
PrivateKey string `json:"privateKey"`
@ -19,4 +22,53 @@ type Server struct {
PostUp string `json:"postUp"`
PreDown string `json:"preDown"`
PostDown string `json:"postDown"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
}
func (a Server) 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 must be between 2-40 chars"))
}
// check if the address empty
if a.Address == "" {
errs = append(errs, fmt.Errorf("address is required"))
}
// check if the address are valid
for _, address := range strings.Split(a.Address, ",") {
if !util.IsValidCidr(strings.TrimSpace(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 dns empty
if a.Dns == "" {
errs = append(errs, fmt.Errorf("dns is required"))
}
// check if the address are valid
for _, dns := range strings.Split(a.Dns, ",") {
if !util.IsValidIp(strings.TrimSpace(dns)) {
errs = append(errs, fmt.Errorf("dns %s is invalid", dns))
}
}
return errs
}