From b41f622655b19c4a1310fbd92da80da36f006722 Mon Sep 17 00:00:00 2001 From: core Date: Mon, 2 Oct 2023 10:21:08 -0400 Subject: [PATCH] zone rendering --- cmd/pancheri-render/main.go | 59 +++++++++++++++++++++++++ rule.go | 23 +++++++++- zone.go | 85 +++++++++++++++++++++++++++++++++++++ zone_example.yml | 18 +++----- 4 files changed, 170 insertions(+), 15 deletions(-) create mode 100644 cmd/pancheri-render/main.go create mode 100644 zone.go diff --git a/cmd/pancheri-render/main.go b/cmd/pancheri-render/main.go new file mode 100644 index 0000000..fe96161 --- /dev/null +++ b/cmd/pancheri-render/main.go @@ -0,0 +1,59 @@ +package main + +import ( + "flag" + "git.e3t.cc/e3team/pancheri" + "net" + "os" +) + +func main() { + //configPath := flag.String("zone", "", "Zone file to render") + printUsage := flag.Bool("help", false, "Print command line usage") + + flag.Parse() + + if *printUsage { + flag.Usage() + os.Exit(0) + } + + var aRecords []pancheri.RecordA + aRecords = append(aRecords, pancheri.RecordA{ + In: "test.xe.", + Ip: net.ParseIP("1.2.3.4"), + TTL: 600, + }) + + var aaaaRecords []pancheri.RecordAAAA + aaaaRecords = append(aaaaRecords, pancheri.RecordAAAA{ + In: "testv6.xe.", + Ip: net.ParseIP("bd1f:f314:5398:0e3d:b3e0:f427:73ef:60fb"), + TTL: 600, + }) + + var cnameRecords []pancheri.RecordCNAME + cnameRecords = append(cnameRecords, pancheri.RecordCNAME{ + In: "cname.xe.", + Target: "test.xe.", + TTL: 600, + }) + + var txtRecords []pancheri.RecordTXT + txtRecords = append(txtRecords, pancheri.RecordTXT{ + In: "txt.xe.", + Content: []string{"WHY HELLO THERE MY FELLOW E3TEAMERS"}, + TTL: 600, + }) + + zone := pancheri.Zone{ + ReducedHash: "0433da05bf22d86c1886fca6e3e2c3239b86f1e6ebea9b94201483c8596c0468", + Root: "xe", + ARecords: aRecords, + AAAARecords: aaaaRecords, + CNAMERecords: cnameRecords, + TXTRecords: txtRecords, + Zonefile: "zone_example.yml", + } + println(zone.RenderZone()) +} diff --git a/rule.go b/rule.go index ac65c31..bf4f667 100644 --- a/rule.go +++ b/rule.go @@ -1,12 +1,31 @@ package pancheri +import "net" + const ( RuleTypeA = "A" RuleTypeAAAA = "AAAA" RuleTypeCNAME = "CNAME" - RuleTypeMX = "MX" RuleTypeTXT = "TXT" ) -type Rule interface { +type RecordA struct { + In string + Ip net.IP + TTL uint +} +type RecordAAAA struct { + In string + Ip net.IP + TTL uint +} +type RecordCNAME struct { + In string + Target string + TTL uint +} +type RecordTXT struct { + In string + Content []string + TTL uint } diff --git a/zone.go b/zone.go new file mode 100644 index 0000000..4e404b4 --- /dev/null +++ b/zone.go @@ -0,0 +1,85 @@ +package pancheri + +import ( + "fmt" + "github.com/miekg/dns" + "time" +) + +type Zone struct { + Root string + ReducedHash string + Zonefile string + ARecords []RecordA + AAAARecords []RecordAAAA + CNAMERecords []RecordCNAME + TXTRecords []RecordTXT +} + +func (z *Zone) RenderZone() string { + outString := "" + outString += fmt.Sprintf(";; Rendered zonefile for %s (rsha %s) at %s\n", z.Zonefile, z.ReducedHash, time.Now().Format(time.RFC3339)) + outString += ";; Generated by pancheri-render. Note: this will NOT work out of the box!\n" + outString += ";; At the very least, you'll need to change the SOA and NS values.\n" + outString += "\n" + outString += ";; SOA & NS records\n" + outString += ";; TODO\n" + outString += "\n" + 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 += ";; 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 += ";; 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 += ";; 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" + } + + return outString +} diff --git a/zone_example.yml b/zone_example.yml index cb42e79..d0a9f22 100644 --- a/zone_example.yml +++ b/zone_example.yml @@ -2,23 +2,15 @@ zone: root: 'xe' records: - type: 'A' - domains: ['some_a'] + domains: ['test'] ipv4: '1.2.3.4' - - type: 'AAAA' - domains: ['some_aaaa'] + domains: ['testv6'] ipv6: 'bd1f:f314:5398:0e3d:b3e0:f427:73ef:60fb' - - type: 'CNAME' - domains: ['some_cname'] - content: 'some_a' - - - type: 'MX' - domains: ['some_mail_domain'] - mailserver: 'mail.e3t.cc' - priority: 10 - + domains: ['cname'] + content: 'test' - type: 'TXT' - domains: ['some_txt'] + domains: ['txt'] content: | WHY HELLO THERE MY FELLOW E3TEAMERS \ No newline at end of file