func updateRecord(r dns.RR, q *dns.Question) { var ( rr dns.RR name string rtype uint16 ttl uint32 ip net.IP ) header := r.Header() name = header.Name rtype = header.Rrtype ttl = header.Ttl if _, ok := dns.IsDomainName(name); ok { if header.Class == dns.ClassANY && header.Rdlength == 0 { // Delete record deleteRecord(name, rtype) } else { // Add record rheader := dns.RR_Header{ Name: name, Rrtype: rtype, Class: dns.ClassINET, Ttl: ttl, } if a, ok := r.(*dns.A); ok { rrr, err := getRecord(name, rtype) if err == nil { rr = rrr.(*dns.A) } else { rr = new(dns.A) } ip = a.A rr.(*dns.A).Hdr = rheader rr.(*dns.A).A = ip } else if a, ok := r.(*dns.AAAA); ok { rrr, err := getRecord(name, rtype) if err == nil { rr = rrr.(*dns.AAAA) } else { rr = new(dns.AAAA) } ip = a.AAAA rr.(*dns.AAAA).Hdr = rheader rr.(*dns.AAAA).AAAA = ip } storeRecord(rr) } } }
func getKey(domain string, rtype uint16) (r string, e error) { if n, ok := dns.IsDomainName(domain); ok { labels := dns.SplitDomainName(domain) // Reverse domain, starting from top-level domain var tmp string for i := 0; i < int(math.Floor(float64(n/2))); i++ { tmp = labels[i] labels[i] = labels[n-1] labels[n-1] = tmp } reverse_domain := strings.Join(labels, ".") r = strings.Join([]string{reverse_domain, strconv.Itoa(int(rtype))}, "_") } else { e = errors.New("Invailid domain: " + domain) log.Println(e.Error()) } return r, e }