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

refactor backend and frontend, migrate structs, organize front view, add mtu

This commit is contained in:
vx3r
2020-02-19 16:19:16 +09:00
parent 6cafb97e31
commit 0aad10cb63
19 changed files with 1085 additions and 735 deletions

View File

@ -3,7 +3,6 @@ package model
import (
"fmt"
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/util"
"strings"
"time"
)
@ -13,8 +12,8 @@ type Client struct {
Name string `json:"name"`
Email string `json:"email"`
Enable bool `json:"enable"`
AllowedIPs string `json:"allowedIPs"`
Address string `json:"address"`
AllowedIPs []string `json:"allowedIPs"`
Address []string `json:"address"`
PrivateKey string `json:"privateKey"`
PublicKey string `json:"publicKey"`
Created time.Time `json:"created"`
@ -41,22 +40,22 @@ func (a Client) IsValid() []error {
errs = append(errs, fmt.Errorf("email %s is invalid", a.Email))
}
// check if the allowedIPs empty
if a.AllowedIPs == "" {
if len(a.AllowedIPs) == 0 {
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)) {
for _, allowedIP := range a.AllowedIPs {
if !util.IsValidCidr(allowedIP) {
errs = append(errs, fmt.Errorf("allowedIP %s is invalid", allowedIP))
}
}
// check if the address empty
if a.Address == "" {
if len(a.Address) == 0 {
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)) {
for _, address := range a.Address {
if !util.IsValidCidr(address) {
errs = append(errs, fmt.Errorf("address %s is invalid", address))
}
}

View File

@ -3,21 +3,20 @@ package model
import (
"fmt"
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/util"
"strings"
"time"
)
// Server structure
type Server struct {
Name string `json:"name"`
Address string `json:"address"`
Address []string `json:"address"`
ListenPort int `json:"listenPort"`
Mtu int `json:"mtu"`
PrivateKey string `json:"privateKey"`
PublicKey string `json:"publicKey"`
PresharedKey string `json:"presharedKey"`
Endpoint string `json:"endpoint"`
PersistentKeepalive int `json:"persistentKeepalive"`
Dns string `json:"dns"`
Dns []string `json:"dns"`
PreUp string `json:"preUp"`
PostUp string `json:"postUp"`
PreDown string `json:"preDown"`
@ -29,21 +28,13 @@ type Server struct {
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 == "" {
if len(a.Address) == 0 {
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)) {
for _, address := range a.Address {
if !util.IsValidCidr(address) {
errs = append(errs, fmt.Errorf("address %s is invalid", address))
}
}
@ -59,13 +50,13 @@ func (a Server) IsValid() []error {
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 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 strings.Split(a.Dns, ",") {
if !util.IsValidIp(strings.TrimSpace(dns)) {
for _, dns := range a.Dns {
if !util.IsValidIp(dns) {
errs = append(errs, fmt.Errorf("dns %s is invalid", dns))
}
}