Skip to content

Commit

Permalink
home: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Jul 9, 2024
1 parent 4db7cdc commit 58fe497
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 28 deletions.
2 changes: 1 addition & 1 deletion internal/home/controlinstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 9 additions & 6 deletions internal/home/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package home

import (
"fmt"
"log/slog"
"net"
"net/netip"
"net/url"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
38 changes: 20 additions & 18 deletions internal/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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")
Expand Down
12 changes: 9 additions & 3 deletions internal/home/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"io/fs"
"log/slog"
"net/http"
"net/netip"
"runtime"
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 58fe497

Please sign in to comment.