mirror of https://github.com/0xERR0R/blocky.git
moving model types to the separate package
This commit is contained in:
parent
c866b71860
commit
63735546bb
|
@ -0,0 +1,76 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// ResponseType represents the type of the response
|
||||
type ResponseType int
|
||||
|
||||
const (
|
||||
// RESOLVED the response was resolved by the external upstream resolver
|
||||
RESOLVED ResponseType = iota
|
||||
|
||||
// CACHED the response was resolved from cache
|
||||
CACHED
|
||||
|
||||
// BLOCKED the query was blocked
|
||||
BLOCKED
|
||||
|
||||
// CONDITIONAL the query was resolved by the conditional upstream resolver
|
||||
CONDITIONAL
|
||||
|
||||
// CUSTOMDNS the query was resolved by a custom rule
|
||||
CUSTOMDNS
|
||||
)
|
||||
|
||||
func (r ResponseType) String() string {
|
||||
names := [...]string{
|
||||
"RESOLVED",
|
||||
"CACHED",
|
||||
"BLOCKED",
|
||||
"CONDITIONAL",
|
||||
"CUSTOMDNS"}
|
||||
|
||||
return names[r]
|
||||
}
|
||||
|
||||
// Response represents the response of a DNS query
|
||||
type Response struct {
|
||||
Res *dns.Msg
|
||||
Reason string
|
||||
RType ResponseType
|
||||
}
|
||||
|
||||
// RequestProtocol represents the server protocol
|
||||
type RequestProtocol uint8
|
||||
|
||||
const (
|
||||
// TCP is the TPC protocol
|
||||
TCP RequestProtocol = iota
|
||||
|
||||
// UDP is the UDP protocol
|
||||
UDP
|
||||
)
|
||||
|
||||
func (r RequestProtocol) String() string {
|
||||
names := [...]string{
|
||||
"TCP",
|
||||
"UDP"}
|
||||
|
||||
return names[r]
|
||||
}
|
||||
|
||||
// Request represents client's DNS request
|
||||
type Request struct {
|
||||
ClientIP net.IP
|
||||
Protocol RequestProtocol
|
||||
ClientNames []string
|
||||
Req *dns.Msg
|
||||
Log *logrus.Entry
|
||||
RequestTS time.Time
|
||||
}
|
|
@ -12,6 +12,7 @@ import (
|
|||
"github.com/0xERR0R/blocky/evt"
|
||||
"github.com/0xERR0R/blocky/lists"
|
||||
"github.com/0xERR0R/blocky/log"
|
||||
"github.com/0xERR0R/blocky/model"
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
|
@ -202,7 +203,7 @@ func determineWhitelistOnlyGroups(cfg *config.BlockingConfig) (result map[string
|
|||
|
||||
// sets answer and/or return code for DNS response, if request should be blocked
|
||||
func (r *BlockingResolver) handleBlocked(logger *logrus.Entry,
|
||||
request *Request, question dns.Question, reason string) (*Response, error) {
|
||||
request *model.Request, question dns.Question, reason string) (*model.Response, error) {
|
||||
response := new(dns.Msg)
|
||||
response.SetReply(request.Req)
|
||||
|
||||
|
@ -210,7 +211,7 @@ func (r *BlockingResolver) handleBlocked(logger *logrus.Entry,
|
|||
|
||||
logger.Debugf("blocking request '%s'", reason)
|
||||
|
||||
return &Response{Res: response, RType: BLOCKED, Reason: reason}, nil
|
||||
return &model.Response{Res: response, RType: model.BLOCKED, Reason: reason}, nil
|
||||
}
|
||||
|
||||
// Configuration returns the current resolver configuration
|
||||
|
@ -256,7 +257,7 @@ func (r *BlockingResolver) hasWhiteListOnlyAllowed(groupsToCheck []string) bool
|
|||
}
|
||||
|
||||
func (r *BlockingResolver) handleBlacklist(groupsToCheck []string,
|
||||
request *Request, logger *logrus.Entry) (*Response, error) {
|
||||
request *model.Request, logger *logrus.Entry) (*model.Response, error) {
|
||||
logger.WithField("groupsToCheck", strings.Join(groupsToCheck, "; ")).Debug("checking groups for request")
|
||||
whitelistOnlyAllowed := r.hasWhiteListOnlyAllowed(groupsToCheck)
|
||||
|
||||
|
@ -282,7 +283,7 @@ func (r *BlockingResolver) handleBlacklist(groupsToCheck []string,
|
|||
}
|
||||
|
||||
// Resolve checks the query against the blacklist and delegates to next resolver if domain is not blocked
|
||||
func (r *BlockingResolver) Resolve(request *Request) (*Response, error) {
|
||||
func (r *BlockingResolver) Resolve(request *model.Request) (*model.Response, error) {
|
||||
logger := withPrefix(request.Log, "blacklist_resolver")
|
||||
groupsToCheck := r.groupsToCheckForClient(request)
|
||||
|
||||
|
@ -340,7 +341,7 @@ func (r *BlockingResolver) isGroupDisabled(group string) bool {
|
|||
}
|
||||
|
||||
// returns groups which should be checked for client's request
|
||||
func (r *BlockingResolver) groupsToCheckForClient(request *Request) []string {
|
||||
func (r *BlockingResolver) groupsToCheckForClient(request *model.Request) []string {
|
||||
var groups []string
|
||||
// try client names
|
||||
for _, cName := range request.ClientNames {
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
. "github.com/0xERR0R/blocky/helpertest"
|
||||
"github.com/0xERR0R/blocky/lists"
|
||||
. "github.com/0xERR0R/blocky/log"
|
||||
. "github.com/0xERR0R/blocky/model"
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
|
||||
"os"
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
|
||||
"github.com/0xERR0R/blocky/config"
|
||||
"github.com/0xERR0R/blocky/evt"
|
||||
"github.com/0xERR0R/blocky/model"
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
|
||||
"github.com/0xERR0R/go-cache"
|
||||
|
@ -123,7 +124,7 @@ func (r *CachingResolver) Configuration() (result []string) {
|
|||
// Resolve checks if the current query result is already in the cache and returns it
|
||||
// or delegates to the next resolver
|
||||
//nolint:gocognit,funlen
|
||||
func (r *CachingResolver) Resolve(request *Request) (response *Response, err error) {
|
||||
func (r *CachingResolver) Resolve(request *model.Request) (response *model.Response, err error) {
|
||||
logger := withPrefix(request.Log, "caching_resolver")
|
||||
|
||||
if r.maxCacheTimeSec < 0 {
|
||||
|
@ -164,12 +165,12 @@ func (r *CachingResolver) Resolve(request *Request) (response *Response, err err
|
|||
rr.Header().Ttl = remainingTTL
|
||||
}
|
||||
|
||||
return &Response{Res: resp, RType: CACHED, Reason: "CACHED"}, nil
|
||||
return &model.Response{Res: resp, RType: model.CACHED, Reason: "CACHED"}, nil
|
||||
}
|
||||
// Answer with response code != OK
|
||||
resp.Rcode = val.(int)
|
||||
|
||||
return &Response{Res: resp, RType: CACHED, Reason: "CACHED NEGATIVE"}, nil
|
||||
return &model.Response{Res: resp, RType: model.CACHED, Reason: "CACHED NEGATIVE"}, nil
|
||||
}
|
||||
|
||||
evt.Bus().Publish(evt.CachingResultCacheMiss, domain)
|
||||
|
@ -199,7 +200,7 @@ func (r *CachingResolver) trackQueryDomainNameCount(domain string, cacheKey stri
|
|||
}
|
||||
}
|
||||
|
||||
func (r *CachingResolver) putInCache(cacheKey string, response *Response, prefetch bool) {
|
||||
func (r *CachingResolver) putInCache(cacheKey string, response *model.Response, prefetch bool) {
|
||||
answer := response.Res.Answer
|
||||
|
||||
if response.Res.Rcode == dns.RcodeSuccess {
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"github.com/0xERR0R/blocky/config"
|
||||
. "github.com/0xERR0R/blocky/evt"
|
||||
. "github.com/0xERR0R/blocky/helpertest"
|
||||
. "github.com/0xERR0R/blocky/model"
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
|
||||
"github.com/0xERR0R/go-cache"
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/0xERR0R/blocky/config"
|
||||
"github.com/0xERR0R/blocky/model"
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
|
||||
"github.com/0xERR0R/go-cache"
|
||||
|
@ -63,7 +64,7 @@ func (r *ClientNamesResolver) Configuration() (result []string) {
|
|||
}
|
||||
|
||||
// Resolve tries to resolve the client name from the ip address
|
||||
func (r *ClientNamesResolver) Resolve(request *Request) (*Response, error) {
|
||||
func (r *ClientNamesResolver) Resolve(request *model.Request) (*model.Response, error) {
|
||||
clientNames := r.getClientNames(request)
|
||||
|
||||
request.ClientNames = clientNames
|
||||
|
@ -73,7 +74,7 @@ func (r *ClientNamesResolver) Resolve(request *Request) (*Response, error) {
|
|||
}
|
||||
|
||||
// returns names of client
|
||||
func (r *ClientNamesResolver) getClientNames(request *Request) []string {
|
||||
func (r *ClientNamesResolver) getClientNames(request *model.Request) []string {
|
||||
ip := request.ClientIP
|
||||
|
||||
if ip == nil {
|
||||
|
@ -106,7 +107,7 @@ func (r *ClientNamesResolver) resolveClientNames(ip net.IP, logger *logrus.Entry
|
|||
if r.externalResolver != nil {
|
||||
reverse, _ := dns.ReverseAddr(ip.String())
|
||||
|
||||
resp, err := r.externalResolver.Resolve(&Request{
|
||||
resp, err := r.externalResolver.Resolve(&model.Request{
|
||||
Req: util.NewMsgWithQuestion(reverse, dns.TypePTR),
|
||||
Log: logger,
|
||||
})
|
||||
|
|
|
@ -8,6 +8,8 @@ import (
|
|||
"github.com/0xERR0R/blocky/config"
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
|
||||
. "github.com/0xERR0R/blocky/model"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/0xERR0R/blocky/config"
|
||||
"github.com/0xERR0R/blocky/model"
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
|
@ -65,7 +66,7 @@ func (r *ConditionalUpstreamResolver) applyRewrite(domain string) string {
|
|||
}
|
||||
|
||||
// Resolve uses the conditional resolver to resolve the query
|
||||
func (r *ConditionalUpstreamResolver) Resolve(request *Request) (*Response, error) {
|
||||
func (r *ConditionalUpstreamResolver) Resolve(request *model.Request) (*model.Response, error) {
|
||||
logger := withPrefix(request.Log, "conditional_resolver")
|
||||
|
||||
if len(r.mapping) > 0 {
|
||||
|
@ -82,7 +83,7 @@ func (r *ConditionalUpstreamResolver) Resolve(request *Request) (*Response, erro
|
|||
|
||||
if err == nil {
|
||||
response.Reason = "CONDITIONAL"
|
||||
response.RType = CONDITIONAL
|
||||
response.RType = model.CONDITIONAL
|
||||
response.Res.Question[0].Name = question.Name
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"github.com/0xERR0R/blocky/config"
|
||||
. "github.com/0xERR0R/blocky/helpertest"
|
||||
"github.com/0xERR0R/blocky/log"
|
||||
. "github.com/0xERR0R/blocky/model"
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/0xERR0R/blocky/config"
|
||||
"github.com/0xERR0R/blocky/model"
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
|
@ -56,7 +57,7 @@ func isSupportedType(ip net.IP, question dns.Question) bool {
|
|||
(strings.Contains(ip.String(), ":") && question.Qtype == dns.TypeAAAA)
|
||||
}
|
||||
|
||||
func (r *CustomDNSResolver) handleReverseDNS(request *Request) *Response {
|
||||
func (r *CustomDNSResolver) handleReverseDNS(request *model.Request) *model.Response {
|
||||
question := request.Req.Question[0]
|
||||
if question.Qtype == dns.TypePTR {
|
||||
urls, found := r.reverseAddresses[question.Name]
|
||||
|
@ -72,7 +73,7 @@ func (r *CustomDNSResolver) handleReverseDNS(request *Request) *Response {
|
|||
response.Answer = append(response.Answer, ptr)
|
||||
}
|
||||
|
||||
return &Response{Res: response, RType: CUSTOMDNS, Reason: "CUSTOM DNS"}
|
||||
return &model.Response{Res: response, RType: model.CUSTOMDNS, Reason: "CUSTOM DNS"}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -80,7 +81,7 @@ func (r *CustomDNSResolver) handleReverseDNS(request *Request) *Response {
|
|||
}
|
||||
|
||||
// Resolve uses internal mapping to resolve the query
|
||||
func (r *CustomDNSResolver) Resolve(request *Request) (*Response, error) {
|
||||
func (r *CustomDNSResolver) Resolve(request *model.Request) (*model.Response, error) {
|
||||
logger := withPrefix(request.Log, "custom_dns_resolver")
|
||||
|
||||
reverseResp := r.handleReverseDNS(request)
|
||||
|
@ -111,13 +112,13 @@ func (r *CustomDNSResolver) Resolve(request *Request) (*Response, error) {
|
|||
"domain": domain,
|
||||
}).Debugf("returning custom dns entry")
|
||||
|
||||
return &Response{Res: response, RType: CUSTOMDNS, Reason: "CUSTOM DNS"}, nil
|
||||
return &model.Response{Res: response, RType: model.CUSTOMDNS, Reason: "CUSTOM DNS"}, nil
|
||||
}
|
||||
|
||||
// Mapping exists for this domain, but for another type
|
||||
// return NOERROR with empty result
|
||||
|
||||
return &Response{Res: response, RType: CUSTOMDNS, Reason: "CUSTOM DNS"}, nil
|
||||
return &model.Response{Res: response, RType: model.CUSTOMDNS, Reason: "CUSTOM DNS"}, nil
|
||||
}
|
||||
|
||||
if i := strings.Index(domain, "."); i >= 0 {
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
|
||||
"github.com/0xERR0R/blocky/config"
|
||||
. "github.com/0xERR0R/blocky/helpertest"
|
||||
. "github.com/0xERR0R/blocky/model"
|
||||
"github.com/miekg/dns"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package resolver
|
||||
|
||||
import "github.com/miekg/dns"
|
||||
import (
|
||||
"github.com/0xERR0R/blocky/model"
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// IPv6Checker can drop all AAAA query (empty ANSWER with NOERROR)
|
||||
type IPv6Checker struct {
|
||||
|
@ -8,12 +11,12 @@ type IPv6Checker struct {
|
|||
disableAAAA bool
|
||||
}
|
||||
|
||||
func (r *IPv6Checker) Resolve(request *Request) (*Response, error) {
|
||||
func (r *IPv6Checker) Resolve(request *model.Request) (*model.Response, error) {
|
||||
if r.disableAAAA && request.Req.Question[0].Qtype == dns.TypeAAAA {
|
||||
response := new(dns.Msg)
|
||||
response.SetRcode(request.Req, dns.RcodeSuccess)
|
||||
|
||||
return &Response{Res: response, RType: RESOLVED}, nil
|
||||
return &model.Response{Res: response, RType: model.RESOLVED}, nil
|
||||
}
|
||||
|
||||
return r.next.Resolve(request)
|
||||
|
|
|
@ -3,6 +3,8 @@ package resolver
|
|||
import (
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
|
||||
. "github.com/0xERR0R/blocky/model"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
"github.com/0xERR0R/blocky/config"
|
||||
"github.com/0xERR0R/blocky/metrics"
|
||||
"github.com/0xERR0R/blocky/model"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
|
@ -23,7 +24,7 @@ type MetricsResolver struct {
|
|||
}
|
||||
|
||||
// Resolve resolves the passed request
|
||||
func (m *MetricsResolver) Resolve(request *Request) (*Response, error) {
|
||||
func (m *MetricsResolver) Resolve(request *model.Request) (*model.Response, error) {
|
||||
response, err := m.next.Resolve(request)
|
||||
|
||||
if m.cfg.Enable {
|
||||
|
|
|
@ -5,6 +5,8 @@ import (
|
|||
|
||||
"github.com/0xERR0R/blocky/config"
|
||||
|
||||
. "github.com/0xERR0R/blocky/model"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
|
|
@ -11,6 +11,8 @@ import (
|
|||
"github.com/0xERR0R/blocky/config"
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
|
||||
"github.com/0xERR0R/blocky/model"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
@ -24,9 +26,9 @@ func (r *resolverMock) Configuration() (result []string) {
|
|||
return
|
||||
}
|
||||
|
||||
func (r *resolverMock) Resolve(req *Request) (*Response, error) {
|
||||
func (r *resolverMock) Resolve(req *model.Request) (*model.Response, error) {
|
||||
args := r.Called(req)
|
||||
resp, ok := args.Get(0).(*Response)
|
||||
resp, ok := args.Get(0).(*model.Response)
|
||||
|
||||
if ok {
|
||||
return resp, args.Error((1))
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/0xERR0R/blocky/config"
|
||||
"github.com/0xERR0R/blocky/model"
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
|
||||
"github.com/mroth/weightedrand"
|
||||
|
@ -30,7 +31,7 @@ type upstreamResolverStatus struct {
|
|||
}
|
||||
|
||||
type requestResponse struct {
|
||||
response *Response
|
||||
response *model.Response
|
||||
err error
|
||||
}
|
||||
|
||||
|
@ -95,7 +96,7 @@ func (r ParallelBestResolver) String() string {
|
|||
return fmt.Sprintf("parallel upstreams '%s'", strings.Join(result, "; "))
|
||||
}
|
||||
|
||||
func (r *ParallelBestResolver) resolversForClient(request *Request) (result []*upstreamResolverStatus) {
|
||||
func (r *ParallelBestResolver) resolversForClient(request *model.Request) (result []*upstreamResolverStatus) {
|
||||
// try client names
|
||||
for _, cName := range request.ClientNames {
|
||||
for clientDefinition, upstreams := range r.resolversPerClient {
|
||||
|
@ -128,7 +129,7 @@ func (r *ParallelBestResolver) resolversForClient(request *Request) (result []*u
|
|||
}
|
||||
|
||||
// Resolve sends the query request to multiple upstream resolvers and returns the fastest result
|
||||
func (r *ParallelBestResolver) Resolve(request *Request) (*Response, error) {
|
||||
func (r *ParallelBestResolver) Resolve(request *model.Request) (*model.Response, error) {
|
||||
logger := request.Log.WithField("prefix", parallelResolverLogger)
|
||||
|
||||
resolvers := r.resolversForClient(request)
|
||||
|
@ -206,7 +207,7 @@ func weightedRandom(in []*upstreamResolverStatus, exclude Resolver) *upstreamRes
|
|||
return c.Pick().(*upstreamResolverStatus)
|
||||
}
|
||||
|
||||
func resolve(req *Request, resolver *upstreamResolverStatus, ch chan<- requestResponse) {
|
||||
func resolve(req *model.Request, resolver *upstreamResolverStatus, ch chan<- requestResponse) {
|
||||
resp, err := resolver.resolver.Resolve(req)
|
||||
|
||||
// update the last error time
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"github.com/0xERR0R/blocky/config"
|
||||
. "github.com/0xERR0R/blocky/helpertest"
|
||||
. "github.com/0xERR0R/blocky/log"
|
||||
. "github.com/0xERR0R/blocky/model"
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
"github.com/miekg/dns"
|
||||
. "github.com/onsi/ginkgo"
|
||||
|
|
|
@ -12,6 +12,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/0xERR0R/blocky/config"
|
||||
"github.com/0xERR0R/blocky/model"
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
|
@ -35,8 +36,8 @@ type QueryLoggingResolver struct {
|
|||
}
|
||||
|
||||
type queryLogEntry struct {
|
||||
request *Request
|
||||
response *Response
|
||||
request *model.Request
|
||||
response *model.Response
|
||||
start time.Time
|
||||
durationMs int64
|
||||
logger *logrus.Entry
|
||||
|
@ -110,7 +111,7 @@ func (r *QueryLoggingResolver) doCleanUp() {
|
|||
}
|
||||
|
||||
// Resolve logs the query, duration and the result
|
||||
func (r *QueryLoggingResolver) Resolve(request *Request) (*Response, error) {
|
||||
func (r *QueryLoggingResolver) Resolve(request *model.Request) (*model.Response, error) {
|
||||
logger := withPrefix(request.Log, queryLoggingResolverPrefix)
|
||||
|
||||
start := time.Now()
|
||||
|
|
|
@ -13,6 +13,7 @@ import (
|
|||
|
||||
"github.com/0xERR0R/blocky/config"
|
||||
. "github.com/0xERR0R/blocky/log"
|
||||
. "github.com/0xERR0R/blocky/model"
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
|
|
|
@ -7,42 +7,13 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/0xERR0R/blocky/log"
|
||||
"github.com/0xERR0R/blocky/model"
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// RequestProtocol represents the server protocol
|
||||
type RequestProtocol uint8
|
||||
|
||||
const (
|
||||
// TCP is the TPC protocol
|
||||
TCP RequestProtocol = iota
|
||||
|
||||
// UDP is the UDP protocol
|
||||
UDP
|
||||
)
|
||||
|
||||
func (r RequestProtocol) String() string {
|
||||
names := [...]string{
|
||||
"TCP",
|
||||
"UDP"}
|
||||
|
||||
return names[r]
|
||||
}
|
||||
|
||||
// Request represents client's DNS request
|
||||
type Request struct {
|
||||
ClientIP net.IP
|
||||
Protocol RequestProtocol
|
||||
ClientNames []string
|
||||
Req *dns.Msg
|
||||
Log *logrus.Entry
|
||||
RequestTS time.Time
|
||||
}
|
||||
|
||||
func newRequest(question string, rType uint16, logger ...*logrus.Entry) *Request {
|
||||
func newRequest(question string, rType uint16, logger ...*logrus.Entry) *model.Request {
|
||||
var loggerEntry *logrus.Entry
|
||||
if len(logger) == 1 {
|
||||
loggerEntry = logger[0]
|
||||
|
@ -50,67 +21,29 @@ func newRequest(question string, rType uint16, logger ...*logrus.Entry) *Request
|
|||
loggerEntry = logrus.NewEntry(log.Log())
|
||||
}
|
||||
|
||||
return &Request{
|
||||
return &model.Request{
|
||||
Req: util.NewMsgWithQuestion(question, rType),
|
||||
Log: loggerEntry,
|
||||
Protocol: UDP,
|
||||
Protocol: model.UDP,
|
||||
}
|
||||
}
|
||||
|
||||
func newRequestWithClient(question string, rType uint16, ip string, clientNames ...string) *Request {
|
||||
return &Request{
|
||||
func newRequestWithClient(question string, rType uint16, ip string, clientNames ...string) *model.Request {
|
||||
return &model.Request{
|
||||
ClientIP: net.ParseIP(ip),
|
||||
ClientNames: clientNames,
|
||||
Req: util.NewMsgWithQuestion(question, rType),
|
||||
Log: logrus.NewEntry(log.Log()),
|
||||
RequestTS: time.Time{},
|
||||
Protocol: UDP,
|
||||
Protocol: model.UDP,
|
||||
}
|
||||
}
|
||||
|
||||
// ResponseType represents the type of the response
|
||||
type ResponseType int
|
||||
|
||||
const (
|
||||
// RESOLVED the response was resolved by the external upstream resolver
|
||||
RESOLVED ResponseType = iota
|
||||
|
||||
// CACHED the response was resolved from cache
|
||||
CACHED
|
||||
|
||||
// BLOCKED the query was blocked
|
||||
BLOCKED
|
||||
|
||||
// CONDITIONAL the query was resolved by the conditional upstream resolver
|
||||
CONDITIONAL
|
||||
|
||||
// CUSTOMDNS the query was resolved by a custom rule
|
||||
CUSTOMDNS
|
||||
)
|
||||
|
||||
func (r ResponseType) String() string {
|
||||
names := [...]string{
|
||||
"RESOLVED",
|
||||
"CACHED",
|
||||
"BLOCKED",
|
||||
"CONDITIONAL",
|
||||
"CUSTOMDNS"}
|
||||
|
||||
return names[r]
|
||||
}
|
||||
|
||||
// Response represents the response of a DNS query
|
||||
type Response struct {
|
||||
Res *dns.Msg
|
||||
Reason string
|
||||
RType ResponseType
|
||||
}
|
||||
|
||||
// Resolver generic interface for all resolvers
|
||||
type Resolver interface {
|
||||
|
||||
// Resolve performs resolution of a DNS request
|
||||
Resolve(req *Request) (*Response, error)
|
||||
Resolve(req *model.Request) (*model.Response, error)
|
||||
|
||||
// Configuration prints current resolver configuration
|
||||
Configuration() []string
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/0xERR0R/blocky/model"
|
||||
"github.com/0xERR0R/blocky/stats"
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
|
||||
|
@ -19,8 +20,8 @@ type StatsResolver struct {
|
|||
}
|
||||
|
||||
type statsEntry struct {
|
||||
request *Request
|
||||
response *Response
|
||||
request *model.Request
|
||||
response *model.Response
|
||||
}
|
||||
|
||||
type resolverStatRecorder struct {
|
||||
|
@ -51,7 +52,7 @@ func (r *StatsResolver) collectStats() {
|
|||
}
|
||||
|
||||
// Resolve calculates query statistics
|
||||
func (r *StatsResolver) Resolve(request *Request) (*Response, error) {
|
||||
func (r *StatsResolver) Resolve(request *model.Request) (*model.Response, error) {
|
||||
resp, err := r.next.Resolve(request)
|
||||
|
||||
if err == nil {
|
||||
|
@ -121,7 +122,7 @@ func createRecorders() []*resolverStatRecorder {
|
|||
return util.ExtractDomain(e.request.Req.Question[0])
|
||||
}),
|
||||
newRecorderWithMax("Top 20 blocked queries", 20, func(e *statsEntry) string {
|
||||
if e.response.RType == BLOCKED {
|
||||
if e.response.RType == model.BLOCKED {
|
||||
return util.ExtractDomain(e.request.Req.Question[0])
|
||||
}
|
||||
return ""
|
||||
|
|
|
@ -3,6 +3,8 @@ package resolver
|
|||
import (
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
|
||||
. "github.com/0xERR0R/blocky/model"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/0xERR0R/blocky/config"
|
||||
"github.com/0xERR0R/blocky/model"
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
|
@ -32,7 +33,7 @@ type UpstreamResolver struct {
|
|||
|
||||
type upstreamClient interface {
|
||||
callExternal(msg *dns.Msg, upstreamURL string,
|
||||
protocol RequestProtocol) (response *dns.Msg, rtt time.Duration, err error)
|
||||
protocol model.RequestProtocol) (response *dns.Msg, rtt time.Duration, err error)
|
||||
}
|
||||
|
||||
type dnsUpstreamClient struct {
|
||||
|
@ -82,7 +83,7 @@ func createUpstreamClient(cfg config.Upstream) (client upstreamClient, upstreamU
|
|||
}
|
||||
|
||||
func (r *httpUpstreamClient) callExternal(msg *dns.Msg,
|
||||
upstreamURL string, _ RequestProtocol) (*dns.Msg, time.Duration, error) {
|
||||
upstreamURL string, _ model.RequestProtocol) (*dns.Msg, time.Duration, error) {
|
||||
start := time.Now()
|
||||
|
||||
rawDNSMessage, err := msg.Pack()
|
||||
|
@ -127,8 +128,8 @@ func (r *httpUpstreamClient) callExternal(msg *dns.Msg,
|
|||
}
|
||||
|
||||
func (r *dnsUpstreamClient) callExternal(msg *dns.Msg,
|
||||
upstreamURL string, protocol RequestProtocol) (response *dns.Msg, rtt time.Duration, err error) {
|
||||
if protocol == TCP {
|
||||
upstreamURL string, protocol model.RequestProtocol) (response *dns.Msg, rtt time.Duration, err error) {
|
||||
if protocol == model.TCP {
|
||||
response, rtt, err = r.tcpClient.Exchange(msg, upstreamURL)
|
||||
if err != nil {
|
||||
// try UDP as fallback
|
||||
|
@ -170,7 +171,7 @@ func (r UpstreamResolver) String() string {
|
|||
}
|
||||
|
||||
// Resolve calls external resolver
|
||||
func (r *UpstreamResolver) Resolve(request *Request) (response *Response, err error) {
|
||||
func (r *UpstreamResolver) Resolve(request *model.Request) (response *model.Response, err error) {
|
||||
logger := withPrefix(request.Log, "upstream_resolver")
|
||||
|
||||
attempt := 1
|
||||
|
@ -190,7 +191,7 @@ func (r *UpstreamResolver) Resolve(request *Request) (response *Response, err er
|
|||
"response_time_ms": rtt.Milliseconds(),
|
||||
}).Debugf("received response from upstream")
|
||||
|
||||
return &Response{Res: resp, Reason: fmt.Sprintf("RESOLVED (%s)", r.upstreamURL)}, nil
|
||||
return &model.Response{Res: resp, Reason: fmt.Sprintf("RESOLVED (%s)", r.upstreamURL)}, nil
|
||||
}
|
||||
|
||||
var netErr net.Error
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
|
||||
"github.com/0xERR0R/blocky/config"
|
||||
. "github.com/0xERR0R/blocky/helpertest"
|
||||
. "github.com/0xERR0R/blocky/model"
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
"github.com/miekg/dns"
|
||||
. "github.com/onsi/ginkgo"
|
||||
|
|
|
@ -13,6 +13,7 @@ import (
|
|||
"github.com/0xERR0R/blocky/config"
|
||||
"github.com/0xERR0R/blocky/log"
|
||||
"github.com/0xERR0R/blocky/metrics"
|
||||
"github.com/0xERR0R/blocky/model"
|
||||
"github.com/0xERR0R/blocky/resolver"
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
|
||||
|
@ -251,14 +252,14 @@ func (s *Server) Stop() {
|
|||
}
|
||||
}
|
||||
|
||||
func createResolverRequest(remoteAddress net.Addr, request *dns.Msg) *resolver.Request {
|
||||
func createResolverRequest(remoteAddress net.Addr, request *dns.Msg) *model.Request {
|
||||
clientIP, protocol := resolveClientIPAndProtocol(remoteAddress)
|
||||
|
||||
return newRequest(clientIP, protocol, request)
|
||||
}
|
||||
|
||||
func newRequest(clientIP net.IP, protocol resolver.RequestProtocol, request *dns.Msg) *resolver.Request {
|
||||
return &resolver.Request{
|
||||
func newRequest(clientIP net.IP, protocol model.RequestProtocol, request *dns.Msg) *model.Request {
|
||||
return &model.Request{
|
||||
ClientIP: clientIP,
|
||||
Protocol: protocol,
|
||||
Req: request,
|
||||
|
@ -323,12 +324,12 @@ func (s *Server) OnHealthCheck(w dns.ResponseWriter, request *dns.Msg) {
|
|||
util.LogOnError("can't write message: ", err)
|
||||
}
|
||||
|
||||
func resolveClientIPAndProtocol(addr net.Addr) (ip net.IP, protocol resolver.RequestProtocol) {
|
||||
func resolveClientIPAndProtocol(addr net.Addr) (ip net.IP, protocol model.RequestProtocol) {
|
||||
if t, ok := addr.(*net.UDPAddr); ok {
|
||||
return t.IP, resolver.UDP
|
||||
return t.IP, model.UDP
|
||||
} else if t, ok := addr.(*net.TCPAddr); ok {
|
||||
return t.IP, resolver.TCP
|
||||
return t.IP, model.TCP
|
||||
}
|
||||
|
||||
return nil, resolver.TCP
|
||||
return nil, model.TCP
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ import (
|
|||
"github.com/0xERR0R/blocky/api"
|
||||
"github.com/0xERR0R/blocky/config"
|
||||
"github.com/0xERR0R/blocky/log"
|
||||
"github.com/0xERR0R/blocky/resolver"
|
||||
"github.com/0xERR0R/blocky/model"
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
"github.com/0xERR0R/blocky/web"
|
||||
|
||||
|
@ -94,7 +94,7 @@ func (s *Server) processDohMessage(rawMsg []byte, rw http.ResponseWriter, req *h
|
|||
return
|
||||
}
|
||||
|
||||
r := newRequest(net.ParseIP(extractIP(req)), resolver.TCP, msg)
|
||||
r := newRequest(net.ParseIP(extractIP(req)), model.TCP, msg)
|
||||
|
||||
resResponse, err := s.queryResolver.Resolve(r)
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@ import (
|
|||
"github.com/0xERR0R/blocky/config"
|
||||
. "github.com/0xERR0R/blocky/helpertest"
|
||||
. "github.com/0xERR0R/blocky/log"
|
||||
"github.com/0xERR0R/blocky/model"
|
||||
"github.com/0xERR0R/blocky/resolver"
|
||||
"github.com/0xERR0R/blocky/util"
|
||||
|
||||
|
@ -564,14 +565,14 @@ var _ = Describe("Running DNS server", func() {
|
|||
It("should correct resolve client IP", func() {
|
||||
ip, protocol := resolveClientIPAndProtocol(&net.UDPAddr{IP: net.ParseIP("192.168.178.88")})
|
||||
Expect(ip).Should(Equal(net.ParseIP("192.168.178.88")))
|
||||
Expect(protocol).Should(Equal(resolver.UDP))
|
||||
Expect(protocol).Should(Equal(model.UDP))
|
||||
})
|
||||
})
|
||||
Context("TCP address", func() {
|
||||
It("should correct resolve client IP", func() {
|
||||
ip, protocol := resolveClientIPAndProtocol(&net.TCPAddr{IP: net.ParseIP("192.168.178.88")})
|
||||
Expect(ip).Should(Equal(net.ParseIP("192.168.178.88")))
|
||||
Expect(protocol).Should(Equal(resolver.TCP))
|
||||
Expect(protocol).Should(Equal(model.TCP))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue