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 } for index, record := range zone.TomlRecords { if !strings.EqualFold(record.Type, "TXT") || len(record.Value) <= 255 { continue } var newValue string var split string for count, char := range record.Value { split = fmt.Sprintf("%s%c", split, char) if (count+1)%255 == 0 || (count+1) == len(record.Value) { if newValue == "" { newValue = fmt.Sprintf("\"%s\"", split) } else { newValue = fmt.Sprintf("%s \"%s\"", newValue, split) } split = "" } } zone.TomlRecords[index].Value = newValue } 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 } for index, record := range zone.TomlRecords { if !strings.EqualFold(record.Type, "TXT") || len(record.Value) <= 255 { continue } newValue := strings.ReplaceAll(record.Value, "\" \"", "") newValue = strings.ReplaceAll(newValue, "\"", "") zone.TomlRecords[index].Value = newValue } return toml.NewEncoder(file).Encode(&zone) }