mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-26 06:55:48 +03:00
3f1fd56b17
Merge in DNS/adguard-home from 3172-mobileconfig to master
Updates #3172.
Updates #2497.
Squashed commit of the following:
commit 30549ef4eda9d88f0738089e901492d7369caa25
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Tue Jun 1 21:00:17 2021 +0300
all: log changes
commit 9b9429447430a8e5656b992c04c4a74606dc5f9f
Author: Ildar Kamalov <ik@adguard.com>
Date: Tue Jun 1 17:56:59 2021 +0300
client: always show port input
commit 6d6a0bdfaa849220a5ddb4a17502ab05379d7a1c
Merge: 13a3bffd 77946a7f
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Tue Jun 1 17:50:41 2021 +0300
Merge branch 'master' into 3172-mobileconfig
commit 13a3bffd4dd6ccabf3d261f17b2c758a5c61eb9c
Author: Ildar Kamalov <ik@adguard.com>
Date: Tue Jun 1 17:20:17 2021 +0300
client: add port to mobile config form
commit f6abe0b6044572f3801c31b683e76f90c4a28487
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Mon May 31 19:43:37 2021 +0300
home: imp cyclo
commit c304a0bacdca6f8b5ffd21f3d00c8244ea9e4e36
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Mon May 31 18:19:46 2021 +0300
home: reduce allocs
commit 10a7678861079b710bb0ef14569c60a09612ec70
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Mon May 24 20:05:08 2021 +0300
all: make the host parameter required
172 lines
4.1 KiB
Go
172 lines
4.1 KiB
Go
package home
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"path"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
"github.com/AdguardTeam/golibs/log"
|
|
uuid "github.com/satori/go.uuid"
|
|
"howett.net/plist"
|
|
)
|
|
|
|
type dnsSettings struct {
|
|
DNSProtocol string
|
|
ServerURL string `plist:",omitempty"`
|
|
ServerName string `plist:",omitempty"`
|
|
clientID string
|
|
}
|
|
|
|
type payloadContent struct {
|
|
Name string
|
|
PayloadDescription string
|
|
PayloadDisplayName string
|
|
PayloadIdentifier string
|
|
PayloadType string
|
|
PayloadUUID string
|
|
DNSSettings dnsSettings
|
|
PayloadVersion int
|
|
}
|
|
|
|
type mobileConfig struct {
|
|
PayloadDescription string
|
|
PayloadDisplayName string
|
|
PayloadIdentifier string
|
|
PayloadType string
|
|
PayloadUUID string
|
|
PayloadContent []payloadContent
|
|
PayloadVersion int
|
|
PayloadRemovalDisallowed bool
|
|
}
|
|
|
|
func genUUIDv4() string {
|
|
return uuid.NewV4().String()
|
|
}
|
|
|
|
const (
|
|
dnsProtoHTTPS = "HTTPS"
|
|
dnsProtoTLS = "TLS"
|
|
)
|
|
|
|
func getMobileConfig(d dnsSettings) ([]byte, error) {
|
|
var dspName string
|
|
switch proto := d.DNSProtocol; proto {
|
|
case dnsProtoHTTPS:
|
|
dspName = fmt.Sprintf("%s DoH", d.ServerName)
|
|
u := &url.URL{
|
|
Scheme: schemeHTTPS,
|
|
Host: d.ServerName,
|
|
Path: path.Join("/dns-query", d.clientID),
|
|
}
|
|
d.ServerURL = u.String()
|
|
// 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 = ""
|
|
case dnsProtoTLS:
|
|
dspName = fmt.Sprintf("%s DoT", d.ServerName)
|
|
if d.clientID != "" {
|
|
d.ServerName = d.clientID + "." + d.ServerName
|
|
}
|
|
default:
|
|
return nil, fmt.Errorf("bad dns protocol %q", proto)
|
|
}
|
|
|
|
data := mobileConfig{
|
|
PayloadContent: []payloadContent{{
|
|
Name: dspName,
|
|
PayloadDescription: "Configures device to use AdGuard Home",
|
|
PayloadDisplayName: dspName,
|
|
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",
|
|
PayloadDisplayName: dspName,
|
|
PayloadIdentifier: genUUIDv4(),
|
|
PayloadRemovalDisallowed: false,
|
|
PayloadType: "Configuration",
|
|
PayloadUUID: genUUIDv4(),
|
|
PayloadVersion: 1,
|
|
}
|
|
|
|
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 = dnsforward.ValidateClientID(clientID)
|
|
if err != nil {
|
|
respondJSONError(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
}
|
|
}
|
|
|
|
d := dnsSettings{
|
|
DNSProtocol: dnsp,
|
|
ServerName: host,
|
|
clientID: clientID,
|
|
}
|
|
|
|
mobileconfig, err := getMobileConfig(d)
|
|
if err != nil {
|
|
respondJSONError(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/xml")
|
|
|
|
const (
|
|
dohContDisp = `attachment; filename=doh.mobileconfig`
|
|
dotContDisp = `attachment; filename=dot.mobileconfig`
|
|
)
|
|
|
|
contDisp := dohContDisp
|
|
if dnsp == dnsProtoTLS {
|
|
contDisp = dotContDisp
|
|
}
|
|
|
|
w.Header().Set("Content-Disposition", 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)
|
|
}
|