2022-10-04 16:02:55 +03:00
|
|
|
package websvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/netip"
|
|
|
|
"time"
|
|
|
|
|
2023-08-29 20:03:40 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
2022-10-10 14:05:24 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/agh"
|
2022-10-04 16:02:55 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HTTP Settings Handlers
|
|
|
|
|
|
|
|
// ReqPatchSettingsHTTP describes the request to the PATCH /api/v1/settings/http
|
|
|
|
// HTTP API.
|
|
|
|
type ReqPatchSettingsHTTP struct {
|
|
|
|
// TODO(a.garipov): Add more as we go.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Add wait time.
|
|
|
|
|
2023-08-29 20:03:40 +03:00
|
|
|
Addresses []netip.AddrPort `json:"addresses"`
|
|
|
|
SecureAddresses []netip.AddrPort `json:"secure_addresses"`
|
|
|
|
Timeout aghhttp.JSONDuration `json:"timeout"`
|
2022-10-04 16:02:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPAPIHTTPSettings are the HTTP settings as used by the HTTP API. See the
|
|
|
|
// HttpSettings object in the OpenAPI specification.
|
|
|
|
type HTTPAPIHTTPSettings struct {
|
|
|
|
// TODO(a.garipov): Add more as we go.
|
|
|
|
|
2023-08-29 20:03:40 +03:00
|
|
|
Addresses []netip.AddrPort `json:"addresses"`
|
|
|
|
SecureAddresses []netip.AddrPort `json:"secure_addresses"`
|
|
|
|
Timeout aghhttp.JSONDuration `json:"timeout"`
|
|
|
|
ForceHTTPS bool `json:"force_https"`
|
2022-10-04 16:02:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// handlePatchSettingsHTTP is the handler for the PATCH /api/v1/settings/http
|
|
|
|
// HTTP API.
|
|
|
|
func (svc *Service) handlePatchSettingsHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
req := &ReqPatchSettingsHTTP{}
|
|
|
|
|
|
|
|
// TODO(a.garipov): Validate nulls and proper JSON patch.
|
|
|
|
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&req)
|
|
|
|
if err != nil {
|
2023-08-29 20:03:40 +03:00
|
|
|
aghhttp.WriteJSONResponseError(w, r, fmt.Errorf("decoding: %w", err))
|
2022-10-04 16:02:55 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newConf := &Config{
|
2023-07-20 18:57:06 +03:00
|
|
|
Pprof: &PprofConfig{
|
|
|
|
Port: svc.pprofPort,
|
|
|
|
Enabled: svc.pprof != nil,
|
|
|
|
},
|
2022-10-04 16:02:55 +03:00
|
|
|
ConfigManager: svc.confMgr,
|
2023-06-13 13:41:13 +03:00
|
|
|
Frontend: svc.frontend,
|
2022-10-04 16:02:55 +03:00
|
|
|
TLS: svc.tls,
|
|
|
|
Addresses: req.Addresses,
|
|
|
|
SecureAddresses: req.SecureAddresses,
|
|
|
|
Timeout: time.Duration(req.Timeout),
|
|
|
|
ForceHTTPS: svc.forceHTTPS,
|
|
|
|
}
|
|
|
|
|
2023-08-29 20:03:40 +03:00
|
|
|
aghhttp.WriteJSONResponseOK(w, r, &HTTPAPIHTTPSettings{
|
2022-10-04 16:02:55 +03:00
|
|
|
Addresses: newConf.Addresses,
|
|
|
|
SecureAddresses: newConf.SecureAddresses,
|
2023-08-29 20:03:40 +03:00
|
|
|
Timeout: aghhttp.JSONDuration(newConf.Timeout),
|
2022-10-04 16:02:55 +03:00
|
|
|
ForceHTTPS: newConf.ForceHTTPS,
|
|
|
|
})
|
|
|
|
|
|
|
|
cancelUpd := func() {}
|
|
|
|
updCtx := context.Background()
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
if deadline, ok := ctx.Deadline(); ok {
|
|
|
|
updCtx, cancelUpd = context.WithDeadline(updCtx, deadline)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Launch the new HTTP service in a separate goroutine to let this handler
|
|
|
|
// finish and thus, this server to shutdown.
|
2023-06-19 12:21:32 +03:00
|
|
|
go svc.relaunch(updCtx, cancelUpd, newConf)
|
|
|
|
}
|
2022-10-04 16:02:55 +03:00
|
|
|
|
2023-06-19 12:21:32 +03:00
|
|
|
// relaunch updates the web service in the configuration manager and starts it.
|
|
|
|
// It is intended to be used as a goroutine.
|
|
|
|
func (svc *Service) relaunch(ctx context.Context, cancel context.CancelFunc, newConf *Config) {
|
|
|
|
defer log.OnPanic("websvc: relaunching")
|
2022-10-04 16:02:55 +03:00
|
|
|
|
2023-06-19 12:21:32 +03:00
|
|
|
defer cancel()
|
2022-10-04 16:02:55 +03:00
|
|
|
|
2023-06-19 12:21:32 +03:00
|
|
|
err := svc.confMgr.UpdateWeb(ctx, newConf)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("websvc: updating web: %s", err)
|
2022-10-04 16:02:55 +03:00
|
|
|
|
2023-06-19 12:21:32 +03:00
|
|
|
return
|
|
|
|
}
|
2022-10-04 16:02:55 +03:00
|
|
|
|
2023-06-19 12:21:32 +03:00
|
|
|
// TODO(a.garipov): Consider better ways to do this.
|
|
|
|
const maxUpdDur = 5 * time.Second
|
|
|
|
updStart := time.Now()
|
|
|
|
var newSvc agh.ServiceWithConfig[*Config]
|
|
|
|
for newSvc = svc.confMgr.Web(); newSvc == svc; {
|
|
|
|
if time.Since(updStart) >= maxUpdDur {
|
|
|
|
log.Error("websvc: failed to update svc after %s", maxUpdDur)
|
2022-10-04 16:02:55 +03:00
|
|
|
|
2023-06-19 12:21:32 +03:00
|
|
|
return
|
2022-10-04 16:02:55 +03:00
|
|
|
}
|
2023-06-19 12:21:32 +03:00
|
|
|
|
|
|
|
log.Debug("websvc: waiting for new websvc to be configured")
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = newSvc.Start()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("websvc: new svc failed to start with error: %s", err)
|
|
|
|
}
|
2022-10-04 16:02:55 +03:00
|
|
|
}
|