home: imp code

This commit is contained in:
Dimitry Kolyshev 2024-07-09 13:29:19 +03:00
parent 4db7cdc0c4
commit 58fe497eec
4 changed files with 39 additions and 28 deletions

View file

@ -433,7 +433,7 @@ func (web *webAPI) handleInstallConfigure(w http.ResponseWriter, r *http.Request
// moment we'll allow setting up TLS in the initial configuration or the // moment we'll allow setting up TLS in the initial configuration or the
// configuration itself will use HTTPS protocol, because the underlying // configuration itself will use HTTPS protocol, because the underlying
// functions potentially restart the HTTPS server. // functions potentially restart the HTTPS server.
err = startMods() err = startMods(web.logger)
if err != nil { if err != nil {
Context.firstRun = true Context.firstRun = true
copyInstallSettings(config, curConfig) copyInstallSettings(config, curConfig)

View file

@ -2,6 +2,7 @@ package home
import ( import (
"fmt" "fmt"
"log/slog"
"net" "net"
"net/netip" "net/netip"
"net/url" "net/url"
@ -43,8 +44,8 @@ func onConfigModified() {
// initDNS updates all the fields of the [Context] needed to initialize the DNS // initDNS updates all the fields of the [Context] needed to initialize the DNS
// server and initializes it at last. It also must not be called unless // server and initializes it at last. It also must not be called unless
// [config] and [Context] are initialized. // [config] and [Context] are initialized. l must not be nil.
func initDNS() (err error) { func initDNS(l *slog.Logger) (err error) {
anonymizer := config.anonymizer() anonymizer := config.anonymizer()
statsDir, querylogDir, err := checkStatsAndQuerylogDirs(&Context, config) statsDir, querylogDir, err := checkStatsAndQuerylogDirs(&Context, config)
@ -113,13 +114,14 @@ func initDNS() (err error) {
anonymizer, anonymizer,
httpRegister, httpRegister,
tlsConf, tlsConf,
l,
) )
} }
// initDNSServer initializes the [context.dnsServer]. To only use the internal // initDNSServer initializes the [context.dnsServer]. To only use the internal
// proxy, none of the arguments are required, but tlsConf still must not be nil, // proxy, none of the arguments are required, but tlsConf and l still must not
// in other cases all the arguments also must not be nil. It also must not be // be nil, in other cases all the arguments also must not be nil. It also must
// called unless [config] and [Context] are initialized. // not be called unless [config] and [Context] are initialized.
func initDNSServer( func initDNSServer(
filters *filtering.DNSFilter, filters *filtering.DNSFilter,
sts stats.Interface, sts stats.Interface,
@ -128,9 +130,10 @@ func initDNSServer(
anonymizer *aghnet.IPMut, anonymizer *aghnet.IPMut,
httpReg aghhttp.RegisterFunc, httpReg aghhttp.RegisterFunc,
tlsConf *tlsConfigSettings, tlsConf *tlsConfigSettings,
l *slog.Logger,
) (err error) { ) (err error) {
Context.dnsServer, err = dnsforward.NewServer(dnsforward.DNSCreateParams{ Context.dnsServer, err = dnsforward.NewServer(dnsforward.DNSCreateParams{
Logger: Context.logger, Logger: l,
DNSFilter: filters, DNSFilter: filters,
Stats: sts, Stats: sts,
QueryLog: qlog, QueryLog: qlog,

View file

@ -62,9 +62,6 @@ type homeContext struct {
// configuration files, for example /etc/hosts. // configuration files, for example /etc/hosts.
etcHosts *aghnet.HostsContainer etcHosts *aghnet.HostsContainer
// logger is an instance of [*slog.Logger]. It is never nil.
logger *slog.Logger
// mux is our custom http.ServeMux. // mux is our custom http.ServeMux.
mux *http.ServeMux mux *http.ServeMux
@ -140,13 +137,12 @@ func Main(clientBuildFS fs.FS) {
} }
// setupContext initializes [Context] fields. It also reads and upgrades // setupContext initializes [Context] fields. It also reads and upgrades
// config file if necessary. l must not be nil. // config file if necessary.
func setupContext(opts options, l *slog.Logger) (err error) { func setupContext(opts options) (err error) {
Context.firstRun = detectFirstRun() Context.firstRun = detectFirstRun()
Context.tlsRoots = aghtls.SystemRootCAs() Context.tlsRoots = aghtls.SystemRootCAs()
Context.mux = http.NewServeMux() Context.mux = http.NewServeMux()
Context.logger = l
if Context.firstRun { if Context.firstRun {
log.Info("This is the first time AdGuard Home is launched") log.Info("This is the first time AdGuard Home is launched")
@ -487,7 +483,12 @@ func checkPorts() (err error) {
return nil return nil
} }
func initWeb(opts options, clientBuildFS fs.FS, upd *updater.Updater) (web *webAPI, err error) { func initWeb(
opts options,
clientBuildFS fs.FS,
upd *updater.Updater,
l *slog.Logger,
) (web *webAPI, err error) {
var clientFS fs.FS var clientFS fs.FS
if opts.localFrontend { if opts.localFrontend {
log.Info("warning: using local frontend files") log.Info("warning: using local frontend files")
@ -529,7 +530,7 @@ func initWeb(opts options, clientBuildFS fs.FS, upd *updater.Updater) (web *webA
serveHTTP3: config.DNS.ServeHTTP3, serveHTTP3: config.DNS.ServeHTTP3,
} }
web = newWebAPI(webConf) web = newWebAPI(webConf, l)
if web == nil { if web == nil {
return nil, fmt.Errorf("initializing web: %w", err) return nil, fmt.Errorf("initializing web: %w", err)
} }
@ -568,7 +569,7 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
log.Info("AdGuard Home is running as a service") log.Info("AdGuard Home is running as a service")
} }
err = setupContext(opts, slogLogger) err = setupContext(opts)
fatalOnError(err) fatalOnError(err)
err = configureOS(config) err = configureOS(config)
@ -614,7 +615,7 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
// TODO(e.burkov): This could be made earlier, probably as the option's // TODO(e.burkov): This could be made earlier, probably as the option's
// effect. // effect.
cmdlineUpdate(opts, upd) cmdlineUpdate(opts, upd, slogLogger)
if !Context.firstRun { if !Context.firstRun {
// Save the updated config. // Save the updated config.
@ -642,11 +643,11 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
onConfigModified() onConfigModified()
} }
Context.web, err = initWeb(opts, clientBuildFS, upd) Context.web, err = initWeb(opts, clientBuildFS, upd, slogLogger)
fatalOnError(err) fatalOnError(err)
if !Context.firstRun { if !Context.firstRun {
err = initDNS() err = initDNS(slogLogger)
fatalOnError(err) fatalOnError(err)
Context.tls.start() Context.tls.start()
@ -707,9 +708,10 @@ func (c *configuration) anonymizer() (ipmut *aghnet.IPMut) {
return aghnet.NewIPMut(anonFunc) return aghnet.NewIPMut(anonFunc)
} }
// startMods initializes and starts the DNS server after installation. // startMods initializes and starts the DNS server after installation. l must
func startMods() (err error) { // not be nil.
err = initDNS() func startMods(l *slog.Logger) (err error) {
err = initDNS(l)
if err != nil { if err != nil {
return err return err
} }
@ -969,8 +971,8 @@ type jsonError struct {
Message string `json:"message"` Message string `json:"message"`
} }
// cmdlineUpdate updates current application and exits. // cmdlineUpdate updates current application and exits. l must not be nil.
func cmdlineUpdate(opts options, upd *updater.Updater) { func cmdlineUpdate(opts options, upd *updater.Updater, l *slog.Logger) {
if !opts.performUpdate { if !opts.performUpdate {
return return
} }
@ -980,7 +982,7 @@ func cmdlineUpdate(opts options, upd *updater.Updater) {
// //
// TODO(e.burkov): We could probably initialize the internal resolver // TODO(e.burkov): We could probably initialize the internal resolver
// separately. // separately.
err := initDNSServer(nil, nil, nil, nil, nil, nil, &tlsConfigSettings{}) err := initDNSServer(nil, nil, nil, nil, nil, nil, &tlsConfigSettings{}, l)
fatalOnError(err) fatalOnError(err)
log.Info("cmdline update: performing update") log.Info("cmdline update: performing update")

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"crypto/tls" "crypto/tls"
"io/fs" "io/fs"
"log/slog"
"net/http" "net/http"
"net/netip" "net/netip"
"runtime" "runtime"
@ -90,17 +91,22 @@ type webAPI struct {
// TODO(a.garipov): Refactor all these servers. // TODO(a.garipov): Refactor all these servers.
httpServer *http.Server httpServer *http.Server
// logger is a slog logger used in webAPI. It must not be nil.
logger *slog.Logger
// httpsServer is the server that handles HTTPS traffic. If it is not nil, // httpsServer is the server that handles HTTPS traffic. If it is not nil,
// [Web.http3Server] must also not be nil. // [Web.http3Server] must also not be nil.
httpsServer httpsServer httpsServer httpsServer
} }
// newWebAPI creates a new instance of the web UI and API server. // newWebAPI creates a new instance of the web UI and API server. l must not be
func newWebAPI(conf *webConfig) (w *webAPI) { // nil.
func newWebAPI(conf *webConfig, l *slog.Logger) (w *webAPI) {
log.Info("web: initializing") log.Info("web: initializing")
w = &webAPI{ w = &webAPI{
conf: conf, conf: conf,
logger: l,
} }
clientFS := http.FileServer(http.FS(conf.clientFS)) clientFS := http.FileServer(http.FS(conf.clientFS))