Skip to content

Instantly share code, notes, and snippets.

@weaming
Last active December 26, 2019 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weaming/a724e3927efbdb9179a69d12a290ba44 to your computer and use it in GitHub Desktop.
Save weaming/a724e3927efbdb9179a69d12a290ba44 to your computer and use it in GitHub Desktop.
DNS over HTTP
// https://developers.cloudflare.com/1.1.1.1/dns-over-https/json-format/
package redisHub
import (
"encoding/json"
"fmt"
"log"
"net/http"
"regexp"
"strings"
)
type DNSResponse struct {
Status int `json:"Status"`
TC bool `json:"TC"`
RD bool `json:"RD"`
RA bool `json:"RA"`
AD bool `json:"AD"`
CD bool `json:"CD"`
Question []struct {
Name string `json:"name"`
Type int `json:"type"`
} `json:"Question"`
Answer []struct {
Name string `json:"name"`
Type int `json:"type"`
TTL int `json:"TTL"`
Data string `json:"data"`
} `json:"Answer"`
}
var dnsClient = NewHTTPClient(10)
func cleanHost(host string) string {
if strings.HasPrefix(strings.ToLower(host), "http") {
host = strings.TrimPrefix(host, "https://")
host = strings.TrimPrefix(host, "http://")
return strings.Split(host, "/")[0]
}
return host
}
func checkDNS(host string) error {
host = cleanHost(host)
url := fmt.Sprintf("https://cloudflare-dns.com/dns-query?type=A&name=%s", host)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
req.Header.Set("Accept", "application/dns-json")
resp, err := dnsClient.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("status code of DNS query response is %d", resp.StatusCode)
}
d := &DNSResponse{}
json.NewDecoder(resp.Body).Decode(&d)
for _, x := range d.Answer {
if validIP4(x.Data) {
log.Printf("got DNS for %s host %s\n", x.Data, host)
return nil
}
}
return fmt.Errorf("DNS with type A for host %s has not been found through 1.1.1.1", host)
}
func validIP4(ipAddress string) bool {
ipAddress = strings.Trim(ipAddress, " ")
re, _ := regexp.Compile(`^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$`)
if re.MatchString(ipAddress) {
return true
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment