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"
|
|
|
|
"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"
|
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"
|
|
|
|
)
|
|
|
|
|
2020-12-07 16:04:53 +03:00
|
|
|
type dnsSettings struct {
|
2020-10-08 11:34:36 +03:00
|
|
|
DNSProtocol string
|
|
|
|
ServerURL string `plist:",omitempty"`
|
|
|
|
ServerName string `plist:",omitempty"`
|
2021-01-27 18:32:13 +03:00
|
|
|
clientID string
|
2020-10-08 11:34:36 +03:00
|
|
|
}
|
|
|
|
|
2020-12-07 16:04:53 +03:00
|
|
|
type payloadContent struct {
|
2020-10-08 11:34:36 +03:00
|
|
|
Name string
|
|
|
|
PayloadDescription string
|
|
|
|
PayloadDisplayName string
|
|
|
|
PayloadIdentifier string
|
|
|
|
PayloadType string
|
|
|
|
PayloadUUID string
|
2020-12-07 16:04:53 +03:00
|
|
|
DNSSettings dnsSettings
|
2021-01-27 18:32:13 +03:00
|
|
|
PayloadVersion int
|
2020-10-08 11:34:36 +03:00
|
|
|
}
|
|
|
|
|
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-01-27 18:32:13 +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"
|
|
|
|
)
|
|
|
|
|
2020-12-07 16:04:53 +03:00
|
|
|
func getMobileConfig(d dnsSettings) ([]byte, error) {
|
2021-01-27 18:32:13 +03:00
|
|
|
var dspName string
|
2020-10-29 19:39:11 +03:00
|
|
|
switch d.DNSProtocol {
|
|
|
|
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,
|
|
|
|
Path: "/dns-query",
|
|
|
|
}
|
|
|
|
if d.clientID != "" {
|
|
|
|
u.Path = path.Join(u.Path, d.clientID)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.ServerURL = u.String()
|
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)
|
|
|
|
if d.clientID != "" {
|
|
|
|
d.ServerName = d.clientID + "." + d.ServerName
|
|
|
|
}
|
2020-10-29 19:39:11 +03:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("bad dns protocol %q", d.DNSProtocol)
|
|
|
|
}
|
2020-10-08 11:34:36 +03:00
|
|
|
|
2020-12-07 16:04:53 +03:00
|
|
|
data := mobileConfig{
|
|
|
|
PayloadContent: []payloadContent{{
|
2021-01-27 18:32:13 +03:00
|
|
|
Name: dspName,
|
2020-10-08 11:34:36 +03:00
|
|
|
PayloadDescription: "Configures device to use AdGuard Home",
|
2021-01-27 18:32:13 +03:00
|
|
|
PayloadDisplayName: dspName,
|
2020-10-08 11:34:36 +03: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 18:32:13 +03:00
|
|
|
PayloadDisplayName: dspName,
|
2020-10-08 11:34:36 +03:00
|
|
|
PayloadIdentifier: genUUIDv4(),
|
|
|
|
PayloadRemovalDisallowed: false,
|
|
|
|
PayloadType: "Configuration",
|
|
|
|
PayloadUUID: genUUIDv4(),
|
|
|
|
PayloadVersion: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
return plist.MarshalIndent(data, plist.XMLFormat, "\t")
|
|
|
|
}
|
|
|
|
|
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 == "" {
|
|
|
|
host = Context.tls.conf.ServerName
|
|
|
|
}
|
|
|
|
|
|
|
|
if host == "" {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
|
|
|
const msg = "no host in query parameters and no server_name"
|
2021-01-27 18:32:13 +03:00
|
|
|
err = json.NewEncoder(w).Encode(&jsonError{
|
2020-11-25 18:09:41 +03:00
|
|
|
Message: msg,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("writing 500 json response: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
clientID := q.Get("client_id")
|
|
|
|
err = dnsforward.ValidateClientID(clientID)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(&jsonError{
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("writing 400 json response: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-07 16:04:53 +03:00
|
|
|
d := dnsSettings{
|
2020-11-25 18:09:41 +03:00
|
|
|
DNSProtocol: dnsp,
|
|
|
|
ServerName: host,
|
2021-01-27 18:32:13 +03:00
|
|
|
clientID: clientID,
|
2020-11-25 18:09:41 +03:00
|
|
|
}
|
|
|
|
|
2020-10-29 19:39:11 +03:00
|
|
|
mobileconfig, err := getMobileConfig(d)
|
2020-10-08 11:34:36 +03:00
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusInternalServerError, "plist.MarshalIndent: %s", err)
|
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
|
|
|
|
2020-10-08 11:34:36 +03:00
|
|
|
_, _ = w.Write(mobileconfig)
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:09:41 +03:00
|
|
|
func handleMobileConfigDOH(w http.ResponseWriter, r *http.Request) {
|
|
|
|
handleMobileConfig(w, r, dnsProtoHTTPS)
|
2020-10-08 11:34:36 +03:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:09:41 +03:00
|
|
|
func handleMobileConfigDOT(w http.ResponseWriter, r *http.Request) {
|
|
|
|
handleMobileConfig(w, r, dnsProtoTLS)
|
2020-10-08 11:34:36 +03:00
|
|
|
}
|