File: random_port.R

package info (click to toggle)
r-cran-httpuv 1.6.15%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,292 kB
  • sloc: ansic: 6,499; cpp: 5,501; makefile: 103; sh: 56
file content (133 lines) | stat: -rw-r--r-- 2,218 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#' Find an open TCP port
#'
#' Finds a random available TCP port for listening on, within a specified range
#' of ports. The default range of ports to check is 1024 to 49151, which is the
#' set of TCP User Ports. This function automatically excludes some ports which
#' are considered unsafe by web browsers.
#'
#' @inheritParams runServer
#' @param min Minimum port number.
#' @param max Maximum port number.
#' @param n Number of ports to try before giving up.
#'
#' @return A port that is available to listen on.
#'
#' @examples
#' \dontrun{
#' s <- startServer("127.0.0.1", randomPort(), list())
#' browseURL(paste0("http://127.0.0.1:", s$getPort()))
#'
#' s$stop()
#' }
#'
#' @export
randomPort <- function(min = 1024L, max = 49151L, host = "127.0.0.1", n = 20) {
  min <- max(1L, min)
  max <- min(max, 65535L)
  valid_ports <- setdiff(seq.int(min, max), unsafe_ports)

  n <- min(n, length(valid_ports))
  # Try up to n ports
  try_ports <- if (n < 2) valid_ports else sample(valid_ports, n)

  for (port in try_ports) {
    if (is_port_available(port, host)) {
      return(port)
    }
  }

  error_unavailable_port()
}

is_port_available <- function(port, host = "127.0.0.1") {
  tryCatch(
    {
      s <- startServer(host, port, list(), quiet = TRUE)
      s$stop()
      TRUE
    },
    error = function(e) FALSE
  )
}

error_unavailable_port <- function(message = "Cannot find an available port.") {
  stop(
    structure(
      list(message = message, call = sys.call(-1)),
      class = c("httpuv_unavailable_port", "error", "condition")
    )
  )
}

# Ports that are considered unsafe by Chrome
# http://superuser.com/questions/188058/which-ports-are-considered-unsafe-on-chrome
# https://github.com/rstudio/shiny/issues/1784
unsafe_ports <- c(
  1,
  7,
  9,
  11,
  13,
  15,
  17,
  19,
  20,
  21,
  22,
  23,
  25,
  37,
  42,
  43,
  53,
  77,
  79,
  87,
  95,
  101,
  102,
  103,
  104,
  109,
  110,
  111,
  113,
  115,
  117,
  119,
  123,
  135,
  139,
  143,
  179,
  389,
  427,
  465,
  512,
  513,
  514,
  515,
  526,
  530,
  531,
  532,
  540,
  548,
  556,
  563,
  587,
  601,
  636,
  993,
  995,
  2049,
  3659,
  4045,
  6000,
  6665,
  6666,
  6667,
  6668,
  6669,
  6697
)