mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-15 18:08:30 -07:00
Pull request: * home: improve naming in mobileconfig handlers
Merge in DNS/adguard-home from 2235-mobileconfig-server-name to master * commit '98f6843aab8ff69606f44f6a5721d5f2a5707a0f': * home: improve naming in mobileconfig handlers
This commit is contained in:
commit
df3fa595a2
@ -1,3 +1,4 @@
|
|||||||
|
// Package home contains AdGuard Home's HTTP API methods.
|
||||||
package home
|
package home
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -2,6 +2,7 @@ package home
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
uuid "github.com/satori/go.uuid"
|
uuid "github.com/satori/go.uuid"
|
||||||
@ -14,7 +15,7 @@ type DNSSettings struct {
|
|||||||
ServerName string `plist:",omitempty"`
|
ServerName string `plist:",omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PayloadContent = struct {
|
type PayloadContent struct {
|
||||||
Name string
|
Name string
|
||||||
PayloadDescription string
|
PayloadDescription string
|
||||||
PayloadDisplayName string
|
PayloadDisplayName string
|
||||||
@ -25,7 +26,7 @@ type PayloadContent = struct {
|
|||||||
DNSSettings DNSSettings
|
DNSSettings DNSSettings
|
||||||
}
|
}
|
||||||
|
|
||||||
type MobileConfig = struct {
|
type MobileConfig struct {
|
||||||
PayloadContent []PayloadContent
|
PayloadContent []PayloadContent
|
||||||
PayloadDescription string
|
PayloadDescription string
|
||||||
PayloadDisplayName string
|
PayloadDisplayName string
|
||||||
@ -40,8 +41,21 @@ func genUUIDv4() string {
|
|||||||
return uuid.NewV4().String()
|
return uuid.NewV4().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMobileConfig(r *http.Request, d DNSSettings) ([]byte, error) {
|
const (
|
||||||
name := fmt.Sprintf("%s DNS over %s", r.Host, d.DNSProtocol)
|
dnsProtoHTTPS = "HTTPS"
|
||||||
|
dnsProtoTLS = "TLS"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getMobileConfig(d DNSSettings) ([]byte, error) {
|
||||||
|
var name string
|
||||||
|
switch d.DNSProtocol {
|
||||||
|
case dnsProtoHTTPS:
|
||||||
|
name = fmt.Sprintf("%s DoH", d.ServerName)
|
||||||
|
case dnsProtoTLS:
|
||||||
|
name = fmt.Sprintf("%s DoT", d.ServerName)
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("bad dns protocol %q", d.DNSProtocol)
|
||||||
|
}
|
||||||
|
|
||||||
data := MobileConfig{
|
data := MobileConfig{
|
||||||
PayloadContent: []PayloadContent{{
|
PayloadContent: []PayloadContent{{
|
||||||
@ -66,9 +80,8 @@ func getMobileConfig(r *http.Request, d DNSSettings) ([]byte, error) {
|
|||||||
return plist.MarshalIndent(data, plist.XMLFormat, "\t")
|
return plist.MarshalIndent(data, plist.XMLFormat, "\t")
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleMobileConfig(w http.ResponseWriter, r *http.Request, d DNSSettings) {
|
func handleMobileConfig(w http.ResponseWriter, d DNSSettings) {
|
||||||
mobileconfig, err := getMobileConfig(r, d)
|
mobileconfig, err := getMobileConfig(d)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpError(w, http.StatusInternalServerError, "plist.MarshalIndent: %s", err)
|
httpError(w, http.StatusInternalServerError, "plist.MarshalIndent: %s", err)
|
||||||
}
|
}
|
||||||
@ -78,15 +91,23 @@ func handleMobileConfig(w http.ResponseWriter, r *http.Request, d DNSSettings) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleMobileConfigDoh(w http.ResponseWriter, r *http.Request) {
|
func handleMobileConfigDoh(w http.ResponseWriter, r *http.Request) {
|
||||||
handleMobileConfig(w, r, DNSSettings{
|
handleMobileConfig(w, DNSSettings{
|
||||||
DNSProtocol: "HTTPS",
|
DNSProtocol: dnsProtoHTTPS,
|
||||||
ServerURL: fmt.Sprintf("https://%s/dns-query", r.Host),
|
ServerURL: fmt.Sprintf("https://%s/dns-query", r.Host),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleMobileConfigDot(w http.ResponseWriter, r *http.Request) {
|
func handleMobileConfigDot(w http.ResponseWriter, r *http.Request) {
|
||||||
handleMobileConfig(w, r, DNSSettings{
|
var err error
|
||||||
DNSProtocol: "TLS",
|
|
||||||
ServerName: r.Host,
|
var host string
|
||||||
|
host, _, err = net.SplitHostPort(r.Host)
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, http.StatusBadRequest, "getting host: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
handleMobileConfig(w, DNSSettings{
|
||||||
|
DNSProtocol: dnsProtoTLS,
|
||||||
|
ServerName: host,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
33
home/mobileconfig_test.go
Normal file
33
home/mobileconfig_test.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package home
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"howett.net/plist"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHandleMobileConfigDot(t *testing.T) {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
var r *http.Request
|
||||||
|
r, err = http.NewRequest(http.MethodGet, "https://example.com:12345/apple/dot.mobileconfig", nil)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
handleMobileConfigDot(w, r)
|
||||||
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
|
|
||||||
|
var mc MobileConfig
|
||||||
|
_, err = plist.Unmarshal(w.Body.Bytes(), &mc)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
if assert.Equal(t, 1, len(mc.PayloadContent)) {
|
||||||
|
assert.Equal(t, "example.com DoT", mc.PayloadContent[0].Name)
|
||||||
|
assert.Equal(t, "example.com DoT", mc.PayloadContent[0].PayloadDisplayName)
|
||||||
|
assert.Equal(t, "example.com", mc.PayloadContent[0].DNSSettings.ServerName)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user