mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2026-12-05 14:43:12 +09:00
Squashed commit of the following: commit 9dd78c7b638b243da783450779129904a41263bb Merge: b0d84ea2db7c97190cAuthor: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Feb 2 14:00:15 2026 +0300 Merge branch 'master' into AGDNS-3568-imp-fswatcher commit b0d84ea2d7b8fa83e97e8a713bd8ece191c72614 Merge: a6b06d465e87f57c1cAuthor: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Feb 2 13:28:23 2026 +0300 Merge branch 'master' into AGDNS-3568-imp-fswatcher commit a6b06d465d8b4cad5020ccf7b5d581f03547e22b Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Jan 30 19:31:40 2026 +0300 all: fix windows commit 26287eaab317b271633b6f0cd487266c79d6ac36 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Jan 30 14:34:17 2026 +0300 aghnet: fix windows test commit e5764470866f4a2a1385fd20cfbaed0dcc89eb49 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Jan 29 21:18:23 2026 +0300 all: downgrade fsnotify commit 9e4162c7b08517cc1738fcf6ca87f00b1c000eb4 Merge: 0139d03f58f55534e5Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Jan 29 21:18:15 2026 +0300 Merge branch 'master' into AGDNS-3568-imp-fswatcher commit 0139d03f50a4562d1566c86cd57dbdcbd88e3bed Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Jan 27 14:04:35 2026 +0300 aghnet: imp doc commit 6688ccd4fccb7618c8dfc19c44b46293c201b01c Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Jan 26 19:59:07 2026 +0300 all: imp fswatcher & hosts container
164 lines
4.1 KiB
Go
164 lines
4.1 KiB
Go
package aghtls
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"sync"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
|
)
|
|
|
|
// DefaultManagerConfig is the configuration structure for [NewDefaultManager].
|
|
type DefaultManagerConfig struct {
|
|
// Logger is used for logging the operation of the manager. It must not be
|
|
// nil.
|
|
Logger *slog.Logger
|
|
|
|
// Watcher is used to watch the TLS certificate and key files. It must not
|
|
// be nil.
|
|
Watcher aghos.FSWatcher
|
|
}
|
|
|
|
// DefaultManager is the default implementation of the [Manager] interface.
|
|
//
|
|
// TODO(e.burkov): Add tests.
|
|
type DefaultManager struct {
|
|
logger *slog.Logger
|
|
pairMu *sync.Mutex
|
|
updates chan UpdateSignal
|
|
watcher aghos.FSWatcher
|
|
pair TLSPair
|
|
}
|
|
|
|
// NewDefaultManager returns a new properly initialized default manager.
|
|
func NewDefaultManager(c *DefaultManagerConfig) (mgr *DefaultManager) {
|
|
return &DefaultManager{
|
|
logger: c.Logger,
|
|
pairMu: &sync.Mutex{},
|
|
pair: TLSPair{},
|
|
// Buffer the channel to avoid missing updates.
|
|
updates: make(chan UpdateSignal, 1),
|
|
watcher: c.Watcher,
|
|
}
|
|
}
|
|
|
|
// type check
|
|
var _ Manager = (*DefaultManager)(nil)
|
|
|
|
// Set implements the [Manager] interface for *DefaultManager.
|
|
func (mgr *DefaultManager) Set(ctx context.Context, certKey TLSPair) (err error) {
|
|
mgr.logger.DebugContext(ctx, "setting", "cert", certKey.CertPath, "key", certKey.KeyPath)
|
|
|
|
var errs []error
|
|
|
|
mgr.pairMu.Lock()
|
|
defer mgr.pairMu.Unlock()
|
|
|
|
old := mgr.pair
|
|
|
|
errs = mgr.appendUnwatchErr(errs, "old cert", old.CertPath)
|
|
errs = mgr.appendUnwatchErr(errs, "old key", old.KeyPath)
|
|
errs = mgr.appendWatchErr(errs, "new cert", certKey.CertPath)
|
|
errs = mgr.appendWatchErr(errs, "new key", certKey.KeyPath)
|
|
|
|
mgr.pair = certKey
|
|
|
|
return errors.Join(errs...)
|
|
}
|
|
|
|
// appendUnwatchErr stops watching a file at path p described by what and
|
|
// appends an error to the errs slice, if any. Empty p is ignored.
|
|
func (mgr *DefaultManager) appendUnwatchErr(errs []error, what, p string) (result []error) {
|
|
if p == "" {
|
|
return errs
|
|
}
|
|
|
|
err := mgr.watcher.Remove(p)
|
|
if err != nil {
|
|
errs = append(errs, fmt.Errorf("unwatching %s %s: %w", what, p, err))
|
|
}
|
|
|
|
return errs
|
|
}
|
|
|
|
// appendWatchErr starts watching a file at path p described by what and
|
|
// appends an error to the errs slice, if any. Empty p is ignored.
|
|
func (mgr *DefaultManager) appendWatchErr(errs []error, what, p string) (result []error) {
|
|
if p == "" {
|
|
return errs
|
|
}
|
|
|
|
err := mgr.watcher.Add(p)
|
|
if err != nil {
|
|
errs = append(errs, fmt.Errorf("watching %s %s: %w", what, p, err))
|
|
}
|
|
|
|
return errs
|
|
}
|
|
|
|
// Refresh implements the [service.Refresher] interface for *DefaultManager.
|
|
func (mgr *DefaultManager) Refresh(ctx context.Context) (err error) {
|
|
mgr.logger.DebugContext(ctx, "refreshing")
|
|
|
|
select {
|
|
case mgr.updates <- UpdateSignal{}:
|
|
return nil
|
|
case <-ctx.Done():
|
|
return fmt.Errorf("refreshing: %w", ctx.Err())
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// Start implements the [service.Interface] interface for *DefaultManager.
|
|
func (mgr *DefaultManager) Start(ctx context.Context) (err error) {
|
|
err = mgr.watcher.Start(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("starting watcher: %w", err)
|
|
}
|
|
|
|
go mgr.handleEvents(ctx)
|
|
|
|
return nil
|
|
}
|
|
|
|
// Shutdown implements the [service.Interface] interface for *DefaultManager.
|
|
func (mgr *DefaultManager) Shutdown(ctx context.Context) (err error) {
|
|
defer close(mgr.updates)
|
|
|
|
err = mgr.watcher.Shutdown(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("shutting down watcher: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Updates implements the [Manager] interface for *DefaultManager.
|
|
func (mgr *DefaultManager) Updates(ctx context.Context) (updates <-chan UpdateSignal) {
|
|
return mgr.updates
|
|
}
|
|
|
|
// handleEvents handles changes of the tracked files. It is intended to be run
|
|
// in a separate goroutine.
|
|
func (mgr *DefaultManager) handleEvents(ctx context.Context) {
|
|
defer slogutil.RecoverAndLog(ctx, mgr.logger)
|
|
|
|
eventsCh := mgr.watcher.Events()
|
|
if eventsCh == nil {
|
|
mgr.logger.DebugContext(ctx, "watcher does not emit events")
|
|
|
|
return
|
|
}
|
|
|
|
for range eventsCh {
|
|
err := mgr.Refresh(ctx)
|
|
if err != nil {
|
|
mgr.logger.ErrorContext(ctx, "refreshing", slogutil.KeyError, err)
|
|
}
|
|
}
|
|
}
|