Debian Codegraph @

R/nslookup.R

raw
1 #' Lookup a hostname
2 #'
3 #' The `nslookup` function is similar to `nsl` but works on all platforms
4 #' and can resolve ipv6 addresses if supported by the OS. Default behavior raises an
5 #' error if lookup fails.
6 #'
7 #' The `has_internet` function tests for internet connectivity by performing a
8 #' dns lookup. If a proxy server is detected, it will also check for connectivity by
9 #' connecting via the proxy.
10 #'
11 #' @export
12 #' @param host a string with a hostname
13 #' @param error raise an error for failed DNS lookup. Otherwise returns `NULL`.
14 #' @param ipv4_only always return ipv4 address. Set to `FALSE` to allow for ipv6 as well.
15 #' @param multiple returns multiple ip addresses if possible
16 #' @rdname nslookup
17 #' @useDynLib curl R_nslookup
18 #' @examples # Should always work if we are online
19 #' nslookup("www.google.com")
20 #'
21 #' # If your OS supports IPv6
22 #' nslookup("ipv6.test-ipv6.com", ipv4_only = FALSE, error = FALSE)
23 nslookup <- function(host, ipv4_only = FALSE, multiple = FALSE, error = TRUE){
24 stopifnot(is.character(host))
25 host <- enc2utf8(host)
26 if(grepl("://", host, fixed = TRUE))
27 stop("This looks like a URL, not a hostname")
28 out <- .Call(R_nslookup, host[1], as.logical(ipv4_only))
29 if(isTRUE(error) && is.null(out))
30 stop("Unable to resolve host: ", host)
31 if(isTRUE(multiple))
32 return(unique(out))
33 utils::head(out, 1)
34 }
35
36 #' @export
37 #' @rdname nslookup
38 has_internet <- local({
39 proxy_vars_previous <- NULL
40 has_internet_via_proxy <- NULL
41 function(){
42 # do not wait for nslookup() if we know proxy works
43 if(isTRUE(has_internet_via_proxy))
44 return(TRUE)
45
46 # Method 1: try DNS lookup. May resolve to 8.8.8.8 or 8.8.4.4
47 ip_addr <- nslookup('dns.google.com', multiple = TRUE, error = FALSE, ipv4_only = TRUE)
48 if(any(c("8.8.4.4", "8.8.8.8") %in% ip_addr))
49 return(TRUE)
50
51 ip_addr <- nslookup('time.cloudflare.com', multiple = TRUE, error = FALSE, ipv4_only = TRUE)
52 if(any(c("162.159.200.1", "162.159.200.123") %in% ip_addr))
53 return(TRUE)
54
55 # Method 2: look for a proxy server
56 proxy_vars <- Sys.getenv(c('ALL_PROXY', 'https_proxy', 'HTTPS_PROXY', 'HTTPS_proxy'), NA)
57
58 # If variables are unchanged, use the cached result
59 if(length(has_internet_via_proxy) && identical(proxy_vars, proxy_vars_previous)) {
60 return(has_internet_via_proxy)
61 } else {
62 proxy_vars_previous <<- proxy_vars
63 }
64
65 # Try via a proxy (mostly for Windows)
66 test_url <- 'http://captive.apple.com/hotspot-detect.html'
67 ie_proxy <- ie_get_proxy_for_url(test_url)
68
69 handle <- if(any(!is.na(proxy_vars))){
70 cat("Testing for internet connectivity via https_proxy... ", file = stderr())
71 new_handle(CONNECTTIMEOUT = 5)
72 } else if(length(ie_proxy)){
73 cat("Testing for internet connectivity via IE proxy... ", file = stderr())
74 new_handle(CONNECTTIMEOUT = 5, proxy = ie_proxy)
75 } else {
76 # No proxy server found. Do not cache in case user sets one later.
77 return(FALSE)
78 }
79 req <- try(curl_fetch_memory(url = test_url, handle = handle), silent = TRUE)
80 has_internet_via_proxy <<- is.list(req) && identical(req$status_code, 200L) &&
81 grepl("Success", rawToChar(req$content))
82 cat(ifelse(has_internet_via_proxy, "success!\n", "failed.\n"), file = stderr())
83 return(has_internet_via_proxy)
84 }
85 })