all: ecosia safesearch

This commit is contained in:
Dimitry Kolyshev 2024-07-30 13:49:51 +03:00
parent bf1101b460
commit 5ad88d914c
12 changed files with 122 additions and 1 deletions

View File

@ -2,4 +2,4 @@
package configmigrate package configmigrate
// LastSchemaVersion is the most recent schema version. // LastSchemaVersion is the most recent schema version.
const LastSchemaVersion uint = 28 const LastSchemaVersion uint = 29

View File

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

View File

@ -120,6 +120,7 @@ func (m *Migrator) upgradeConfigSchema(current, target uint, diskConf yobj) (err
25: migrateTo26, 25: migrateTo26,
26: migrateTo27, 26: migrateTo27,
27: migrateTo28, 27: migrateTo28,
28: migrateTo29,
} }
for i, migrate := range upgrades[current:target] { for i, migrate := range upgrades[current:target] {

View File

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

View File

@ -22,6 +22,7 @@ type SafeSearchConfig struct {
Bing bool `yaml:"bing" json:"bing"` Bing bool `yaml:"bing" json:"bing"`
DuckDuckGo bool `yaml:"duckduckgo" json:"duckduckgo"` DuckDuckGo bool `yaml:"duckduckgo" json:"duckduckgo"`
Ecosia bool `yaml:"duckduckgo" json:"ecosia"`
Google bool `yaml:"google" json:"google"` Google bool `yaml:"google" json:"google"`
Pixabay bool `yaml:"pixabay" json:"pixabay"` Pixabay bool `yaml:"pixabay" json:"pixabay"`
Yandex bool `yaml:"yandex" json:"yandex"` Yandex bool `yaml:"yandex" json:"yandex"`

View File

@ -14,6 +14,9 @@ var pixabay string
//go:embed rules/duckduckgo.txt //go:embed rules/duckduckgo.txt
var duckduckgo string var duckduckgo string
//go:embed rules/ecosia.txt
var ecosia string
//go:embed rules/yandex.txt //go:embed rules/yandex.txt
var yandex string var yandex string
@ -27,6 +30,7 @@ var youtube string
var safeSearchRules = map[Service]string{ var safeSearchRules = map[Service]string{
Bing: bing, Bing: bing,
DuckDuckGo: duckduckgo, DuckDuckGo: duckduckgo,
Ecosia: ecosia,
Google: google, Google: google,
Pixabay: pixabay, Pixabay: pixabay,
Yandex: yandex, Yandex: yandex,

View File

@ -0,0 +1 @@
|www.ecosia.org^$dnsrewrite=NOERROR;CNAME;strict-safe-search.ecosia.org

View File

@ -28,6 +28,7 @@ type Service string
const ( const (
Bing Service = "bing" Bing Service = "bing"
DuckDuckGo Service = "duckduckgo" DuckDuckGo Service = "duckduckgo"
Ecosia Service = "ecosia"
Google Service = "google" Google Service = "google"
Pixabay Service = "pixabay" Pixabay Service = "pixabay"
Yandex Service = "yandex" Yandex Service = "yandex"
@ -41,6 +42,8 @@ func isServiceProtected(s filtering.SafeSearchConfig, service Service) (ok bool)
return s.Bing return s.Bing
case DuckDuckGo: case DuckDuckGo:
return s.DuckDuckGo return s.DuckDuckGo
case Ecosia:
return s.Ecosia
case Google: case Google:
return s.Google return s.Google
case Pixabay: case Pixabay:

View File

@ -25,6 +25,7 @@ var defaultSafeSearchConf = filtering.SafeSearchConfig{
Enabled: true, Enabled: true,
Bing: true, Bing: true,
DuckDuckGo: true, DuckDuckGo: true,
Ecosia: true,
Google: true, Google: true,
Pixabay: true, Pixabay: true,
Yandex: true, Yandex: true,

View File

@ -34,6 +34,7 @@ var testConf = filtering.SafeSearchConfig{
Bing: true, Bing: true,
DuckDuckGo: true, DuckDuckGo: true,
Ecosia: true,
Google: true, Google: true,
Pixabay: true, Pixabay: true,
Yandex: true, Yandex: true,

View File

@ -248,6 +248,7 @@ func copySafeSearch(
if conf.Enabled { if conf.Enabled {
conf.Bing = true conf.Bing = true
conf.DuckDuckGo = true conf.DuckDuckGo = true
conf.Ecosia = true
conf.Google = true conf.Google = true
conf.Pixabay = true conf.Pixabay = true
conf.Yandex = true conf.Yandex = true

View File

@ -423,6 +423,7 @@ var config = &configuration{
Enabled: false, Enabled: false,
Bing: true, Bing: true,
DuckDuckGo: true, DuckDuckGo: true,
Ecosia: true,
Google: true, Google: true,
Pixabay: true, Pixabay: true,
Yandex: true, Yandex: true,