AdGuardHome/internal/filtering
Dimitry Kolyshev 2b5e4850d0 Pull request: 1163 safesearch vol.2
Merge in DNS/adguard-home from 1163-safesearch-1-2-1 to master

Squashed commit of the following:

commit d3a5ebef35210019842145074e898129b42f1f2c
Merge: b85264ae c6706445
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Wed Mar 15 09:17:53 2023 +0700

    Merge remote-tracking branch 'origin/master' into 1163-safesearch-1-2-1

    # Conflicts:
    #	CHANGELOG.md

commit b85264aefc5f191ac6cb194b519f03ba15829a4e
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Tue Mar 14 00:16:07 2023 +0700

    home: imp code

commit ac2ed7a5ce8db40628e7d4d1c8634641e5f38b0b
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Mon Mar 13 23:02:06 2023 +0700

    all: changelog

commit f0fccafcb01f50c7051df53bbe9b02cab75aa71e
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Mon Mar 13 22:42:36 2023 +0700

    all: changelog

commit 37df29bf6372939644fb28e3d70365496e0cb4f6
Merge: b227b277 595484e0
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Mon Mar 13 22:38:57 2023 +0700

    Merge remote-tracking branch 'origin/master' into 1163-safesearch-1-2-1

commit b227b2775b4866d69241ad87acf99700715552cb
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Mon Mar 13 16:56:01 2023 +0700

    all: imp docs

commit 6fd39fc3565c3f4bc7a7113d17733c20dfe24d8d
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Mon Mar 13 16:55:03 2023 +0700

    home: imp code

commit 3bb3bb7c7dcf97b2a5602a7d2b6770c08b4d863d
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Mon Mar 13 12:16:53 2023 +0700

    home: imp docs

commit 5f573a56a9fd9942ad677fa0fae6b24228dab653
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Mon Mar 13 11:56:47 2023 +0700

    home: imp code

commit 23eeb5552cf2510596b2311cc3eda53ac678ffcc
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Fri Mar 10 10:57:33 2023 +0700

    home: imp code

commit 643de2fca1b5917c61fe83e1e472222404f3cd21
Merge: dada6e63 a2053526
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Thu Mar 9 21:03:08 2023 +0700

    Merge remote-tracking branch 'origin/master' into 1163-safesearch-1-2-1

commit dada6e63ca5324d30775e2da1727da891743f654
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Thu Mar 9 17:09:03 2023 +0700

    all: imp docs

commit 81a180d99dd9a995440d5f4e2ebca34678e7d0c7
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Thu Mar 9 15:12:43 2023 +0700

    all: imp code

commit fa84877bc777004d246d71d0a9ae0bd9ee568a91
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Thu Mar 9 10:53:05 2023 +0700

    all: imp code

commit 6d7e02e745d72921a693d4f09eec7ce21c2aefd4
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Thu Mar 9 10:40:02 2023 +0700

    all: imp docs

commit 0a4332997070fb8d2fb3a34d32b92f57a325ff06
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Tue Mar 7 22:00:52 2023 +0700

    safesearch: fix merge

commit 145c2222ba4cf7f8909b816d83829d2217c94243
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Thu Mar 2 11:41:48 2023 +0700

    safesearch: fix merge

commit 14c6a8005fe15b5d5a39f91b17c96d8670975811
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Wed Mar 1 12:50:09 2023 +0700

    all: docs

commit 2a85c8831866bf1c34c423a289461fc1e32667b5
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date:   Wed Mar 1 12:47:00 2023 +0700

    all: use safesearch package
2023-03-15 14:31:07 +03:00
..
rewrite Pull request: 2499 rewrite: storage vol.2 2022-12-05 14:46:51 +03:00
safesearch Pull request 1163 safesearch package vol.1 2023-03-07 17:52:03 +03:00
tests 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
dnsrewrite_test.go Pull request: 4871 imp filtering 2022-09-23 13:23:35 +03:00
dnsrewrite.go Pull request: 4865-refactor-dns-handlers 2022-09-02 14:52:19 +03:00
filter_test.go Pull request: 5258-good-old-filters 2022-12-20 16:40:42 +03:00
filter.go Pull request: 5290-rules-count 2022-12-23 17:11:11 +03:00
filtering_test.go Pull request: 1163 safesearch vol.2 2023-03-15 14:31:07 +03:00
filtering.go Pull request: 1163 safesearch vol.2 2023-03-15 14:31:07 +03:00
http_test.go Pull request: 5258-good-old-filters 2022-12-20 16:40:42 +03:00
http.go Pull request: 3972-hostlists-services 2022-10-27 15:46:25 +03:00
README.md Pull request: all: less annoying pkg names 2021-05-21 16:15:47 +03:00
rewritehttp.go Pull request: 2499 rewrite: storage vol.2 2022-12-05 14:46:51 +03:00
rewrites_test.go Pull request: 2499 rewrite: storage vol.2 2022-12-05 14:46:51 +03:00
rewrites.go Pull request 1743: upd-sorting 2023-02-21 16:38:22 +03:00
safebrowsing_test.go Pull request: 4871 imp filtering 2022-09-23 13:23:35 +03:00
safebrowsing.go Pull request 1743: upd-sorting 2023-02-21 16:38:22 +03:00
safesearch.go Pull request: 1163 safesearch vol.2 2023-03-15 14:31:07 +03:00
safesearchhttp.go Pull request: 1163 safesearch vol.2 2023-03-15 14:31:07 +03:00
servicelist.go Pull request 1765: upd-all 2023-03-09 13:26:20 +03:00

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