Skip to content

Commit d28f33a

Browse files
committed
Pull request 2624: AGDNS-3869-rm-the-old-logger-from-some-entities
Squashed commit of the following: commit dc9d1f9 Author: Maksim Kazantsev <m.kazantsev@adguard.com> Date: Fri Apr 10 12:39:28 2026 +0300 home: upd var name; commit ef34751 Merge: 1a2cfce 397ed06 Author: Ainar Garipov <a.garipov@adguard.com> Date: Fri Apr 10 12:26:58 2026 +0300 Merge branch 'master' into AGDNS-3869-rm-the-old-logger-from-some-entities commit 1a2cfce Author: Maksim Kazantsev <m.kazantsev@adguard.com> Date: Thu Apr 9 16:47:50 2026 +0300 home: imp docs; minor fix; commit f5abd19 Author: Maksim Kazantsev <m.kazantsev@adguard.com> Date: Thu Apr 9 16:17:07 2026 +0300 home: imp docs; commit 6e1cf72 Author: Maksim Kazantsev <m.kazantsev@adguard.com> Date: Thu Apr 9 16:09:30 2026 +0300 home: imp log; commit 98c6fa0 Author: Maksim Kazantsev <m.kazantsev@adguard.com> Date: Wed Apr 8 15:08:43 2026 +0300 ossvc: actialize docs; commit dad5875 Author: Maksim Kazantsev <m.kazantsev@adguard.com> Date: Wed Apr 8 14:52:27 2026 +0300 home: imp code; add docs, contracts; commit 334a75e Author: Maksim Kazantsev <m.kazantsev@adguard.com> Date: Wed Apr 8 13:36:04 2026 +0300 home: rm old logger;
1 parent 397ed06 commit d28f33a

7 files changed

Lines changed: 126 additions & 64 deletions

File tree

internal/home/authhttp.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/AdguardTeam/AdGuardHome/internal/aghuser"
1919
"github.com/AdguardTeam/golibs/errors"
2020
"github.com/AdguardTeam/golibs/httphdr"
21-
"github.com/AdguardTeam/golibs/log"
2221
"github.com/AdguardTeam/golibs/logutil/slogutil"
2322
"github.com/AdguardTeam/golibs/netutil"
2423
"github.com/AdguardTeam/golibs/netutil/httputil"
@@ -82,18 +81,27 @@ func realIP(r *http.Request) (ip netip.Addr, err error) {
8281
}
8382

