sacrebleu-dns/core/parseQuery.go

44 lines
1.3 KiB
Go
Raw Normal View History

2020-12-13 03:01:04 +00:00
package core
import (
"fmt"
"github.com/miekg/dns"
2020-12-28 00:34:26 +00:00
"github.com/outout14/sacrebleu-api/api/types"
2020-12-13 18:26:55 +00:00
"github.com/outout14/sacrebleu-dns/utils"
2020-12-13 03:01:04 +00:00
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
)
/*
Qtype memo
A = 1
NS = 2
PTR = 12
TXT = 16
AAAA = 28
*/
//Function called by handleDnsRequest to parse the query from records
2020-12-14 22:20:24 +00:00
//Requires dns.ReponseWriter args
2020-12-13 03:01:04 +00:00
func parseQuery(m *dns.Msg) {
for _, q := range m.Question {
log.Infof("DNS : Query for %s (type : %v)\n", q.Name, q.Qtype) //Log
2020-12-28 00:34:26 +00:00
records := utils.GetRecord(types.Record{Fqdn: q.Name, Qtype: q.Qtype}) //Get the record in the SQL or Redis database
2020-12-27 23:05:16 +00:00
for _, record := range records {
if record.Content != "" { //If the record is not empty
log.Infof("DNS : Record found for '%s' => '%s'\n", q.Name, record.Content) //Log the content as INFO
rr, err := dns.NewRR(fmt.Sprintf("%s %v %s %s", q.Name, record.TTL, dns.TypeToString[q.Qtype], record.Content)) //Create the response
if err == nil { //If no err
m.Answer = append(m.Answer, rr) //Append the record to the response
}
} else { //If the record is empty log it as DEBUG
logrus.Debugf("DNS : No record for '%s' (type '%v')\n", record.Fqdn, record.Qtype)
2020-12-13 03:01:04 +00:00
}
}
}
}