| 1 |
.onAttach <- function(libname, pkgname){ |
| 2 |
version <- curl_version() |
| 3 |
ssl <- sub("\\(.*\\)\\W*", "", version$ssl_version) |
| 4 |
msg <- paste("Using libcurl", version$version, "with", ssl) |
| 5 |
packageStartupMessage(msg) |
| 6 |
if(grepl("redhat", R.version$platform) && !('smtp' %in% version$protocols)){ |
| 7 |
packageStartupMessage(c("Your system runs libcurl-minimal which does not support all protocols: ", |
| 8 |
"See also https://github.com/jeroen/curl/issues/350")) |
| 9 |
} |
| 10 |
try({ |
| 11 |
proxy <- Sys.getenv('ALL_PROXY') |
| 12 |
if(nchar(proxy)){ |
| 13 |
proxy_info <- curl::curl_parse_url(proxy) |
| 14 |
packageStartupMessage(sprintf("Using proxy server %s://%s:%s", |
| 15 |
proxy_info$scheme, proxy_info$host, proxy_info$port)) |
| 16 |
} |
| 17 |
}, silent = TRUE) |
| 18 |
} |
| 19 |
|
| 20 |
.onLoad <- function(libname, pkgname){ |
| 21 |
if(grepl('emscripten', R.version[['platform']])){ |
| 22 |
set_emscripten_gateway() |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
# Sets a default http gateway for using curl in WebR |
| 27 |
# See https://github.com/r-wasm/ws-proxy |
| 28 |
# Note socks5 proxy is just a dumb gateway that relays the encrypted https |
| 29 |
# traffic as-is. There is no snooping or tempering with cert verification |
| 30 |
# possible by the proxy server. |
| 31 |
set_emscripten_gateway <- function(){ |
| 32 |
proxy <- Sys.getenv('ALL_PROXY') |
| 33 |
|
| 34 |
# TODO: fix this unfortunate default envvar in webR? |
| 35 |
if(proxy == '' || proxy == "socks5h://localhost:8580"){ |
| 36 |
try({ |
| 37 |
# Note the websocket runs wss (port 443) but inside we mimic plain http |
| 38 |
# therefore for curl it looks like http:// but on 443. But it is actually |
| 39 |
# https because of the wss:// layer in emscripten. |
| 40 |
h <- new_handle(connecttimeout = 2, noproxy = '*') |
| 41 |
req <- curl_fetch_memory("http://get-ws-proxy.r-universe.dev:443", handle = h) |
| 42 |
if(req$status == 200){ |
| 43 |
wsproxy <- rawToChar(req$content) |
| 44 |
if(grepl('^socks5h://', wsproxy)){ |
| 45 |
Sys.setenv(ALL_PROXY = wsproxy) |
| 46 |
} |
| 47 |
} |
| 48 |
}, silent = TRUE) |
| 49 |
} |
| 50 |
} |