Files
routedns/blocklistdb-regexp.go
Frank Olbricht 654fd43c83 Return PTR records for hosts file blocklists (#115)
* Return PTR records for hosts file blocklists

* Undo testing changes in example file
2020-12-29 18:19:26 -07:00

57 lines
1.1 KiB
Go

package rdns
import (
"net"
"regexp"
"strings"
"github.com/miekg/dns"
)
// RegexpDB holds a list of regular expressions against which it evaluates DNS queries.
type RegexpDB struct {
rules []*regexp.Regexp
loader BlocklistLoader
}
var _ BlocklistDB = &RegexpDB{}
// NewRegexpDB returns a new instance of a matcher for a list of regular expressions.
func NewRegexpDB(loader BlocklistLoader) (*RegexpDB, error) {
rules, err := loader.Load()
if err != nil {
return nil, err
}
var filters []*regexp.Regexp
for _, r := range rules {
r = strings.TrimSpace(r)
if r == "" || strings.HasPrefix(r, "#") {
continue
}
re, err := regexp.Compile(r)
if err != nil {
return nil, err
}
filters = append(filters, re)
}
return &RegexpDB{filters, loader}, nil
}
func (m *RegexpDB) Reload() (BlocklistDB, error) {
return NewRegexpDB(m.loader)
}
func (m *RegexpDB) Match(q dns.Question) (net.IP, string, string, bool) {
for _, rule := range m.rules {
if rule.MatchString(q.Name) {
return nil, "", rule.String(), true
}
}
return nil, "", "", false
}
func (m *RegexpDB) String() string {
return "Regexp"
}