2020-10-08 08:34:36 +00:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
2020-11-25 15:09:41 +00:00
|
|
|
"encoding/json"
|
2020-10-08 08:34:36 +00:00
|
|
|
"fmt"
|
2021-09-17 15:31:07 +00:00
|
|
|
"net"
|
2020-10-08 08:34:36 +00:00
|
|
|
"net/http"
|
2021-01-27 15:32:13 +00:00
|
|
|
"net/url"
|
|
|
|
"path"
|
2020-10-08 08:34:36 +00:00
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
|
2021-06-01 18:06:55 +00:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2020-11-25 15:09:41 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2020-10-08 08:34:36 +00:00
|
|
|
uuid "github.com/satori/go.uuid"
|
|
|
|
"howett.net/plist"
|
|
|
|
)
|
|
|
|
|
2021-09-13 13:00:36 +00:00
|
|
|
// dnsSettings is the DNSSetting.DNSSettings mobileconfig profile.
|
|
|
|
//
|
|
|
|
// See https://developer.apple.com/documentation/devicemanagement/dnssettings/dnssettings.
|
2020-12-07 13:04:53 +00:00
|
|
|
type dnsSettings struct {
|
2021-09-13 13:00:36 +00:00
|
|
|
// DNSProtocol is the required protocol to be used. The valid values
|
|
|
|
// are "HTTPS" and "TLS".
|
2020-10-08 08:34:36 +00:00
|
|
|
DNSProtocol string
|
2021-09-13 13:00:36 +00: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 15:31:07 +00:00
|
|
|
// ServerAddresses is a list IP addresses of the server.
|
|
|
|
ServerAddresses []net.IP `plist:",omitempty"`
|
2020-10-08 08:34:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 13:00:36 +00:00
|
|
|
// payloadContent is a Device Management Profile payload.
|
|
|
|
//
|
|
|
|
// See https://developer.apple.com/documentation/devicemanagement/configuring_multiple_devices_using_profiles#3234127.
|
2020-12-07 13:04:53 +00:00
|
|
|
type payloadContent struct {
|
2021-09-13 13:00:36 +00:00
|
|
|
DNSSettings *dnsSettings
|
|
|
|
|
2020-10-08 08:34:36 +00:00
|
|
|
PayloadType string
|
2021-09-13 13:00:36 +00:00
|
|
|
PayloadIdentifier string
|
2020-10-08 08:34:36 +00:00
|
|
|
PayloadUUID string
|
2021-09-13 13:00:36 +00:00
|
|
|
PayloadDisplayName string
|
|
|
|
PayloadDescription string
|
2021-01-27 15:32:13 +00:00
|
|
|
PayloadVersion int
|
2020-10-08 08:34:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 13:00:36 +00: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 13:04:53 +00:00
|
|
|
type mobileConfig struct {
|
2020-10-08 08:34:36 +00:00
|
|
|
PayloadDescription string
|
|
|
|
PayloadDisplayName string
|
|
|
|
PayloadIdentifier string
|
|
|
|
PayloadType string
|
|
|
|
PayloadUUID string
|
2021-09-13 13:00:36 +00:00
|
|
|
PayloadContent []*payloadContent
|
2020-10-08 08:34:36 +00:00
|
|
|
PayloadVersion int
|
2021-01-27 15:32:13 +00:00
|
|
|
PayloadRemovalDisallowed bool
|
2020-10-08 08:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func genUUIDv4() string {
|
|
|
|
return uuid.NewV4().String()
|
|
|
|
}
|
|
|
|
|
2020-10-29 16:39:11 +00:00
|
|
|
const (
|
|
|
|
dnsProtoHTTPS = "HTTPS"
|
|
|
|
dnsProtoTLS = "TLS"
|
|
|
|
)
|
|
|
|
|
2021-09-13 13:00:36 +00:00
|
|
|
func encodeMobileConfig(d *dnsSettings, clientID string) ([]byte, error) {
|
2021-01-27 15:32:13 +00:00
|
|
|
var dspName string
|
2021-06-01 18:06:55 +00:00
|
|
|
switch proto := d.DNSProtocol; proto {
|
2020-10-29 16:39:11 +00:00
|
|
|
case dnsProtoHTTPS:
|
2021-01-27 15:32:13 +00:00
|
|
|
dspName = fmt.Sprintf("%s DoH", d.ServerName)
|
|
|
|
u := &url.URL{
|
2021-03-15 11:19:04 +00:00
|
|
|
Scheme: schemeHTTPS,
|
2021-01-27 15:32:13 +00:00
|
|
|
Host: d.ServerName,
|
2021-09-13 13:00:36 +00:00
|
|
|
Path: path.Join("/dns-query", clientID),
|
2021-01-27 15:32:13 +00:00
|
|
|
}
|
|
|
|
d.ServerURL = u.String()
|
2021-09-13 13:00:36 +00:00
|
|
|
|
2021-06-01 18:06:55 +00:00
|
|
|
// Empty the ServerName field since it is only must be presented
|
|
|
|
// in DNS-over-TLS configuration.
|
|
|
|
d.ServerName = ""
|
2020-10-29 16:39:11 +00:00
|
|
|
case dnsProtoTLS:
|
2021-01-27 15:32:13 +00:00
|
|
|
dspName = fmt.Sprintf("%s DoT", d.ServerName)
|
2021-09-13 13:00:36 +00:00
|
|
|
if clientID != "" {
|
|
|
|
d.ServerName = clientID + "." + d.ServerName
|
2021-01-27 15:32:13 +00:00
|
|
|
}
|
2020-10-29 16:39:11 +00:00
|
|
|
default:
|
2021-06-01 18:06:55 +00:00
|
|
|
return nil, fmt.Errorf("bad dns protocol %q", proto)
|
2020-10-29 16:39:11 +00:00
|
|
|
}
|
2020-10-08 08:34:36 +00:00
|
|
|
|
2021-09-13 13:00:36 +00: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 08:34:36 +00:00
|
|
|
PayloadUUID: genUUIDv4(),
|
2021-09-13 13:00:36 +00:00
|
|
|
PayloadDisplayName: dspName,
|
|
|
|
PayloadDescription: "Configures device to use AdGuard Home",
|
2020-10-08 08:34:36 +00:00
|
|
|
PayloadVersion: 1,
|
|
|
|
DNSSettings: d,
|
|
|
|
}},
|
|
|
|
PayloadVersion: 1,
|
2021-09-13 13:00:36 +00:00
|
|
|
PayloadRemovalDisallowed: false,
|
2020-10-08 08:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return plist.MarshalIndent(data, plist.XMLFormat, "\t")
|
|
|
|
}
|
|
|
|
|
2021-06-01 18:06:55 +00: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 15:09:41 +00:00
|
|
|
func handleMobileConfig(w http.ResponseWriter, r *http.Request, dnsp string) {
|
2021-01-27 15:32:13 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
q := r.URL.Query()
|
|
|
|
host := q.Get("host")
|
2020-11-25 15:09:41 +00:00
|
|
|
if host == "" {
|
2021-06-01 18:06:55 +00:00
|
|
|
respondJSONError(w, http.StatusInternalServerError, string(errEmptyHost))
|
2020-11-25 15:09:41 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
clientID := q.Get("client_id")
|
2021-04-07 13:36:38 +00:00
|
|
|
if clientID != "" {
|
|
|
|
err = dnsforward.ValidateClientID(clientID)
|
2021-01-27 15:32:13 +00:00
|
|
|
if err != nil {
|
2021-06-01 18:06:55 +00:00
|
|
|
respondJSONError(w, http.StatusBadRequest, err.Error())
|
2021-04-07 13:36:38 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-01-27 15:32:13 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 15:31:07 +00:00
|
|
|
dnsIPs, err := collectDNSIPs()
|
|
|
|
if err != nil {
|
|
|
|
// Don't add a lot of formatting, since the error is already
|
|
|
|
// wrapped by collectDNSIPs.
|
|
|
|
respondJSONError(w, http.StatusInternalServerError, err.Error())
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-13 13:00:36 +00:00
|
|
|
d := &dnsSettings{
|
|
|
|
DNSProtocol: dnsp,
|
|
|
|
ServerName: host,
|
2021-09-17 15:31:07 +00:00
|
|
|
ServerAddresses: dnsIPs,
|
2020-11-25 15:09:41 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 13:00:36 +00:00
|
|
|
mobileconfig, err := encodeMobileConfig(d, clientID)
|
2020-10-08 08:34:36 +00:00
|
|
|
if err != nil {
|
2021-06-01 18:06:55 +00:00
|
|
|
respondJSONError(w, http.StatusInternalServerError, err.Error())
|
2020-11-25 15:09:41 +00:00
|
|
|
|
|
|
|
return
|
2020-10-08 08:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/xml")
|
2021-01-27 15:32:13 +00:00
|
|
|
|
2021-03-17 10:51:26 +00: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 08:34:36 +00:00
|
|
|
_, _ = w.Write(mobileconfig)
|
|
|
|
}
|
|
|
|
|
2021-06-18 15:13:36 +00:00
|
|
|
func handleMobileConfigDoH(w http.ResponseWriter, r *http.Request) {
|
2020-11-25 15:09:41 +00:00
|
|
|
handleMobileConfig(w, r, dnsProtoHTTPS)
|
2020-10-08 08:34:36 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 15:13:36 +00:00
|
|
|
func handleMobileConfigDoT(w http.ResponseWriter, r *http.Request) {
|
2020-11-25 15:09:41 +00:00
|
|
|
handleMobileConfig(w, r, dnsProtoTLS)
|
2020-10-08 08:34:36 +00:00
|
|
|
}
|
2021-09-17 15:31:07 +00:00
|
|
|
|
|
|
|
// collectDNSIPs returns a slice of IP addresses the server is listening
|
|
|
|
// on, including the addresses on all interfaces in cases of unspecified IPs but
|
|
|
|
// excluding loopback addresses.
|
|
|
|
func collectDNSIPs() (ips []net.IP, err error) {
|
|
|
|
// TODO(a.garipov): This really shouldn't be a function that parses
|
|
|
|
// a list of strings. Instead, we need a function that returns this
|
|
|
|
// data as []net.IP or []*netutil.IPPort. Maybe someday.
|
|
|
|
addrs, err := collectDNSAddresses()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range addrs {
|
|
|
|
ip := net.ParseIP(addr)
|
|
|
|
if ip != nil && !ip.IsLoopback() {
|
|
|
|
ips = append(ips, ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ips, nil
|
|
|
|
}
|