Cannot create tunnel to Presto web UI
I am trying to connect to a web server (in this case, the status page for Presto database) that is accessible via SSH and am having trouble creating a tunnel. Creating the tunnel by calling ssh directly works (note that host and port are in a hidden chunk of the reprex):
pid <- sys::exec_background(
"ssh",
args = c(host, "-L", glue::glue("{port}:localhost:{port}"), "-N")
)
Sys.sleep(2) # allow tunnel to be created
res <- httr::GET(glue::glue("http://localhost:{port}/ui/"))
httr::status_code(res)
#> [1] 200
tools::pskill(pid)
Created on 2020-05-26 by the reprex package (v0.3.0)
But creating the tunnel with ssh_tunnel() does not work.
cmd <- glue::glue(
'session <- ssh::ssh_connect("{host}"); ',
'ssh::ssh_tunnel(session, port = {port}, target = "{host}:{port}")'
)
pid <- sys::exec_background("R", c("-e", cmd))
Sys.sleep(5)
res <- httr::GET(glue::glue("http://localhost:{port}/ui/"))
#> Error in curl::curl_fetch_memory(url, handle = handle): Recv failure: Connection reset by peer
httr::status_code(res)
#> Error in httr::status_code(res): object 'res' not found
tools::pskill(pid)
Created on 2020-05-26 by the reprex package (v0.3.0)
The error from GET() is more descriptive when run interactively:
Error: libssh failure at 'channel_open_forward': Channel opening failure: channel 43 error (2) Connection refused
Execution halted
Warning message:
Disconnecting from unused ssh session. Please use ssh_disconnect()
Error in curl::curl_fetch_memory(url, handle = handle) :
Recv failure: Connection reset by peer
I would have expected the ssh_tunnel() approach to be equivalent to directly calling ssh. Curiously, I can use the ssh_tunnel() approach to connect to a different host and service (RStudio Server) and get the message
/ Tunneled 448 bytes...tunnel closed!
One possibility is that ssh_tunnel() eventually creates the tunnel using the string "localhost", which may be handled oddly by Presto? It seems that MySQL, for example, treats localhost differently from 127.0.0.1.