Refactorization and further implementation

- renamed clientoinfo to updaterequest
- added userconfig skeleton
- added hashed password skeleton
This commit is contained in:
2021-08-21 20:34:45 +02:00
parent a6a065cafb
commit 9f1b9f1690
8 changed files with 149 additions and 28 deletions

View File

@@ -1,70 +0,0 @@
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
}

View File

@@ -3,8 +3,9 @@ package service
import "github.com/spf13/viper"
type config struct {
DNSServer string
DefaultTTL uint32
DNSServer string
DNSDefaultTTL uint32
UsersConfigDir string
}
var C config
@@ -16,9 +17,11 @@ func init() {
func SetConfigDefaults() {
viper.SetDefault("Service.DNS.Server", "10.10.11.254:53")
viper.SetDefault("Service.DNS.DefaultTTL", 60)
viper.SetDefault("Service.Users.ConfigDir", "users/")
}
func LoadConfig() {
C.DNSServer = viper.GetString("Service.DNS.Server")
C.DefaultTTL = viper.GetUint32("Service.DNS.DefaultTTL")
C.DNSDefaultTTL = viper.GetUint32("Service.DNS.DefaultTTL")
C.UsersConfigDir = viper.GetString("Service.Users.ConfigDir")
}

View File

@@ -42,7 +42,7 @@ func UpdateDNSEntry(domain string, hostname string, ip4 net.IP, ip6 net.IP) erro
Name: fqdn,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: C.DefaultTTL,
Ttl: C.DNSDefaultTTL,
},
A: ip4bin,
})
@@ -58,7 +58,7 @@ func UpdateDNSEntry(domain string, hostname string, ip4 net.IP, ip6 net.IP) erro
Name: fqdn,
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET,
Ttl: C.DefaultTTL,
Ttl: C.DNSDefaultTTL,
},
AAAA: ip6bin,
})

30
service/userconfig.go Normal file
View File

@@ -0,0 +1,30 @@
package service
import (
"fmt"
"log"
"os"
"path"
"github.com/spf13/viper"
)
func LoadConfigForUser(username string) (*viper.Viper, error) {
configFile := fmt.Sprintf("%s/%s.yml", C.UsersConfigDir, username)
configFile = path.Clean(configFile)
log.Printf("Trying to load config file %s for user %s", configFile, username)
if _, err := os.Stat(configFile); err != nil {
return nil, fmt.Errorf("cannot stat the file %s: %v", configFile, err)
}
v := viper.New()
v.SetConfigFile(configFile)
err := v.ReadInConfig()
if err != nil {
return nil, fmt.Errorf("failed to parse config file %s: %v", configFile, err)
}
return v, nil
}