R/check_ip.R

Defines functions check_ip

Documented in check_ip

#' Check IP
#'
#' check what your IP is
#'
#' @importFrom httr GET http_status content
#' @importFrom jsonlite prettify
#'
#' @return IP information
#' @export
#'
#' @details
#' Use \code{\link[httr]{GET}} to get information about the web page
#' https://api-ipv4.ip.sb/ip & https://api.ip.sb/geoip
#'
#' @examples
#' \dontrun{
#' check_ip()
#' }
check_ip <- function() {

  message("check what your ip is: ")

  # Define the URLs
  ip_urls <- c(
    "https://api-ipv4.ip.sb/ip",
    "https://api.ip.sb/ip"
  )
  geoip_urls <- c(
    "https://api.ip.sb/geoip",
    "https://ipinfo.io/json"
  )

  fetch_text <- function(urls) {
    for (url in urls) {
      response <- tryCatch(
        GET(url, httr::user_agent("r.proxy")),
        error = function(e) NULL
      )

      if (is.null(response) || http_status(response)$category != "Success") {
        next
      }

      text <- content(response, "text", encoding = "UTF-8")
      if (!is.na(text) && nzchar(text)) {
        return(text)
      }
    }

    stop("No successful response.", call. = FALSE)
  }

  message("IPv4: ")
  tryCatch({
    ip <- fetch_text(ip_urls)
    message(trimws(ip))
  }, error = function(e) {
    message("Unable to retrieve IPv4 address.")
  })

  message("Details: ")
  tryCatch({
    details <- fetch_text(geoip_urls)
    message(jsonlite::prettify(details))
  }, error = function(e) {
    message("Unable to retrieve IP details.")
  })

}

Try the r.proxy package in your browser

Any scripts or data that you put into this service are public.

r.proxy documentation built on May 20, 2026, 5:08 p.m.