2020-10-08 11:34:36 +03:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
2020-11-25 18:09:41 +03:00
|
|
|
"encoding/json"
|
2020-10-08 11:34:36 +03:00
|
|
|
"fmt"
|
2021-09-17 18:31:07 +03:00
|
|
|
"net"
|
2020-10-08 11:34:36 +03:00
|
|
|
"net/http"
|
2021-01-27 18:32:13 +03:00
|
|
|
"net/url"
|
|
|
|
"path"
|
2020-10-08 11:34:36 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
|
2021-06-01 21:06:55 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2020-11-25 18:09:41 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2020-10-08 11:34:36 +03:00
|
|
|
uuid "github.com/satori/go.uuid"
|
|
|
|
"howett.net/plist"
|
|
|
|
)
|
|
|
|
|
2021-09-13 16:00:36 +03:00
|
|
|
// dnsSettings is the DNSSetting.DNSSettings mobileconfig profile.
|
|
|
|
//
|
|
|
|
// See https://developer.apple.com/documentation/devicemanagement/dnssettings/dnssettings.
|
2020-12-07 16:04:53 +03:00
|
|
|
type dnsSettings struct {
|
2021-09-13 16:00:36 +03:00
|
|
|
// DNSProtocol is the required protocol to be used. The valid values
|
|
|
|
// are "HTTPS" and "TLS".
|
2020-10-08 11:34:36 +03:00
|
|
|
DNSProtocol string
|
2021-09-13 16:00:36 +03:00
|
|
|
|
|
|
|
// 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"`
|
|
|
|
|
2021-09-17 18:31:07 +03:00
|
|
|
// ServerAddresses is a list IP addresses of the server.
|
2021-09-30 13:42:33 +03:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Allow users to set this.
|
|
|
|
//
|
|
|
|
// See https://github.com/AdguardTeam/AdGuardHome/issues/3607.
|
2021-09-17 18:31:07 +03:00
|
|
|
ServerAddresses []net.IP `plist:",omitempty"`
|
2020-10-08 11:34:36 +03:00
|
|
|
}
|
|
|
|
|
2021-09-13 16:00:36 +03:00
|
|
|
// payloadContent is a Device Management Profile payload.
|
|
|
|
//
|
|
|
|
// See https://developer.apple.com/documentation/devicemanagement/configuring_multiple_devices_using_profiles#3234127.
|
2020-12-07 16:04:53 +03:00
|
|
|
type payloadContent struct {
|
2021-09-13 16:00:36 +03:00
|
|
|
DNSSettings *dnsSettings
|
|
|
|
|
2020-10-08 11:34:36 +03:00
|
|
|
PayloadType string
|
2021-09-13 16:00:36 +03:00
|
|
|
PayloadIdentifier string
|
2020-10-08 11:34:36 +03:00
|
|
|
PayloadUUID string
|
2021-09-13 16:00:36 +03:00
|
|
|
PayloadDisplayName string
|
|
|
|
PayloadDescription string
|
2021-01-27 18:32:13 +03:00
|
|
|
PayloadVersion int
|
2020-10-08 11:34:36 +03:00
|
|
|
}
|
|
|
|
|
2021-09-13 16:00:36 +03:00
|
|
|
// 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.
|
2020-12-07 16:04:53 +03:00
|
|
|
type mobileConfig struct {
|
2020-10-08 11:34:36 +03:00
|
|
|
PayloadDescription string
|
|
|
|
PayloadDisplayName string
|
|
|
|
PayloadIdentifier string
|
|
|
|
PayloadType string
|
|
|
|
PayloadUUID string
|
2021-09-13 16:00:36 +03:00
|
|
|
PayloadContent []*payloadContent
|
2020-10-08 11:34:36 +03:00
|
|
|
PayloadVersion int
|
2021-01-27 18:32:13 +03:00
|
|
|
PayloadRemovalDisallowed bool
|
2020-10-08 11:34:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func genUUIDv4() string {
|
|
|
|
return uuid.NewV4().String()
|
|
|
|
}
|
|
|
|
|
2020-10-29 19:39:11 +03:00
|
|
|
const (
|
|
|
|
dnsProtoHTTPS = "HTTPS"
|
|
|
|
dnsProtoTLS = "TLS"
|
|
|
|
)
|
|
|
|
|
2021-09-13 16:00:36 +03:00
|
|
|
func encodeMobileConfig(d *dnsSettings, clientID string) ([]byte, error) {
|
2021-01-27 18:32:13 +03:00
|
|
|
var dspName string
|
2021-06-01 21:06:55 +03:00
|
|
|
switch proto := d.DNSProtocol; proto {
|
2020-10-29 19:39:11 +03:00
|
|
|
case dnsProtoHTTPS:
|
2021-01-27 18:32:13 +03:00
|
|
|
dspName = fmt.Sprintf("%s DoH", d.ServerName)
|
|
|
|
u := &url.URL{
|
2021-03-15 14:19:04 +03:00
|
|
|
Scheme: schemeHTTPS,
|
2021-01-27 18:32:13 +03:00
|
|
|
Host: d.ServerName,
|
2021-09-13 16:00:36 +03:00
|
|
|
Path: path.Join("/dns-query", clientID),
|
2021-01-27 18:32:13 +03:00
|
|
|
}
|
|
|
|
d.ServerURL = u.String()
|
2021-09-13 16:00:36 +03:00
|
|
|
|
2021-06-01 21:06:55 +03:00
|
|
|
// Empty the ServerName field since it is only must be presented
|
|
|
|
// in DNS-over-TLS configuration.
|
|
|
|
d.ServerName = ""
|
2020-10-29 19:39:11 +03:00
|
|
|
case dnsProtoTLS:
|
2021-01-27 18:32:13 +03:00
|
|
|
dspName = fmt.Sprintf("%s DoT", d.ServerName)
|
2021-09-13 16:00:36 +03:00
|
|
|
if clientID != "" {
|
|
|
|
d.ServerName = clientID + "." + d.ServerName
|
2021-01-27 18:32:13 +03:00
|
|
|
}
|
2020-10-29 19:39:11 +03:00
|
|
|
default:
|
2021-06-01 21:06:55 +03:00
|
|
|
return nil, fmt.Errorf("bad dns protocol %q", proto)
|
2020-10-29 19:39:11 +03:00
|
|
|
}
|
2020-10-08 11:34:36 +03:00
|
|
|
|
2021-09-13 16:00:36 +03:00
|
|
|
payloadID := fmt.Sprintf("%s.%s", dnsSettingsPayloadType, genUUIDv4())
|
|
|
|
data := &mobileConfig{
|
|
|
|
PayloadDescription: "Adds AdGuard Home to macOS Big Sur " +
|
|
|
|
"and iOS 14 or newer systems",
|
|
|
|
PayloadDisplayName: dspName,
|
|
|
|
PayloadIdentifier: genUUIDv4(),
|
|
|
|
PayloadType: "Configuration",
|
|
|
|
PayloadUUID: genUUIDv4(),
|
|
|
|
PayloadContent: []*payloadContent{{
|
|
|
|
PayloadType: dnsSettingsPayloadType,
|
|
|
|
PayloadIdentifier: payloadID,
|
2020-10-08 11:34:36 +03:00
|
|
|
PayloadUUID: genUUIDv4(),
|
2021-09-13 16:00:36 +03:00
|
|
|
PayloadDisplayName: dspName,
|
|
|
|
PayloadDescription: "Configures device to use AdGuard Home",
|
2020-10-08 11:34:36 +03:00
|
|
|
PayloadVersion: 1,
|
|
|
|
DNSSettings: d,
|
|
|
|
}},
|
|
|
|
PayloadVersion: 1,
|
2021-09-13 16:00:36 +03:00
|
|
|
PayloadRemovalDisallowed: false,
|
2020-10-08 11:34:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return plist.MarshalIndent(data, plist.XMLFormat, "\t")
|
|
|
|
}
|
|
|
|
|
2021-06-01 21:06:55 +03:00
|
|
|
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"
|
|
|
|
|
2020-11-25 18:09:41 +03:00
|
|
|
func handleMobileConfig(w http.ResponseWriter, r *http.Request, dnsp string) {
|
2021-01-27 18:32:13 +03:00
|
|
|
var err error
|
|
|
|
|
|
|
|
q := r.URL.Query()
|
|
|
|
host := q.Get("host")
|
2020-11-25 18:09:41 +03:00
|
|
|
if host == "" {
|
2021-06-01 21:06:55 +03:00
|
|
|
respondJSONError(w, http.StatusInternalServerError, string(errEmptyHost))
|
2020-11-25 18:09:41 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
clientID := q.Get("client_id")
|
2021-04-07 16:36:38 +03:00
|
|
|
if clientID != "" {
|
|
|
|
err = dnsforward.ValidateClientID(clientID)
|
2021-01-27 18:32:13 +03:00
|
|
|
if err != nil {
|
2021-06-01 21:06:55 +03:00
|
|
|
respondJSONError(w, http.StatusBadRequest, err.Error())
|
2021-04-07 16:36:38 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-01-27 18:32:13 +03:00
|
|
|
}
|
|
|
|
|
2021-09-13 16:00:36 +03:00
|
|
|
d := &dnsSettings{
|
2021-09-30 13:42:33 +03:00
|
|
|
DNSProtocol: dnsp,
|
|
|
|
ServerName: host,
|
2020-11-25 18:09:41 +03:00
|
|
|
}
|
|
|
|
|
2021-09-13 16:00:36 +03:00
|
|
|
mobileconfig, err := encodeMobileConfig(d, clientID)
|
2020-10-08 11:34:36 +03:00
|
|
|
if err != nil {
|
2021-06-01 21:06:55 +03:00
|
|
|
respondJSONError(w, http.StatusInternalServerError, err.Error())
|
2020-11-25 18:09:41 +03:00
|
|
|
|
|
|
|
return
|
2020-10-08 11:34:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/xml")
|
2021-01-27 18:32:13 +03:00
|
|
|
|
2021-03-17 13:51:26 +03:00
|
|
|
const (
|
|
|
|
dohContDisp = `attachment; filename=doh.mobileconfig`
|
|
|
|
dotContDisp = `attachment; filename=dot.mobileconfig`
|
|
|
|
)
|
|
|
|
|
|
|
|
contDisp := dohContDisp
|
|
|
|
if dnsp == dnsProtoTLS {
|
|
|
|
contDisp = dotContDisp
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Disposition", contDisp)
|
|
|
|
|
2020-10-08 11:34:36 +03:00
|
|
|
_, _ = w.Write(mobileconfig)
|
|
|
|
}
|
|
|
|
|
2021-06-18 18:13:36 +03:00
|
|
|
func handleMobileConfigDoH(w http.ResponseWriter, r *http.Request) {
|
2020-11-25 18:09:41 +03:00
|
|
|
handleMobileConfig(w, r, dnsProtoHTTPS)
|
2020-10-08 11:34:36 +03:00
|
|
|
}
|
|
|
|
|
2021-06-18 18:13:36 +03:00
|
|
|
func handleMobileConfigDoT(w http.ResponseWriter, r *http.Request) {
|
2020-11-25 18:09:41 +03:00
|
|
|
handleMobileConfig(w, r, dnsProtoTLS)
|
2020-10-08 11:34:36 +03:00
|
|
|
}
|