first commit

This commit is contained in:
yann
2025-11-01 17:25:35 +01:00
commit 155791d05a
85 changed files with 3776 additions and 0 deletions

64
model/client.go Normal file
View File

@@ -0,0 +1,64 @@
package model
import (
"fmt"
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/util"
"time"
)
// Client structure
type Client struct {
Id string `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Enable bool `json:"enable"`
IgnorePersistentKeepalive bool `json:"ignorePersistentKeepalive"`
PresharedKey string `json:"presharedKey"`
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 est requis"))
}
// 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"))
}
// 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))
}
}
// check if the allowedIPs empty
if len(a.AllowedIPs) == 0 {
errs = append(errs, fmt.Errorf("allowedIPs field est requis"))
}
// 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))
}
}
// check if the address empty
if len(a.Address) == 0 {
errs = append(errs, fmt.Errorf("address field est requis"))
}
// 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))
}
}
return errs
}

64
model/server.go Normal file
View File

@@ -0,0 +1,64 @@
package model
import (
"fmt"
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/util"
"time"
)
// Server structure
type Server struct {
Address []string `json:"address"`
ListenPort int `json:"listenPort"`
Mtu int `json:"mtu"`
PrivateKey string `json:"privateKey"`
PublicKey string `json:"publicKey"`
Endpoint string `json:"endpoint"`
PersistentKeepalive int `json:"persistentKeepalive"`
Dns []string `json:"dns"`
PreUp string `json:"preUp"`
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 address empty
if len(a.Address) == 0 {
errs = append(errs, fmt.Errorf("address est requis"))
}
// 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 est requis"))
}
// 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))
}
}
return errs
}