From 0d2abbe0502a7189d8b61eebe9659611399855d1 Mon Sep 17 00:00:00 2001 From: bakito Date: Sat, 27 Mar 2021 23:40:59 +0100 Subject: [PATCH] first commit --- .gitignore | 2 + Makefile | 22 ++++++ go.mod | 9 +++ go.sum | 61 +++++++++++++++++ main.go | 122 ++++++++++++++++++++++++++++++++++ pkg/client/client.go | 155 +++++++++++++++++++++++++++++++++++++++++++ pkg/log/log.go | 28 ++++++++ pkg/types/types.go | 107 +++++++++++++++++++++++++++++ 8 files changed, 506 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 pkg/client/client.go create mode 100644 pkg/log/log.go create mode 100644 pkg/types/types.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c6ef218 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2283cd2 --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ + +# Run go fmt against code +fmt: + go fmt ./... + gofmt -s -w . + +# Run go vet against code +vet: + go vet ./... + +# Run golangci-lint +lint: + golangci-lint run + +# Run go mod tidy +tidy: + go mod tidy + +# Run tests +test: tidy fmt vet + go test ./... -coverprofile=coverage.out + go tool cover -func=coverage.out diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7a49b2c --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module github.com/bakito/adguardhome-sync + +go 1.16 + +require ( + github.com/go-resty/resty/v2 v2.5.0 + go.uber.org/zap v1.16.0 + golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..c8ca93e --- /dev/null +++ b/go.sum @@ -0,0 +1,61 @@ +github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-resty/resty/v2 v2.5.0 h1:WFb5bD49/85PO7WgAjZ+/TJQ+Ty1XOcWEfD1zIFCM1c= +github.com/go-resty/resty/v2 v2.5.0/go.mod h1:B88+xCTEwvfD94NOuE6GS1wMlnoKNY8eEiNizfNwOwA= +github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk= +go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= +go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A= +go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= +go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee h1:0mgffUl7nfd+FpvXMVz4IDEaUSmT1ysygQC7qYo7sG4= +go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= +go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM= +go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210119194325-5f4716e94777 h1:003p0dJM77cxMSyCPFphvZf/Y5/NXf5fzg6ufd1/Oew= +golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5 h1:hKsoRgsbwY1NafxrwTs+k64bikrLBkAgPir1TNCj3Zs= +golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM= +honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= diff --git a/main.go b/main.go new file mode 100644 index 0000000..c09842a --- /dev/null +++ b/main.go @@ -0,0 +1,122 @@ +package main + +import ( + "github.com/bakito/adguardhome-sync/pkg/client" + "github.com/bakito/adguardhome-sync/pkg/log" + "os" +) + +const ( + envOriginApiURL = "ORIGIN_API_URL" + envOriginUsername = "ORIGIN_USERNAME" + envOriginPassword = "ORIGIN_PASSWORD" + envReplicaApiURL = "REPLICA_API_URL" + envReplicaUsername = "REPLICA_USERNAME" + envOReplicaPassword = "REPLICA_PASSWORD" +) + +var ( + l = log.GetLogger("main") +) + +func main() { + // Create a Resty Client + + origin, err := client.New(os.Getenv(envOriginApiURL), os.Getenv(envOriginUsername), os.Getenv(envOriginPassword)) + if err != nil { + panic(err) + } + replica, err := client.New(os.Getenv(envReplicaApiURL), os.Getenv(envReplicaUsername), os.Getenv(envOReplicaPassword)) + if err != nil { + panic(err) + } + + err = syncRewrites(err, origin, replica) + if err != nil { + panic(err) + } + err = syncFilters(err, origin, replica) + if err != nil { + panic(err) + } + + // POST http://192.168.2.207/control/filtering/config {"interval":24,"enabled":false} + // POST http://192.168.2.207/control/dns_config {"protection_enabled":false} +} + +func syncFilters(err error, origin client.Client, replica client.Client) error { + of, err := origin.Filtering() + if err != nil { + return err + } + rf, err := replica.Filtering() + if err != nil { + return err + } + + fa, fd := rf.Filters.Merge(of.Filters) + + err = replica.AddFilters(false, fa...) + if err != nil { + return err + } + + if len(fa) > 0 { + err = replica.RefreshFilters(false) + if err != nil { + return err + } + } + + err = replica.DeleteFilters(false, fd...) + if err != nil { + return err + } + + fa, fd = rf.WhitelistFilters.Merge(of.WhitelistFilters) + err = replica.AddFilters(true, fa...) + if err != nil { + return err + } + + if len(fa) > 0 { + err = replica.RefreshFilters(true) + if err != nil { + return err + } + } + + err = replica.DeleteFilters(true, fd...) + if err != nil { + return err + } + + if of.UserRules.String() != rf.UserRules.String() { + return replica.SetCustomRules(of.UserRules) + } + + return nil +} + +func syncRewrites(err error, origin client.Client, replica client.Client) error { + originRewrites, err := origin.RewriteList() + if err != nil { + return err + } + replicaRewrites, err := replica.RewriteList() + if err != nil { + return err + } + + a, r := replicaRewrites.Merge(originRewrites) + + err = replica.AddRewriteEntries(a...) + if err != nil { + return err + } + err = replica.DeleteRewriteEntries(r...) + if err != nil { + return err + } + return err +} diff --git a/pkg/client/client.go b/pkg/client/client.go new file mode 100644 index 0000000..ff50ea0 --- /dev/null +++ b/pkg/client/client.go @@ -0,0 +1,155 @@ +package client + +import ( + "fmt" + "github.com/bakito/adguardhome-sync/pkg/log" + "github.com/bakito/adguardhome-sync/pkg/types" + "github.com/go-resty/resty/v2" + "go.uber.org/zap" + "net/url" +) + +var ( + l = log.GetLogger("client") +) + +func New(apiURL string, username string, password string) (Client, error) { + + cl := resty.New().SetHostURL(apiURL).SetDisableWarn(true) + if username != "" && password != "" { + cl = cl.SetBasicAuth(username, password) + } + + u, err := url.Parse(apiURL) + if err != nil { + return nil, err + } + + return &client{ + client: cl, + log: l.With("host", u.Host), + }, nil +} + +type Client interface { + Status() (*types.Status, error) + RewriteList() (*types.RewriteEntries, error) + AddRewriteEntries(e ...types.RewriteEntry) error + DeleteRewriteEntries(e ...types.RewriteEntry) error + + Filtering() (*types.FilteringStatus, error) + AddFilters(whitelist bool, e ...types.Filter) error + DeleteFilters(whitelist bool, e ...types.Filter) error + RefreshFilters(whitelist bool) error + SetCustomRules(rules types.UserRules) error + + ToggleSaveBrowsing(enable bool) error + ToggleParental(enable bool) error + ToggleSafeSearch(enable bool) error +} + +type client struct { + client *resty.Client + log *zap.SugaredLogger +} + +func (cl *client) Status() (*types.Status, error) { + status := &types.Status{} + _, err := cl.client.R().EnableTrace().SetResult(status).Get("status") + return status, err + +} + +func (cl *client) RewriteList() (*types.RewriteEntries, error) { + rewrites := &types.RewriteEntries{} + _, err := cl.client.R().EnableTrace().SetResult(&rewrites).Get("/rewrite/list") + return rewrites, err +} + +func (cl *client) AddRewriteEntries(entries ...types.RewriteEntry) error { + for _, e := range entries { + cl.log.With("domain", e.Domain, "answer", e.Answer).Info("Add rewrite entry") + _, err := cl.client.R().EnableTrace().SetBody(&e).Post("/rewrite/add") + if err != nil { + return err + } + } + return nil +} + +func (cl *client) DeleteRewriteEntries(entries ...types.RewriteEntry) error { + for _, e := range entries { + cl.log.With("domain", e.Domain, "answer", e.Answer).Info("Delete rewrite entry") + _, err := cl.client.R().EnableTrace().SetBody(&e).Post("/rewrite/delete") + if err != nil { + return err + } + } + return nil +} + +func (cl *client) ToggleSaveBrowsing(enable bool) error { + return cl.toggle("safebrowsing", enable) +} + +func (cl *client) ToggleParental(enable bool) error { + return cl.toggle("parental", enable) +} + +func (cl *client) ToggleSafeSearch(enable bool) error { + return cl.toggle("safesearch", enable) +} + +func (cl *client) toggle(mode string, enable bool) error { + cl.log.With("mode", mode, "enable", enable).Info("Toggle") + var target string + if enable { + target = "enable" + } else { + target = "disable" + } + _, err := cl.client.R().EnableTrace().Post(fmt.Sprintf("/%s/%s", mode, target)) + return err +} + +func (cl *client) Filtering() (*types.FilteringStatus, error) { + f := &types.FilteringStatus{} + _, err := cl.client.R().EnableTrace().SetResult(f).Get("/filtering/status") + return f, err +} + +func (cl *client) AddFilters(whitelist bool, filters ...types.Filter) error { + for _, f := range filters { + cl.log.With("url", f.URL, "whitelist", whitelist).Info("Add filter") + ff := &types.Filter{Name: f.Name, URL: f.URL, Whitelist: whitelist} + _, err := cl.client.R().EnableTrace().SetBody(ff).Post("/filtering/add_url") + if err != nil { + return err + } + } + return nil +} + +func (cl *client) DeleteFilters(whitelist bool, filters ...types.Filter) error { + for _, f := range filters { + cl.log.With("url", f.URL, "whitelist", whitelist).Info("Delete filter") + ff := &types.Filter{URL: f.URL, Whitelist: whitelist} + _, err := cl.client.R().EnableTrace().SetBody(ff).Post("/filtering/remove_url") + if err != nil { + return err + } + } + return nil +} + +func (cl *client) RefreshFilters(whitelist bool) error { + cl.log.With("whitelist", whitelist).Info("Refresh filter") + _, err := cl.client.R().EnableTrace().SetBody(&types.RefreshFilter{Whitelist: whitelist}).Post("/filtering/refresh") + return err +} + +func (cl *client) SetCustomRules(rules types.UserRules) error { + cl.log.With("rules", len(rules)).Info("Set user rules") + _, err := cl.client.R().EnableTrace().SetBody(rules.String()).Post("/filtering/set_rules") + return err +} diff --git a/pkg/log/log.go b/pkg/log/log.go new file mode 100644 index 0000000..4678f7e --- /dev/null +++ b/pkg/log/log.go @@ -0,0 +1,28 @@ +package log + +import ( + "go.uber.org/zap" +) + +var rootLogger *zap.Logger + +// GetLogger returns a named logger +func GetLogger(name string) *zap.SugaredLogger { + return rootLogger.Named(name).Sugar() +} + +func init() { + level := zap.InfoLevel + + cfg := zap.Config{ + Level: zap.NewAtomicLevelAt(level), + Development: false, + Encoding: "console", + EncoderConfig: zap.NewDevelopmentEncoderConfig(), + OutputPaths: []string{"stderr"}, + ErrorOutputPaths: []string{"stderr"}, + } + + rootLogger, _ = cfg.Build() + rootLogger.Sugar() +} diff --git a/pkg/types/types.go b/pkg/types/types.go new file mode 100644 index 0000000..0c8d4d4 --- /dev/null +++ b/pkg/types/types.go @@ -0,0 +1,107 @@ +package types + +import ( + "fmt" + "strings" + "time" +) + +type Status struct { + DNSAddresses []string `json:"dns_addresses"` + DNSPort int `json:"dns_port"` + HTTPPort int `json:"http_port"` + ProtectionEnabled bool `json:"protection_enabled"` + DhcpAvailable bool `json:"dhcp_available"` + Running bool `json:"running"` + Version string `json:"version"` + Language string `json:"language"` +} + +type RewriteEntries []RewriteEntry + +func (rwe *RewriteEntries) Merge(other *RewriteEntries) (RewriteEntries, RewriteEntries) { + current := make(map[string]RewriteEntry) + + var adds RewriteEntries + var removes RewriteEntries + for _, rr := range *rwe { + current[rr.Key()] = rr + } + + for _, rr := range *other { + if _, ok := current[rr.Key()]; ok { + delete(current, rr.Key()) + } else { + adds = append(adds, rr) + } + } + + for _, rr := range current { + removes = append(removes, rr) + } + + return adds, removes +} + +type RewriteEntry struct { + Domain string `json:"domain"` + Answer string `json:"answer"` +} + +func (re *RewriteEntry) Key() string { + return fmt.Sprintf("%s#%s", re.Domain, re.Answer) +} + +type Filters []Filter + +type Filter struct { + ID int `json:"id"` + Enabled bool `json:"enabled"` + URL string `json:"url"` // needed for add + Name string `json:"name"` // needed for add + RulesCount int `json:"rules_count"` + LastUpdated time.Time `json:"last_updated"` + Whitelist bool `json:"whitelist"` // needed for add +} + +type FilteringStatus struct { + Enabled bool `json:"enabled"` + Interval int `json:"interval"` + Filters Filters `json:"filters"` + WhitelistFilters Filters `json:"whitelist_filters"` + UserRules UserRules `json:"user_rules"` +} + +type UserRules []string + +func (ur *UserRules) String() string { + return strings.Join(*ur, "\n") +} + +type RefreshFilter struct { + Whitelist bool `json:"whitelist"` +} + +func (fs *Filters) Merge(other Filters) (Filters, Filters) { + current := make(map[string]Filter) + + var adds Filters + var removes Filters + for _, f := range *fs { + current[f.URL] = f + } + + for _, rr := range other { + if _, ok := current[rr.URL]; ok { + delete(current, rr.URL) + } else { + adds = append(adds, rr) + } + } + + for _, rr := range current { + removes = append(removes, rr) + } + + return adds, removes +}