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
// 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,
26: migrateTo27,
27: migrateTo28,
28: migrateTo29,
}
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"`
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"`

View file

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

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 (
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:

View file

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

View file

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

View file

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

View file

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