Add DefaultTTL

This commit is contained in:
nemunaire 2020-06-08 16:34:28 +02:00
parent 29409dc607
commit f166e5b59b
2 changed files with 39 additions and 13 deletions

View File

@ -76,7 +76,7 @@ func analyzeDomain(opts *config.Options, domain *happydns.Domain, body io.Reader
}
}
services, aliases, err := svcs.AnalyzeZone(domain.DomainName, zone)
services, aliases, defaultTTL, err := svcs.AnalyzeZone(domain.DomainName, zone)
if err != nil {
return APIErrorResponse{
err: err,
@ -85,8 +85,9 @@ func analyzeDomain(opts *config.Options, domain *happydns.Domain, body io.Reader
return APIResponse{
response: map[string]interface{}{
"aliases": aliases,
"services": services,
"aliases": aliases,
"services": services,
"defaultTTL": defaultTTL,
},
}
}

View File

@ -42,10 +42,11 @@ import (
)
type Analyzer struct {
origin string
zone []dns.RR
services map[string][]*happydns.ServiceCombined
aliases map[string][]string
origin string
zone []dns.RR
services map[string][]*happydns.ServiceCombined
aliases map[string][]string
defaultTTL uint32
}
type AnalyzerRecordFilter struct {
@ -103,10 +104,15 @@ func (a *Analyzer) useRR(rr dns.RR, domain string, svc happydns.Service) error {
}
}
var ttl uint32 = 0
if rr.Header().Ttl != a.defaultTTL {
ttl = rr.Header().Ttl
}
a.services[domain] = append(a.services[domain], &happydns.ServiceCombined{svc, happydns.ServiceType{
Type: reflect.Indirect(reflect.ValueOf(svc)).Type().String(),
Domain: domain,
Ttl: rr.Header().Ttl,
Ttl: ttl,
Comment: svc.GenComment(a.origin),
NbResources: svc.GetNbResources(),
}})
@ -114,12 +120,31 @@ func (a *Analyzer) useRR(rr dns.RR, domain string, svc happydns.Service) error {
return nil
}
func AnalyzeZone(origin string, zone []dns.RR) (svcs map[string][]*happydns.ServiceCombined, aliases map[string][]string, err error) {
func getMostUsedTTL(zone []dns.RR) uint32 {
ttls := map[uint32]int{}
for _, rr := range zone {
ttls[rr.Header().Ttl] += 1
}
var max uint32 = 0
for k, v := range ttls {
if w, ok := ttls[max]; !ok || v > w {
max = k
}
}
return max
}
func AnalyzeZone(origin string, zone []dns.RR) (svcs map[string][]*happydns.ServiceCombined, aliases map[string][]string, defaultTTL uint32, err error) {
defaultTTL = getMostUsedTTL(zone)
a := Analyzer{
origin: origin,
zone: zone,
services: map[string][]*happydns.ServiceCombined{},
aliases: map[string][]string{},
origin: origin,
zone: zone,
services: map[string][]*happydns.ServiceCombined{},
aliases: map[string][]string{},
defaultTTL: defaultTTL,
}
// Find services between all registered ones