aboutsummaryrefslogtreecommitdiff
path: root/plugin/pkg/doh/doh.go
diff options
context:
space:
mode:
Diffstat (limited to 'plugin/pkg/doh/doh.go')
-rw-r--r--plugin/pkg/doh/doh.go23
1 files changed, 20 insertions, 3 deletions
diff --git a/plugin/pkg/doh/doh.go b/plugin/pkg/doh/doh.go
index 9d5305b34..faddfc8aa 100644
--- a/plugin/pkg/doh/doh.go
+++ b/plugin/pkg/doh/doh.go
@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
+ "strings"
"github.com/miekg/dns"
)
@@ -16,18 +17,30 @@ const MimeType = "application/dns-message"
// Path is the URL path that should be used.
const Path = "/dns-query"
-// NewRequest returns a new DoH request given a method, URL (without any paths, so exclude /dns-query) and dns.Msg.
+// NewRequest returns a new DoH request given a HTTP method, URL and dns.Msg.
+//
+// The URL should not have a path, so please exclude /dns-query. The URL will
+// be prefixed with https:// by default, unless it's already prefixed with
+// either http:// or https://.
func NewRequest(method, url string, m *dns.Msg) (*http.Request, error) {
buf, err := m.Pack()
if err != nil {
return nil, err
}
+ if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
+ url = fmt.Sprintf("https://%s", url)
+ }
+
switch method {
case http.MethodGet:
b64 := base64.RawURLEncoding.EncodeToString(buf)
- req, err := http.NewRequest(http.MethodGet, "https://"+url+Path+"?dns="+b64, nil)
+ req, err := http.NewRequest(
+ http.MethodGet,
+ fmt.Sprintf("%s%s?dns=%s", url, Path, b64),
+ nil,
+ )
if err != nil {
return req, err
}
@@ -37,7 +50,11 @@ func NewRequest(method, url string, m *dns.Msg) (*http.Request, error) {
return req, nil
case http.MethodPost:
- req, err := http.NewRequest(http.MethodPost, "https://"+url+Path+"?bla=foo:443", bytes.NewReader(buf))
+ req, err := http.NewRequest(
+ http.MethodPost,
+ fmt.Sprintf("%s%s?bla=foo:443", url, Path),
+ bytes.NewReader(buf),
+ )
if err != nil {
return req, err
}