2020-10-29 16:39:11 +00:00
|
|
|
// Package home contains AdGuard Home's HTTP API methods.
|
2019-06-10 09:33:19 +01:00
|
|
|
package home
|
2018-08-30 15:25:33 +01:00
|
|
|
|
|
|
|
import (
|
2019-04-25 12:57:03 +01:00
|
|
|
"context"
|
2020-03-04 12:11:17 +00:00
|
|
|
"crypto/x509"
|
2018-08-30 15:25:33 +01:00
|
|
|
"fmt"
|
2021-05-21 12:55:42 +01:00
|
|
|
"io/fs"
|
2018-08-30 15:25:33 +01:00
|
|
|
"net/http"
|
2022-10-14 13:29:44 +01:00
|
|
|
"net/netip"
|
2023-09-21 15:07:57 +01:00
|
|
|
"net/url"
|
2018-08-30 15:25:33 +01:00
|
|
|
"os"
|
2018-12-05 12:36:18 +00:00
|
|
|
"os/signal"
|
2023-09-21 15:07:57 +01:00
|
|
|
"path"
|
2018-08-30 15:25:33 +01:00
|
|
|
"path/filepath"
|
2019-02-05 11:09:05 +00:00
|
|
|
"runtime"
|
2024-02-08 17:39:18 +00:00
|
|
|
"slices"
|
2019-02-12 18:14:02 +00:00
|
|
|
"sync"
|
2018-12-05 12:36:18 +00:00
|
|
|
"syscall"
|
2019-06-06 01:00:15 +01:00
|
|
|
"time"
|
2018-08-30 15:25:33 +01:00
|
|
|
|
2022-01-19 17:45:50 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
|
2022-09-23 11:23:35 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
2021-03-16 16:42:15 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
2022-01-26 11:39:34 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtls"
|
2023-08-24 11:42:17 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/arpdb"
|
2020-10-30 10:32:02 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpd"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
|
2021-05-21 14:15:47 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2023-04-27 14:39:35 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering/hashprefix"
|
2023-03-15 11:31:07 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering/safesearch"
|
2020-10-30 10:32:02 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/querylog"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/stats"
|
2021-01-13 13:18:51 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/updater"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/version"
|
2023-04-27 14:39:35 +01:00
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
2021-05-24 15:28:11 +01:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2023-12-07 13:48:55 +00:00
|
|
|
"github.com/AdguardTeam/golibs/hostsfile"
|
2019-02-25 13:44:22 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-08-09 14:03:37 +01:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2023-12-07 13:48:55 +00:00
|
|
|
"github.com/AdguardTeam/golibs/osutil"
|
2023-05-10 14:30:03 +01:00
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
2019-02-05 11:09:05 +00:00
|
|
|
)
|
|
|
|
|
2019-12-11 09:38:58 +00:00
|
|
|
// Global context
|
|
|
|
type homeContext struct {
|
2020-02-13 15:42:07 +00:00
|
|
|
// Modules
|
|
|
|
// --
|
|
|
|
|
2021-10-14 17:39:21 +01:00
|
|
|
clients clientsContainer // per-client-settings module
|
2022-08-04 17:05:28 +01:00
|
|
|
stats stats.Interface // statistics module
|
2021-10-14 17:39:21 +01:00
|
|
|
queryLog querylog.QueryLog // query log module
|
|
|
|
dnsServer *dnsforward.Server // DNS module
|
2022-09-13 21:45:35 +01:00
|
|
|
dhcpServer dhcpd.Interface // DHCP module
|
2021-10-14 17:39:21 +01:00
|
|
|
auth *Auth // HTTP authentication module
|
2022-09-23 11:23:35 +01:00
|
|
|
filters *filtering.DNSFilter // DNS filtering module
|
2023-04-11 15:22:51 +01:00
|
|
|
web *webAPI // Web (HTTP, HTTPS) module
|
2022-10-14 17:37:14 +01:00
|
|
|
tls *tlsManager // TLS module
|
2023-03-28 11:27:46 +01:00
|
|
|
|
|
|
|
// etcHosts contains IP-hostname mappings taken from the OS-specific hosts
|
|
|
|
// configuration files, for example /etc/hosts.
|
2021-10-14 17:39:21 +01:00
|
|
|
etcHosts *aghnet.HostsContainer
|
2021-11-17 14:21:10 +00:00
|
|
|
|
2020-11-25 12:50:59 +00:00
|
|
|
// mux is our custom http.ServeMux.
|
|
|
|
mux *http.ServeMux
|
|
|
|
|
2020-02-13 15:42:07 +00:00
|
|
|
// Runtime properties
|
|
|
|
// --
|
|
|
|
|
2023-07-19 14:57:57 +01:00
|
|
|
configFilename string // Config filename (can be overridden via the command line arguments)
|
|
|
|
workDir string // Location of our directory, used to protect against CWD being somewhere else
|
|
|
|
pidFileName string // PID file name. Empty if no PID file was created.
|
|
|
|
controlLock sync.Mutex
|
|
|
|
tlsRoots *x509.CertPool // list of root CAs for TLSv1.2
|
2022-10-14 18:14:07 +01:00
|
|
|
|
|
|
|
// tlsCipherIDs are the ID of the cipher suites that AdGuard Home must use.
|
|
|
|
tlsCipherIDs []uint16
|
|
|
|
|
2022-11-08 08:36:42 +00:00
|
|
|
// firstRun, if true, tells AdGuard Home to only start the web interface
|
|
|
|
// service, and only serve the first-run APIs.
|
|
|
|
firstRun bool
|
2020-02-13 15:42:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getDataDir returns path to the directory where we store databases and filters
|
|
|
|
func (c *homeContext) getDataDir() string {
|
|
|
|
return filepath.Join(c.workDir, dataDir)
|
2019-12-11 09:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Context - a global context object
|
|
|
|
var Context homeContext
|
|
|
|
|
2019-07-09 16:52:18 +01:00
|
|
|
// Main is the entry point
|
2021-05-21 12:55:42 +01:00
|
|
|
func Main(clientBuildFS fs.FS) {
|
2022-10-05 15:07:08 +01:00
|
|
|
initCmdLineOpts()
|
|
|
|
|
|
|
|
// The configuration file path can be overridden, but other command-line
|
|
|
|
// options have to override config values. Therefore, do it manually
|
|
|
|
// instead of using package flag.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): The comment above is most likely false. Replace with
|
|
|
|
// package flag.
|
|
|
|
opts := loadCmdLineOpts()
|
2018-08-30 15:25:33 +01:00
|
|
|
|
2023-08-28 14:40:46 +01:00
|
|
|
done := make(chan struct{})
|
|
|
|
|
2023-07-19 14:57:57 +01:00
|
|
|
signals := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT)
|
2023-08-28 14:40:46 +01:00
|
|
|
|
2020-02-13 15:42:07 +00:00
|
|
|
go func() {
|
2020-02-18 11:49:50 +00:00
|
|
|
for {
|
2023-07-19 14:57:57 +01:00
|
|
|
sig := <-signals
|
2020-11-05 12:20:57 +00:00
|
|
|
log.Info("Received signal %q", sig)
|
2020-02-18 11:49:50 +00:00
|
|
|
switch sig {
|
|
|
|
case syscall.SIGHUP:
|
2023-03-01 10:27:24 +00:00
|
|
|
Context.clients.reloadARP()
|
2022-10-14 17:37:14 +01:00
|
|
|
Context.tls.reload()
|
2020-02-18 11:49:50 +00:00
|
|
|
default:
|
2021-01-26 16:44:19 +00:00
|
|
|
cleanup(context.Background())
|
2020-02-18 11:49:50 +00:00
|
|
|
cleanupAlways()
|
2023-08-28 14:40:46 +01:00
|
|
|
close(done)
|
2020-02-18 11:49:50 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-13 15:42:07 +00:00
|
|
|
}()
|
|
|
|
|
2022-10-05 15:07:08 +01:00
|
|
|
if opts.serviceControlAction != "" {
|
2023-08-31 15:50:46 +01:00
|
|
|
handleServiceControlAction(opts, clientBuildFS, signals, done)
|
2021-06-03 19:04:13 +01:00
|
|
|
|
2020-06-11 08:24:43 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-05 11:09:05 +00:00
|
|
|
// run the protection
|
2023-08-28 14:40:46 +01:00
|
|
|
run(opts, clientBuildFS, done)
|
2019-02-04 10:54:53 +00:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
// setupContext initializes [Context] fields. It also reads and upgrades
|
|
|
|
// config file if necessary.
|
|
|
|
func setupContext(opts options) (err error) {
|
2023-07-19 14:57:57 +01:00
|
|
|
Context.firstRun = detectFirstRun()
|
2019-04-01 10:22:54 +01:00
|
|
|
|
2022-10-14 17:03:03 +01:00
|
|
|
Context.tlsRoots = aghtls.SystemRootCAs()
|
2023-05-10 14:30:03 +01:00
|
|
|
Context.mux = http.NewServeMux()
|
|
|
|
|
2023-07-19 14:57:57 +01:00
|
|
|
if Context.firstRun {
|
|
|
|
log.Info("This is the first time AdGuard Home is launched")
|
|
|
|
checkPermissions()
|
2021-04-16 08:32:41 +01:00
|
|
|
|
2023-07-19 14:57:57 +01:00
|
|
|
return nil
|
|
|
|
}
|
2019-04-30 13:26:57 +01:00
|
|
|
|
2023-08-31 14:34:15 +01:00
|
|
|
err = parseConfig()
|
2023-07-19 14:57:57 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error("parsing configuration file: %s", err)
|
2022-04-26 11:04:16 +01:00
|
|
|
|
2023-07-19 14:57:57 +01:00
|
|
|
os.Exit(1)
|
2019-02-04 10:54:53 +00:00
|
|
|
}
|
2020-11-25 12:50:59 +00:00
|
|
|
|
2023-07-19 14:57:57 +01:00
|
|
|
if opts.checkConfig {
|
|
|
|
log.Info("configuration file is ok")
|
2019-02-04 10:54:53 +00:00
|
|
|
|
2023-07-19 14:57:57 +01:00
|
|
|
os.Exit(0)
|
2022-10-05 15:07:08 +01:00
|
|
|
}
|
|
|
|
|
2023-11-16 11:05:10 +00:00
|
|
|
if !opts.noEtcHosts {
|
2023-07-19 14:57:57 +01:00
|
|
|
err = setupHostsContainer()
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error, because it's informative enough as is.
|
|
|
|
return err
|
|
|
|
}
|
2022-10-05 15:07:08 +01:00
|
|
|
}
|
2023-07-19 14:57:57 +01:00
|
|
|
|
|
|
|
return nil
|
2022-10-05 15:07:08 +01:00
|
|
|
}
|
|
|
|
|
2021-06-04 14:35:34 +01:00
|
|
|
// logIfUnsupported logs a formatted warning if the error is one of the
|
|
|
|
// unsupported errors and returns nil. If err is nil, logIfUnsupported returns
|
2022-01-26 11:39:34 +00:00
|
|
|
// nil. Otherwise, it returns err.
|
2021-06-04 14:35:34 +01:00
|
|
|
func logIfUnsupported(msg string, err error) (outErr error) {
|
2021-12-16 17:54:59 +00:00
|
|
|
if errors.As(err, new(*aghos.UnsupportedError)) {
|
2021-06-04 14:35:34 +01:00
|
|
|
log.Debug(msg, err)
|
2021-12-16 17:54:59 +00:00
|
|
|
|
|
|
|
return nil
|
2021-06-04 14:35:34 +01:00
|
|
|
}
|
|
|
|
|
2021-12-16 17:54:59 +00:00
|
|
|
return err
|
2021-06-04 14:35:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// configureOS sets the OS-related configuration.
|
|
|
|
func configureOS(conf *configuration) (err error) {
|
|
|
|
osConf := conf.OSConfig
|
|
|
|
if osConf == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if osConf.Group != "" {
|
|
|
|
err = aghos.SetGroup(osConf.Group)
|
|
|
|
err = logIfUnsupported("warning: setting group", err)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("setting group: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("group set to %s", osConf.Group)
|
|
|
|
}
|
|
|
|
|
|
|
|
if osConf.User != "" {
|
|
|
|
err = aghos.SetUser(osConf.User)
|
|
|
|
err = logIfUnsupported("warning: setting user", err)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("setting user: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("user set to %s", osConf.User)
|
|
|
|
}
|
|
|
|
|
|
|
|
if osConf.RlimitNoFile != 0 {
|
|
|
|
err = aghos.SetRlimit(osConf.RlimitNoFile)
|
|
|
|
err = logIfUnsupported("warning: setting rlimit", err)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("setting rlimit: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("rlimit_nofile set to %d", osConf.RlimitNoFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-17 14:21:10 +00:00
|
|
|
// setupHostsContainer initializes the structures to keep up-to-date the hosts
|
|
|
|
// provided by the OS.
|
|
|
|
func setupHostsContainer() (err error) {
|
2023-03-28 11:27:46 +01:00
|
|
|
hostsWatcher, err := aghos.NewOSWritesWatcher()
|
2021-11-17 14:21:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("initing hosts watcher: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-12-07 13:48:55 +00:00
|
|
|
paths, err := hostsfile.DefaultHostsPaths()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting default system hosts paths: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
Context.etcHosts, err = aghnet.NewHostsContainer(osutil.RootDirFS(), hostsWatcher, paths...)
|
2021-11-17 14:21:10 +00:00
|
|
|
if err != nil {
|
2023-03-28 11:27:46 +01:00
|
|
|
closeErr := hostsWatcher.Close()
|
2023-11-16 11:05:10 +00:00
|
|
|
if errors.Is(err, aghnet.ErrNoHostsPaths) {
|
2021-11-17 14:21:10 +00:00
|
|
|
log.Info("warning: initing hosts container: %s", err)
|
|
|
|
|
2023-11-16 11:05:10 +00:00
|
|
|
return closeErr
|
2021-11-17 14:21:10 +00:00
|
|
|
}
|
|
|
|
|
2023-11-16 11:05:10 +00:00
|
|
|
return errors.Join(fmt.Errorf("initializing hosts container: %w", err), closeErr)
|
2021-11-17 14:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
// setupOpts sets up command-line options.
|
|
|
|
func setupOpts(opts options) (err error) {
|
|
|
|
err = setupBindOpts(opts)
|
2023-04-27 14:39:35 +01:00
|
|
|
if err != nil {
|
2023-05-10 14:30:03 +01:00
|
|
|
// Don't wrap the error, because it's informative enough as is.
|
|
|
|
return err
|
2023-04-27 14:39:35 +01:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
if len(opts.pidFile) != 0 && writePIDFile(opts.pidFile) {
|
|
|
|
Context.pidFileName = opts.pidFile
|
2023-04-27 14:39:35 +01:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
return nil
|
|
|
|
}
|
2023-04-27 14:39:35 +01:00
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
// initContextClients initializes Context clients and related fields.
|
|
|
|
func initContextClients() (err error) {
|
2023-08-30 16:26:02 +01:00
|
|
|
err = setupDNSFilteringConf(config.Filtering)
|
2023-03-15 11:31:07 +00:00
|
|
|
if err != nil {
|
2023-05-10 14:30:03 +01:00
|
|
|
// Don't wrap the error, because it's informative enough as is.
|
|
|
|
return err
|
2023-03-15 11:31:07 +00:00
|
|
|
}
|
|
|
|
|
2023-07-19 14:57:57 +01:00
|
|
|
//lint:ignore SA1019 Migration is not over.
|
2020-02-13 15:42:07 +00:00
|
|
|
config.DHCP.WorkDir = Context.workDir
|
2023-04-18 13:12:11 +01:00
|
|
|
config.DHCP.DataDir = Context.getDataDir()
|
2019-10-11 17:56:18 +01:00
|
|
|
config.DHCP.HTTPRegister = httpRegister
|
|
|
|
config.DHCP.ConfigModified = onConfigModified
|
2020-11-16 16:01:12 +00:00
|
|
|
|
2021-06-16 14:48:46 +01:00
|
|
|
Context.dhcpServer, err = dhcpd.Create(config.DHCP)
|
|
|
|
if Context.dhcpServer == nil || err != nil {
|
2021-04-29 14:00:07 +01:00
|
|
|
// TODO(a.garipov): There are a lot of places in the code right
|
|
|
|
// now which assume that the DHCP server can be nil despite this
|
|
|
|
// condition. Inspect them and perhaps rewrite them to use
|
|
|
|
// Enabled() instead.
|
2021-06-03 19:04:13 +01:00
|
|
|
return fmt.Errorf("initing dhcp: %w", err)
|
2020-03-13 14:30:09 +00:00
|
|
|
}
|
2020-11-16 16:01:12 +00:00
|
|
|
|
2023-08-24 11:42:17 +01:00
|
|
|
var arpDB arpdb.Interface
|
2022-04-26 11:04:16 +01:00
|
|
|
if config.Clients.Sources.ARP {
|
2023-08-24 11:42:17 +01:00
|
|
|
arpDB = arpdb.New()
|
2021-04-12 16:31:45 +01:00
|
|
|
}
|
2021-12-13 12:18:21 +00:00
|
|
|
|
2023-11-16 11:05:10 +00:00
|
|
|
return Context.clients.Init(
|
2023-05-10 14:30:03 +01:00
|
|
|
config.Clients.Persistent,
|
|
|
|
Context.dhcpServer,
|
|
|
|
Context.etcHosts,
|
2023-08-24 11:42:17 +01:00
|
|
|
arpDB,
|
2023-08-30 16:26:02 +01:00
|
|
|
config.Filtering,
|
2023-05-10 14:30:03 +01:00
|
|
|
)
|
|
|
|
}
|
2019-09-26 14:40:52 +01:00
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
// setupBindOpts overrides bind host/port from the opts.
|
|
|
|
func setupBindOpts(opts options) (err error) {
|
2023-06-29 13:29:52 +01:00
|
|
|
bindAddr := opts.bindAddr
|
|
|
|
if bindAddr != (netip.AddrPort{}) {
|
|
|
|
config.HTTPConfig.Address = bindAddr
|
2022-08-03 12:36:18 +01:00
|
|
|
|
2023-06-29 13:29:52 +01:00
|
|
|
if config.HTTPConfig.Address.Port() != 0 {
|
|
|
|
err = checkPorts()
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error, because it's informative enough as is.
|
|
|
|
return err
|
|
|
|
}
|
2021-12-16 17:54:59 +00:00
|
|
|
}
|
|
|
|
|
2023-06-29 13:29:52 +01:00
|
|
|
return nil
|
2019-02-04 10:54:53 +00:00
|
|
|
}
|
2023-05-10 14:30:03 +01:00
|
|
|
|
2023-06-29 13:29:52 +01:00
|
|
|
if opts.bindPort != 0 {
|
|
|
|
config.HTTPConfig.Address = netip.AddrPortFrom(
|
|
|
|
config.HTTPConfig.Address.Addr(),
|
2023-10-05 13:26:19 +01:00
|
|
|
opts.bindPort,
|
2023-06-29 13:29:52 +01:00
|
|
|
)
|
2023-06-23 08:03:01 +01:00
|
|
|
|
|
|
|
err = checkPorts()
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error, because it's informative enough as is.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 13:29:52 +01:00
|
|
|
if opts.bindHost.IsValid() {
|
|
|
|
config.HTTPConfig.Address = netip.AddrPortFrom(
|
|
|
|
opts.bindHost,
|
|
|
|
config.HTTPConfig.Address.Port(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// setupDNSFilteringConf sets up DNS filtering configuration settings.
|
|
|
|
func setupDNSFilteringConf(conf *filtering.Config) (err error) {
|
|
|
|
const (
|
|
|
|
dnsTimeout = 3 * time.Second
|
|
|
|
|
|
|
|
sbService = "safe browsing"
|
|
|
|
defaultSafeBrowsingServer = `https://family.adguard-dns.com/dns-query`
|
|
|
|
sbTXTSuffix = `sb.dns.adguard.com.`
|
|
|
|
|
|
|
|
pcService = "parental control"
|
|
|
|
defaultParentalServer = `https://family.adguard-dns.com/dns-query`
|
|
|
|
pcTXTSuffix = `pc.dns.adguard.com.`
|
|
|
|
)
|
|
|
|
|
|
|
|
conf.EtcHosts = Context.etcHosts
|
2024-01-18 13:14:11 +00:00
|
|
|
// TODO(s.chzhen): Use empty interface.
|
|
|
|
if Context.etcHosts == nil {
|
|
|
|
conf.EtcHosts = nil
|
|
|
|
}
|
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
conf.ConfigModified = onConfigModified
|
|
|
|
conf.HTTPRegister = httpRegister
|
|
|
|
conf.DataDir = Context.getDataDir()
|
|
|
|
conf.Filters = slices.Clone(config.Filters)
|
|
|
|
conf.WhitelistFilters = slices.Clone(config.WhitelistFilters)
|
|
|
|
conf.UserRules = slices.Clone(config.UserRules)
|
2023-07-18 15:02:07 +01:00
|
|
|
conf.HTTPClient = httpClient()
|
2023-05-10 14:30:03 +01:00
|
|
|
|
|
|
|
cacheTime := time.Duration(conf.CacheTime) * time.Minute
|
|
|
|
|
|
|
|
upsOpts := &upstream.Options{
|
|
|
|
Timeout: dnsTimeout,
|
2023-11-16 11:05:10 +00:00
|
|
|
Bootstrap: upstream.StaticResolver{
|
|
|
|
// 94.140.14.15.
|
|
|
|
netip.AddrFrom4([4]byte{94, 140, 14, 15}),
|
|
|
|
// 94.140.14.16.
|
|
|
|
netip.AddrFrom4([4]byte{94, 140, 14, 16}),
|
|
|
|
// 2a10:50c0::bad1:ff.
|
|
|
|
netip.AddrFrom16([16]byte{42, 16, 80, 192, 12: 186, 209, 0, 255}),
|
|
|
|
// 2a10:50c0::bad2:ff.
|
|
|
|
netip.AddrFrom16([16]byte{42, 16, 80, 192, 12: 186, 210, 0, 255}),
|
2023-05-10 14:30:03 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
sbUps, err := upstream.AddressToUpstream(defaultSafeBrowsingServer, upsOpts)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("converting safe browsing server: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.SafeBrowsingChecker = hashprefix.New(&hashprefix.Config{
|
|
|
|
Upstream: sbUps,
|
|
|
|
ServiceName: sbService,
|
|
|
|
TXTSuffix: sbTXTSuffix,
|
|
|
|
CacheTime: cacheTime,
|
|
|
|
CacheSize: conf.SafeBrowsingCacheSize,
|
|
|
|
})
|
|
|
|
|
2023-09-08 13:04:25 +01:00
|
|
|
// Protect against invalid configuration, see #6181.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Validate against an empty host instead of setting it to
|
|
|
|
// default.
|
|
|
|
if conf.SafeBrowsingBlockHost == "" {
|
|
|
|
host := defaultSafeBrowsingBlockHost
|
|
|
|
log.Info("%s: warning: empty blocking host; using default: %q", sbService, host)
|
|
|
|
|
|
|
|
conf.SafeBrowsingBlockHost = host
|
2023-08-30 16:26:02 +01:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
parUps, err := upstream.AddressToUpstream(defaultParentalServer, upsOpts)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("converting parental server: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.ParentalControlChecker = hashprefix.New(&hashprefix.Config{
|
|
|
|
Upstream: parUps,
|
|
|
|
ServiceName: pcService,
|
|
|
|
TXTSuffix: pcTXTSuffix,
|
|
|
|
CacheTime: cacheTime,
|
2023-07-10 18:00:29 +01:00
|
|
|
CacheSize: conf.ParentalCacheSize,
|
2023-05-10 14:30:03 +01:00
|
|
|
})
|
|
|
|
|
2023-09-08 13:04:25 +01:00
|
|
|
// Protect against invalid configuration, see #6181.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Validate against an empty host instead of setting it to
|
|
|
|
// default.
|
|
|
|
if conf.ParentalBlockHost == "" {
|
|
|
|
host := defaultParentalBlockHost
|
|
|
|
log.Info("%s: warning: empty blocking host; using default: %q", pcService, host)
|
|
|
|
|
|
|
|
conf.ParentalBlockHost = host
|
2023-08-30 16:26:02 +01:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
conf.SafeSearchConf.CustomResolver = safeSearchResolver{}
|
|
|
|
conf.SafeSearch, err = safesearch.NewDefault(
|
|
|
|
conf.SafeSearchConf,
|
|
|
|
"default",
|
|
|
|
conf.SafeSearchCacheSize,
|
|
|
|
cacheTime,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("initializing safesearch: %w", err)
|
2020-02-18 16:27:09 +00:00
|
|
|
}
|
2021-06-03 19:04:13 +01:00
|
|
|
|
|
|
|
return nil
|
2020-11-20 14:32:41 +00:00
|
|
|
}
|
|
|
|
|
2023-03-15 11:31:07 +00:00
|
|
|
// checkPorts is a helper for ports validation in config.
|
|
|
|
func checkPorts() (err error) {
|
|
|
|
tcpPorts := aghalg.UniqChecker[tcpPort]{}
|
2023-06-29 13:29:52 +01:00
|
|
|
addPorts(tcpPorts, tcpPort(config.HTTPConfig.Address.Port()))
|
2023-03-15 11:31:07 +00:00
|
|
|
|
|
|
|
udpPorts := aghalg.UniqChecker[udpPort]{}
|
|
|
|
addPorts(udpPorts, udpPort(config.DNS.Port))
|
|
|
|
|
|
|
|
if config.TLS.Enabled {
|
|
|
|
addPorts(
|
|
|
|
tcpPorts,
|
|
|
|
tcpPort(config.TLS.PortHTTPS),
|
|
|
|
tcpPort(config.TLS.PortDNSOverTLS),
|
|
|
|
tcpPort(config.TLS.PortDNSCrypt),
|
|
|
|
)
|
|
|
|
|
|
|
|
addPorts(udpPorts, udpPort(config.TLS.PortDNSOverQUIC))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = tcpPorts.Validate(); err != nil {
|
|
|
|
return fmt.Errorf("validating tcp ports: %w", err)
|
|
|
|
} else if err = udpPorts.Validate(); err != nil {
|
|
|
|
return fmt.Errorf("validating udp ports: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-19 14:57:57 +01:00
|
|
|
func initWeb(opts options, clientBuildFS fs.FS, upd *updater.Updater) (web *webAPI, err error) {
|
2023-01-23 09:47:59 +00:00
|
|
|
var clientFS fs.FS
|
2022-10-05 15:07:08 +01:00
|
|
|
if opts.localFrontend {
|
2021-05-21 12:55:42 +01:00
|
|
|
log.Info("warning: using local frontend files")
|
|
|
|
|
|
|
|
clientFS = os.DirFS("build/static")
|
|
|
|
} else {
|
|
|
|
clientFS, err = fs.Sub(clientBuildFS, "build/static")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting embedded client subdir: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-19 14:57:57 +01:00
|
|
|
disableUpdate := opts.disableUpdate || version.Channel() == version.ChannelDevelopment
|
|
|
|
if disableUpdate {
|
|
|
|
log.Info("AdGuard Home updates are disabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
webConf := &webConfig{
|
|
|
|
updater: upd,
|
|
|
|
|
|
|
|
clientFS: clientFS,
|
|
|
|
|
2023-10-05 13:26:19 +01:00
|
|
|
BindAddr: config.HTTPConfig.Address,
|
2021-05-21 12:55:42 +01:00
|
|
|
|
|
|
|
ReadTimeout: readTimeout,
|
|
|
|
ReadHeaderTimeout: readHdrTimeout,
|
|
|
|
WriteTimeout: writeTimeout,
|
|
|
|
|
2023-07-19 14:57:57 +01:00
|
|
|
firstRun: Context.firstRun,
|
|
|
|
disableUpdate: disableUpdate,
|
|
|
|
runningAsService: opts.runningAsService,
|
|
|
|
serveHTTP3: config.DNS.ServeHTTP3,
|
2021-05-21 12:55:42 +01:00
|
|
|
}
|
|
|
|
|
2023-07-19 14:57:57 +01:00
|
|
|
web = newWebAPI(webConf)
|
2021-05-21 12:55:42 +01:00
|
|
|
if web == nil {
|
|
|
|
return nil, fmt.Errorf("initializing web: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return web, nil
|
|
|
|
}
|
|
|
|
|
2021-06-03 19:04:13 +01:00
|
|
|
func fatalOnError(err error) {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 14:00:52 +00:00
|
|
|
// run configures and starts AdGuard Home.
|
2023-08-28 14:40:46 +01:00
|
|
|
func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
|
2023-05-10 14:30:03 +01:00
|
|
|
// Configure config filename.
|
2022-10-05 15:07:08 +01:00
|
|
|
initConfigFilename(opts)
|
2020-11-20 14:32:41 +00:00
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
// Configure working dir and config path.
|
|
|
|
err := initWorkingDir(opts)
|
|
|
|
fatalOnError(err)
|
2020-11-20 14:32:41 +00:00
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
// Configure log level and output.
|
|
|
|
err = configureLogger(opts)
|
|
|
|
fatalOnError(err)
|
2020-11-20 14:32:41 +00:00
|
|
|
|
2022-01-11 18:33:14 +00:00
|
|
|
// Print the first message after logger is configured.
|
2022-09-29 17:04:26 +01:00
|
|
|
log.Info(version.Full())
|
2022-01-11 18:33:14 +00:00
|
|
|
log.Debug("current working directory is %s", Context.workDir)
|
2022-10-05 15:07:08 +01:00
|
|
|
if opts.runningAsService {
|
2020-11-20 14:32:41 +00:00
|
|
|
log.Info("AdGuard Home is running as a service")
|
|
|
|
}
|
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
err = setupContext(opts)
|
|
|
|
fatalOnError(err)
|
2020-11-20 14:32:41 +00:00
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
err = configureOS(config)
|
2021-06-04 14:35:34 +01:00
|
|
|
fatalOnError(err)
|
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
// Clients package uses filtering package's static data
|
|
|
|
// (filtering.BlockedSvcKnown()), so we have to initialize filtering static
|
|
|
|
// data first, but also to avoid relying on automatic Go init() function.
|
2021-05-21 14:15:47 +01:00
|
|
|
filtering.InitModule()
|
2020-11-20 14:32:41 +00:00
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
err = initContextClients()
|
|
|
|
fatalOnError(err)
|
|
|
|
|
|
|
|
err = setupOpts(opts)
|
2021-06-03 19:04:13 +01:00
|
|
|
fatalOnError(err)
|
2018-08-30 15:25:33 +01:00
|
|
|
|
2023-09-21 15:07:57 +01:00
|
|
|
execPath, err := os.Executable()
|
|
|
|
fatalOnError(errors.Annotate(err, "getting executable path: %w"))
|
|
|
|
|
|
|
|
u := &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
// TODO(a.garipov): Make configurable.
|
|
|
|
Host: "static.adtidy.org",
|
|
|
|
Path: path.Join("adguardhome", version.Channel(), "version.json"),
|
|
|
|
}
|
|
|
|
|
2023-07-19 14:57:57 +01:00
|
|
|
upd := updater.NewUpdater(&updater.Config{
|
2023-09-21 15:07:57 +01:00
|
|
|
Client: config.Filtering.HTTPClient,
|
|
|
|
Version: version.Version(),
|
|
|
|
Channel: version.Channel(),
|
|
|
|
GOARCH: runtime.GOARCH,
|
|
|
|
GOOS: runtime.GOOS,
|
|
|
|
GOARM: version.GOARM(),
|
|
|
|
GOMIPS: version.GOMIPS(),
|
|
|
|
WorkDir: Context.workDir,
|
|
|
|
ConfName: config.getConfigFilename(),
|
|
|
|
ExecPath: execPath,
|
|
|
|
VersionCheckURL: u.String(),
|
2023-07-19 14:57:57 +01:00
|
|
|
})
|
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
// TODO(e.burkov): This could be made earlier, probably as the option's
|
2023-01-09 10:38:31 +00:00
|
|
|
// effect.
|
2023-07-19 14:57:57 +01:00
|
|
|
cmdlineUpdate(opts, upd)
|
2023-01-09 10:38:31 +00:00
|
|
|
|
2020-02-13 15:42:07 +00:00
|
|
|
if !Context.firstRun {
|
2023-05-10 14:30:03 +01:00
|
|
|
// Save the updated config.
|
2021-06-03 19:04:13 +01:00
|
|
|
err = config.write()
|
|
|
|
fatalOnError(err)
|
2020-04-22 14:00:26 +01:00
|
|
|
|
2023-08-02 15:39:33 +01:00
|
|
|
if config.HTTPConfig.Pprof.Enabled {
|
|
|
|
startPprof(config.HTTPConfig.Pprof.Port)
|
2020-04-22 14:00:26 +01:00
|
|
|
}
|
2020-02-18 16:27:09 +00:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
dir := Context.getDataDir()
|
|
|
|
err = os.MkdirAll(dir, 0o755)
|
|
|
|
fatalOnError(errors.Annotate(err, "creating DNS data dir at %s: %w", dir))
|
2018-08-30 15:25:33 +01:00
|
|
|
|
2022-10-05 15:07:08 +01:00
|
|
|
GLMode = opts.glinetMode
|
2021-05-21 12:55:42 +01:00
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
// Init auth module.
|
|
|
|
Context.auth, err = initUsers()
|
|
|
|
fatalOnError(err)
|
2020-02-19 12:24:55 +00:00
|
|
|
|
2023-12-12 10:16:01 +00:00
|
|
|
Context.tls, err = newTLSManager(config.TLS, config.DNS.ServePlainDNS)
|
2022-10-14 17:37:14 +01:00
|
|
|
if err != nil {
|
2022-11-25 12:41:54 +00:00
|
|
|
log.Error("initializing tls: %s", err)
|
|
|
|
onConfigModified()
|
2020-02-19 12:28:06 +00:00
|
|
|
}
|
|
|
|
|
2023-07-19 14:57:57 +01:00
|
|
|
Context.web, err = initWeb(opts, clientBuildFS, upd)
|
2021-06-03 19:04:13 +01:00
|
|
|
fatalOnError(err)
|
2020-02-18 16:27:09 +00:00
|
|
|
|
|
|
|
if !Context.firstRun {
|
2023-01-09 10:38:31 +00:00
|
|
|
err = initDNS()
|
2021-06-03 19:04:13 +01:00
|
|
|
fatalOnError(err)
|
2021-03-12 11:32:08 +00:00
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
Context.tls.start()
|
2020-02-19 12:28:06 +00:00
|
|
|
|
2019-10-09 17:51:26 +01:00
|
|
|
go func() {
|
2023-07-18 15:02:07 +01:00
|
|
|
startErr := startDNSServer()
|
|
|
|
if startErr != nil {
|
2021-02-16 15:46:49 +00:00
|
|
|
closeDNSServer()
|
2023-07-18 15:02:07 +01:00
|
|
|
fatalOnError(startErr)
|
2019-10-09 17:51:26 +01:00
|
|
|
}
|
|
|
|
}()
|
2018-09-06 00:00:57 +01:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
if Context.dhcpServer != nil {
|
2021-04-21 12:42:19 +01:00
|
|
|
err = Context.dhcpServer.Start()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("starting dhcp server: %s", err)
|
|
|
|
}
|
2019-02-01 16:25:04 +00:00
|
|
|
}
|
2018-12-28 18:01:16 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 15:22:51 +01:00
|
|
|
Context.web.start()
|
2020-02-18 16:27:09 +00:00
|
|
|
|
2023-08-28 14:40:46 +01:00
|
|
|
// Wait for other goroutines to complete their job.
|
|
|
|
<-done
|
2020-02-18 16:27:09 +00:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
// initUsers initializes context auth module. Clears config users field.
|
|
|
|
func initUsers() (auth *Auth, err error) {
|
|
|
|
sessFilename := filepath.Join(Context.getDataDir(), "sessions.db")
|
|
|
|
|
|
|
|
var rateLimiter *authRateLimiter
|
|
|
|
if config.AuthAttempts > 0 && config.AuthBlockMin > 0 {
|
|
|
|
blockDur := time.Duration(config.AuthBlockMin) * time.Minute
|
|
|
|
rateLimiter = newAuthRateLimiter(blockDur, config.AuthAttempts)
|
|
|
|
} else {
|
|
|
|
log.Info("authratelimiter is disabled")
|
|
|
|
}
|
|
|
|
|
2023-06-29 13:29:52 +01:00
|
|
|
sessionTTL := config.HTTPConfig.SessionTTL.Seconds()
|
|
|
|
auth = InitAuth(sessFilename, config.Users, uint32(sessionTTL), rateLimiter)
|
2023-05-10 14:30:03 +01:00
|
|
|
if auth == nil {
|
|
|
|
return nil, errors.Error("initializing auth module failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Users = nil
|
|
|
|
|
|
|
|
return auth, nil
|
|
|
|
}
|
|
|
|
|
2023-01-09 10:38:31 +00:00
|
|
|
func (c *configuration) anonymizer() (ipmut *aghnet.IPMut) {
|
|
|
|
var anonFunc aghnet.IPMutFunc
|
|
|
|
if c.DNS.AnonymizeClientIP {
|
|
|
|
anonFunc = querylog.AnonymizeIP
|
|
|
|
}
|
|
|
|
|
|
|
|
return aghnet.NewIPMut(anonFunc)
|
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
// startMods initializes and starts the DNS server after installation.
|
2023-01-09 10:38:31 +00:00
|
|
|
func startMods() (err error) {
|
|
|
|
err = initDNS()
|
2020-02-19 12:28:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-14 17:37:14 +01:00
|
|
|
Context.tls.start()
|
2020-02-19 12:28:06 +00:00
|
|
|
|
|
|
|
err = startDNSServer()
|
|
|
|
if err != nil {
|
|
|
|
closeDNSServer()
|
2022-10-14 17:37:14 +01:00
|
|
|
|
2020-02-19 12:28:06 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-10-14 17:37:14 +01:00
|
|
|
|
2020-02-19 12:28:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-23 16:04:26 +01:00
|
|
|
// Check if the current user permissions are enough to run AdGuard Home
|
2020-06-23 16:02:28 +01:00
|
|
|
func checkPermissions() {
|
|
|
|
log.Info("Checking if AdGuard Home has necessary permissions")
|
2019-04-01 10:22:54 +01:00
|
|
|
|
2022-03-31 17:56:50 +01:00
|
|
|
if ok, err := aghnet.CanBindPrivilegedPorts(); !ok || err != nil {
|
2019-04-01 10:22:54 +01:00
|
|
|
log.Fatal("This is the first launch of AdGuard Home. You must run it as Administrator.")
|
2020-06-23 16:02:28 +01:00
|
|
|
}
|
2019-04-01 10:22:54 +01:00
|
|
|
|
2020-06-23 16:02:28 +01:00
|
|
|
// We should check if AdGuard Home is able to bind to port 53
|
2022-11-09 11:37:07 +00:00
|
|
|
err := aghnet.CheckPort("tcp", netip.AddrPortFrom(netutil.IPv4Localhost(), defaultPortDNS))
|
2022-03-31 17:56:50 +01:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, os.ErrPermission) {
|
|
|
|
log.Fatal(`Permission check failed.
|
2020-06-23 16:02:28 +01:00
|
|
|
|
|
|
|
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:
|
2022-03-31 17:56:50 +01:00
|
|
|
https://github.com/AdguardTeam/AdGuardHome/wiki/Getting-Started#running-without-superuser`)
|
|
|
|
}
|
2020-06-23 16:02:28 +01:00
|
|
|
|
2022-03-31 17:56:50 +01:00
|
|
|
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,
|
|
|
|
)
|
2019-04-01 10:22:54 +01:00
|
|
|
}
|
2020-06-23 16:02:28 +01:00
|
|
|
|
2022-03-31 17:56:50 +01:00
|
|
|
log.Info("AdGuard Home can bind to port 53")
|
2019-04-01 10:22:54 +01:00
|
|
|
}
|
|
|
|
|
2019-04-05 10:19:28 +01:00
|
|
|
// Write PID to a file
|
|
|
|
func writePIDFile(fn string) bool {
|
|
|
|
data := fmt.Sprintf("%d", os.Getpid())
|
2021-05-21 12:55:42 +01:00
|
|
|
err := os.WriteFile(fn, []byte(data), 0o644)
|
2019-04-05 10:19:28 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Couldn't write PID to file %s: %v", fn, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
// initConfigFilename sets up context config file path. This file path can be
|
|
|
|
// overridden by command-line arguments, or is set to default.
|
2022-10-05 15:07:08 +01:00
|
|
|
func initConfigFilename(opts options) {
|
2023-05-10 14:30:03 +01:00
|
|
|
Context.configFilename = stringutil.Coalesce(opts.confFilename, "AdGuardHome.yaml")
|
2020-09-28 08:57:56 +01:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:30:03 +01:00
|
|
|
// initWorkingDir initializes the workDir. If no command-line arguments are
|
|
|
|
// specified, the directory with the binary file is used.
|
|
|
|
func initWorkingDir(opts options) (err error) {
|
2020-02-11 09:59:21 +00:00
|
|
|
execPath, err := os.Executable()
|
2019-02-05 11:09:05 +00:00
|
|
|
if err != nil {
|
2023-05-10 14:30:03 +01:00
|
|
|
// Don't wrap the error, because it's informative enough as is.
|
|
|
|
return err
|
2019-02-05 11:09:05 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 15:07:08 +01:00
|
|
|
if opts.workDir != "" {
|
2019-02-05 17:35:48 +00:00
|
|
|
// If there is a custom config file, use it's directory as our working dir
|
2022-10-05 15:07:08 +01:00
|
|
|
Context.workDir = opts.workDir
|
2019-02-05 11:09:05 +00:00
|
|
|
} else {
|
2020-02-13 15:42:07 +00:00
|
|
|
Context.workDir = filepath.Dir(execPath)
|
2019-02-05 11:09:05 +00:00
|
|
|
}
|
2021-01-27 15:32:13 +00:00
|
|
|
|
2021-03-12 11:32:08 +00:00
|
|
|
workDir, err := filepath.EvalSymlinks(Context.workDir)
|
|
|
|
if err != nil {
|
2023-05-10 14:30:03 +01:00
|
|
|
// Don't wrap the error, because it's informative enough as is.
|
|
|
|
return err
|
2021-01-27 15:32:13 +00:00
|
|
|
}
|
2021-03-12 11:32:08 +00:00
|
|
|
|
|
|
|
Context.workDir = workDir
|
2023-05-10 14:30:03 +01:00
|
|
|
|
|
|
|
return nil
|
2019-02-04 10:54:53 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:44:19 +00:00
|
|
|
// cleanup stops and resets all the modules.
|
|
|
|
func cleanup(ctx context.Context) {
|
2021-06-16 14:48:46 +01:00
|
|
|
log.Info("stopping AdGuard Home")
|
2019-02-04 10:54:53 +00:00
|
|
|
|
2020-02-19 12:24:55 +00:00
|
|
|
if Context.web != nil {
|
2023-04-11 15:22:51 +01:00
|
|
|
Context.web.close(ctx)
|
2020-02-19 12:24:55 +00:00
|
|
|
Context.web = nil
|
|
|
|
}
|
|
|
|
if Context.auth != nil {
|
|
|
|
Context.auth.Close()
|
|
|
|
Context.auth = nil
|
|
|
|
}
|
2020-02-18 16:27:09 +00:00
|
|
|
|
2018-12-05 12:36:18 +00:00
|
|
|
err := stopDNSServer()
|
|
|
|
if err != nil {
|
2021-06-16 14:48:46 +01:00
|
|
|
log.Error("stopping dns server: %s", err)
|
2018-12-05 12:36:18 +00:00
|
|
|
}
|
2020-07-03 16:20:01 +01:00
|
|
|
|
|
|
|
if Context.dhcpServer != nil {
|
2021-06-16 14:48:46 +01:00
|
|
|
err = Context.dhcpServer.Stop()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("stopping dhcp server: %s", err)
|
|
|
|
}
|
2019-02-04 10:54:53 +00:00
|
|
|
}
|
2020-02-19 12:28:06 +00:00
|
|
|
|
2021-10-14 17:39:21 +01:00
|
|
|
if Context.etcHosts != nil {
|
|
|
|
if err = Context.etcHosts.Close(); err != nil {
|
2021-11-17 14:21:10 +00:00
|
|
|
log.Error("closing hosts container: %s", err)
|
2021-10-14 17:39:21 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-20 12:05:43 +00:00
|
|
|
|
2020-02-19 12:28:06 +00:00
|
|
|
if Context.tls != nil {
|
|
|
|
Context.tls = nil
|
|
|
|
}
|
2018-12-05 12:36:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 10:19:28 +01:00
|
|
|
// This function is called before application exits
|
|
|
|
func cleanupAlways() {
|
2020-02-13 15:42:07 +00:00
|
|
|
if len(Context.pidFileName) != 0 {
|
|
|
|
_ = os.Remove(Context.pidFileName)
|
2019-04-05 10:19:28 +01:00
|
|
|
}
|
2022-10-14 17:37:14 +01:00
|
|
|
|
|
|
|
log.Info("stopped")
|
2019-04-05 10:19:28 +01:00
|
|
|
}
|
|
|
|
|
2020-09-07 09:10:56 +01:00
|
|
|
func exitWithError() {
|
|
|
|
os.Exit(64)
|
2019-02-04 10:54:53 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 15:07:08 +01:00
|
|
|
// loadCmdLineOpts reads command line arguments and initializes configuration
|
|
|
|
// from them. If there is an error or an effect, loadCmdLineOpts processes them
|
|
|
|
// and exits.
|
|
|
|
func loadCmdLineOpts() (opts options) {
|
|
|
|
opts, eff, err := parseCmdOpts(os.Args[0], os.Args[1:])
|
2020-09-07 09:10:56 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
2022-10-05 15:07:08 +01:00
|
|
|
printHelp(os.Args[0])
|
|
|
|
|
2020-09-07 09:10:56 +01:00
|
|
|
exitWithError()
|
2022-10-05 15:07:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if eff != nil {
|
|
|
|
err = eff()
|
2020-09-07 09:10:56 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
exitWithError()
|
2019-01-24 17:11:01 +00:00
|
|
|
}
|
2022-10-05 15:07:08 +01:00
|
|
|
|
|
|
|
os.Exit(0)
|
2019-01-24 17:11:01 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 15:07:08 +01:00
|
|
|
return opts
|
2019-01-24 17:11:01 +00:00
|
|
|
}
|
2019-02-22 14:59:42 +00:00
|
|
|
|
2021-06-01 19:06:55 +01:00
|
|
|
// printWebAddrs prints addresses built from proto, addr, and an appropriate
|
2023-01-23 09:47:59 +00:00
|
|
|
// port. At least one address is printed with the value of port. Output
|
|
|
|
// example:
|
2021-06-01 19:06:55 +01:00
|
|
|
//
|
2023-01-23 09:47:59 +00:00
|
|
|
// go to http://127.0.0.1:80
|
2023-10-05 13:26:19 +01:00
|
|
|
func printWebAddrs(proto, addr string, port uint16) {
|
2023-01-23 09:47:59 +00:00
|
|
|
log.Printf("go to %s://%s", proto, netutil.JoinHostPort(addr, port))
|
2021-06-01 19:06:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// printHTTPAddresses prints the IP addresses which user can use to access the
|
2021-03-15 11:19:04 +00:00
|
|
|
// admin interface. proto is either schemeHTTP or schemeHTTPS.
|
2019-02-22 14:59:42 +00:00
|
|
|
func printHTTPAddresses(proto string) {
|
2020-02-19 12:28:06 +00:00
|
|
|
tlsConf := tlsConfigSettings{}
|
2020-04-07 17:24:29 +01:00
|
|
|
if Context.tls != nil {
|
|
|
|
Context.tls.WriteDiskConfig(&tlsConf)
|
|
|
|
}
|
2020-05-16 00:02:50 +01:00
|
|
|
|
2023-10-05 13:26:19 +01:00
|
|
|
port := config.HTTPConfig.Address.Port()
|
2022-09-23 11:23:35 +01:00
|
|
|
if proto == aghhttp.SchemeHTTPS {
|
2021-06-01 19:06:55 +01:00
|
|
|
port = tlsConf.PortHTTPS
|
2020-05-16 00:02:50 +01:00
|
|
|
}
|
|
|
|
|
2021-12-16 17:54:59 +00:00
|
|
|
// TODO(e.burkov): Inspect and perhaps merge with the previous condition.
|
2022-09-23 11:23:35 +01:00
|
|
|
if proto == aghhttp.SchemeHTTPS && tlsConf.ServerName != "" {
|
2023-01-23 09:47:59 +00:00
|
|
|
printWebAddrs(proto, tlsConf.ServerName, tlsConf.PortHTTPS)
|
2019-02-22 14:59:42 +00:00
|
|
|
|
2021-06-01 19:06:55 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-29 13:29:52 +01:00
|
|
|
bindHost := config.HTTPConfig.Address.Addr()
|
|
|
|
if !bindHost.IsUnspecified() {
|
|
|
|
printWebAddrs(proto, bindHost.String(), port)
|
2021-06-01 19:06:55 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ifaces, err := aghnet.GetValidNetInterfacesForWeb()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("web: getting iface ips: %s", err)
|
|
|
|
// That's weird, but we'll ignore it.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Find out when it happens.
|
2023-06-29 13:29:52 +01:00
|
|
|
printWebAddrs(proto, bindHost.String(), port)
|
2021-06-01 19:06:55 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, iface := range ifaces {
|
|
|
|
for _, addr := range iface.Addresses {
|
2023-06-29 13:29:52 +01:00
|
|
|
printWebAddrs(proto, addr.String(), port)
|
2020-12-29 16:53:56 +00:00
|
|
|
}
|
2019-02-22 14:59:42 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-13 15:42:07 +00:00
|
|
|
|
|
|
|
// -------------------
|
|
|
|
// first run / install
|
|
|
|
// -------------------
|
|
|
|
func detectFirstRun() bool {
|
|
|
|
configfile := Context.configFilename
|
|
|
|
if !filepath.IsAbs(configfile) {
|
|
|
|
configfile = filepath.Join(Context.workDir, Context.configFilename)
|
|
|
|
}
|
|
|
|
_, err := os.Stat(configfile)
|
2021-01-27 15:32:13 +00:00
|
|
|
return errors.Is(err, os.ErrNotExist)
|
2020-02-13 15:42:07 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 15:09:41 +00:00
|
|
|
// jsonError is a generic JSON error response.
|
|
|
|
//
|
2023-01-09 10:38:31 +00:00
|
|
|
// TODO(a.garipov): Merge together with the implementations in [dhcpd] and other
|
|
|
|
// packages after refactoring the web handler registering.
|
2020-11-25 15:09:41 +00:00
|
|
|
type jsonError struct {
|
|
|
|
// Message is the error message, an opaque string.
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
2022-11-15 14:44:50 +00:00
|
|
|
|
|
|
|
// cmdlineUpdate updates current application and exits.
|
2023-07-19 14:57:57 +01:00
|
|
|
func cmdlineUpdate(opts options, upd *updater.Updater) {
|
2022-11-15 14:44:50 +00:00
|
|
|
if !opts.performUpdate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-09 10:38:31 +00:00
|
|
|
// Initialize the DNS server to use the internal resolver which the updater
|
|
|
|
// needs to be able to resolve the update source hostname.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): We could probably initialize the internal resolver
|
|
|
|
// separately.
|
|
|
|
err := initDNSServer(nil, nil, nil, nil, nil, nil, &tlsConfigSettings{})
|
|
|
|
fatalOnError(err)
|
2022-11-15 14:44:50 +00:00
|
|
|
|
2023-01-09 10:38:31 +00:00
|
|
|
log.Info("cmdline update: performing update")
|
2022-11-15 14:44:50 +00:00
|
|
|
|
2023-07-19 14:57:57 +01:00
|
|
|
info, err := upd.VersionInfo(true)
|
2022-11-15 14:44:50 +00:00
|
|
|
if err != nil {
|
2023-07-19 14:57:57 +01:00
|
|
|
vcu := upd.VersionCheckURL()
|
2022-11-15 14:44:50 +00:00
|
|
|
log.Error("getting version info from %s: %s", vcu, err)
|
|
|
|
|
2023-01-09 10:38:31 +00:00
|
|
|
os.Exit(1)
|
2022-11-15 14:44:50 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 10:38:31 +00:00
|
|
|
if info.NewVersion == version.Version() {
|
2022-11-15 14:44:50 +00:00
|
|
|
log.Info("no updates available")
|
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2023-07-19 14:57:57 +01:00
|
|
|
err = upd.Update(Context.firstRun)
|
2022-11-15 14:44:50 +00:00
|
|
|
fatalOnError(err)
|
|
|
|
|
2023-01-09 10:38:31 +00:00
|
|
|
err = restartService()
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("restarting service: %s", err)
|
|
|
|
log.Info("AdGuard Home was not installed as a service. " +
|
|
|
|
"Please restart running instances of AdGuardHome manually.")
|
|
|
|
}
|
|
|
|
|
2022-11-15 14:44:50 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|