refactored record rendering

This commit is contained in:
core 2023-10-02 10:24:01 -04:00
parent b41f622655
commit 1a84386394
Signed by: core
GPG Key ID: FDBF740DADDCEECF
2 changed files with 59 additions and 38 deletions

56
rule.go
View File

@ -1,6 +1,9 @@
package pancheri package pancheri
import "net" import (
"github.com/miekg/dns"
"net"
)
const ( const (
RuleTypeA = "A" RuleTypeA = "A"
@ -14,18 +17,69 @@ type RecordA struct {
Ip net.IP Ip net.IP
TTL uint 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 { type RecordAAAA struct {
In string In string
Ip net.IP Ip net.IP
TTL uint 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 { type RecordCNAME struct {
In string In string
Target string Target string
TTL uint 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 { type RecordTXT struct {
In string In string
Content []string Content []string
TTL uint 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
}

41
zone.go
View File

@ -2,7 +2,6 @@ package pancheri
import ( import (
"fmt" "fmt"
"github.com/miekg/dns"
"time" "time"
) )
@ -28,57 +27,25 @@ func (z *Zone) RenderZone() string {
outString += ";; A Records\n" outString += ";; A Records\n"
for _, record := range z.ARecords { for _, record := range z.ARecords {
r := new(dns.A) outString += record.Render().String() + "\n"
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 += ";; AAAA Records\n" outString += ";; AAAA Records\n"
for _, record := range z.AAAARecords { for _, record := range z.AAAARecords {
r := new(dns.AAAA) outString += record.Render().String() + "\n"
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 += ";; CNAME Records\n" outString += ";; CNAME Records\n"
for _, record := range z.CNAMERecords { for _, record := range z.CNAMERecords {
r := new(dns.CNAME) outString += record.Render().String() + "\n"
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 += ";; TXT Records\n" outString += ";; TXT Records\n"
for _, record := range z.TXTRecords { for _, record := range z.TXTRecords {
r := new(dns.TXT) outString += record.Render().String() + "\n"
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"
} }
return outString return outString