From 5ad88d914cf9b03f399efd481ae39ebd56243e66 Mon Sep 17 00:00:00 2001 From: Dimitry Kolyshev Date: Tue, 30 Jul 2024 13:49:51 +0300 Subject: [PATCH] all: ecosia safesearch --- internal/configmigrate/configmigrate.go | 2 +- .../configmigrate/migrations_internal_test.go | 60 +++++++++++++++++++ internal/configmigrate/migrator.go | 1 + internal/configmigrate/v29.go | 47 +++++++++++++++ internal/filtering/safesearch.go | 1 + internal/filtering/safesearch/rules.go | 4 ++ .../filtering/safesearch/rules/ecosia.txt | 1 + internal/filtering/safesearch/safesearch.go | 3 + .../safesearch/safesearch_internal_test.go | 1 + .../filtering/safesearch/safesearch_test.go | 1 + internal/home/clientshttp.go | 1 + internal/home/config.go | 1 + 12 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 internal/configmigrate/v29.go create mode 100644 internal/filtering/safesearch/rules/ecosia.txt diff --git a/internal/configmigrate/configmigrate.go b/internal/configmigrate/configmigrate.go index 6e8845e0..cba61247 100644 --- a/internal/configmigrate/configmigrate.go +++ b/internal/configmigrate/configmigrate.go @@ -2,4 +2,4 @@ package configmigrate // LastSchemaVersion is the most recent schema version. -const LastSchemaVersion uint = 28 +const LastSchemaVersion uint = 29 diff --git a/internal/configmigrate/migrations_internal_test.go b/internal/configmigrate/migrations_internal_test.go index 5349102f..2a3b7596 100644 --- a/internal/configmigrate/migrations_internal_test.go +++ b/internal/configmigrate/migrations_internal_test.go @@ -1728,3 +1728,63 @@ func TestUpgradeSchema27to28(t *testing.T) { }) } } + +func TestUpgradeSchema28to29(t *testing.T) { + const newSchemaVer = 29 + + testCases := []struct { + in yobj + want yobj + name string + }{{ + name: "empty", + in: yobj{}, + want: yobj{ + "schema_version": newSchemaVer, + }, + }, { + name: "disabled", + in: yobj{ + "dns": yobj{ + "safe_search": yobj{ + "enabled": false, + }, + }, + }, + want: yobj{ + "schema_version": newSchemaVer, + "dns": yobj{ + "safe_search": yobj{ + "enabled": false, + }, + }, + }, + }, { + name: "enabled", + in: yobj{ + "dns": yobj{ + "safe_search": yobj{ + "enabled": true, + }, + }, + }, + want: yobj{ + "schema_version": newSchemaVer, + "dns": yobj{ + "safe_search": yobj{ + "ecosia": true, + "enabled": true, + }, + }, + }, + }} + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + err := migrateTo29(tc.in) + require.NoError(t, err) + + assert.Equal(t, tc.want, tc.in) + }) + } +} diff --git a/internal/configmigrate/migrator.go b/internal/configmigrate/migrator.go index ebdf6ba7..06dbdb90 100644 --- a/internal/configmigrate/migrator.go +++ b/internal/configmigrate/migrator.go @@ -120,6 +120,7 @@ func (m *Migrator) upgradeConfigSchema(current, target uint, diskConf yobj) (err 25: migrateTo26, 26: migrateTo27, 27: migrateTo28, + 28: migrateTo29, } for i, migrate := range upgrades[current:target] { diff --git a/internal/configmigrate/v29.go b/internal/configmigrate/v29.go new file mode 100644 index 00000000..ec174131 --- /dev/null +++ b/internal/configmigrate/v29.go @@ -0,0 +1,47 @@ +package configmigrate + +// migrateTo29 performs the following changes: +// +// # BEFORE: +// 'dns': +// 'safe_search': +// 'enabled': true +// 'bing': true +// 'duckduckgo': true +// 'google': true +// 'pixabay': true +// 'yandex': true +// 'youtube': true +// # … +// # … +// +// # AFTER: +// 'dns': +// 'safe_search': +// 'enabled': true +// 'bing': true +// 'duckduckgo': true +// 'ecosia': true +// 'google': true +// 'pixabay': true +// 'yandex': true +// 'youtube': true +// # … +// # … +func migrateTo29(diskConf yobj) (err error) { + diskConf["schema_version"] = 29 + + dns, ok, err := fieldVal[yobj](diskConf, "dns") + if !ok { + return err + } + + safeSearch, _, _ := fieldVal[yobj](dns, "safe_search") + if safeSearch["enabled"] == true { + safeSearch["ecosia"] = true + } + + dns["safe_search"] = safeSearch + + return nil +} diff --git a/internal/filtering/safesearch.go b/internal/filtering/safesearch.go index 39c05140..5517e52f 100644 --- a/internal/filtering/safesearch.go +++ b/internal/filtering/safesearch.go @@ -22,6 +22,7 @@ type SafeSearchConfig struct { Bing bool `yaml:"bing" json:"bing"` DuckDuckGo bool `yaml:"duckduckgo" json:"duckduckgo"` + Ecosia bool `yaml:"duckduckgo" json:"ecosia"` Google bool `yaml:"google" json:"google"` Pixabay bool `yaml:"pixabay" json:"pixabay"` Yandex bool `yaml:"yandex" json:"yandex"` diff --git a/internal/filtering/safesearch/rules.go b/internal/filtering/safesearch/rules.go index 75e512e8..8a784ceb 100644 --- a/internal/filtering/safesearch/rules.go +++ b/internal/filtering/safesearch/rules.go @@ -14,6 +14,9 @@ var pixabay string //go:embed rules/duckduckgo.txt var duckduckgo string +//go:embed rules/ecosia.txt +var ecosia string + //go:embed rules/yandex.txt var yandex string @@ -27,6 +30,7 @@ var youtube string var safeSearchRules = map[Service]string{ Bing: bing, DuckDuckGo: duckduckgo, + Ecosia: ecosia, Google: google, Pixabay: pixabay, Yandex: yandex, diff --git a/internal/filtering/safesearch/rules/ecosia.txt b/internal/filtering/safesearch/rules/ecosia.txt new file mode 100644 index 00000000..d75b34d3 --- /dev/null +++ b/internal/filtering/safesearch/rules/ecosia.txt @@ -0,0 +1 @@ +|www.ecosia.org^$dnsrewrite=NOERROR;CNAME;strict-safe-search.ecosia.org diff --git a/internal/filtering/safesearch/safesearch.go b/internal/filtering/safesearch/safesearch.go index 7ea1e3ad..9417102f 100644 --- a/internal/filtering/safesearch/safesearch.go +++ b/internal/filtering/safesearch/safesearch.go @@ -28,6 +28,7 @@ type Service string const ( Bing Service = "bing" DuckDuckGo Service = "duckduckgo" + Ecosia Service = "ecosia" Google Service = "google" Pixabay Service = "pixabay" Yandex Service = "yandex" @@ -41,6 +42,8 @@ func isServiceProtected(s filtering.SafeSearchConfig, service Service) (ok bool) return s.Bing case DuckDuckGo: return s.DuckDuckGo + case Ecosia: + return s.Ecosia case Google: return s.Google case Pixabay: diff --git a/internal/filtering/safesearch/safesearch_internal_test.go b/internal/filtering/safesearch/safesearch_internal_test.go index c6790dc3..24282b75 100644 --- a/internal/filtering/safesearch/safesearch_internal_test.go +++ b/internal/filtering/safesearch/safesearch_internal_test.go @@ -25,6 +25,7 @@ var defaultSafeSearchConf = filtering.SafeSearchConfig{ Enabled: true, Bing: true, DuckDuckGo: true, + Ecosia: true, Google: true, Pixabay: true, Yandex: true, diff --git a/internal/filtering/safesearch/safesearch_test.go b/internal/filtering/safesearch/safesearch_test.go index 9526c791..bcd3534d 100644 --- a/internal/filtering/safesearch/safesearch_test.go +++ b/internal/filtering/safesearch/safesearch_test.go @@ -34,6 +34,7 @@ var testConf = filtering.SafeSearchConfig{ Bing: true, DuckDuckGo: true, + Ecosia: true, Google: true, Pixabay: true, Yandex: true, diff --git a/internal/home/clientshttp.go b/internal/home/clientshttp.go index a8b31835..aae8c34a 100644 --- a/internal/home/clientshttp.go +++ b/internal/home/clientshttp.go @@ -248,6 +248,7 @@ func copySafeSearch( if conf.Enabled { conf.Bing = true conf.DuckDuckGo = true + conf.Ecosia = true conf.Google = true conf.Pixabay = true conf.Yandex = true diff --git a/internal/home/config.go b/internal/home/config.go index 2c15740c..b8d69dae 100644 --- a/internal/home/config.go +++ b/internal/home/config.go @@ -423,6 +423,7 @@ var config = &configuration{ Enabled: false, Bing: true, DuckDuckGo: true, + Ecosia: true, Google: true, Pixabay: true, Yandex: true,