146 lines
3.5 KiB
Go
146 lines
3.5 KiB
Go
package dnsconfig
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"git.readonly.ch/bouzoure/gestion-dns/helpers"
|
|
)
|
|
|
|
type Variable struct {
|
|
Name string
|
|
Type string
|
|
Value string
|
|
}
|
|
|
|
type Record struct {
|
|
Name string
|
|
Type string
|
|
Value string
|
|
TTL int
|
|
}
|
|
|
|
type Zone struct {
|
|
Domain string
|
|
Records []Record
|
|
}
|
|
|
|
func GetZones() ([]Zone, error) {
|
|
log := helpers.GetLogger()
|
|
var zones []Zone
|
|
|
|
tomlZones, err := GetTomlZones()
|
|
if err != nil {
|
|
return zones, err
|
|
}
|
|
|
|
for _, tomlZone := range tomlZones {
|
|
var zone Zone
|
|
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))
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
return zones, nil
|
|
}
|