4f534f7cf1
Signed-off-by: Alejandro Mery <amery@jpi.io>
173 lines
4.4 KiB
Go
173 lines
4.4 KiB
Go
package dns
|
|
|
|
// func (mgr *Manager) WriteTo(w io.Writer) (int64, error) {
|
|
// var buf bytes.Buffer
|
|
//
|
|
// var zoneNames sort.StringSlice
|
|
//
|
|
// // sort zones
|
|
// for name := range mgr.zones {
|
|
// zoneNames = append(zoneNames, name)
|
|
// }
|
|
// sort.Sort(zoneNames)
|
|
//
|
|
// // records
|
|
// zones := make(map[string][]libdns.Record)
|
|
//
|
|
// for _, name := range zoneNames {
|
|
// z := mgr.zones[name]
|
|
//
|
|
// if buf.Len() > 0 {
|
|
// _, _ = buf.WriteRune('\n')
|
|
// }
|
|
//
|
|
// // servers
|
|
// err := mgr.writeZoneMachinesToBuffer(&buf, name, z)
|
|
// if err != nil {
|
|
// return 0, err
|
|
// }
|
|
//
|
|
// // zone alias
|
|
// _, _ = fmt.Fprintf(&buf, "; %s\n", name)
|
|
// records := mgr.genAliasRecords(name, true, z)
|
|
// mgr.Sort(records)
|
|
// mgr.writeRecordsToBuffer(&buf, "", records...)
|
|
//
|
|
// zones[name] = records
|
|
// }
|
|
//
|
|
// mgr.writeRegionToBuffer(&buf, zones, "uk", "ssd-lon")
|
|
// mgr.writeRegionToBuffer(&buf, zones, "nl", "ssd-ams")
|
|
// mgr.writeRegionToBuffer(&buf, zones, "de", "htz-fsn")
|
|
// mgr.writeRegionToBuffer(&buf, zones, "eu", "ssd-ams", "htz-fsn")
|
|
// mgr.writeRegionToBuffer(&buf, zones, "europe", "ssd-ams", "ssd-lon", "htz-fsn")
|
|
// mgr.writeRegionToBuffer(&buf, zones, "earth", "ssd-ams", "ssd-lon", "htz-fsn")
|
|
//
|
|
// return buf.WriteTo(w)
|
|
// }
|
|
//
|
|
// func (mgr *Manager) writeRecordsToBuffer(buf *bytes.Buffer, rename string, records ...libdns.Record) {
|
|
// for _, rr := range records {
|
|
// name := core.IIf(rename != "", rename, rr.Name)
|
|
// fqdn := fmt.Sprintf("%s.%s.", name, mgr.domain)
|
|
//
|
|
// _, _ = fmt.Fprintf(buf, "%s\t%v\tIN\t%s\t%v\n",
|
|
// fqdn, int(rr.TTL/time.Second), rr.Type, rr.Value)
|
|
// }
|
|
// }
|
|
//
|
|
// func (mgr *Manager) writeRegionToBuffer(buf *bytes.Buffer, cache map[string][]libdns.Record, name string, zones ...string) {
|
|
// var alias string
|
|
//
|
|
// _, _ = fmt.Fprintf(buf, "\n; %s\n", name)
|
|
//
|
|
// switch {
|
|
// case mgr.suffix == "":
|
|
// // no suffix
|
|
// case strings.HasSuffix(name, mgr.suffix):
|
|
// // suffixed
|
|
// default:
|
|
// // unsuffixed
|
|
// alias = name
|
|
// name = name + mgr.suffix
|
|
// }
|
|
//
|
|
// records := []libdns.Record{}
|
|
// for _, zone := range zones {
|
|
// records = append(records, cache[zone]...)
|
|
// }
|
|
// mgr.Sort(records)
|
|
// mgr.writeRecordsToBuffer(buf, alias, records...)
|
|
//
|
|
// if alias != "" {
|
|
// rr := libdns.Record{
|
|
// Name: alias,
|
|
// TTL: 3600 * time.Second,
|
|
// Type: "CNAME",
|
|
// Value: name + "." + mgr.domain + ".",
|
|
// }
|
|
//
|
|
// mgr.writeRecordsToBuffer(buf, alias, rr)
|
|
// }
|
|
// }
|
|
//
|
|
// func (mgr *Manager) writeZoneMachinesToBuffer(buf *bytes.Buffer, zone_name string, z *Zone) error {
|
|
// // title
|
|
// _, _ = fmt.Fprintf(buf, ";\n; %s\n;\n", zone_name)
|
|
//
|
|
// // machine records
|
|
// records := mgr.genMachineRecords(z)
|
|
// mgr.writeRecordsToBuffer(buf, "", records...)
|
|
//
|
|
// return nil
|
|
// }
|
|
//
|
|
// func (mgr *Manager) genMachineRecords(z *Zone) []libdns.Record {
|
|
// var out []libdns.Record
|
|
//
|
|
// for _, p := range z.Machines {
|
|
// pqdn := fmt.Sprintf("%s-%v%s", z.Name, p.ID, mgr.suffix)
|
|
//
|
|
// for _, addr := range p.Addrs {
|
|
// out = append(out, libdns.Record{
|
|
// Name: pqdn,
|
|
// Type: core.IIf(addr.Is6(), "AAAA", "A"),
|
|
// TTL: time.Second,
|
|
// Value: addr.String(),
|
|
// })
|
|
// }
|
|
// }
|
|
//
|
|
// mgr.Sort(out)
|
|
// return out
|
|
// }
|
|
//
|
|
// func (mgr *Manager) genAliasRecords(name string, activeOnly bool, zones ...*Zone) []libdns.Record {
|
|
// var out []libdns.Record
|
|
//
|
|
// fqdn := fmt.Sprintf("%s%s", name, mgr.suffix)
|
|
// for _, z := range zones {
|
|
// for _, p := range z.Machines {
|
|
// if p.Active || !activeOnly {
|
|
// for _, addr := range p.Addrs {
|
|
// out = append(out, libdns.Record{
|
|
// Name: fqdn,
|
|
// Type: core.IIf(addr.Is6(), "AAAA", "A"),
|
|
// TTL: time.Second,
|
|
// Value: addr.String(),
|
|
// })
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
//
|
|
// return out
|
|
// }
|
|
//
|
|
// func (mgr *Manager) Sort(records []libdns.Record) {
|
|
// sort.SliceStable(records, func(i, j int) bool {
|
|
// a := &records[i]
|
|
// b := &records[j]
|
|
// switch {
|
|
// case a.Name < b.Name:
|
|
// return true
|
|
// case a.Name > b.Name:
|
|
// return false
|
|
// case a.Type < b.Type:
|
|
// return true
|
|
// case a.Type > b.Type:
|
|
// return false
|
|
// case a.Type == "A" || a.Type == "AAAA":
|
|
// var aAddr, bAddr netip.Addr
|
|
// aAddr.UnmarshalText([]byte(a.Value))
|
|
// bAddr.UnmarshalText([]byte(b.Value))
|
|
// return aAddr.Less(bAddr)
|
|
// case a.Value < b.Value:
|
|
// return true
|
|
// default:
|
|
// return false
|
|
// }
|
|
// })
|
|
// }
|
|
//
|