2018-11-27 04:56:21 +00:00
|
|
|
package resolver
|
|
|
|
|
|
|
|
import (
|
2018-11-30 18:24:57 +00:00
|
|
|
"encoding/json"
|
2018-11-27 04:56:21 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.linuxrocker.com/mattburchett/go_tab-magic/pkg/config"
|
2018-11-30 18:24:57 +00:00
|
|
|
"git.linuxrocker.com/mattburchett/go_tab-magic/pkg/model"
|
2018-11-27 04:56:21 +00:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LookupName returns IPv4 address from A record or error.
|
|
|
|
func lookupName(fqdn, serverAddr string) (string, error) {
|
|
|
|
m := &dns.Msg{}
|
|
|
|
m.SetQuestion(dns.Fqdn(fqdn), dns.TypeA)
|
|
|
|
in, err := dns.Exchange(m, serverAddr)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(in.Answer) < 1 {
|
|
|
|
return "", errors.New("no Answer")
|
|
|
|
}
|
|
|
|
if a, ok := in.Answer[0].(*dns.A); ok {
|
|
|
|
ip := a.A.String()
|
|
|
|
return ip, nil
|
|
|
|
}
|
|
|
|
return "", errors.New("no A record returned")
|
|
|
|
}
|
|
|
|
|
2018-11-30 20:20:43 +00:00
|
|
|
func PerformZoneTransfer(config config.Config) []string {
|
2018-11-27 04:56:21 +00:00
|
|
|
data := make([]string, 0)
|
|
|
|
|
|
|
|
// Do the transfer
|
|
|
|
for _, i := range config.Domains {
|
|
|
|
server := fmt.Sprintf("%s:%d", config.Resolver, config.ResolverPort)
|
|
|
|
tr := dns.Transfer{}
|
|
|
|
m := &dns.Msg{}
|
|
|
|
m.SetAxfr(dns.Fqdn(i))
|
|
|
|
in, err := tr.In(m, server)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
for ex := range in {
|
|
|
|
for _, a := range ex.RR {
|
|
|
|
var ip, hostname, txt string
|
|
|
|
switch v := a.(type) {
|
|
|
|
case *dns.TXT:
|
|
|
|
txt = string(v.Txt[0])
|
|
|
|
hostname = v.Hdr.Name
|
|
|
|
cip, err := lookupName(strings.TrimRight(v.Hdr.Name, "."), server)
|
|
|
|
if err != nil || cip == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ip = cip
|
|
|
|
case *dns.A:
|
|
|
|
ip = v.A.String()
|
|
|
|
hostname = v.Hdr.Name
|
|
|
|
case *dns.CNAME:
|
|
|
|
cip, err := lookupName(v.Target, server)
|
|
|
|
if err != nil || cip == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ip = cip
|
|
|
|
hostname = v.Hdr.Name
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
2018-11-30 18:27:51 +00:00
|
|
|
data = append(data, fmt.Sprintf("%v %v %v", strings.TrimRight(hostname, "."), ip, txt))
|
2018-11-27 04:56:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-30 20:21:31 +00:00
|
|
|
// fmt.Println(data)
|
2018-11-30 20:20:43 +00:00
|
|
|
// resultsToJSON(data)
|
|
|
|
|
|
|
|
return data
|
2018-11-30 18:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func resultsToJSON(data []string) {
|
2018-11-30 20:20:43 +00:00
|
|
|
jsonData := make([][]byte, 0)
|
2018-11-30 18:24:57 +00:00
|
|
|
for _, i := range data {
|
|
|
|
splitStrings := strings.Split(i, " ")
|
2018-11-30 18:27:18 +00:00
|
|
|
hostname := splitStrings[0]
|
|
|
|
ip := splitStrings[1]
|
|
|
|
txt := splitStrings[2]
|
|
|
|
dns := &model.Results{IP: ip, Hostname: hostname, TXT: txt}
|
2018-11-30 18:24:57 +00:00
|
|
|
b, err := json.Marshal(dns)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2018-11-30 20:20:43 +00:00
|
|
|
jsonData = append(jsonData, b)
|
2018-11-30 18:24:57 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-27 04:56:21 +00:00
|
|
|
}
|