AdGuardHome/internal/filtering
Dimitry Kolyshev e6f8aeeebe Pull request: 2499 rewrite: storage vol.1
Merge in DNS/adguard-home from 2499-rewrites to master

Squashed commit of the following:

commit 3f5f8e1354cbfa2de2bea69b1caa5dfbcb84ddb1
Merge: c84a86fb fafd7a1e
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Mon Nov 28 13:13:31 2022 +0200

    Merge remote-tracking branch 'origin/master' into 2499-rewrites

commit c84a86fba1c9cd77c5893e056cd85f8aa6597afc
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Mon Nov 28 12:50:26 2022 +0200

    rewrite: todos

commit 3b33a79bea65650ee7dc920554773d1d1d2c67f5
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Mon Nov 28 11:45:05 2022 +0200

    rewrite: todos

commit 15022994e7af8e5ee5929edff8ce98356a1cb27b
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Fri Nov 25 12:15:27 2022 +0200

    rewrite: imp code

commit b3c1949a585a8ff83c046921288ce2fdb5b36cee
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Fri Nov 25 11:33:25 2022 +0200

    rewrite: imp code

commit 80fe50a86fc6e5ed5cf5b0d2e0be667b1b9221a8
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Fri Nov 25 11:13:39 2022 +0200

    rewrite: imp code

commit 5288ede0e8df5ca1d12c9e1e8341db703729c71a
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Fri Nov 25 11:04:11 2022 +0200

    Revert "all: rewrite"

    This reverts commit 32ad8d76861bd9919da61635e6f871adcc36c999.

commit cff6494fde44646f5ac54173380719557e0c0e02
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Fri Nov 25 11:04:11 2022 +0200

    Revert "all: rewrite"

    This reverts commit 65e44e92b009c561ddb2c68f56e04d57b83ba247.

commit e0fe877da4ee820838acc1ac6111809e7d0bc72e
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Fri Nov 25 11:04:11 2022 +0200

    Revert "filtering: imp code"

    This reverts commit c882da3309297f44ccaf38274ec4ef0ef2fec7b3.

commit 8e3f9d4a7ac2e5b114064e77ec009a2457b28a73
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Fri Nov 25 11:04:11 2022 +0200

    Revert "rewrite: imp code"

    This reverts commit ce2332932bef46186c0addf5e7a1ca648cdd9f22.

commit ce2332932bef46186c0addf5e7a1ca648cdd9f22
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Fri Nov 25 10:56:48 2022 +0200

    rewrite: imp code

commit c882da3309297f44ccaf38274ec4ef0ef2fec7b3
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Thu Nov 24 13:39:26 2022 +0200

    filtering: imp code

commit 65e44e92b009c561ddb2c68f56e04d57b83ba247
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Thu Nov 24 13:25:10 2022 +0200

    all: rewrite

commit 32ad8d76861bd9919da61635e6f871adcc36c999
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Thu Nov 24 13:19:55 2022 +0200

    all: rewrite

commit 941538abfe
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Tue Nov 22 12:54:55 2022 +0200

    rewrite: storage tests

commit 0a1ad86ea7
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Tue Nov 22 12:46:56 2022 +0200

    rewrite: imp code

commit f10a45361c
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Mon Nov 21 14:29:44 2022 +0200

    rewrite: storage

commit ff91bb81a6
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Sun Nov 20 13:25:05 2022 +0200

    rewrite: storage
2022-11-28 14:19:56 +03:00
..
rewrite Pull request: 2499 rewrite: storage vol.1 2022-11-28 14:19:56 +03:00
tests Pull request: all: less annoying pkg names 2021-05-21 16:15:47 +03:00
README.md Pull request: all: less annoying pkg names 2021-05-21 16:15:47 +03:00
blocked.go Pull request: 3972-hostlists-services 2022-10-27 15:46:25 +03:00
blocked_test.go Pull request: all: imp build tags 2021-06-15 19:42:41 +03:00
dnsrewrite.go Pull request: 4865-refactor-dns-handlers 2022-09-02 14:52:19 +03:00
dnsrewrite_test.go Pull request: 4871 imp filtering 2022-09-23 13:23:35 +03:00
filter.go Pull request: 4916 Editing filter 2022-10-21 20:14:43 +03:00
filter_test.go Pull request: 5035-more-clients-netip-addr 2022-11-09 14:37:07 +03:00
filtering.go Pull request: 2499 rewrite: storage vol.1 2022-11-28 14:19:56 +03:00
filtering_test.go Pull request: imp-tests 2022-10-27 16:11:36 +03:00
http.go Pull request: 3972-hostlists-services 2022-10-27 15:46:25 +03:00
http_test.go Pull request: 4916 Editing filter 2022-10-21 20:14:43 +03:00
rewrites.go Pull request: 2499 rewrite: storage vol.1 2022-11-28 14:19:56 +03:00
rewrites_test.go Pull request: 2499 rewrite: storage vol.1 2022-11-28 14:19:56 +03:00
safebrowsing.go Pull request: imp-json-resp 2022-10-04 14:35:10 +03:00
safebrowsing_test.go Pull request: 4871 imp filtering 2022-09-23 13:23:35 +03:00
safesearch.go Pull request: imp-json-resp 2022-10-04 14:35:10 +03:00
servicelist.go Pull request: upd-data 2022-11-23 15:46:03 +03:00

README.md

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)
    }
}