Replace flat with "@" + implement vars with "$" + add special type "A+AAAA" + resolver (cache, retry, multi-results)
This commit is contained in:
parent
e1fc586076
commit
d145ffa61a
4 changed files with 204 additions and 70 deletions
81
dnsconfig/toml_zones.go
Normal file
81
dnsconfig/toml_zones.go
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
package dnsconfig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type TomlVariables struct {
|
||||
Name string `toml:"name"`
|
||||
Type string `toml:"type"`
|
||||
Value string `toml:"value"`
|
||||
}
|
||||
|
||||
type TomlRecord struct {
|
||||
Name string `toml:"name"`
|
||||
Type string `toml:"type"`
|
||||
Value string `toml:"value"`
|
||||
TTL int `toml:"ttl"`
|
||||
}
|
||||
|
||||
type TomlZone struct {
|
||||
Domain string `toml:"domain"`
|
||||
DefaultTTL int `toml:"default_ttl"`
|
||||
TomlRecords []TomlRecord `toml:"record"`
|
||||
TomlVariables []TomlVariables `toml:"variable"`
|
||||
}
|
||||
|
||||
func GetTomlZones() ([]TomlZone, error) {
|
||||
var zones []TomlZone
|
||||
|
||||
files, err := os.ReadDir("./zones/")
|
||||
if err != nil {
|
||||
return zones, err
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
name := file.Name()
|
||||
path := fmt.Sprintf("./zones/%s", name)
|
||||
|
||||
if !strings.HasSuffix(strings.ToLower(name), ".toml") {
|
||||
continue
|
||||
}
|
||||
|
||||
var zone TomlZone
|
||||
_, err = toml.DecodeFile(path, &zone)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if len(zone.Domain) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if zone.DefaultTTL <= 0 {
|
||||
zone.DefaultTTL = 3600
|
||||
}
|
||||
|
||||
zones = append(zones, zone)
|
||||
}
|
||||
|
||||
return zones, nil
|
||||
}
|
||||
|
||||
func CreateTomlZone(zone TomlZone) error {
|
||||
if len(zone.Domain) == 0 {
|
||||
return fmt.Errorf("zone name cannot be empty")
|
||||
}
|
||||
|
||||
name := strings.ReplaceAll(strings.ToLower(zone.Domain), ".", "_")
|
||||
path := fmt.Sprintf("./zones/%s.toml", name)
|
||||
|
||||
file, err := os.Create(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return toml.NewEncoder(file).Encode(&zone)
|
||||
}
|
||||
|
|
@ -1,55 +1,142 @@
|
|||
package dnsconfig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"git.readonly.ch/bouzoure/gestion-dns/helpers"
|
||||
)
|
||||
|
||||
type Variable struct {
|
||||
Name string
|
||||
Type string
|
||||
Value string
|
||||
}
|
||||
|
||||
type Record struct {
|
||||
Name string `toml:"name"`
|
||||
Type string `toml:"type"`
|
||||
Value string `toml:"value"`
|
||||
Flat bool `toml:"flat"`
|
||||
TTL int `toml:"ttl"`
|
||||
Name string
|
||||
Type string
|
||||
Value string
|
||||
TTL int
|
||||
}
|
||||
|
||||
type Zone struct {
|
||||
Domain string `toml:"domain"`
|
||||
DefaultTTL int `toml:"default_ttl"`
|
||||
Records []Record `toml:"record"`
|
||||
Domain string
|
||||
Records []Record
|
||||
}
|
||||
|
||||
func GetZones() ([]Zone, error) {
|
||||
log := helpers.GetLogger()
|
||||
var zones []Zone
|
||||
|
||||
files, err := os.ReadDir("./zones/")
|
||||
tomlZones, err := GetTomlZones()
|
||||
if err != nil {
|
||||
return zones, err
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
name := file.Name()
|
||||
path := fmt.Sprintf("./zones/%s", name)
|
||||
|
||||
if !strings.HasSuffix(strings.ToLower(name), ".toml") {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, tomlZone := range tomlZones {
|
||||
var zone Zone
|
||||
_, err = toml.DecodeFile(path, &zone)
|
||||
if err != nil {
|
||||
continue
|
||||
zone.Domain = tomlZone.Domain
|
||||
|
||||
var tmpVariablesTypes []Variable
|
||||
for _, tomlVariable := range tomlZone.TomlVariables {
|
||||
if strings.ToUpper(tomlVariable.Type) == "A+AAAA" {
|
||||
tmpVariablesTypes = append(tmpVariablesTypes, Variable{
|
||||
Name: tomlVariable.Name,
|
||||
Value: tomlVariable.Value,
|
||||
Type: "A",
|
||||
})
|
||||
|
||||
tmpVariablesTypes = append(tmpVariablesTypes, Variable{
|
||||
Name: tomlVariable.Name,
|
||||
Value: tomlVariable.Value,
|
||||
Type: "AAAA",
|
||||
})
|
||||
} else {
|
||||
tmpVariablesTypes = append(tmpVariablesTypes, Variable(tomlVariable))
|
||||
}
|
||||
}
|
||||
|
||||
if len(zone.Domain) == 0 {
|
||||
continue
|
||||
var variablesResolved []Variable
|
||||
for _, tmpVariable := range tmpVariablesTypes {
|
||||
if strings.HasPrefix(tmpVariable.Value, "@") {
|
||||
dnsName := strings.Replace(tmpVariable.Value, "@", "", 1)
|
||||
resolveResults := helpers.ResolveRecord(dnsName, tmpVariable.Type)
|
||||
|
||||
for _, resolveResult := range resolveResults {
|
||||
variablesResolved = append(variablesResolved, Variable{
|
||||
Name: tmpVariable.Name,
|
||||
Value: resolveResult,
|
||||
Type: tmpVariable.Type,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
variablesResolved = append(variablesResolved, tmpVariable)
|
||||
}
|
||||
}
|
||||
|
||||
if zone.DefaultTTL <= 0 {
|
||||
zone.DefaultTTL = 3600
|
||||
var tmpRecordsTypes []Record
|
||||
for _, tomlRecord := range tomlZone.TomlRecords {
|
||||
if strings.EqualFold(tomlRecord.Type, "A+AAAA") {
|
||||
tmpRecordsTypes = append(tmpRecordsTypes, Record{
|
||||
Name: tomlRecord.Name,
|
||||
Value: tomlRecord.Value,
|
||||
TTL: tomlRecord.TTL,
|
||||
Type: "A",
|
||||
})
|
||||
|
||||
tmpRecordsTypes = append(tmpRecordsTypes, Record{
|
||||
Name: tomlRecord.Name,
|
||||
Value: tomlRecord.Value,
|
||||
TTL: tomlRecord.TTL,
|
||||
Type: "AAAA",
|
||||
})
|
||||
} else {
|
||||
tmpRecordsTypes = append(tmpRecordsTypes, Record(tomlRecord))
|
||||
}
|
||||
}
|
||||
|
||||
var tmpRecordsResolved []Record
|
||||
for _, tmpRecord := range tmpRecordsTypes {
|
||||
if strings.HasPrefix(tmpRecord.Value, "@") {
|
||||
dnsName := strings.Replace(tmpRecord.Value, "@", "", 1)
|
||||
resolveResults := helpers.ResolveRecord(dnsName, tmpRecord.Type)
|
||||
|
||||
for _, resolveResult := range resolveResults {
|
||||
tmpRecordsResolved = append(tmpRecordsResolved, Record{
|
||||
Name: tmpRecord.Name,
|
||||
Value: resolveResult,
|
||||
TTL: tmpRecord.TTL,
|
||||
Type: tmpRecord.Type,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
tmpRecordsResolved = append(tmpRecordsResolved, tmpRecord)
|
||||
}
|
||||
}
|
||||
|
||||
for _, tmpRecord := range tmpRecordsResolved {
|
||||
if tmpRecord.TTL <= 0 {
|
||||
tmpRecord.TTL = tomlZone.DefaultTTL
|
||||
}
|
||||
|
||||
// TODO: Handle vars
|
||||
if strings.HasPrefix(tmpRecord.Value, "$") {
|
||||
varName := strings.Replace(tmpRecord.Value, "$", "", 1)
|
||||
found := false
|
||||
for _, variable := range variablesResolved {
|
||||
if strings.EqualFold(varName, variable.Name) && strings.EqualFold(tmpRecord.Type, variable.Type) {
|
||||
found = true
|
||||
tmpRecord.Value = variable.Value
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
log.Error("Could not find a variable with matching type", "name", varName, "type", tmpRecord.Type)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
zone.Records = append(zone.Records, tmpRecord)
|
||||
}
|
||||
|
||||
zones = append(zones, zone)
|
||||
|
|
@ -57,19 +144,3 @@ func GetZones() ([]Zone, error) {
|
|||
|
||||
return zones, nil
|
||||
}
|
||||
|
||||
func CreateZone(zone Zone) error {
|
||||
if len(zone.Domain) == 0 {
|
||||
return fmt.Errorf("zone name cannot be empty")
|
||||
}
|
||||
|
||||
name := strings.ReplaceAll(strings.ToLower(zone.Domain), ".", "_")
|
||||
path := fmt.Sprintf("./zones/%s.toml", name)
|
||||
|
||||
file, err := os.Create(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return toml.NewEncoder(file).Encode(&zone)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue