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