* safebrowsing/parental: split some code
* dnsfilter.CheckHost() doesn't support host="hostname." (with a trailing dot) anymore
This commit is contained in:
parent
d659e7ee08
commit
ccf72b6008
|
@ -510,7 +510,21 @@ func (d *Dnsfilter) checkSafeBrowsing(host string) (Result, error) {
|
|||
}
|
||||
return result, nil
|
||||
}
|
||||
result, err := d.lookupCommon(host, &gctx.stats.Safebrowsing, gctx.safebrowsingCache, true, format, handleBody)
|
||||
|
||||
// check cache
|
||||
cachedValue, isFound := getCachedResult(gctx.safebrowsingCache, host)
|
||||
if isFound {
|
||||
atomic.AddUint64(&gctx.stats.Safebrowsing.CacheHits, 1)
|
||||
log.Tracef("%s: found in the lookup cache %p", host, gctx.safebrowsingCache)
|
||||
return cachedValue, nil
|
||||
}
|
||||
|
||||
result, err := d.lookupCommon(host, &gctx.stats.Safebrowsing, true, format, handleBody)
|
||||
|
||||
if err == nil {
|
||||
setCacheResult(gctx.safebrowsingCache, host, result)
|
||||
}
|
||||
|
||||
return result, err
|
||||
}
|
||||
|
||||
|
@ -562,7 +576,21 @@ func (d *Dnsfilter) checkParental(host string) (Result, error) {
|
|||
}
|
||||
return result, nil
|
||||
}
|
||||
result, err := d.lookupCommon(host, &gctx.stats.Parental, gctx.parentalCache, false, format, handleBody)
|
||||
|
||||
// check cache
|
||||
cachedValue, isFound := getCachedResult(gctx.parentalCache, host)
|
||||
if isFound {
|
||||
atomic.AddUint64(&gctx.stats.Parental.CacheHits, 1)
|
||||
log.Tracef("%s: found in the lookup cache %p", host, gctx.parentalCache)
|
||||
return cachedValue, nil
|
||||
}
|
||||
|
||||
result, err := d.lookupCommon(host, &gctx.stats.Parental, false, format, handleBody)
|
||||
|
||||
if err == nil {
|
||||
setCacheResult(gctx.parentalCache, host, result)
|
||||
}
|
||||
|
||||
return result, err
|
||||
}
|
||||
|
||||
|
@ -570,18 +598,7 @@ type formatHandler func(hashparam string) string
|
|||
type bodyHandler func(body []byte, hashes map[string]bool) (Result, error)
|
||||
|
||||
// real implementation of lookup/check
|
||||
func (d *Dnsfilter) lookupCommon(host string, lookupstats *LookupStats, cache *fastcache.Cache, hashparamNeedSlash bool, format formatHandler, handleBody bodyHandler) (Result, error) {
|
||||
// if host ends with a dot, trim it
|
||||
host = strings.ToLower(strings.Trim(host, "."))
|
||||
|
||||
// check cache
|
||||
cachedValue, isFound := getCachedResult(cache, host)
|
||||
if isFound {
|
||||
atomic.AddUint64(&lookupstats.CacheHits, 1)
|
||||
log.Tracef("%s: found in the lookup cache %p", host, cache)
|
||||
return cachedValue, nil
|
||||
}
|
||||
|
||||
func (d *Dnsfilter) lookupCommon(host string, lookupstats *LookupStats, hashparamNeedSlash bool, format formatHandler, handleBody bodyHandler) (Result, error) {
|
||||
// convert hostname to hash parameters
|
||||
hashparam, hashes := hostnameToHashParam(host, hashparamNeedSlash)
|
||||
|
||||
|
@ -613,20 +630,16 @@ func (d *Dnsfilter) lookupCommon(host string, lookupstats *LookupStats, cache *f
|
|||
switch {
|
||||
case resp.StatusCode == 204:
|
||||
// empty result, save cache
|
||||
setCacheResult(cache, host, Result{})
|
||||
return Result{}, nil
|
||||
case resp.StatusCode != 200:
|
||||
// error, don't save cache
|
||||
return Result{}, nil
|
||||
return Result{}, fmt.Errorf("HTTP status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
result, err := handleBody(body, hashes)
|
||||
if err != nil {
|
||||
// error, don't save cache
|
||||
return Result{}, err
|
||||
}
|
||||
|
||||
setCacheResult(cache, host, result)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -144,9 +144,7 @@ func TestSafeBrowsing(t *testing.T) {
|
|||
if gctx.stats.Safebrowsing.Requests != 1 {
|
||||
t.Errorf("Safebrowsing lookup positive cache is not working: %v", gctx.stats.Safebrowsing.Requests)
|
||||
}
|
||||
d.checkMatch(t, "wmconvirus.narod.ru.")
|
||||
d.checkMatch(t, "test.wmconvirus.narod.ru")
|
||||
d.checkMatch(t, "test.wmconvirus.narod.ru.")
|
||||
d.checkMatchEmpty(t, "yandex.ru")
|
||||
d.checkMatchEmpty(t, "pornhub.com")
|
||||
l := gctx.stats.Safebrowsing.Requests
|
||||
|
@ -166,9 +164,7 @@ func TestParallelSB(t *testing.T) {
|
|||
t.Run(fmt.Sprintf("aaa%d", i), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
d.checkMatch(t, "wmconvirus.narod.ru")
|
||||
d.checkMatch(t, "wmconvirus.narod.ru.")
|
||||
d.checkMatch(t, "test.wmconvirus.narod.ru")
|
||||
d.checkMatch(t, "test.wmconvirus.narod.ru.")
|
||||
d.checkMatchEmpty(t, "yandex.ru")
|
||||
d.checkMatchEmpty(t, "pornhub.com")
|
||||
})
|
||||
|
@ -368,8 +364,6 @@ func TestParentalControl(t *testing.T) {
|
|||
t.Errorf("Parental lookup positive cache is not working")
|
||||
}
|
||||
d.checkMatch(t, "www.pornhub.com")
|
||||
d.checkMatch(t, "pornhub.com.")
|
||||
d.checkMatch(t, "www.pornhub.com.")
|
||||
d.checkMatchEmpty(t, "www.yandex.ru")
|
||||
d.checkMatchEmpty(t, "yandex.ru")
|
||||
l := gctx.stats.Parental.Requests
|
||||
|
|
Loading…
Reference in New Issue