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
// configuration itself will use HTTPS protocol, because the underlying
// functions potentially restart the HTTPS server.
err = startMods()
err = startMods(web.logger)
if err != nil {
Context.firstRun = true
copyInstallSettings(config, curConfig)

View File

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

View File

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

View File

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