2021-01-13 17:07:04 +00:00
|
|
|
package mock
|
2020-04-01 22:58:44 +00:00
|
|
|
|
|
|
|
import (
|
2021-01-07 21:00:12 +00:00
|
|
|
"net"
|
|
|
|
|
2020-04-01 22:58:44 +00:00
|
|
|
"github.com/miekg/dns"
|
2020-10-02 09:56:48 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2021-01-07 21:00:12 +00:00
|
|
|
"gitlab.com/inetmock/inetmock/pkg/audit"
|
|
|
|
"gitlab.com/inetmock/inetmock/pkg/audit/details"
|
2020-12-26 13:11:49 +00:00
|
|
|
"gitlab.com/inetmock/inetmock/pkg/logging"
|
2020-04-01 22:58:44 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type regexHandler struct {
|
2021-01-07 21:00:12 +00:00
|
|
|
handlerName string
|
|
|
|
routes []resolverRule
|
|
|
|
fallback ResolverFallback
|
|
|
|
auditEmitter audit.Emitter
|
|
|
|
logger logging.Logger
|
2020-04-01 22:58:44 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 09:56:48 +00:00
|
|
|
func (rh *regexHandler) AddRule(rule resolverRule) {
|
|
|
|
rh.routes = append(rh.routes, rule)
|
2020-04-01 22:58:44 +00:00
|
|
|
}
|
|
|
|
|
2021-01-07 21:00:12 +00:00
|
|
|
func (rh *regexHandler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
|
2020-10-02 09:56:48 +00:00
|
|
|
timer := prometheus.NewTimer(requestDurationHistogram.WithLabelValues(rh.handlerName))
|
|
|
|
defer func() {
|
|
|
|
timer.ObserveDuration()
|
|
|
|
}()
|
|
|
|
|
2021-01-07 21:00:12 +00:00
|
|
|
rh.recordRequest(r, w.LocalAddr(), w.RemoteAddr())
|
|
|
|
|
2020-04-01 22:58:44 +00:00
|
|
|
m := new(dns.Msg)
|
|
|
|
m.Compress = false
|
|
|
|
m.SetReply(r)
|
|
|
|
|
2021-01-07 21:00:12 +00:00
|
|
|
if r.Opcode == dns.OpcodeQuery {
|
2020-10-02 09:56:48 +00:00
|
|
|
rh.handleQuery(m)
|
2020-04-01 22:58:44 +00:00
|
|
|
}
|
|
|
|
if err := w.WriteMsg(m); err != nil {
|
2020-10-02 09:56:48 +00:00
|
|
|
rh.logger.Error(
|
2020-04-01 22:58:44 +00:00
|
|
|
"Failed to write DNS response message",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 21:00:12 +00:00
|
|
|
func (rh *regexHandler) handleQuery(m *dns.Msg) {
|
2020-04-01 22:58:44 +00:00
|
|
|
for _, q := range m.Question {
|
|
|
|
switch q.Qtype {
|
|
|
|
case dns.TypeA:
|
2020-10-02 09:56:48 +00:00
|
|
|
totalHandledRequestsCounter.WithLabelValues(rh.handlerName).Inc()
|
|
|
|
for _, rule := range rh.routes {
|
2021-01-07 21:00:12 +00:00
|
|
|
if !rule.pattern.MatchString(q.Name) {
|
|
|
|
continue
|
2020-04-01 22:58:44 +00:00
|
|
|
}
|
2021-01-07 21:00:12 +00:00
|
|
|
m.Authoritative = true
|
|
|
|
answer := &dns.A{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: q.Name,
|
|
|
|
Rrtype: dns.TypeA,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
Ttl: 60,
|
|
|
|
},
|
|
|
|
A: rule.response,
|
|
|
|
}
|
|
|
|
m.Answer = append(m.Answer, answer)
|
|
|
|
rh.logger.Info(
|
|
|
|
"matched DNS rule",
|
|
|
|
zap.String("pattern", rule.pattern.String()),
|
|
|
|
zap.String("response", rule.response.String()),
|
|
|
|
)
|
|
|
|
return
|
2020-04-01 22:58:44 +00:00
|
|
|
}
|
2020-10-02 09:56:48 +00:00
|
|
|
rh.handleFallbackForMessage(m, q)
|
|
|
|
default:
|
|
|
|
unhandledRequestsCounter.WithLabelValues(rh.handlerName).Inc()
|
|
|
|
rh.logger.Warn(
|
|
|
|
"Unhandled DNS question type - no response will be sent",
|
|
|
|
zap.Uint16("question_type", q.Qtype),
|
|
|
|
)
|
2020-04-01 22:58:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 21:00:12 +00:00
|
|
|
func (rh *regexHandler) handleFallbackForMessage(m *dns.Msg, q dns.Question) {
|
2020-10-02 09:56:48 +00:00
|
|
|
fallbackIP := rh.fallback.GetIP()
|
2020-04-01 22:58:44 +00:00
|
|
|
answer := &dns.A{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: q.Name,
|
|
|
|
Rrtype: dns.TypeA,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
Ttl: 60,
|
|
|
|
},
|
|
|
|
A: fallbackIP,
|
|
|
|
}
|
2020-10-02 09:56:48 +00:00
|
|
|
rh.logger.Info(
|
2020-04-01 22:58:44 +00:00
|
|
|
"Falling back to generated IP",
|
|
|
|
zap.String("response", fallbackIP.String()),
|
|
|
|
)
|
|
|
|
m.Authoritative = true
|
|
|
|
m.Answer = append(m.Answer, answer)
|
|
|
|
}
|
2021-01-07 21:00:12 +00:00
|
|
|
|
|
|
|
func (rh *regexHandler) recordRequest(m *dns.Msg, localAddr, remoteAddr net.Addr) {
|
|
|
|
dnsDetails := &details.DNS{
|
|
|
|
OPCode: details.DNSOpCode(m.Opcode),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, q := range m.Question {
|
|
|
|
dnsDetails.Questions = append(dnsDetails.Questions, details.DNSQuestion{
|
|
|
|
RRType: details.ResourceRecordType(q.Qtype),
|
|
|
|
Name: q.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
ev := audit.Event{
|
|
|
|
Transport: guessTransportFromAddr(localAddr),
|
|
|
|
Application: audit.AppProtocol_DNS,
|
|
|
|
ProtocolDetails: dnsDetails,
|
|
|
|
}
|
|
|
|
|
|
|
|
ev.SetSourceIPFromAddr(remoteAddr)
|
|
|
|
ev.SetDestinationIPFromAddr(localAddr)
|
|
|
|
|
|
|
|
rh.auditEmitter.Emit(ev)
|
|
|
|
}
|
|
|
|
|
|
|
|
func guessTransportFromAddr(addr net.Addr) audit.TransportProtocol {
|
|
|
|
switch addr.(type) {
|
|
|
|
case *net.TCPAddr:
|
|
|
|
return audit.TransportProtocol_TCP
|
|
|
|
case *net.UDPAddr:
|
|
|
|
return audit.TransportProtocol_UDP
|
|
|
|
default:
|
|
|
|
return audit.TransportProtocol_UNKNOWN_TRANSPORT
|
|
|
|
}
|
|
|
|
}
|