Torben Nehmer
9f1b9f1690
- renamed clientoinfo to updaterequest - added userconfig skeleton - added hashed password skeleton
81 lines
1.7 KiB
Go
81 lines
1.7 KiB
Go
package webapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"net/url"
|
|
)
|
|
|
|
type UpdateRequest struct {
|
|
IPv4 net.IP
|
|
IPv6 net.IP
|
|
UserName string
|
|
Password string
|
|
Domain string
|
|
DualStack bool
|
|
IPv6Net *net.IPNet
|
|
}
|
|
|
|
func (ur *UpdateRequest) String() string {
|
|
return fmt.Sprintf("IPv4: %v, IPv6: %v, UserName: %v, Password: %v, Domain: %v, DualStack: %v, IPv6Net: %v",
|
|
ur.IPv4, ur.IPv6, ur.UserName, ur.Password, ur.Domain, ur.DualStack, ur.IPv6Net)
|
|
}
|
|
|
|
func (ur *UpdateRequest) PrettyPrint() string {
|
|
s, err := json.MarshalIndent(ur, "", " ")
|
|
if err != nil {
|
|
log.Fatalf("Failed to pretty print UpdateRequest via JSON: %v", err)
|
|
}
|
|
return string(s)
|
|
}
|
|
|
|
func createUpdateRequestFromForm(form url.Values) (*UpdateRequest, error) {
|
|
ur := &UpdateRequest{}
|
|
|
|
if form.Get("IPv4") != "" {
|
|
if ur.IPv4 = net.ParseIP(form.Get("IPv4")); ur.IPv4 == nil {
|
|
return nil, errors.New("could not parse IPv4 address")
|
|
}
|
|
}
|
|
|
|
if form.Get("IPv6") != "" {
|
|
if ur.IPv6 = net.ParseIP(form.Get("IPv6")); ur.IPv6 == nil {
|
|
return nil, errors.New("could not parse IPv6 address")
|
|
}
|
|
}
|
|
|
|
ur.UserName = form.Get("UserName")
|
|
if ur.UserName == "" {
|
|
return nil, errors.New("a UserName must be specified")
|
|
}
|
|
|
|
ur.Password = form.Get("Password")
|
|
if ur.Password == "" {
|
|
return nil, errors.New("a Password must be specified")
|
|
}
|
|
|
|
ur.Domain = form.Get("Domain")
|
|
if ur.Domain == "" {
|
|
return nil, errors.New("a Domain must be specified")
|
|
}
|
|
|
|
if form.Get("DualStack") == "1" {
|
|
ur.DualStack = true
|
|
} else {
|
|
ur.DualStack = false
|
|
}
|
|
|
|
if ip6net := form.Get("IPv6Net"); ip6net != "" {
|
|
_, ipnet, err := net.ParseCIDR(ip6net)
|
|
if err != nil {
|
|
return nil, errors.New("could not parse IPv6Net")
|
|
}
|
|
ur.IPv6Net = ipnet
|
|
}
|
|
|
|
return ur, nil
|
|
}
|