AdGuardHome/internal/home/client.go
Stanislav Chzhen 9241393ed2 Pull request 2083: AG-27492-client-runtime
Squashed commit of the following:

commit e4c2abd37f5885a12da5c68179e3dba18534c3be
Merge: 7411b40b2 dae304fc3
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Dec 13 13:15:40 2023 +0300

    Merge branch 'master' into AG-27492-client-runtime

commit 7411b40b234a438df4c84823fcb19efecf5eb64d
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Dec 12 17:51:51 2023 +0300

    all: imp code

commit d5edd0258779c6dfba56da1e4d79e0380b137385
Merge: 371f5b23c c908eec5d
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Dec 12 13:45:33 2023 +0300

    Merge branch 'master' into AG-27492-client-runtime

commit 371f5b23c9507fb6e8e8f8ec1f644ca8e67ea412
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Dec 12 13:44:38 2023 +0300

    client: imp code

commit 9aefb1443082a342270a7e5e77a07d5d1518e327
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Fri Dec 8 17:08:07 2023 +0300

    all: imp code

commit 3aa51d10aadfd124f36238a31f3d623706e3c870
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Dec 5 19:20:41 2023 +0300

    all: imp docs

commit 71d7187e0c14e143865b055e9b01ff61a2e30083
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Fri Dec 1 17:17:06 2023 +0300

    all: imp code

commit e09f8a0bd90f295b5a580e24b2696cc78993713a
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Thu Nov 30 14:46:53 2023 +0300

    all: imp log msg

commit ce0a9454df9a173694395b4dd8b05347a2e56fdc
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Nov 28 16:21:01 2023 +0300

    all: imp code

commit e84f176aaba6953b22e13a5fa84079a832d30a24
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Nov 22 17:40:18 2023 +0300

    all: add client runtime
2023-12-13 13:39:52 +03:00

85 lines
2.3 KiB
Go

package home
import (
"fmt"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
"github.com/AdguardTeam/AdGuardHome/internal/filtering/safesearch"
"github.com/AdguardTeam/dnsproxy/proxy"
"github.com/AdguardTeam/golibs/stringutil"
)
// Client contains information about persistent clients.
type Client struct {
// upstreamConfig is the custom upstream configuration for this client. If
// it's nil, it has not been initialized yet. If it's non-nil and empty,
// there are no valid upstreams. If it's non-nil and non-empty, these
// upstream must be used.
upstreamConfig *proxy.CustomUpstreamConfig
// TODO(d.kolyshev): Make safeSearchConf a pointer.
safeSearchConf filtering.SafeSearchConfig
SafeSearch filtering.SafeSearch
// BlockedServices is the configuration of blocked services of a client.
BlockedServices *filtering.BlockedServices
Name string
IDs []string
Tags []string
Upstreams []string
UpstreamsCacheSize uint32
UpstreamsCacheEnabled bool
UseOwnSettings bool
FilteringEnabled bool
SafeBrowsingEnabled bool
ParentalEnabled bool
UseOwnBlockedServices bool
IgnoreQueryLog bool
IgnoreStatistics bool
}
// ShallowClone returns a deep copy of the client, except upstreamConfig,
// safeSearchConf, SafeSearch fields, because it's difficult to copy them.
func (c *Client) ShallowClone() (sh *Client) {
clone := *c
clone.BlockedServices = c.BlockedServices.Clone()
clone.IDs = stringutil.CloneSlice(c.IDs)
clone.Tags = stringutil.CloneSlice(c.Tags)
clone.Upstreams = stringutil.CloneSlice(c.Upstreams)
return &clone
}
// closeUpstreams closes the client-specific upstream config of c if any.
func (c *Client) closeUpstreams() (err error) {
if c.upstreamConfig != nil {
if err = c.upstreamConfig.Close(); err != nil {
return fmt.Errorf("closing upstreams of client %q: %w", c.Name, err)
}
}
return nil
}
// setSafeSearch initializes and sets the safe search filter for this client.
func (c *Client) setSafeSearch(
conf filtering.SafeSearchConfig,
cacheSize uint,
cacheTTL time.Duration,
) (err error) {
ss, err := safesearch.NewDefault(conf, fmt.Sprintf("client %q", c.Name), cacheSize, cacheTTL)
if err != nil {
// Don't wrap the error, because it's informative enough as is.
return err
}
c.SafeSearch = ss
return nil
}