2020-02-18 21:59:18 +08:00
|
|
|
package models
|
|
|
|
|
|
|
|
// methods that make RecordConfig meet the dns.RR interface.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
//// Header Header returns the header of an resource record.
|
|
|
|
//func (rc *RecordConfig) Header() *dns.RR_Header {
|
|
|
|
// log.Fatal("Header not implemented")
|
|
|
|
// return nil
|
|
|
|
//}
|
|
|
|
|
|
|
|
// String returns the text representation of the resource record.
|
|
|
|
func (rc *RecordConfig) String() string {
|
|
|
|
return rc.GetTargetCombined()
|
|
|
|
}
|
|
|
|
|
|
|
|
//// copy returns a copy of the RR
|
|
|
|
//func (rc *RecordConfig) copy() dns.RR {
|
|
|
|
// log.Fatal("Copy not implemented")
|
|
|
|
// return dns.TypeToRR[dns.TypeA]()
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//// len returns the length (in octets) of the uncompressed RR in wire format.
|
|
|
|
//func (rc *RecordConfig) len() int {
|
|
|
|
// log.Fatal("len not implemented")
|
|
|
|
// return 0
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//// pack packs an RR into wire format.
|
|
|
|
//func (rc *RecordConfig) pack([]byte, int, map[string]int, bool) (int, error) {
|
|
|
|
// log.Fatal("pack not implemented")
|
|
|
|
// return 0, nil
|
|
|
|
//}
|
|
|
|
|
|
|
|
// Conversions
|
|
|
|
|
|
|
|
// RRstoRCs converts []dns.RR to []RecordConfigs.
|
2021-07-06 23:03:29 +08:00
|
|
|
func RRstoRCs(rrs []dns.RR, origin string) (Records, error) {
|
2020-02-18 21:59:18 +08:00
|
|
|
rcs := make(Records, 0, len(rrs))
|
|
|
|
for _, r := range rrs {
|
2021-07-06 23:03:29 +08:00
|
|
|
rc, err := RRtoRC(r, origin)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-18 21:59:18 +08:00
|
|
|
rcs = append(rcs, &rc)
|
|
|
|
}
|
2021-07-06 23:03:29 +08:00
|
|
|
return rcs, nil
|
2020-02-18 21:59:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// RRtoRC converts dns.RR to RecordConfig
|
2021-07-06 23:03:29 +08:00
|
|
|
func RRtoRC(rr dns.RR, origin string) (RecordConfig, error) {
|
2020-02-18 21:59:18 +08:00
|
|
|
// Convert's dns.RR into our native data type (RecordConfig).
|
|
|
|
// Records are translated directly with no changes.
|
|
|
|
header := rr.Header()
|
|
|
|
rc := new(RecordConfig)
|
|
|
|
rc.Type = dns.TypeToString[header.Rrtype]
|
|
|
|
rc.TTL = header.Ttl
|
|
|
|
rc.Original = rr
|
|
|
|
rc.SetLabelFromFQDN(strings.TrimSuffix(header.Name, "."), origin)
|
2021-07-06 23:03:29 +08:00
|
|
|
var err error
|
2020-02-18 21:59:18 +08:00
|
|
|
switch v := rr.(type) { // #rtype_variations
|
|
|
|
case *dns.A:
|
2021-07-06 23:03:29 +08:00
|
|
|
err = rc.SetTarget(v.A.String())
|
2020-02-18 21:59:18 +08:00
|
|
|
case *dns.AAAA:
|
2021-07-06 23:03:29 +08:00
|
|
|
err = rc.SetTarget(v.AAAA.String())
|
2020-02-18 21:59:18 +08:00
|
|
|
case *dns.CAA:
|
2021-07-06 23:03:29 +08:00
|
|
|
err = rc.SetTargetCAA(v.Flag, v.Tag, v.Value)
|
2020-02-18 21:59:18 +08:00
|
|
|
case *dns.CNAME:
|
2021-07-06 23:03:29 +08:00
|
|
|
err = rc.SetTarget(v.Target)
|
2020-05-30 22:40:21 +08:00
|
|
|
case *dns.DS:
|
2021-07-06 23:03:29 +08:00
|
|
|
err = rc.SetTargetDS(v.KeyTag, v.Algorithm, v.DigestType, v.Digest)
|
2023-03-17 02:04:20 +08:00
|
|
|
case *dns.LOC:
|
|
|
|
err = rc.SetTargetLOC(v.Version, v.Latitude, v.Longitude, v.Altitude, v.Size, v.HorizPre, v.VertPre)
|
2020-02-18 21:59:18 +08:00
|
|
|
case *dns.MX:
|
2021-07-06 23:03:29 +08:00
|
|
|
err = rc.SetTargetMX(v.Preference, v.Mx)
|
2023-03-29 07:30:01 +08:00
|
|
|
case *dns.NAPTR:
|
|
|
|
err = rc.SetTargetNAPTR(v.Order, v.Preference, v.Flags, v.Service, v.Regexp, v.Replacement)
|
2020-02-18 21:59:18 +08:00
|
|
|
case *dns.NS:
|
2021-07-06 23:03:29 +08:00
|
|
|
err = rc.SetTarget(v.Ns)
|
2020-02-18 21:59:18 +08:00
|
|
|
case *dns.PTR:
|
2021-07-06 23:03:29 +08:00
|
|
|
err = rc.SetTarget(v.Ptr)
|
2020-02-18 21:59:18 +08:00
|
|
|
case *dns.SOA:
|
2021-07-06 23:03:29 +08:00
|
|
|
err = rc.SetTargetSOA(v.Ns, v.Mbox, v.Serial, v.Refresh, v.Retry, v.Expire, v.Minttl)
|
2020-02-18 21:59:18 +08:00
|
|
|
case *dns.SRV:
|
2021-07-06 23:03:29 +08:00
|
|
|
err = rc.SetTargetSRV(v.Priority, v.Weight, v.Port, v.Target)
|
2020-02-18 21:59:18 +08:00
|
|
|
case *dns.SSHFP:
|
2021-07-06 23:03:29 +08:00
|
|
|
err = rc.SetTargetSSHFP(v.Algorithm, v.Type, v.FingerPrint)
|
2020-02-18 21:59:18 +08:00
|
|
|
case *dns.TLSA:
|
2021-07-06 23:03:29 +08:00
|
|
|
err = rc.SetTargetTLSA(v.Usage, v.Selector, v.MatchingType, v.Certificate)
|
2020-02-18 21:59:18 +08:00
|
|
|
case *dns.TXT:
|
2021-07-06 23:03:29 +08:00
|
|
|
err = rc.SetTargetTXTs(v.Txt)
|
2023-06-02 12:10:35 +08:00
|
|
|
case *dns.DHCID:
|
|
|
|
err = rc.SetTarget(v.Digest)
|
2020-02-18 21:59:18 +08:00
|
|
|
default:
|
2021-12-14 22:47:32 +08:00
|
|
|
return *rc, fmt.Errorf("rrToRecord: Unimplemented zone record type=%s (%v)", rc.Type, rr)
|
2020-02-18 21:59:18 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
2021-07-06 23:03:29 +08:00
|
|
|
return *rc, fmt.Errorf("unparsable record received: %w", err)
|
2020-02-18 21:59:18 +08:00
|
|
|
}
|
2021-07-06 23:03:29 +08:00
|
|
|
return *rc, nil
|
2020-02-18 21:59:18 +08:00
|
|
|
}
|