// SPDX-License-Identifier: BSD-3-Clause package query import ( "bytes" "fmt" "io" "net/http" "time" "git.froth.zone/sam/awl/cli" "git.froth.zone/sam/awl/internal/structs" "github.com/miekg/dns" ) type HTTPSResolver struct { server string opts cli.Options } func (r *HTTPSResolver) LookUp(msg *dns.Msg) (structs.Response, error) { var resp structs.Response httpR := &http.Client{} buf, err := msg.Pack() if err != nil { return structs.Response{}, err } r.opts.Logger.Debug("making DoH request") // query := server + "?dns=" + base64.RawURLEncoding.EncodeToString(buf) req, err := http.NewRequest("POST", r.server, bytes.NewBuffer(buf)) if err != nil { return structs.Response{}, fmt.Errorf("DoH: %s", err.Error()) } req.Header.Set("Content-Type", "application/dns-message") req.Header.Set("Accept", "application/dns-message") now := time.Now() res, err := httpR.Do(req) resp.RTT = time.Since(now) if err != nil { return structs.Response{}, fmt.Errorf("DoH HTTP request error: %s", err.Error()) } defer res.Body.Close() if res.StatusCode != http.StatusOK { return structs.Response{}, fmt.Errorf("DoH server responded with HTTP %d", res.StatusCode) } fullRes, err := io.ReadAll(res.Body) if err != nil { return structs.Response{}, fmt.Errorf("DoH body read error: %s", err.Error()) } resp.DNS = &dns.Msg{} r.opts.Logger.Debug("unpacking response") err = resp.DNS.Unpack(fullRes) if err != nil { return structs.Response{}, fmt.Errorf("DoH dns message unpack error: %s", err.Error()) } return resp, nil }