package dns import ( "fmt" "net" "code.crute.me/mcrute/go_ddns_manager/bind" "github.com/miekg/dns" ) func makeHeader(name string, zone *bind.Zone, t uint16, ttl int) dns.RR_Header { return dns.RR_Header{ Name: fmt.Sprintf("%s.%s", name, zone.Name), Rrtype: t, Class: dns.ClassINET, Ttl: uint32(ttl), } } func toRRSet(z *bind.Zone, rr ...RR) []dns.RR { o := []dns.RR{} for _, v := range rr { o = append(o, v.ToDNS(z)) } return o } type RR interface { ToDNS(*bind.Zone) dns.RR } type A struct { Name string Ttl int A net.IP } func (r *A) ToDNS(zone *bind.Zone) dns.RR { return &dns.A{ Hdr: makeHeader(r.Name, zone, dns.TypeA, r.Ttl), A: r.A, } } type AAAA struct { Name string Ttl int AAAA net.IP } func (r *AAAA) ToDNS(zone *bind.Zone) dns.RR { return &dns.AAAA{ Hdr: makeHeader(r.Name, zone, dns.TypeAAAA, r.Ttl), AAAA: r.AAAA, } } type CAA struct { Name string Ttl int Flag uint8 Tag string Value string } func (r *CAA) ToDNS(zone *bind.Zone) dns.RR { return &dns.CAA{ Hdr: makeHeader(r.Name, zone, dns.TypeCAA, r.Ttl), Flag: r.Flag, Tag: r.Tag, Value: r.Value, } } type CERT struct { Name string Ttl int Type uint16 KeyTag uint16 Algorithm uint8 Certificate string } func (r *CERT) ToDNS(zone *bind.Zone) dns.RR { return &dns.CERT{ Hdr: makeHeader(r.Name, zone, dns.TypeCERT, r.Ttl), Type: r.Type, KeyTag: r.KeyTag, Algorithm: r.Algorithm, Certificate: r.Certificate, } } type CNAME struct { Name string Ttl int Target string } func (r *CNAME) ToDNS(zone *bind.Zone) dns.RR { return &dns.CNAME{ Hdr: makeHeader(r.Name, zone, dns.TypeCNAME, r.Ttl), Target: r.Target, } } type DNAME struct { Name string Ttl int Target string } func (r *DNAME) ToDNS(zone *bind.Zone) dns.RR { return &dns.DNAME{ Hdr: makeHeader(r.Name, zone, dns.TypeDNAME, r.Ttl), Target: r.Target, } } type LOC struct { Name string Ttl int Version uint8 Size uint8 HorizPre uint8 VertPre uint8 Latitude uint32 Longitude uint32 Altitude uint32 } func (r *LOC) ToDNS(zone *bind.Zone) dns.RR { return &dns.LOC{ Hdr: makeHeader(r.Name, zone, dns.TypeLOC, r.Ttl), Version: r.Version, Size: r.Size, HorizPre: r.HorizPre, VertPre: r.VertPre, Latitude: r.Latitude, Longitude: r.Longitude, Altitude: r.Altitude, } } type MX struct { Name string Ttl int Preference uint16 Mx string } func (r *MX) ToDNS(zone *bind.Zone) dns.RR { return &dns.MX{ Hdr: makeHeader(r.Name, zone, dns.TypeMX, r.Ttl), Preference: r.Preference, Mx: r.Mx, } } type NAPTR struct { Name string Ttl int Order uint16 Preference uint16 Flags string Service string Regexp string Replacement string } func (r *NAPTR) ToDNS(zone *bind.Zone) dns.RR { return &dns.NAPTR{ Hdr: makeHeader(r.Name, zone, dns.TypeNAPTR, r.Ttl), Order: r.Order, Preference: r.Preference, Flags: r.Flags, Service: r.Service, Regexp: r.Regexp, Replacement: r.Replacement, } } type NS struct { Name string Ttl int Ns string } func (r *NS) ToDNS(zone *bind.Zone) dns.RR { return &dns.NS{ Hdr: makeHeader(r.Name, zone, dns.TypeNS, r.Ttl), Ns: r.Ns, } } type OPENPGPKEY struct { Name string Ttl int PublicKey string } func (r *OPENPGPKEY) ToDNS(zone *bind.Zone) dns.RR { return &dns.OPENPGPKEY{ Hdr: makeHeader(r.Name, zone, dns.TypeOPENPGPKEY, r.Ttl), PublicKey: r.PublicKey, } } type PTR struct { Name string Ttl int Ptr string } func (r *PTR) ToDNS(zone *bind.Zone) dns.RR { return &dns.PTR{ Hdr: makeHeader(r.Name, zone, dns.TypePTR, r.Ttl), Ptr: r.Ptr, } } type SOA struct { Name string Ttl int Ns string Mbox string Serial uint32 Refresh uint32 Retry uint32 Expire uint32 Minttl uint32 } func (r *SOA) ToDNS(zone *bind.Zone) dns.RR { return &dns.SOA{ Hdr: makeHeader(r.Name, zone, dns.TypeSOA, r.Ttl), Ns: r.Ns, Mbox: r.Mbox, Serial: r.Serial, Refresh: r.Refresh, Retry: r.Retry, Expire: r.Expire, Minttl: r.Minttl, } } type SRV struct { Name string Ttl int Priority uint16 Weight uint16 Port uint16 Target string } func (r *SRV) ToDNS(zone *bind.Zone) dns.RR { return &dns.SRV{ Hdr: makeHeader(r.Name, zone, dns.TypeSRV, r.Ttl), Priority: r.Priority, Weight: r.Weight, Port: r.Port, Target: r.Target, } } type SSHFP struct { Name string Ttl int Algorithm uint8 Type uint8 FingerPrint string } func (r *SSHFP) ToDNS(zone *bind.Zone) dns.RR { return &dns.SSHFP{ Hdr: makeHeader(r.Name, zone, dns.TypeSSHFP, r.Ttl), Algorithm: r.Algorithm, Type: r.Type, FingerPrint: r.FingerPrint, } } type TXT struct { Name string Ttl int Txt []string } func (r *TXT) ToDNS(zone *bind.Zone) dns.RR { return &dns.TXT{ Hdr: makeHeader(r.Name, zone, dns.TypeTXT, r.Ttl), Txt: r.Txt, } }