Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dnsp

import (
"log"
"net"
"regexp"
"sync"
"time"
Expand Down Expand Up @@ -37,6 +39,12 @@ type Server struct {
}
}

const (
notIPQuery = 0
_IP4Query = 4
_IP6Query = 6
)

// NewServer creates a new Server with the given options.
func NewServer(o Options) (*Server, error) {
if err := o.validate(); err != nil {
Expand Down Expand Up @@ -75,9 +83,39 @@ func NewServer(o Options) (*Server, error) {
return
}

q := r.Question[0]

// Filter Questions:
if r.Question = s.filter(r.Question); len(r.Question) == 0 {
w.WriteMsg(r)
IPQuery := s.isIPQuery(q)
m := new(dns.Msg)
m.SetReply(r)

switch IPQuery {
case _IP4Query:
rr_header := dns.RR_Header{
Name: q.Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 600,
}

a := &dns.A{rr_header, net.ParseIP("192.168.1.117").To4()}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't just use this random IP because it may be an actual IP to some user later. In an ideal world I would think we would just return an NXDOMAIN result.

m.Answer = append(m.Answer, a)

case _IP6Query:
rr_header := dns.RR_Header{
Name: q.Name,
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET,
Ttl: 600,
}
aaaa := &dns.AAAA{rr_header, net.ParseIP("192.168.1.117").To16()}
m.Answer = append(m.Answer, aaaa)
}

log.Printf("blocked: %s", q.Name)
w.WriteMsg(m)
return
}

Expand All @@ -95,6 +133,21 @@ func NewServer(o Options) (*Server, error) {
return &s, nil
}

func (h *Server) isIPQuery(q dns.Question) int {
if q.Qclass != dns.ClassINET {
return notIPQuery
}

switch q.Qtype {
case dns.TypeA:
return _IP4Query
case dns.TypeAAAA:
return _IP6Query
default:
return notIPQuery
}
}

// ListenAndServe runs the server
func (s *Server) ListenAndServe() error {
return s.s.ListenAndServe()
Expand Down