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
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
}

41
zone.go
View File

@ -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