2023-06-13 13:41:13 +03:00
|
|
|
// Package cmd is the AdGuard Home entry point. It assembles the configuration
|
|
|
|
// file manager, sets up signal processing logic, and so on.
|
2022-04-26 20:50:09 +03:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Move to the upper-level internal/.
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/fs"
|
2022-10-10 14:05:24 +03:00
|
|
|
"os"
|
2022-04-26 20:50:09 +03:00
|
|
|
"time"
|
|
|
|
|
2022-10-10 14:05:24 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/configmgr"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/version"
|
2022-04-26 20:50:09 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
)
|
|
|
|
|
2023-06-13 13:41:13 +03:00
|
|
|
// Main is the entry point of AdGuard Home.
|
2023-06-29 19:10:39 +03:00
|
|
|
func Main(embeddedFrontend fs.FS) {
|
2023-06-20 14:11:34 +03:00
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
cmdName := os.Args[0]
|
|
|
|
opts, err := parseOptions(cmdName, os.Args[1:])
|
|
|
|
exitCode, needExit := processOptions(opts, cmdName, err)
|
|
|
|
if needExit {
|
|
|
|
os.Exit(exitCode)
|
|
|
|
}
|
2022-04-26 20:50:09 +03:00
|
|
|
|
2023-06-20 14:11:34 +03:00
|
|
|
err = setLog(opts)
|
|
|
|
check(err)
|
2022-04-26 20:50:09 +03:00
|
|
|
|
2022-10-10 14:05:24 +03:00
|
|
|
log.Info("starting adguard home, version %s, pid %d", version.Version(), os.Getpid())
|
|
|
|
|
2023-06-20 14:11:34 +03:00
|
|
|
if opts.workDir != "" {
|
|
|
|
log.Info("changing working directory to %q", opts.workDir)
|
|
|
|
err = os.Chdir(opts.workDir)
|
|
|
|
check(err)
|
|
|
|
}
|
2022-04-26 20:50:09 +03:00
|
|
|
|
2023-06-29 19:10:39 +03:00
|
|
|
frontend, err := frontendFromOpts(opts, embeddedFrontend)
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
confMgrConf := &configmgr.Config{
|
|
|
|
Frontend: frontend,
|
|
|
|
WebAddr: opts.webAddr,
|
|
|
|
Start: start,
|
|
|
|
FileName: opts.confFile,
|
|
|
|
}
|
|
|
|
|
|
|
|
confMgr, err := newConfigMgr(confMgrConf)
|
2023-06-13 13:41:13 +03:00
|
|
|
check(err)
|
2022-10-10 14:05:24 +03:00
|
|
|
|
|
|
|
web := confMgr.Web()
|
|
|
|
err = web.Start()
|
2023-06-13 13:41:13 +03:00
|
|
|
check(err)
|
2022-10-10 14:05:24 +03:00
|
|
|
|
|
|
|
dns := confMgr.DNS()
|
|
|
|
err = dns.Start()
|
2023-06-13 13:41:13 +03:00
|
|
|
check(err)
|
2022-04-26 20:50:09 +03:00
|
|
|
|
|
|
|
sigHdlr := newSignalHandler(
|
2023-06-29 19:10:39 +03:00
|
|
|
confMgrConf,
|
|
|
|
opts.pidFile,
|
2022-04-26 20:50:09 +03:00
|
|
|
web,
|
2022-10-10 14:05:24 +03:00
|
|
|
dns,
|
2022-04-26 20:50:09 +03:00
|
|
|
)
|
|
|
|
|
2023-06-13 13:41:13 +03:00
|
|
|
sigHdlr.handle()
|
2022-04-26 20:50:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// defaultTimeout is the timeout used for some operations where another timeout
|
|
|
|
// hasn't been defined yet.
|
2023-06-13 13:41:13 +03:00
|
|
|
const defaultTimeout = 5 * time.Second
|
2022-04-26 20:50:09 +03:00
|
|
|
|
|
|
|
// ctxWithDefaultTimeout is a helper function that returns a context with
|
|
|
|
// timeout set to defaultTimeout.
|
|
|
|
func ctxWithDefaultTimeout() (ctx context.Context, cancel context.CancelFunc) {
|
|
|
|
return context.WithTimeout(context.Background(), defaultTimeout)
|
|
|
|
}
|
|
|
|
|
2023-06-29 14:34:06 +03:00
|
|
|
// newConfigMgr returns a new configuration manager using defaultTimeout as the
|
|
|
|
// context timeout.
|
2023-06-29 19:10:39 +03:00
|
|
|
func newConfigMgr(c *configmgr.Config) (m *configmgr.Manager, err error) {
|
2023-06-29 14:34:06 +03:00
|
|
|
ctx, cancel := ctxWithDefaultTimeout()
|
|
|
|
defer cancel()
|
|
|
|
|
2023-06-29 19:10:39 +03:00
|
|
|
return configmgr.New(ctx, c)
|
2023-06-29 14:34:06 +03:00
|
|
|
}
|
|
|
|
|
2023-06-13 13:41:13 +03:00
|
|
|
// check is a simple error-checking helper. It must only be used within Main.
|
|
|
|
func check(err error) {
|
2022-04-26 20:50:09 +03:00
|
|
|
if err != nil {
|
2023-06-13 13:41:13 +03:00
|
|
|
panic(err)
|
2022-04-26 20:50:09 +03:00
|
|
|
}
|
|
|
|
}
|