From 3db7457d303219130ff4131bcb91771e51a85c8d Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 8 Jan 2021 16:37:00 +0100 Subject: [PATCH] Fix minor bugs --- intel/entity.go | 9 +++++++-- intel/filterlists/lookup.go | 19 +++++++++---------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/intel/entity.go b/intel/entity.go index 16d83707..c98126f0 100644 --- a/intel/entity.go +++ b/intel/entity.go @@ -292,6 +292,7 @@ func (e *Entity) getDomainLists(ctx context.Context) { return } + var err error e.loadDomainListOnce.Do(func() { var domainsToInspect = []string{domain} @@ -314,10 +315,10 @@ func (e *Entity) getDomainLists(ctx context.Context) { for _, d := range domains { log.Tracer(ctx).Tracef("intel: loading domain list for %s", d) - list, err := filterlists.LookupDomain(d) + var list []string + list, err = filterlists.LookupDomain(d) if err != nil { log.Tracer(ctx).Errorf("intel: failed to get domain blocklists for %s: %s", d, err) - e.loadDomainListOnce = sync.Once{} return } @@ -325,6 +326,10 @@ func (e *Entity) getDomainLists(ctx context.Context) { } e.domainListLoaded = true }) + + if err != nil { + e.loadDomainListOnce = sync.Once{} + } } func splitDomain(domain string) []string { diff --git a/intel/filterlists/lookup.go b/intel/filterlists/lookup.go index 827aeab9..a975c281 100644 --- a/intel/filterlists/lookup.go +++ b/intel/filterlists/lookup.go @@ -2,9 +2,7 @@ package filterlists import ( "errors" - "fmt" "net" - "strings" "github.com/safing/portbase/database" "github.com/safing/portbase/log" @@ -55,15 +53,16 @@ func LookupCountry(country string) ([]string, error) { // LookupDomain returns a list of sources that mark the domain // as blocked. If domain is not stored in the cache database -// a nil slice is returned. +// a nil slice is returned. The caller is responsible for making +// sure that the given domain is valid and canonical. func LookupDomain(domain string) ([]string, error) { - // make sure we only fully qualified domains - // ending in a dot. - domain = strings.ToLower(domain) - if domain[len(domain)-1] != '.' { - domain += "." + switch domain { + case "", ".": + // Return no lists for empty domains and the root zone. + return nil, nil + default: + return lookupBlockLists("domain", domain) } - return lookupBlockLists("domain", domain) } // LookupASNString returns a list of sources that mark the ASN @@ -89,7 +88,7 @@ func LookupIP(ip net.IP) ([]string, error) { func LookupIPString(ipStr string) ([]string, error) { ip := net.ParseIP(ipStr) if ip == nil { - return nil, fmt.Errorf("invalid IP") + return nil, errors.New("invalid IP") } return LookupIP(ip)