From 1a84386394ea8189d04626ede75c504b6699ac0b Mon Sep 17 00:00:00 2001 From: core Date: Mon, 2 Oct 2023 10:24:01 -0400 Subject: [PATCH] refactored record rendering --- rule.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- zone.go | 41 ++++------------------------------------- 2 files changed, 59 insertions(+), 38 deletions(-) diff --git a/rule.go b/rule.go index bf4f667..8f1aeea 100644 --- a/rule.go +++ b/rule.go @@ -1,6 +1,9 @@ package pancheri -import "net" +import ( + "github.com/miekg/dns" + "net" +) const ( RuleTypeA = "A" @@ -14,18 +17,69 @@ type RecordA struct { Ip net.IP TTL uint } + +func (record *RecordA) Render() *dns.A { + r := new(dns.A) + r.Hdr = dns.RR_Header{ + Name: record.In, + Rrtype: dns.TypeA, + Class: dns.ClassINET, + Ttl: uint32(record.TTL), + } + r.A = record.Ip + return r +} + type RecordAAAA struct { In string Ip net.IP TTL uint } + +func (record *RecordAAAA) Render() *dns.AAAA { + r := new(dns.AAAA) + r.Hdr = dns.RR_Header{ + Name: record.In, + Rrtype: dns.TypeAAAA, + Class: dns.ClassINET, + Ttl: uint32(record.TTL), + } + r.AAAA = record.Ip + return r +} + type RecordCNAME struct { In string Target string TTL uint } + +func (record *RecordCNAME) Render() *dns.CNAME { + r := new(dns.CNAME) + r.Hdr = dns.RR_Header{ + Name: record.In, + Rrtype: dns.TypeCNAME, + Class: dns.ClassINET, + Ttl: uint32(record.TTL), + } + r.Target = record.Target + return r +} + type RecordTXT struct { In string Content []string TTL uint } + +func (record *RecordTXT) Render() *dns.TXT { + r := new(dns.TXT) + r.Hdr = dns.RR_Header{ + Name: record.In, + Rrtype: dns.TypeTXT, + Class: dns.ClassINET, + Ttl: uint32(record.TTL), + } + r.Txt = record.Content + return r +} diff --git a/zone.go b/zone.go index 4e404b4..55341ab 100644 --- a/zone.go +++ b/zone.go @@ -2,7 +2,6 @@ package pancheri import ( "fmt" - "github.com/miekg/dns" "time" ) @@ -28,57 +27,25 @@ func (z *Zone) RenderZone() string { outString += ";; A Records\n" for _, record := range z.ARecords { - r := new(dns.A) - r.Hdr = dns.RR_Header{ - Name: record.In, - Rrtype: dns.TypeA, - Class: dns.ClassINET, - Ttl: uint32(record.TTL), - } - r.A = record.Ip - outString += r.String() + "\n" + outString += record.Render().String() + "\n" } outString += ";; AAAA Records\n" for _, record := range z.AAAARecords { - r := new(dns.AAAA) - r.Hdr = dns.RR_Header{ - Name: record.In, - Rrtype: dns.TypeAAAA, - Class: dns.ClassINET, - Ttl: uint32(record.TTL), - } - r.AAAA = record.Ip - outString += r.String() + "\n" + outString += record.Render().String() + "\n" } outString += ";; CNAME Records\n" for _, record := range z.CNAMERecords { - r := new(dns.CNAME) - r.Hdr = dns.RR_Header{ - Name: record.In, - Rrtype: dns.TypeCNAME, - Class: dns.ClassINET, - Ttl: uint32(record.TTL), - } - r.Target = record.Target - outString += r.String() + "\n" + outString += record.Render().String() + "\n" } outString += ";; TXT Records\n" for _, record := range z.TXTRecords { - r := new(dns.TXT) - r.Hdr = dns.RR_Header{ - Name: record.In, - Rrtype: dns.TypeTXT, - Class: dns.ClassINET, - Ttl: uint32(record.TTL), - } - r.Txt = record.Content - outString += r.String() + "\n" + outString += record.Render().String() + "\n" } return outString