# README
AdGuard Home's DNS filtering go library
Example use:
[ -z "$GOPATH" ] && export GOPATH=$HOME/go
go get -d github.com/AdguardTeam/AdGuardHome/filtering
Create file filter.go
package main
import (
"github.com/AdguardTeam/AdGuardHome/filtering"
"log"
)
func main() {
filter := filtering.New()
filter.AddRule("||dou*ck.net^")
host := "www.doubleclick.net"
res, err := filter.CheckHost(host)
if err != nil {
// temporary failure
log.Fatalf("Failed to check host %q: %s", host, err)
}
if res.IsFiltered {
log.Printf("Host %s is filtered, reason - %q, matched rule: %q", host, res.Reason, res.Rule)
} else {
log.Printf("Host %s is not filtered, reason - %q", host, res.Reason)
}
}
And then run it:
go run filter.go
You will get:
2000/01/01 00:00:00 Host www.doubleclick.net is filtered, reason - 'FilteredBlackList', matched rule: '||dou*ck.net^'
You can also enable checking against AdGuard's SafeBrowsing:
package main
import (
"github.com/AdguardTeam/AdGuardHome/filtering"
"log"
)
func main() {
filter := filtering.New()
filter.EnableSafeBrowsing()
host := "wmconvirus.narod.ru" // hostname for testing safebrowsing
res, err := filter.CheckHost(host)
if err != nil {
// temporary failure
log.Fatalf("Failed to check host %q: %s", host, err)
}
if res.IsFiltered {
log.Printf("Host %s is filtered, reason - %q, matched rule: %q", host, res.Reason, res.Rule)
} else {
log.Printf("Host %s is not filtered, reason - %q", host, res.Reason)
}
}
# Packages
Package hashprefix used for safe browsing and parent control.
Package rewrite implements DNS Rewrites storage and request matching.
Package rulelist contains the implementation of the standard rule-list filter that wraps an urlfilter filtering-engine.
Package safesearch implements safesearch host matching.
# Functions
InitModule manually initializes blocked services map.
New creates properly initialized DNS Filter that is ready to be used.
ValidateUpdateIvl returns false if i is not a valid filters update interval.
# Constants
The IDs of built-in filter lists.
BlockingModeCustomIP means respond with a custom IP address.
BlockingModeDefault is the same as BlockingModeNullIP for Adblock-style rules, but responds with the IP address specified in the rule when blocked by an `/etc/hosts`-style rule.
BlockingModeNullIP means respond with a zero IP address: "0.0.0.0" for A requests and "::" for AAAA ones.
BlockingModeNXDOMAIN means respond with the NXDOMAIN code.
BlockingModeREFUSED means respond with the REFUSED code.
The IDs of built-in filter lists.
FilteredBlockedService - the host is blocked by "blocked services" settings.
FilteredBlockList - the host was matched to be advertising host.
FilteredInvalid - the request was invalid and was not processed.
FilteredParental - the host was matched to be outside of parental control settings.
FilteredSafeBrowsing - the host was matched to be malicious/phishing.
FilteredSafeSearch - the host was replaced with safesearch variant.
NotFilteredAllowList - the host is explicitly allowed.
NotFilteredError is returned when there was an error during checking.
NotFilteredNotFound - host was not find in any checks, default value for result.
The IDs of built-in filter lists.
Rewritten is returned when there was a rewrite by a legacy DNS rewrite rule.
RewrittenAutoHosts is returned when there was a rewrite by autohosts rules (/etc/hosts and so on).
RewrittenRule is returned when a $dnsrewrite filter rule was applied.
The IDs of built-in filter lists.
The IDs of built-in filter lists.
The IDs of built-in filter lists.
# Structs
BlockedServices is the configuration of blocked services.
Config allows you to configure DNS filtering with New() or just change variables directly.
DNSFilter matches hostnames and DNS requests against filtering rules.
DNSRewriteResult is the result of application of $dnsrewrite rules.
Filter represents a filter list.
FilterYAML represents a filter list in the configuration file.
LegacyRewrite is a single legacy DNS rewrite record.
LookupStats store stats collected during safebrowsing or parental checks.
Result contains the result of a request check.
ResultRule contains information about applied rules.
SafeSearchConfig is a struct with safe search related settings.
ServiceEntry - blocked service array element.
Settings are custom filtering settings for a client.
Stats store LookupStats for safebrowsing, parental and safesearch.
# Interfaces
Checker is used for safe browsing or parental control hash-prefix filtering.
Resolver is the interface for net.Resolver to simplify testing.
SafeSearch interface describes a service for search engines hosts rewrites.
# Type aliases
BlockingMode is an enum of all allowed blocking modes.
DNSRewriteResultResponse is the collection of DNS response records the server returns.
Reason holds an enum detailing why it was filtered or not filtered.