8483
// writeErrorWithIP is like [aghhttp.Error], but includes the remote IP address
85-
// when it writes to the log.
86-
func writeErrorWithIP(
84+
// when it writes to the log. r, w and err must not be nil.
85+
func (web *webAPI) writeErrorWithIP(
86+
ctx context.Context,
87+
err error,
8788
r *http.Request,
8889
w http.ResponseWriter,
8990
code int,
9091
remoteIP string,
91-
format string,
92-
args ...any,
9392
) {
94-
text := fmt.Sprintf(format, args...)
95-
log.Error("%s %s %s: from ip %s: %s", r.Method, r.Host, r.URL, remoteIP, text)
96-
http.Error(w, text, code)
93+
web.logger.ErrorContext(
94+
ctx,
95+
"http error",
96+
"host", r.Host,
97+
"method", r.Method,
98+
"url", r.URL,
99+
"status", code,
100+
"ip", remoteIP,
101+
slogutil.KeyError, err,
102+
)
103+
104+
http.Error(w, err.Error(), code)
97105
}
98106

99107
// handleLogin is the handler for the POST /control/login HTTP API.
@@ -114,13 +122,13 @@ func (web *webAPI) handleLogin(w http.ResponseWriter, r *http.Request) {
114122
//
115123
// See https://github.com/AdguardTeam/AdGuardHome/issues/2799.
116124
if remoteIPStr, err = netutil.SplitHost(r.RemoteAddr); err != nil {
117-
writeErrorWithIP(
125+
web.writeErrorWithIP(
126+
ctx,
127+
fmt.Errorf("auth: getting remote address: %w", err),
118128
r,
119129
w,
120130
http.StatusBadRequest,
121131
r.RemoteAddr,
122-
"auth: getting remote address: %s",
123-
err,
124132
)
125133

126134
return
@@ -129,13 +137,13 @@ func (web *webAPI) handleLogin(w http.ResponseWriter, r *http.Request) {
129137
if rateLimiter := web.auth.rateLimiter; rateLimiter != nil {
130138
if left := rateLimiter.check(remoteIPStr); left > 0 {
131139
w.Header().Set(httphdr.RetryAfter, strconv.Itoa(int(left.Seconds())))
132-
writeErrorWithIP(
140+
web.writeErrorWithIP(
141+
ctx,
142+
fmt.Errorf("auth: blocked for %s", left),
133143
r,
134144
w,
135145
http.StatusTooManyRequests,
136146
remoteIPStr,
137-
"auth: blocked for %s",
138-
left,
139147
)
140148

141149
return
@@ -154,13 +162,13 @@ func (web *webAPI) handleLogin(w http.ResponseWriter, r *http.Request) {
154162

155163
remoteIP, err := netip.ParseAddr(remoteIPStr)
156164
if err != nil {
157-
writeErrorWithIP(
165+
web.writeErrorWithIP(
166+
ctx,
167+
fmt.Errorf("auth: parsing remote address: %w", err),
158168
r,
159169
w,
160170
http.StatusInternalServerError,
161171
r.RemoteAddr,
162-
"auth: parsing remote address: %s",
163-
err,
164172
)
165173

166174
return
@@ -173,7 +181,7 @@ func (web *webAPI) handleLogin(w http.ResponseWriter, r *http.Request) {
173181

174182
cookie, err := newCookie(ctx, web.auth, req, remoteIPStr)
175183
if err != nil {
176-
writeErrorWithIP(r, w, http.StatusForbidden, logIP, "%s", err)
184+
web.writeErrorWithIP(ctx, err, r, w, http.StatusForbidden, logIP)
177185

178186
return
179187
}

internal/home/control.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,18 @@ func (web *webAPI) registerControlHandlers() {
199199
web.httpReg.Register(http.MethodGet, "/control/profile", web.handleGetProfile)
200200
web.httpReg.Register(http.MethodPut, "/control/profile/update", web.handlePutProfile)
201201

202+
mobileConfHandler := newMobileConfigHandler(&mobileConfigHandlerConfig{
203+
logger: web.baseLogger,
204+
})
205+
202206
// No authentication is required for DoH/DoT configuration endpoints.
203207
mux.Handle(
204208
"/apple/doh.mobileconfig",
205-
web.postInstallHandler(http.HandlerFunc(handleMobileConfigDoH)),
209+
web.postInstallHandler(http.HandlerFunc(mobileConfHandler.handleMobileConfigDoH)),
206210
)
207211
mux.Handle(
208212
"/apple/dot.mobileconfig",
209-
web.postInstallHandler(http.HandlerFunc(handleMobileConfigDoT)),
213+
web.postInstallHandler(http.HandlerFunc(mobileConfHandler.handleMobileConfigDoT)),
210214
)
211215

212216
web.registerAuthHandlers()

internal/home/home.go

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -204,51 +204,52 @@ func setupContext(
204204
// logIfUnsupported logs a formatted warning if the error is one of the
205205
// unsupported errors and returns nil. If err is nil, logIfUnsupported returns
206206
// nil. Otherwise, it returns err.
207-
func logIfUnsupported(msg string, err error) (outErr error) {
207+
func logIfUnsupported(ctx context.Context, l *slog.Logger, msg string, err error) (outErr error) {
208208
if errors.Is(err, errors.ErrUnsupported) {
209-
log.Debug(msg, err)
209+
l.DebugContext(ctx, msg, slogutil.KeyError, err)
210210

211211
return nil
212212
}
213213

214214
return err
215215
}
216216

217-
// configureOS sets the OS-related configuration.
218-
func configureOS(conf *configuration) (err error) {
217+
// configureOS sets the OS-related configuration. l and conf must not be nil.
218+
// conf must be valid.
219+
func configureOS(ctx context.Context, l *slog.Logger, conf *configuration) (err error) {
219220
osConf := conf.OSConfig
220221
if osConf == nil {
221222
return nil
222223
}
223224

224225
if osConf.Group != "" {
225226
err = aghos.SetGroup(osConf.Group)
226-
err = logIfUnsupported("warning: setting group", err)
227+
err = logIfUnsupported(ctx, l, "warning: setting group", err)
227228
if err != nil {
228229
return fmt.Errorf("setting group: %w", err)
229230
}
230231

231-
log.Info("group set to %s", osConf.Group)
232+
l.InfoContext(ctx, "group set", "groupname", osConf.Group)
232233
}
233234

234235
if osConf.User != "" {
235236
err = aghos.SetUser(osConf.User)
236-
err = logIfUnsupported("warning: setting user", err)
237+
err = logIfUnsupported(ctx, l, "warning: setting user", err)
237238
if err != nil {
238239
return fmt.Errorf("setting user: %w", err)
239240
}
240241

241-
log.Info("user set to %s", osConf.User)
242+
l.InfoContext(ctx, "user set", "username", osConf.User)
242243
}
243244

244245
if osConf.RlimitNoFile != 0 {
245246
err = aghos.SetRlimit(osConf.RlimitNoFile)
246-
err = logIfUnsupported("warning: setting rlimit", err)
247+
err = logIfUnsupported(ctx, l, "warning: setting rlimit", err)
247248
if err != nil {
248249
return fmt.Errorf("setting rlimit: %w", err)
249250
}
250251

251-
log.Info("rlimit_nofile set to %d", osConf.RlimitNoFile)
252+
l.InfoContext(ctx, "rlimit_nofile set", "rlimit_nofile", osConf.RlimitNoFile)
252253
}
253254

254255
return nil
@@ -754,7 +755,7 @@ func run(
754755
err := setupContext(ctx, baseLogger, opts, workDir, confPath, isFirstRun)
755756
fatalOnError(err)
756757

757-
err = configureOS(config)
758+
err = configureOS(ctx, baseLogger, config)
758759
fatalOnError(err)
759760

760761
// Clients package uses filtering package's static data
@@ -1254,19 +1255,24 @@ func loadCmdLineOpts() (opts options) {
12541255
}
12551256

12561257
// printWebAddrs prints addresses built from proto, addr, and an appropriate
1257-
// port. At least one address is printed with the value of port. Output
1258-
// example:
1258+
// port. At least one address is printed with the value of port. l must not be
1259+
// nil. Output example:
12591260
//
1260-
// go to http://127.0.0.1:80
1261-
func printWebAddrs(proto, addr string, port uint16) {
1262-
log.Printf("go to %s://%s", proto, netutil.JoinHostPort(addr, port))
1261+
// 2026/04/08 14:01:43.794575 56031#1 [info] serving url=http://127.0.0.1:3000
1262+
func printWebAddrs(ctx context.Context, l *slog.Logger, proto, addr string, port uint16) {
1263+
u := &url.URL{
1264+
Scheme: proto,
1265+
Host: netutil.JoinHostPort(addr, port),
1266+
}
1267+
1268+
l.InfoContext(ctx, "serving", "url", u.String())
12631269
}
12641270

12651271
// printHTTPAddresses prints the IP addresses which user can use to access the
12661272
// admin interface. proto is either schemeHTTP or schemeHTTPS.
12671273
//
12681274
// TODO(s.chzhen): Implement separate functions for HTTP and HTTPS.
1269-
func printHTTPAddresses(proto string, tlsMgr *tlsManager) {
1275+
func printHTTPAddresses(ctx context.Context, l *slog.Logger, proto string, tlsMgr *tlsManager) {
12701276
var tlsConf *tlsConfigSettings
12711277
if tlsMgr != nil {
12721278
tlsConf = tlsMgr.config()
@@ -1278,32 +1284,32 @@ func printHTTPAddresses(proto string, tlsMgr *tlsManager) {
12781284
}
12791285

12801286
if proto == urlutil.SchemeHTTPS && tlsConf.ServerName != "" {
1281-
printWebAddrs(proto, tlsConf.ServerName, tlsConf.PortHTTPS)
1287+
printWebAddrs(ctx, l, proto, tlsConf.ServerName, tlsConf.PortHTTPS)
12821288

12831289
return
12841290
}
12851291

12861292
bindHost := config.HTTPConfig.Address.Addr()
12871293
if !bindHost.IsUnspecified() {
1288-
printWebAddrs(proto, bindHost.String(), port)
1294+
printWebAddrs(ctx, l, proto, bindHost.String(), port)
12891295

12901296
return
12911297
}
12921298

12931299
ifaces, err := aghnet.GetValidNetInterfacesForWeb()
12941300
if err != nil {
1295-
log.Error("web: getting iface ips: %s", err)
1301+
l.ErrorContext(ctx, "web: getting iface ips", slogutil.KeyError, err)
12961302
// That's weird, but we'll ignore it.
12971303
//
12981304
// TODO(e.burkov): Find out when it happens.
1299-
printWebAddrs(proto, bindHost.String(), port)
1305+
printWebAddrs(ctx, l, proto, bindHost.String(), port)
13001306

13011307
return
13021308
}
13031309

13041310
for _, iface := range ifaces {
13051311
for _, addr := range iface.Addresses {
1306-
printWebAddrs(proto, addr.String(), port)
1312+
printWebAddrs(ctx, l, proto, addr.String(), port)
13071313
}
13081314
}
13091315
}

internal/home/mobileconfig.go

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package home
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
7+
"log/slog"
68
"net"
79
"net/http"
810
"net/url"
@@ -12,7 +14,7 @@ import (
1214
"github.com/AdguardTeam/AdGuardHome/internal/client"
1315
"github.com/AdguardTeam/golibs/errors"
1416
"github.com/AdguardTeam/golibs/httphdr"
15-
"github.com/AdguardTeam/golibs/log"
17+
"github.com/AdguardTeam/golibs/logutil/slogutil"
1618
"github.com/AdguardTeam/golibs/netutil/urlutil"
1719
"github.com/google/uuid"
1820
"howett.net/plist"
@@ -90,6 +92,27 @@ const (
9092
dnsProtoTLS = "TLS"
9193
)
9294

95+
// mobileConfigHandlerConfig is the configuration structure for
96+
// [newMobileConfigHandler].
97+
type mobileConfigHandlerConfig struct {
98+
// logger is used for logging the operations of mobile config handler. It
99+
// must not be nil.
100+
logger *slog.Logger
101+
}
102+
103+
// mobileConfigHandler handles mobile-config related operations.
104+
type mobileConfigHandler struct {
105+
logger *slog.Logger
106+
}
107+
108+
// newMobileConfigHandler creates a new instance of [*mobileConfigHandler]. c
109+
// must not be nil and must be valid.
110+
func newMobileConfigHandler(c *mobileConfigHandlerConfig) *mobileConfigHandler {
111+
return &mobileConfigHandler{
112+
logger: c.logger.With(slogutil.KeyPrefix, "mobile_handler"),
113+
}
114+
}
115+
93116
func encodeMobileConfig(d *dnsSettings, clientID string) ([]byte, error) {
94117
var dspName string
95118
switch proto := d.DNSProtocol; proto {
@@ -142,25 +165,36 @@ func encodeMobileConfig(d *dnsSettings, clientID string) ([]byte, error) {
142165
return plist.MarshalIndent(data, plist.XMLFormat, "\t")
143166
}
144167

145-
func respondJSONError(w http.ResponseWriter, status int, msg string) {
168+
// respondJSONError writes an internal server error to the header and responds
169+
// to the client with an error. l and w must not be nil.
170+
func respondJSONError(
171+
ctx context.Context,
172+
l *slog.Logger,
173+
w http.ResponseWriter,
174+
status int,
175+
msg string,
176+
) {
146177
w.WriteHeader(http.StatusInternalServerError)
147178
err := json.NewEncoder(w).Encode(&jsonError{
148179
Message: msg,
149180
})
150181
if err != nil {
151-
log.Debug("writing %d json response: %s", status, err)
182+
l.DebugContext(ctx, "writing json response", "status", status, slogutil.KeyError, err)
152183
}
153184
}
154185

186+
// errEmptyHost error indicates that the host and server names are missing from
187+
// the query parameters.
155188
const errEmptyHost errors.Error = "no host in query parameters and no server_name"
156189

157-
func handleMobileConfig(w http.ResponseWriter, r *http.Request, dnsp string) {
190+
func (m *mobileConfigHandler) handleMobileConfig(w http.ResponseWriter, r *http.Request, dnsp string) {
158191
var err error
159192

193+
ctx := r.Context()
160194
q := r.URL.Query()
161195
host := q.Get("host")
162196
if host == "" {
163-
respondJSONError(w, http.StatusInternalServerError, string(errEmptyHost))
197+
respondJSONError(ctx, m.logger, w, http.StatusInternalServerError, string(errEmptyHost))
164198

165199
return
166200
}
@@ -169,7 +203,7 @@ func handleMobileConfig(w http.ResponseWriter, r *http.Request, dnsp string) {
169203
if clientID != "" {
170204
err = client.ValidateClientID(clientID)
171205
if err != nil {
172-
respondJSONError(w, http.StatusBadRequest, err.Error())
206+
respondJSONError(ctx, m.logger, w, http.StatusBadRequest, err.Error())
173207

174208
return
175209
}
@@ -182,7 +216,7 @@ func handleMobileConfig(w http.ResponseWriter, r *http.Request, dnsp string) {
182216

183217
mobileconfig, err := encodeMobileConfig(d, clientID)
184218
if err != nil {
185-
respondJSONError(w, http.StatusInternalServerError, err.Error())
219+
respondJSONError(ctx, m.logger, w, http.StatusInternalServerError, err.Error())
186220

187221
return
188222
}
@@ -204,10 +238,12 @@ func handleMobileConfig(w http.ResponseWriter, r *http.Request, dnsp string) {
204238
_, _ = w.Write(mobileconfig)
205239
}
206240

207-
func handleMobileConfigDoH(w http.ResponseWriter, r *http.Request) {
208-
handleMobileConfig(w, r, dnsProtoHTTPS)
241+
// handleMobileConfigDoH handles getting DNS-over-HTTPS .mobileconfig.
242+
func (m *mobileConfigHandler) handleMobileConfigDoH(w http.ResponseWriter, r *http.Request) {
243+
m.handleMobileConfig(w, r, dnsProtoHTTPS)
209244
}
210245

211-
func handleMobileConfigDoT(w http.ResponseWriter, r *http.Request) {
212-
handleMobileConfig(w, r, dnsProtoTLS)
246+
// handleMobileConfigDoT handles getting DNS-over-TLS .mobileconfig.
247+
func (m *mobileConfigHandler) handleMobileConfigDoT(w http.ResponseWriter, r *http.Request) {
248+
m.handleMobileConfig(w, r, dnsProtoTLS)
213249
}

0 commit comments

Comments
 (0)