Skip to content

Commit 5dfdf2a

Browse files
committed
Merge branch 'master' into AGDNS-2591-web-start-time
2 parents c4b2a56 + 594849b commit 5dfdf2a

File tree

7 files changed

+424
-232
lines changed

7 files changed

+424
-232
lines changed

internal/home/authhttp.go

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -374,37 +374,68 @@ func (mw *authMiddlewareDefault) Wrap(h http.Handler) (wrapped http.Handler) {
374374
}
375375

376376
path := r.URL.Path
377-
u, err := mw.userFromRequest(ctx, r)
378-
if err != nil {
379-
mw.logger.ErrorContext(ctx, "retrieving user from request", slogutil.KeyError, err)
377+
if mw.handleAuthenticatedUser(ctx, w, r, h, path) {
378+
return
380379
}
381380

382-
if u != nil {
383-
if path == "/login.html" {
384-
http.Redirect(w, r, "/", http.StatusFound)
381+
if mw.handlePublicAccess(w, r, h, path) {
382+
return
383+
}
385384

386-
return
387-
}
385+
w.WriteHeader(http.StatusUnauthorized)
386+
})
387+
}
388388

389-
h.ServeHTTP(w, r.WithContext(withWebUser(ctx, u)))
389+
// handleAuthenticatedUser tries to get user from request and processes request
390+
// if user was successfully authenticated. Returns true if request was handled.
391+
func (mw *authMiddlewareDefault) handleAuthenticatedUser(
392+
ctx context.Context,
393+
w http.ResponseWriter,
394+
r *http.Request,
395+
h http.Handler,
396+
path string,
397+
) (ok bool) {
398+
u, err := mw.userFromRequest(ctx, r)
399+
if err != nil {
400+
mw.logger.ErrorContext(ctx, "retrieving user from request", slogutil.KeyError, err)
401+
}
390402

391-
return
392-
}
403+
if u == nil {
404+
return false
405+
}
393406

394-
if isPublicResource(path) {
395-
h.ServeHTTP(w, r)
407+
if path == "/login.html" {
408+
http.Redirect(w, r, "/", http.StatusFound)
396409

397-
return
398-
}
410+
return true
411+
}
399412

400-
if path == "/" || path == "/index.html" {
401-
http.Redirect(w, r, "login.html", http.StatusFound)
413+
h.ServeHTTP(w, r.WithContext(withWebUser(ctx, u)))
402414

403-
return
404-
}
415+
return true
416+
}
405417

406-
w.WriteHeader(http.StatusUnauthorized)
407-
})
418+
// handlePublicAccess handles request if user is trying to access public or root
419+
// pages.
420+
func (mw *authMiddlewareDefault) handlePublicAccess(
421+
w http.ResponseWriter,
422+
r *http.Request,
423+
h http.Handler,
424+
path string,
425+
) (ok bool) {
426+
if isPublicResource(path) {
427+
h.ServeHTTP(w, r)
428+
429+
return true
430+
}
431+
432+
if path == "/" || path == "/index.html" {
433+
http.Redirect(w, r, "login.html", http.StatusFound)
434+
435+
return true
436+
}
437+
438+
return false
408439
}
409440

410441
// needsAuthentication returns true if there are stored web users and requests

internal/home/controlupdate.go

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -228,55 +228,59 @@ func finishUpdate(
228228
cleanup(ctx)
229229
cleanupAlways()
230230

231-
var err error
232231
if runtime.GOOS == "windows" {
233-
if runningAsService {
234-
// NOTE: We can't restart the service via "kardianos/service"
235-
// package, because it kills the process first we can't start a new
236-
// instance, because Windows doesn't allow it.
237-
//
238-
// TODO(a.garipov): Recheck the claim above.
239-
var cmd executil.Command
240-
cmd, err = cmdCons.New(ctx, &executil.CommandConfig{
241-
Path: "cmd",
242-
Args: []string{"/c", "net stop AdGuardHome & net start AdGuardHome"},
243-
})
244-
if err != nil {
245-
panic(fmt.Errorf("constructing cmd: %w", err))
246-
}
247-
248-
err = cmd.Start(ctx)
249-
if err != nil {
250-
panic(fmt.Errorf("restarting service: %w", err))
251-
}
252-
253-
os.Exit(osutil.ExitCodeSuccess)
254-
}
232+
finalizeWindowsUpdate(ctx, l, cmdCons, execPath, runningAsService)
255233

256-
l.InfoContext(ctx, "restarting", "exec_path", execPath, "args", os.Args[1:])
234+
os.Exit(osutil.ExitCodeSuccess)
235+
}
236+
237+
var err error
238+
l.InfoContext(ctx, "restarting", "exec_path", execPath, "args", os.Args[1:])
239+
err = syscall.Exec(execPath, os.Args, os.Environ())
240+
if err != nil {
241+
panic(fmt.Errorf("restarting: %w", err))
242+
}
243+
}
257244

258-
var cmd executil.Command
259-
cmd, err = cmdCons.New(ctx, &executil.CommandConfig{
245+
// finalizeWindowsUpdate completes an update procedure on windows. l and
246+
// cmdCons must not be nil.
247+
func finalizeWindowsUpdate(ctx context.Context,
248+
l *slog.Logger,
249+
cmdCons executil.CommandConstructor,
250+
execPath string,
251+
runningAsService bool,
252+
) {
253+
var commandConf *executil.CommandConfig
254+
255+
if runningAsService {
256+
// NOTE: We can't restart the service via "kardianos/service" package,
257+
// because it kills the process first we can't start a new instance,
258+
// because Windows doesn't allow it.
259+
//
260+
// TODO(a.garipov): Recheck the claim above.
261+
commandConf = &executil.CommandConfig{
262+
Path: "cmd",
263+
Args: []string{"/c", "net stop AdGuardHome & net start AdGuardHome"},
264+
}
265+
} else {
266+
commandConf = &executil.CommandConfig{
260267
Path: execPath,
261268
Args: os.Args[1:],
262269
Stdin: os.Stdin,
263270
Stdout: os.Stdout,
264271
Stderr: os.Stderr,
265-
})
266-
if err != nil {
267-
panic(fmt.Errorf("constructing cmd: %w", err))
268272
}
273+
}
269274

270-
err = cmd.Start(ctx)
271-
if err != nil {
272-
panic(fmt.Errorf("restarting: %w", err))
273-
}
275+
l.InfoContext(ctx, "restarting", "exec_path", execPath, "args", os.Args[1:])
274276

275-
os.Exit(osutil.ExitCodeSuccess)
277+
var cmd executil.Command
278+
cmd, err := cmdCons.New(ctx, commandConf)
279+
if err != nil {
280+
panic(fmt.Errorf("constructing cmd: %w", err))
276281
}
277282

278-
l.InfoContext(ctx, "restarting", "exec_path", execPath, "args", os.Args[1:])
279-
err = syscall.Exec(execPath, os.Args, os.Environ())
283+
err = cmd.Start(ctx)
280284
if err != nil {
281285
panic(fmt.Errorf("restarting: %w", err))
282286
}

0 commit comments

Comments
 (0)