No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

doh-get.R 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #' Make a DoH Request (GET/REST)
  2. #'
  3. #' Issue a `GET` REST API query of type `type` for `name` to the
  4. #' DoH endpoint specified at `server_path`.
  5. #'
  6. #' @param name name to query for
  7. #' @param type DNS query type (defaults to "`A`")
  8. #' @param server_path full URL path to the DoH server quer endpoint (defaults to Quad9).
  9. #' @param extra_params any special `GET` query parameter needed for a given server API endpoint.
  10. #' this should be a named `list`.
  11. #' @return `NULL` (if the query failed) or a `data.frame` (tibble)
  12. #' @references <https://tools.ietf.org/id/draft-ietf-doh-dns-over-https-05.html>
  13. #' @export
  14. #' @examples
  15. #' doh_get("rud.is", "A")
  16. #' doh_get("lenovo.com", "TXT")
  17. #' doh_get(to_inaddr_arpa("104.244.13.104"), "PTR")
  18. doh_get <- function(name, type = "a", extra_params = list(), service_path = "https://9.9.9.9/dns-query") {
  19. stopifnot(is.list(extra_params))
  20. extra_params[["name"]] <- tolower(name)
  21. extra_params[["type"]] <- tolower(type[1])
  22. httr::GET(
  23. url = service_path,
  24. query = extra_params,
  25. .PLAYDOH_UA
  26. ) -> res
  27. httr::stop_for_status(res)
  28. out <- httr::content(res, as = "text", encoding = "UTF-8")
  29. out <- jsonlite::fromJSON(out)
  30. # will be NULL or a data frame
  31. ret <- out[["Answer"]]
  32. # cleanup column names and add query metadata if query was OK
  33. if (length(ret)) {
  34. colnames(ret) <- tolower(colnames(ret))
  35. attr(ret, "question") <- out[["Question"]]
  36. attr(ret, "flags") <- out[c("Status", "TC", "RD", "RA", "AD", "CD")]
  37. attr(ret, "edns_client_subnet") <- out[["edns_client_subnet"]]
  38. attr(ret, "comment") <- out[["comment"]]
  39. class(ret) <- c("tbl_df", "tbl", "data.frame")
  40. ret
  41. }
  42. ret
  43. }