diff --git a/utilities.go b/utilities.go new file mode 100644 index 0000000..8ba90f6 --- /dev/null +++ b/utilities.go @@ -0,0 +1,78 @@ +package dnsexit_manager + +import ( + "errors" + "net" + "strconv" + "strings" + + dns "git.uoc.run.place/OxFF/dns-manager" + dns_req "github.com/miekg/dns" +) + +func (c *client) getNSRecods() (error, *[]string) { + var nameservers []string + + ns, err := net.LookupNS(c.Domain) + if err != nil { + return err, nil + } + + for _, v := range ns { + nameservers = append(nameservers, v.Host[:len(v.Host)-1]+":53") + } + + return nil, &nameservers +} + +func parse_record(record dns_req.RR) (error, *dns.Record) { + splitted := strings.SplitN(record.String(), "\t", -1) + + if len(splitted) == 5 { + var parsed dns.Record + rec_type := dns.RecordType(splitted[3]) + + if !rec_type.Check() { + return errors.New("provided DNS type is not exist in the main library"), nil + } + + parsed.Type = rec_type + ttl, _ := strconv.ParseUint(splitted[1], 10, 64) + + parsed.Name = splitted[0] + parsed.Content = splitted[4] + parsed.TTL = uint(ttl) + + return nil, &parsed + } + + return errors.New("no meaningful record provided"), nil +} + +func find_dns_records(domain string, nameserver string) (error, []*dns.Record) { + var subdomains []*dns.Record + + tr := new(dns_req.Transfer) + m := new(dns_req.Msg) + m.SetAxfr(domain + ".") + + client, err := tr.In(m, nameserver) + if err != nil { + return errors.New("failed to zone transfer in " + err.Error()), nil + } + + for msg := range client { + if msg.Error != nil { + return msg.Error, nil + } + + for _, r := range msg.RR { + err, record := parse_record(r) + if err == nil { + subdomains = append(subdomains, record) + } + } + } + + return nil, subdomains +}