2022-10-04 16:02:55 +03:00
|
|
|
package websvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/netip"
|
|
|
|
"time"
|
|
|
|
|
2023-08-29 20:03:40 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
2022-10-04 16:02:55 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/dnssvc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DNS Settings Handlers
|
|
|
|
|
|
|
|
// ReqPatchSettingsDNS describes the request to the PATCH /api/v1/settings/dns
|
|
|
|
// HTTP API.
|
|
|
|
type ReqPatchSettingsDNS struct {
|
|
|
|
// TODO(a.garipov): Add more as we go.
|
|
|
|
|
2023-08-29 20:03:40 +03:00
|
|
|
Addresses []netip.AddrPort `json:"addresses"`
|
|
|
|
BootstrapServers []string `json:"bootstrap_servers"`
|
|
|
|
UpstreamServers []string `json:"upstream_servers"`
|
|
|
|
DNS64Prefixes []netip.Prefix `json:"dns64_prefixes"`
|
|
|
|
UpstreamTimeout aghhttp.JSONDuration `json:"upstream_timeout"`
|
|
|
|
BootstrapPreferIPv6 bool `json:"bootstrap_prefer_ipv6"`
|
|
|
|
UseDNS64 bool `json:"use_dns64"`
|
2022-10-04 16:02:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPAPIDNSSettings are the DNS settings as used by the HTTP API. See the
|
|
|
|
// DnsSettings object in the OpenAPI specification.
|
|
|
|
type HTTPAPIDNSSettings struct {
|
|
|
|
// TODO(a.garipov): Add more as we go.
|
|
|
|
|
2023-08-29 20:03:40 +03:00
|
|
|
Addresses []netip.AddrPort `json:"addresses"`
|
|
|
|
BootstrapServers []string `json:"bootstrap_servers"`
|
|
|
|
UpstreamServers []string `json:"upstream_servers"`
|
|
|
|
DNS64Prefixes []netip.Prefix `json:"dns64_prefixes"`
|
|
|
|
UpstreamTimeout aghhttp.JSONDuration `json:"upstream_timeout"`
|
|
|
|
BootstrapPreferIPv6 bool `json:"bootstrap_prefer_ipv6"`
|
|
|
|
UseDNS64 bool `json:"use_dns64"`
|
2022-10-04 16:02:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// handlePatchSettingsDNS is the handler for the PATCH /api/v1/settings/dns HTTP
|
|
|
|
// API.
|
|
|
|
func (svc *Service) handlePatchSettingsDNS(w http.ResponseWriter, r *http.Request) {
|
|
|
|
req := &ReqPatchSettingsDNS{
|
|
|
|
Addresses: []netip.AddrPort{},
|
|
|
|
BootstrapServers: []string{},
|
|
|
|
UpstreamServers: []string{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 := &dnssvc.Config{
|
2023-06-28 12:48:53 +03:00
|
|
|
Addresses: req.Addresses,
|
|
|
|
BootstrapServers: req.BootstrapServers,
|
|
|
|
UpstreamServers: req.UpstreamServers,
|
|
|
|
DNS64Prefixes: req.DNS64Prefixes,
|
|
|
|
UpstreamTimeout: time.Duration(req.UpstreamTimeout),
|
|
|
|
BootstrapPreferIPv6: req.BootstrapPreferIPv6,
|
|
|
|
UseDNS64: req.UseDNS64,
|
2022-10-04 16:02:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
err = svc.confMgr.UpdateDNS(ctx, newConf)
|
|
|
|
if err != nil {
|
2023-08-29 20:03:40 +03:00
|
|
|
aghhttp.WriteJSONResponseError(w, r, fmt.Errorf("updating: %w", err))
|
2022-10-04 16:02:55 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newSvc := svc.confMgr.DNS()
|
|
|
|
err = newSvc.Start()
|
|
|
|
if err != nil {
|
2023-08-29 20:03:40 +03:00
|
|
|
aghhttp.WriteJSONResponseError(w, r, fmt.Errorf("starting new service: %w", err))
|
2022-10-04 16:02:55 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-29 20:03:40 +03:00
|
|
|
aghhttp.WriteJSONResponseOK(w, r, &HTTPAPIDNSSettings{
|
2023-06-28 12:48:53 +03:00
|
|
|
Addresses: newConf.Addresses,
|
|
|
|
BootstrapServers: newConf.BootstrapServers,
|
|
|
|
UpstreamServers: newConf.UpstreamServers,
|
|
|
|
DNS64Prefixes: newConf.DNS64Prefixes,
|
2023-08-29 20:03:40 +03:00
|
|
|
UpstreamTimeout: aghhttp.JSONDuration(newConf.UpstreamTimeout),
|
2023-06-28 12:48:53 +03:00
|
|
|
BootstrapPreferIPv6: newConf.BootstrapPreferIPv6,
|
|
|
|
UseDNS64: newConf.UseDNS64,
|
2022-10-04 16:02:55 +03:00
|
|
|
})
|
|
|
|
}
|