Merge in DNS/adguard-home from AGDNS-2750-find-client to master Squashed commit of the following: commit 98f1a8ca4622b6f502a5092273b9724203fe0bd8 Merge: 9270222d84ccc2a213Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Apr 23 17:53:20 2025 +0300 Merge branch 'master' into AGDNS-2750-find-client commit 9270222d8e9e03038e9434b54496cbb6164463cd Merge: 6468ceec8c7c62ad3bAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Apr 21 19:40:58 2025 +0300 Merge branch 'master' into AGDNS-2750-find-client commit 6468ceec82d30084771a53ff6720a8c11c68bf2f Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Apr 21 19:40:52 2025 +0300 home: imp docs commit 3fd4735a0d6db4fdf2d46f3da9794a687fdcaa8b Merge: 1311a5869a8fdf1c55Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 18 19:43:36 2025 +0300 Merge branch 'master' into AGDNS-2750-find-client commit 1311a58695de00f20c9704378ee6e964a44d1c59 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 18 19:42:41 2025 +0300 home: imp code commit b1f2c4c883c9476c5135140abac31f8ae6609b4f Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Apr 16 16:47:59 2025 +0300 home: imp code commit d0a5abd66587c1ad602c2ccf6c8a45a3dfe39a5c Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Apr 15 14:58:31 2025 +0300 client: imp naming commit 5accdca325551237f003f1c416891b488fe5290b Merge: 6a00232f74d258972dAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Apr 14 19:40:40 2025 +0300 Merge branch 'master' into AGDNS-2750-find-client commit 6a00232f76a0fe5ce781aa01637b6e04ace7250d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Apr 14 19:30:32 2025 +0300 home: imp code commit 8633886457c6aab75f5676494b1f49d9811e9ab9 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 11 15:29:25 2025 +0300 all: imp code commit d6f16879e7b054a5ffac59131d2a6eff1da659c0 Merge: 58236fdec6d282ae71Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 10 21:35:23 2025 +0300 Merge branch 'master' into AGDNS-2750-find-client commit 58236fdec5b64e83a44680ff8a89badc18ec81f1 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 10 21:23:01 2025 +0300 all: upd ci commit 3c4d946d7970987677d4ac984394e18987a29f9a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 10 21:16:03 2025 +0300 all: upd go commit cc1c97734506a9ffbe70fd3c676284e58a21ba46 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 10 20:58:56 2025 +0300 all: imp code commit 8f061c933152481a4c80eef2af575efd4919d82b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Apr 9 16:49:11 2025 +0300 all: imp docs commit 8d19355f1c519211a56cec3f23d527922d4f2ee0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Apr 7 21:35:06 2025 +0300 all: imp code commit f1e853f57e5d54d13bedcdab4f8e21e112f3a356 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Apr 2 14:57:40 2025 +0300 all: imp code commit 6a6ac7f899f29ddc90a583c80562233e646ba1d6 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Apr 1 19:51:56 2025 +0300 client: imp tests commit 52040ee7393d0483c682f2f37d7b70f12f9cf621 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Apr 1 19:28:18 2025 +0300 all: imp code commit 1e09208dbd2d35c3f6b2ade169324e23d1a643a5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 26 15:33:02 2025 +0300 all: imp code ... and 2 more commits
198 lines
5.2 KiB
Go
198 lines
5.2 KiB
Go
package home
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"path"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/client"
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
"github.com/AdguardTeam/golibs/httphdr"
|
|
"github.com/AdguardTeam/golibs/log"
|
|
"github.com/AdguardTeam/golibs/netutil/urlutil"
|
|
"github.com/google/uuid"
|
|
"howett.net/plist"
|
|
)
|
|
|
|
// dnsSettings is the DNSSetting.DNSSettings mobileconfig profile.
|
|
//
|
|
// See https://developer.apple.com/documentation/devicemanagement/dnssettings/dnssettings.
|
|
type dnsSettings struct {
|
|
// DNSProtocol is the required protocol to be used. The valid values
|
|
// are "HTTPS" and "TLS".
|
|
DNSProtocol string
|
|
|
|
// ServerURL is the URI template of the DoH server. It must be empty if
|
|
// DNSProtocol is not "HTTPS".
|
|
ServerURL string `plist:",omitempty"`
|
|
|
|
// ServerName is the hostname of the DoT server. It must be empty if
|
|
// DNSProtocol is not "TLS".
|
|
ServerName string `plist:",omitempty"`
|
|
|
|
// ServerAddresses is a list IP addresses of the server.
|
|
//
|
|
// TODO(a.garipov): Allow users to set this.
|
|
//
|
|
// See https://github.com/AdguardTeam/AdGuardHome/issues/3607.
|
|
ServerAddresses []net.IP `plist:",omitempty"`
|
|
}
|
|
|
|
// payloadContent is a Device Management Profile payload.
|
|
//
|
|
// See https://developer.apple.com/documentation/devicemanagement/configuring_multiple_devices_using_profiles#3234127.
|
|
type payloadContent struct {
|
|
DNSSettings *dnsSettings
|
|
|
|
PayloadType string
|
|
PayloadIdentifier string
|
|
PayloadDisplayName string
|
|
PayloadDescription string
|
|
PayloadUUID uuid.UUID
|
|
PayloadVersion int
|
|
}
|
|
|
|
// dnsSettingsPayloadType is the payload type for a DNSSettings profile.
|
|
const dnsSettingsPayloadType = "com.apple.dnsSettings.managed"
|
|
|
|
// mobileConfig contains the TopLevel properties for configuring Device
|
|
// Management Profiles.
|
|
//
|
|
// See https://developer.apple.com/documentation/devicemanagement/toplevel.
|
|
type mobileConfig struct {
|
|
PayloadDescription string
|
|
PayloadDisplayName string
|
|
PayloadType string
|
|
PayloadContent []*payloadContent
|
|
PayloadIdentifier uuid.UUID
|
|
PayloadUUID uuid.UUID
|
|
PayloadVersion int
|
|
PayloadRemovalDisallowed bool
|
|
}
|
|
|
|
const (
|
|
dnsProtoHTTPS = "HTTPS"
|
|
dnsProtoTLS = "TLS"
|
|
)
|
|
|
|
func encodeMobileConfig(d *dnsSettings, clientID string) ([]byte, error) {
|
|
var dspName string
|
|
switch proto := d.DNSProtocol; proto {
|
|
case dnsProtoHTTPS:
|
|
dspName = fmt.Sprintf("%s DoH", d.ServerName)
|
|
u := &url.URL{
|
|
Scheme: urlutil.SchemeHTTPS,
|
|
Host: d.ServerName,
|
|
Path: path.Join("/dns-query", clientID),
|
|
}
|
|
d.ServerURL = u.String()
|
|
|
|
// Empty the ServerName field since it is only must be presented
|
|
// in DNS-over-TLS configuration.
|
|
d.ServerName = ""
|
|
case dnsProtoTLS:
|
|
dspName = fmt.Sprintf("%s DoT", d.ServerName)
|
|
if clientID != "" {
|
|
d.ServerName = clientID + "." + d.ServerName
|
|
}
|
|
default:
|
|
return nil, fmt.Errorf("bad dns protocol %q", proto)
|
|
}
|
|
|
|
payloadID := fmt.Sprintf("%s.%s", dnsSettingsPayloadType, uuid.New())
|
|
data := &mobileConfig{
|
|
PayloadDescription: "Adds AdGuard Home to macOS Big Sur and iOS 14 or newer systems",
|
|
PayloadDisplayName: dspName,
|
|
PayloadType: "Configuration",
|
|
PayloadContent: []*payloadContent{{
|
|
DNSSettings: d,
|
|
|
|
PayloadType: dnsSettingsPayloadType,
|
|
PayloadIdentifier: payloadID,
|
|
PayloadDisplayName: dspName,
|
|
PayloadDescription: "Configures device to use AdGuard Home",
|
|
PayloadUUID: uuid.New(),
|
|
PayloadVersion: 1,
|
|
}},
|
|
PayloadIdentifier: uuid.New(),
|
|
PayloadUUID: uuid.New(),
|
|
PayloadVersion: 1,
|
|
PayloadRemovalDisallowed: false,
|
|
}
|
|
|
|
return plist.MarshalIndent(data, plist.XMLFormat, "\t")
|
|
}
|
|
|
|
func respondJSONError(w http.ResponseWriter, status int, msg string) {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
err := json.NewEncoder(w).Encode(&jsonError{
|
|
Message: msg,
|
|
})
|
|
if err != nil {
|
|
log.Debug("writing %d json response: %s", status, err)
|
|
}
|
|
}
|
|
|
|
const errEmptyHost errors.Error = "no host in query parameters and no server_name"
|
|
|
|
func handleMobileConfig(w http.ResponseWriter, r *http.Request, dnsp string) {
|
|
var err error
|
|
|
|
q := r.URL.Query()
|
|
host := q.Get("host")
|
|
if host == "" {
|
|
respondJSONError(w, http.StatusInternalServerError, string(errEmptyHost))
|
|
|
|
return
|
|
}
|
|
|
|
clientID := q.Get("client_id")
|
|
if clientID != "" {
|
|
err = client.ValidateClientID(clientID)
|
|
if err != nil {
|
|
respondJSONError(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
}
|
|
}
|
|
|
|
d := &dnsSettings{
|
|
DNSProtocol: dnsp,
|
|
ServerName: host,
|
|
}
|
|
|
|
mobileconfig, err := encodeMobileConfig(d, clientID)
|
|
if err != nil {
|
|
respondJSONError(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
}
|
|
|
|
w.Header().Set(httphdr.ContentType, "application/xml")
|
|
|
|
const (
|
|
dohContDisp = `attachment; filename=doh.mobileconfig`
|
|
dotContDisp = `attachment; filename=dot.mobileconfig`
|
|
)
|
|
|
|
contDisp := dohContDisp
|
|
if dnsp == dnsProtoTLS {
|
|
contDisp = dotContDisp
|
|
}
|
|
|
|
w.Header().Set(httphdr.ContentDisposition, contDisp)
|
|
|
|
_, _ = w.Write(mobileconfig)
|
|
}
|
|
|
|
func handleMobileConfigDoH(w http.ResponseWriter, r *http.Request) {
|
|
handleMobileConfig(w, r, dnsProtoHTTPS)
|
|
}
|
|
|
|
func handleMobileConfigDoT(w http.ResponseWriter, r *http.Request) {
|
|
handleMobileConfig(w, r, dnsProtoTLS)
|
|
}
|