home: imp logs

This commit is contained in:
Stanislav Chzhen 2025-09-30 19:16:52 +03:00
parent e655b92bfa
commit f772e6f927

View File

@ -253,9 +253,16 @@ func configureOS(conf *configuration) (err error) {
// setupHostsContainer initializes the structures to keep up-to-date the hosts
// provided by the OS. baseLogger must not be nil.
func setupHostsContainer(ctx context.Context, baseLogger *slog.Logger) (err error) {
l := baseLogger.With(slogutil.KeyPrefix, "hosts")
hostsWatcher, err := aghos.NewOSWritesWatcher(baseLogger.With(slogutil.KeyPrefix, "oswatcher"))
if err != nil {
log.Info("WARNING: initializing filesystem watcher: %s; not watching for changes", err)
l.WarnContext(
ctx,
"initializing filesystem watcher; not watching for changes",
slogutil.KeyError,
err,
)
hostsWatcher = aghos.EmptyFSWatcher{}
}
@ -265,7 +272,6 @@ func setupHostsContainer(ctx context.Context, baseLogger *slog.Logger) (err erro
return fmt.Errorf("getting default system hosts paths: %w", err)
}
l := baseLogger.With(slogutil.KeyPrefix, "hosts")
globalContext.etcHosts, err = aghnet.NewHostsContainer(
ctx,
l,
@ -276,7 +282,7 @@ func setupHostsContainer(ctx context.Context, baseLogger *slog.Logger) (err erro
if err != nil {
closeErr := hostsWatcher.Shutdown(ctx)
if errors.Is(err, aghnet.ErrNoHostsPaths) {
log.Info("warning: initing hosts container: %s", err)
l.WarnContext(ctx, "initializing hosts container", slogutil.KeyError, err)
return closeErr
}
@ -947,38 +953,48 @@ func (c *configuration) anonymizer() (ipmut *aghnet.IPMut) {
return aghnet.NewIPMut(anonFunc)
}
// permCheckHelp is printed when binding to privileged ports is not permitted.
const permCheckHelp = `Permission check failed.
AdGuard Home is not allowed to bind to privileged ports (for instance, port 53).
Please note that this is crucial for a server to be able to use privileged ports.
You have two options:
1. Run AdGuard Home with root privileges.
2. On Linux you can grant the CAP_NET_BIND_SERVICE capability:
https://github.com/AdguardTeam/AdGuardHome/wiki/Getting-Started#running-without-superuser`
// checkNetworkPermissions checks if the current user permissions are enough to
// use the required networking functionality. l must not be nil.
func checkNetworkPermissions(ctx context.Context, l *slog.Logger) {
log.Info("Checking if AdGuard Home has necessary permissions")
l.InfoContext(ctx, "checking if adguard home has the necessary permissions")
if ok, err := aghnet.CanBindPrivilegedPorts(ctx, l); !ok || err != nil {
log.Fatal("This is the first launch of AdGuard Home. You must run it as Administrator.")
l.ErrorContext(
ctx,
"this is the first launch of adguard home; you must run it as administrator.",
)
os.Exit(osutil.ExitCodeFailure)
}
// We should check if AdGuard Home is able to bind to port 53
err := aghnet.CheckPort("tcp", netip.AddrPortFrom(netutil.IPv4Localhost(), defaultPortDNS))
if err != nil {
if errors.Is(err, os.ErrPermission) {
log.Fatal(`Permission check failed.
slogutil.PrintLines(ctx, l, slog.LevelError, "", permCheckHelp)
AdGuard Home is not allowed to bind to privileged ports (for instance, port 53).
Please note, that this is crucial for a server to be able to use privileged ports.
You have two options:
1. Run AdGuard Home with root privileges
2. On Linux you can grant the CAP_NET_BIND_SERVICE capability:
https://github.com/AdguardTeam/AdGuardHome/wiki/Getting-Started#running-without-superuser`)
os.Exit(osutil.ExitCodeFailure)
}
log.Info(
"AdGuard failed to bind to port 53: %s\n\n"+
"Please note, that this is crucial for a DNS server to be able to use that port.",
err,
l.ErrorContext(
ctx,
"failed to bind to port 53; binding to port 53 is required for a dns server",
slogutil.KeyError, err,
)
}
log.Info("AdGuard Home can bind to port 53")
l.InfoContext(ctx, "adguard home can bind to port 53")
}
// Write PID to a file