Skip to content

Commit

Permalink
Improved query logging
Browse files Browse the repository at this point in the history
  • Loading branch information
virtualzone committed Jan 10, 2023
1 parent 3878088 commit 1d5a977
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 8 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.2.1
v0.2.2
107 changes: 107 additions & 0 deletions logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package main

import (
"log"
"net"

"github.com/miekg/dns"
)

var queryTypeNames = map[uint16]string{
dns.TypeNone: "None",
dns.TypeA: "A",
dns.TypeNS: "NS",
dns.TypeMD: "MD",
dns.TypeMF: "MF",
dns.TypeCNAME: "CNAME",
dns.TypeSOA: "SOA",
dns.TypeMB: "MB",
dns.TypeMG: "MG",
dns.TypeMR: "MR",
dns.TypeNULL: "NULL",
dns.TypePTR: "PTR",
dns.TypeHINFO: "HINFO",
dns.TypeMINFO: "MINFO",
dns.TypeMX: "MX",
dns.TypeTXT: "TXT",
dns.TypeRP: "RP",
dns.TypeAFSDB: "AFSDB",
dns.TypeX25: "X25",
dns.TypeISDN: "ISDN",
dns.TypeRT: "RT",
dns.TypeNSAPPTR: "NSAPPTR",
dns.TypeSIG: "SIG",
dns.TypeKEY: "KEY",
dns.TypePX: "PX",
dns.TypeGPOS: "GPOS",
dns.TypeAAAA: "AAAA",
dns.TypeLOC: "LOC",
dns.TypeNXT: "NXT",
dns.TypeEID: "EID",
dns.TypeNIMLOC: "NIMLOC",
dns.TypeSRV: "SRV",
dns.TypeATMA: "ATMA",
dns.TypeNAPTR: "NAPTR",
dns.TypeKX: "KX",
dns.TypeCERT: "CERT",
dns.TypeDNAME: "DNAME",
dns.TypeOPT: "OPT",
dns.TypeAPL: "APL",
dns.TypeDS: "DS",
dns.TypeSSHFP: "SSHFP",
dns.TypeRRSIG: "RRSIG",
dns.TypeNSEC: "NSEC",
dns.TypeDNSKEY: "DNSKEY",
dns.TypeDHCID: "DHCID",
dns.TypeNSEC3: "NSEC3",
dns.TypeNSEC3PARAM: "NSEC3PARAM",
dns.TypeTLSA: "TLSA",
dns.TypeSMIMEA: "SMIMEA",
dns.TypeHIP: "HIP",
dns.TypeNINFO: "NINFO",
dns.TypeRKEY: "RKEY",
dns.TypeTALINK: "TALINK",
dns.TypeCDS: "CDS",
dns.TypeCDNSKEY: "CDNSKEY",
dns.TypeOPENPGPKEY: "OPENPGPKEY",
dns.TypeCSYNC: "CSYNC",
dns.TypeZONEMD: "ZONEMD",
dns.TypeSVCB: "SVCB",
dns.TypeHTTPS: "HTTPS",
dns.TypeSPF: "SPF",
dns.TypeUINFO: "UINFO",
dns.TypeUID: "UID",
dns.TypeGID: "GID",
dns.TypeUNSPEC: "UNSPEC",
dns.TypeNID: "NID",
dns.TypeL32: "L32",
dns.TypeL64: "L64",
dns.TypeLP: "LP",
dns.TypeEUI48: "EUI48",
dns.TypeEUI64: "EUI64",
dns.TypeURI: "URI",
dns.TypeCAA: "CAA",
dns.TypeAVC: "AVC",
dns.TypeTKEY: "TKEY",
dns.TypeTSIG: "TSIG",
dns.TypeIXFR: "IXFR",
dns.TypeAXFR: "AXFR",
dns.TypeMAILB: "MAILB",
dns.TypeMAILA: "MAILA",
dns.TypeANY: "ANY",
dns.TypeTA: "TA",
dns.TypeDLV: "DLV",
dns.TypeReserved: "Reserved",
}

func getQueryTypeText(qtype uint16) string {
res := queryTypeNames[qtype]
if res == "" {
res = "Unknown"
}
return res
}

func logQueryResult(source net.Addr, name string, qtype uint16, result string) {
log.Printf("Query from %s for %s type %s %s\n", source.String(), name, getQueryTypeText(qtype), result)
}
13 changes: 7 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package main

import (
"log"
"net"
"strings"

"github.com/miekg/dns"
)

func parseQuery(m *dns.Msg) {
func parseQuery(source net.Addr, m *dns.Msg) {
for _, q := range m.Question {
name := strings.ToLower(q.Name)
found := false
Expand All @@ -16,27 +17,27 @@ func parseQuery(m *dns.Msg) {
if err == nil {
found = true
m.Answer = append(m.Answer, arr...)
log.Printf("Query for %s resolved as local address\n", name)
logQueryResult(source, name, q.Qtype, "resolved as local address")
}
}
if !found {
arr, err := queryBlacklist(name, q.Qtype)
if err == nil {
found = true
m.Answer = append(m.Answer, arr...)
log.Printf("Query for %s resolved as blacklisted name\n", name)
logQueryResult(source, name, q.Qtype, "resolved as blacklisted name")
}
}
if !found {
arr, err := queryUpstream(name, q.Qtype)
if err == nil {
found = true
m.Answer = append(m.Answer, arr...)
log.Printf("Query for %s resolved via upstream\n", name)
logQueryResult(source, name, q.Qtype, "resolved via upstream")
}
}
if !found {
log.Printf("Query for %s did not resolve\n", name)
logQueryResult(source, name, q.Qtype, "did not resolve")
}
}
}
Expand All @@ -48,7 +49,7 @@ func handleDnsRequest(w dns.ResponseWriter, r *dns.Msg) {

switch r.Opcode {
case dns.OpcodeQuery:
parseQuery(m)
parseQuery(w.RemoteAddr(), m)
}

w.WriteMsg(m)
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// File autogenerated from VERSION, do not change manually!
package main

const AppVersion = "v0.2.1"
const AppVersion = "v0.2.2"

0 comments on commit 1d5a977

Please sign in to comment.