dyndns/service/clientinfo.go
Torben Nehmer 7dd11a0879 First basic version
- NFT and DNS Update skeletons
- Integrated viper configuration management
- Integrated cobra CLI management
- Basic webservice skeleton
2021-08-20 21:10:59 +02:00

71 lines
1.5 KiB
Go

package service
import (
"errors"
"fmt"
"net"
"net/url"
)
type ClientInfo struct {
IPv4 net.IP
IPv6 net.IP
UserName string
Password string
Domain string
DualStack bool
IPv6Net *net.IPNet
}
func (ci *ClientInfo) String() string {
return fmt.Sprintf("IPv4: %v, IPv6: %v, UserName: %v, Password: %v, Domain: %v, DualStack: %v, IPv6Net: %v",
ci.IPv4, ci.IPv6, ci.UserName, ci.Password, ci.Domain, ci.DualStack, ci.IPv6Net)
}
func CreateClientInfoFromForm(form url.Values) (*ClientInfo, error) {
ci := &ClientInfo{}
if form.Get("IPv4") != "" {
if ci.IPv4 = net.ParseIP(form.Get("IPv4")); ci.IPv4 == nil {
return nil, errors.New("could not parse IPv4 address")
}
}
if form.Get("IPv6") != "" {
if ci.IPv6 = net.ParseIP(form.Get("IPv6")); ci.IPv6 == nil {
return nil, errors.New("could not parse IPv6 address")
}
}
ci.UserName = form.Get("UserName")
if ci.UserName == "" {
return nil, errors.New("a UserName must be specified")
}
ci.Password = form.Get("Password")
if ci.Password == "" {
return nil, errors.New("a Password must be specified")
}
ci.Domain = form.Get("Domain")
if ci.Domain == "" {
return nil, errors.New("a Domain must be specified")
}
if form.Get("DualStack") == "1" {
ci.DualStack = true
} else {
ci.DualStack = false
}
if ip6net := form.Get("IPv6Net"); ip6net != "" {
_, ipnet, err := net.ParseCIDR(ip6net)
if err != nil {
return nil, errors.New("could not parse IPv6Net")
}
ci.IPv6Net = ipnet
}
return ci, nil
}