Tools to work with the Google DNS over HTTPS API in R https://cinc.rud.is/web/packages/gdns/
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.

80 lines
3.4KB

  1. ipv4_regex <-
  2. "^(?:(?: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]?)$"
  3. S_GET <- safely(httr::GET)
  4. #' Perform DNS over HTTPS queries using Google
  5. #'
  6. #' Traditional DNS queries and responses are sent over UDP or TCP without
  7. #' encryption. This is vulnerable to eavesdropping and spoofing (including
  8. #' DNS-based Internet filtering). Responses from recursive resolvers to clients
  9. #' are the most vulnerable to undesired or malicious changes, while
  10. #' communications between recursive resolvers and authoritative nameservers
  11. #' often incorporate additional protection.\cr
  12. #' \cr
  13. #' To address this problem, Google Public DNS offers DNS resolution over an
  14. #' encrypted HTTPS connection. DNS-over-HTTPS greatly enhances privacy and
  15. #' security between a client and a recursive resolver, and complements DNSSEC
  16. #' to provide end-to-end authenticated DNS lookups.
  17. #'
  18. #' To perform vectorized queries with only answers (and no metadata) use
  19. #' \code{bulk_query()}).
  20. #'
  21. #' @param name item to lookup. Valid characters are numbers, letters, hyphen, and dot. Length
  22. #' must be between 1 and 255. Names with escaped or non-ASCII characters
  23. #' are not supported. Internationalized domain names must use the
  24. #' punycode format (e.g. "\code{xn--qxam}").\cr
  25. #' \cr If an IPv4 string is input, it will be transformed into
  26. #' a proper format for reverse lookups.
  27. #' @param type RR type can be represented as a number in [1, 65535] or canonical
  28. #' string (A, aaaa, etc). More information on RR types can be
  29. #' found \href{http://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-4}{here}.
  30. #' You can use \code{255} for an \code{ANY} query.
  31. #' @param edns_client_subnet The edns0-client-subnet option. Format is an IP
  32. #' address with a subnet mask. Examples: \code{1.2.3.4/24},
  33. #' \code{2001:700:300::/48}.\cr
  34. #' If you are using DNS-over-HTTPS because of privacy concerns, and do
  35. #' not want any part of your IP address to be sent to authoritative
  36. #' nameservers for geographic location accuracy, use
  37. #' \code{edns_client_subnet=0.0.0.0/0}. Google Public DNS normally sends
  38. #' approximate network information (usually replacing the last part of
  39. #' your IPv4 address with zeroes). \code{0.0.0.0/0} is the default.
  40. #' @return a \code{list} with the query result or \code{NULL} if an error occurred
  41. #' @references \url{https://developers.google.com/speed/public-dns/docs/dns-over-https}
  42. #' @export
  43. #' @examples
  44. #' query("rud.is")
  45. #' query("example.com", "255") # ANY query
  46. #' query("microsoft.com", "MX")
  47. #' query("google-public-dns-a.google.com", "TXT")
  48. #' query("apple.com")
  49. #' query("17.142.160.59", "PTR")
  50. query <- function(name, type="1", edns_client_subnet="0.0.0.0/0") {
  51. if (grepl(ipv4_regex, name)) {
  52. name <- paste0(c(rev(unlist(stringi::stri_split_fixed(name, ".", 4))),
  53. "in-addr.arpa."),
  54. sep="", collapse=".")
  55. }
  56. res <- S_GET(
  57. url = "https://dns.google.com/resolve",
  58. query = list(
  59. name = name,
  60. type = type,
  61. edns_client_subnet = edns_client_subnet
  62. )
  63. )
  64. if (!is.null(res$result)) {
  65. stop_for_status(res$result)
  66. txt <- httr::content(res$result, as="text")
  67. txt <- stringi::stri_enc_toascii(txt)
  68. txt <- stringi::stri_replace_all_regex(txt, "[[:cntrl:][:blank:]\\n ]+", " ")
  69. jsonlite::fromJSON(txt)
  70. } else {
  71. NULL
  72. }
  73. }