Skip to content

Commit

Permalink
Add googlefront provider.
Browse files Browse the repository at this point in the history
This provider fronts dns.google.com with www.google.com.
  • Loading branch information
leonjza committed Nov 6, 2018
1 parent de5fafa commit d2ce0a0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ func init() {
}

rootCmd.PersistentFlags().StringVarP(&dnsProviderName,
"provider", "p", "google", "Preferred DNS provider to use. [possible: google, cloudflare, quad9, raw]")
"provider", "p", "googlefront",
"Preferred DNS provider to use. [possible: googlefront, google, cloudflare, quad9, raw]")
rootCmd.PersistentFlags().BoolVarP(&validateSSL,
"validate-certificate", "K", false, "Validate DoH provider SSL certificates")
}
Expand All @@ -77,6 +78,9 @@ func validateDNSDomain() {

func validateDNSProvider() {
switch dnsProviderName {
case "googlefront":
dnsProvider = dnsclient.NewGoogleFrontDNS()
break
case "google":
dnsProvider = dnsclient.NewGoogleDNS()
break
Expand Down
6 changes: 6 additions & 0 deletions dnsclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ func NewGoogleDNS() *GoogleDNS {
return &GoogleDNS{BaseURL: "https://dns.google.com/resolve"}
}

// NewGoogleFrontDNS starts a new Google DNS-over-HTTPS resolver Client
// The Host header for this request is updated in the client itself
func NewGoogleFrontDNS() *GoogleFrontDNS {
return &GoogleFrontDNS{BaseURL: "https://www.google.com/resolve"}
}

// NewCloudFlareDNS starts a new Cloudflare DNS-over-HTTPS resolver Client
func NewCloudFlareDNS() *CloudflareDNS {
return &CloudflareDNS{BaseURL: "https://cloudflare-dns.com/dns-query"}
Expand Down
70 changes: 70 additions & 0 deletions dnsclient/google_front.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package dnsclient

import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strconv"
"time"

"github.com/miekg/dns"
)

// GoogleFrontDNS is a Client instance resolving using Googles DNS-over-HTTPS service,
// fronted using www.google.com
type GoogleFrontDNS struct {
BaseURL string
}

// Lookup performs a DNS lookup using Google
func (c *GoogleFrontDNS) Lookup(name string, rType uint16) Response {

client := http.Client{
Timeout: time.Second * 20,
}

req, err := http.NewRequest("GET", c.BaseURL, nil)
if err != nil {
log.Fatal(err)
}

// Update the Host client header to dns.google.com
// Ref: https://twitter.com/vysecurity/status/1058947074392125440
req.Host = "dns.google.com"

q := req.URL.Query()
q.Add("name", name)
q.Add("type", strconv.Itoa(int(rType)))
q.Add("cd", "false") // ignore DNSSEC
// TODO: add random_padding
req.URL.RawQuery = q.Encode()

res, err := client.Do(req)
if err != nil {
log.Fatal(err)
}

body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}

dnsRequestResponse := requestResponse{}
err = json.Unmarshal(body, &dnsRequestResponse)
if err != nil {
log.Fatal(err)
}

fout := Response{}

if len(dnsRequestResponse.Answer) <= 0 {
return fout
}

fout.TTL = dnsRequestResponse.Answer[0].TTL
fout.Data = dnsRequestResponse.Answer[0].Data
fout.Status = dns.RcodeToString[dnsRequestResponse.Status]

return fout
}

0 comments on commit d2ce0a0

Please sign in to comment.