mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2025-05-09 17:20:12 +03:00
Pull request: 6263 custom ups cache
Updates #6263. Squashed commit of the following: commitf6b704fb58
Merge:780d001ce
5dd10d9fc
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Nov 24 09:55:04 2023 +0200 Merge remote-tracking branch 'origin/master' into 6263-custom-ups-cache commit780d001cef
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Nov 24 09:53:46 2023 +0200 all: imp locales commit820bcf0e23
Merge:e7ca51563
1320043e9
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Nov 23 11:16:54 2023 +0200 Merge remote-tracking branch 'origin/master' into 6263-custom-ups-cache # Conflicts: # CHANGELOG.md commite7ca515630
Merge:23392d08b
0f5e8ca56
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed Nov 22 09:49:32 2023 +0200 Merge remote-tracking branch 'origin/master' into 6263-custom-ups-cache commit23392d08b1
Merge:a09cd190c
feb15745d
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed Nov 22 09:49:10 2023 +0200 Merge remote-tracking branch 'origin/6263-custom-ups-cache' into 6263-custom-ups-cache commita09cd190c0
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed Nov 22 09:48:43 2023 +0200 home: imp code commitfeb15745d2
Author: Ildar Kamalov <ik@adguard.com> Date: Tue Nov 21 17:27:38 2023 +0300 client: move cache fields to the upstream tab commit789060e4d4
Author: Ildar Kamalov <ik@adguard.com> Date: Tue Nov 21 16:28:03 2023 +0300 client: add form fields commita700bfb42e
Merge:7669419b5
db42254d7
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Nov 21 11:44:14 2023 +0200 Merge remote-tracking branch 'origin/master' into 6263-custom-ups-cache commit7669419b59
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Nov 21 11:34:17 2023 +0200 home: imp code commit4a9dc1d6c1
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon Nov 20 15:08:44 2023 +0200 docs: http client docs commit944e0d7a4b
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon Nov 20 13:51:00 2023 +0200 home: http client dns cache
This commit is contained in:
parent
5dd10d9fc6
commit
e1995408e4
8 changed files with 165 additions and 60 deletions
internal/home
|
@ -56,34 +56,9 @@ type clientJSON struct {
|
|||
|
||||
IgnoreQueryLog aghalg.NullBool `json:"ignore_querylog"`
|
||||
IgnoreStatistics aghalg.NullBool `json:"ignore_statistics"`
|
||||
}
|
||||
|
||||
// copySettings returns a copy of specific settings from JSON or a previous
|
||||
// client.
|
||||
func (j *clientJSON) copySettings(
|
||||
prev *Client,
|
||||
) (weekly *schedule.Weekly, ignoreQueryLog, ignoreStatistics bool) {
|
||||
if j.Schedule != nil {
|
||||
weekly = j.Schedule.Clone()
|
||||
} else if prev != nil && prev.BlockedServices != nil {
|
||||
weekly = prev.BlockedServices.Schedule.Clone()
|
||||
} else {
|
||||
weekly = schedule.EmptyWeekly()
|
||||
}
|
||||
|
||||
if j.IgnoreQueryLog != aghalg.NBNull {
|
||||
ignoreQueryLog = j.IgnoreQueryLog == aghalg.NBTrue
|
||||
} else if prev != nil {
|
||||
ignoreQueryLog = prev.IgnoreQueryLog
|
||||
}
|
||||
|
||||
if j.IgnoreStatistics != aghalg.NBNull {
|
||||
ignoreStatistics = j.IgnoreStatistics == aghalg.NBTrue
|
||||
} else if prev != nil {
|
||||
ignoreStatistics = prev.IgnoreStatistics
|
||||
}
|
||||
|
||||
return weekly, ignoreQueryLog, ignoreStatistics
|
||||
UpstreamsCacheSize uint32 `json:"upstreams_cache_size"`
|
||||
UpstreamsCacheEnabled aghalg.NullBool `json:"upstreams_cache_enabled"`
|
||||
}
|
||||
|
||||
type runtimeClientJSON struct {
|
||||
|
@ -142,42 +117,35 @@ func (clients *clientsContainer) handleGetClients(w http.ResponseWriter, r *http
|
|||
|
||||
// jsonToClient converts JSON object to Client object.
|
||||
func (clients *clientsContainer) jsonToClient(cj clientJSON, prev *Client) (c *Client, err error) {
|
||||
var safeSearchConf filtering.SafeSearchConfig
|
||||
if cj.SafeSearchConf != nil {
|
||||
safeSearchConf = *cj.SafeSearchConf
|
||||
} else {
|
||||
// TODO(d.kolyshev): Remove after cleaning the deprecated
|
||||
// [clientJSON.SafeSearchEnabled] field.
|
||||
safeSearchConf = filtering.SafeSearchConfig{
|
||||
Enabled: cj.SafeSearchEnabled,
|
||||
}
|
||||
safeSearchConf := copySafeSearch(cj.SafeSearchConf, cj.SafeSearchEnabled)
|
||||
|
||||
// Set default service flags for enabled safesearch.
|
||||
if safeSearchConf.Enabled {
|
||||
safeSearchConf.Bing = true
|
||||
safeSearchConf.DuckDuckGo = true
|
||||
safeSearchConf.Google = true
|
||||
safeSearchConf.Pixabay = true
|
||||
safeSearchConf.Yandex = true
|
||||
safeSearchConf.YouTube = true
|
||||
}
|
||||
var ignoreQueryLog bool
|
||||
if cj.IgnoreQueryLog != aghalg.NBNull {
|
||||
ignoreQueryLog = cj.IgnoreQueryLog == aghalg.NBTrue
|
||||
} else if prev != nil {
|
||||
ignoreQueryLog = prev.IgnoreQueryLog
|
||||
}
|
||||
|
||||
weekly, ignoreQueryLog, ignoreStatistics := cj.copySettings(prev)
|
||||
|
||||
bs := &filtering.BlockedServices{
|
||||
Schedule: weekly,
|
||||
IDs: cj.BlockedServices,
|
||||
}
|
||||
err = bs.Validate()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("validating blocked services: %w", err)
|
||||
var ignoreStatistics bool
|
||||
if cj.IgnoreStatistics != aghalg.NBNull {
|
||||
ignoreStatistics = cj.IgnoreStatistics == aghalg.NBTrue
|
||||
} else if prev != nil {
|
||||
ignoreStatistics = prev.IgnoreStatistics
|
||||
}
|
||||
|
||||
var upsCacheEnabled bool
|
||||
var upsCacheSize uint32
|
||||
if prev != nil {
|
||||
upsCacheEnabled, upsCacheSize = prev.UpstreamsCacheEnabled, prev.UpstreamsCacheSize
|
||||
if cj.UpstreamsCacheEnabled != aghalg.NBNull {
|
||||
upsCacheEnabled = cj.UpstreamsCacheEnabled == aghalg.NBTrue
|
||||
upsCacheSize = cj.UpstreamsCacheSize
|
||||
} else if prev != nil {
|
||||
upsCacheEnabled = prev.UpstreamsCacheEnabled
|
||||
upsCacheSize = prev.UpstreamsCacheSize
|
||||
}
|
||||
|
||||
svcs, err := copyBlockedServices(cj.Schedule, cj.BlockedServices, prev)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid blocked services: %w", err)
|
||||
}
|
||||
|
||||
c = &Client{
|
||||
|
@ -185,7 +153,7 @@ func (clients *clientsContainer) jsonToClient(cj clientJSON, prev *Client) (c *C
|
|||
|
||||
Name: cj.Name,
|
||||
|
||||
BlockedServices: bs,
|
||||
BlockedServices: svcs,
|
||||
|
||||
IDs: cj.IDs,
|
||||
Tags: cj.Tags,
|
||||
|
@ -216,6 +184,63 @@ func (clients *clientsContainer) jsonToClient(cj clientJSON, prev *Client) (c *C
|
|||
return c, nil
|
||||
}
|
||||
|
||||
// copySafeSearch returns safe search config created from provided parameters.
|
||||
func copySafeSearch(
|
||||
jsonConf *filtering.SafeSearchConfig,
|
||||
enabled bool,
|
||||
) (conf filtering.SafeSearchConfig) {
|
||||
if jsonConf != nil {
|
||||
return *jsonConf
|
||||
}
|
||||
|
||||
// TODO(d.kolyshev): Remove after cleaning the deprecated
|
||||
// [clientJSON.SafeSearchEnabled] field.
|
||||
conf = filtering.SafeSearchConfig{
|
||||
Enabled: enabled,
|
||||
}
|
||||
|
||||
// Set default service flags for enabled safesearch.
|
||||
if conf.Enabled {
|
||||
conf.Bing = true
|
||||
conf.DuckDuckGo = true
|
||||
conf.Google = true
|
||||
conf.Pixabay = true
|
||||
conf.Yandex = true
|
||||
conf.YouTube = true
|
||||
}
|
||||
|
||||
return conf
|
||||
}
|
||||
|
||||
// copyBlockedServices converts a json blocked services to an internal blocked
|
||||
// services.
|
||||
func copyBlockedServices(
|
||||
sch *schedule.Weekly,
|
||||
svcStrs []string,
|
||||
prev *Client,
|
||||
) (svcs *filtering.BlockedServices, err error) {
|
||||
var weekly *schedule.Weekly
|
||||
if sch != nil {
|
||||
weekly = sch.Clone()
|
||||
} else if prev != nil && prev.BlockedServices != nil {
|
||||
weekly = prev.BlockedServices.Schedule.Clone()
|
||||
} else {
|
||||
weekly = schedule.EmptyWeekly()
|
||||
}
|
||||
|
||||
svcs = &filtering.BlockedServices{
|
||||
Schedule: weekly,
|
||||
IDs: svcStrs,
|
||||
}
|
||||
|
||||
err = svcs.Validate()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("validating blocked services: %w", err)
|
||||
}
|
||||
|
||||
return svcs, nil
|
||||
}
|
||||
|
||||
// clientToJSON converts Client object to JSON.
|
||||
func clientToJSON(c *Client) (cj *clientJSON) {
|
||||
// TODO(d.kolyshev): Remove after cleaning the deprecated
|
||||
|
@ -243,6 +268,9 @@ func clientToJSON(c *Client) (cj *clientJSON) {
|
|||
|
||||
IgnoreQueryLog: aghalg.BoolToNullBool(c.IgnoreQueryLog),
|
||||
IgnoreStatistics: aghalg.BoolToNullBool(c.IgnoreStatistics),
|
||||
|
||||
UpstreamsCacheSize: c.UpstreamsCacheSize,
|
||||
UpstreamsCacheEnabled: aghalg.BoolToNullBool(c.UpstreamsCacheEnabled),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue