|
1 | | -# WebR Proxy |
| 1 | +# WS Proxy |
2 | 2 |
|
3 | | -Simple container with SOCKS proxy server running behind sockify, to connect from WebAssembly. |
| 3 | +Simple container with SOCKS5 proxy running behind sockify to connect from WebAssembly. |
4 | 4 |
|
5 | 5 |
|
6 | | -## Test with a public proxy server |
7 | | - |
8 | | -Open the WebR test app on https://webr.r-wasm.org/latest/ run some code to test: |
| 6 | +## Minimal example with a public proxy server |
9 | 7 |
|
| 8 | +Open the [WebR test app](https://webr.r-wasm.org/latest/) run some code to test. Note how the `ALL_PROXY` variable is all that is needed to make libcurl route all traffic via the proxy server. |
10 | 9 |
|
11 | 10 | ```r |
12 | | -# This routes all traffic over a public proxy server! |
13 | | -Sys.setenv(ALL_PROXY="socks5h://test:yolo@ws.opencpu.org:443") |
14 | | - |
| 11 | +# Install packages |
15 | 12 | install.packages("curl") |
16 | | -library(curl) |
17 | 13 |
|
18 | | -example(curl_fetch_memory) |
| 14 | +# Make a request via the public server server! |
| 15 | +Sys.setenv(ALL_PROXY="socks5h://test:yolo@ws.r-universe.dev:443") |
| 16 | +req <- curl::curl_fetch_memory('https://hb.cran.dev/get') |
| 17 | +cat(rawToChar(req$content)) |
19 | 18 | ``` |
20 | 19 |
|
21 | | -The `ALL_PROXY` variable will make libcurl route all traffic via this proxy server. |
| 20 | +The server `ws.r-universe.dev` is running exactly the same service from this container, but behind cloudflare to improve routing and handle the HTTPS certificates. |
| 21 | + |
| 22 | +## Testing many requests in parallel |
22 | 23 |
|
23 | | -The server `ws.opencpu.org` is running exactly the same service from this container, but behind cloudflare to get improved routing and proper https certificates. |
| 24 | +The following code downloads 200 small text files in parallel over HTTP/2 to your home dir with verbosity turned on. |
| 25 | + |
| 26 | +```r |
| 27 | +install.packages('curl') |
| 28 | +Sys.setenv(ALL_PROXY='socks5h://test:yolo@ws.opencpu.org:443') |
| 29 | +mirror <- 'https://cran.rstudio.com' |
| 30 | +df <- read.dcf(curl::curl(sprintf('%s/src/contrib/PACKAGES', mirror))) |
| 31 | +pkgs <- df[1:200,'Package'] |
| 32 | +urls <- sprintf('%s/web/packages/%s/DESCRIPTION', mirror, pkgs) |
| 33 | +destfiles <- sprintf('~/%s.txt', pkgs) |
| 34 | +results <- curl::multi_download(urls, destfiles, progress = FALSE, multiplex = TRUE, verbose=TRUE) |
| 35 | +all(results$status == 200) |
| 36 | + |
| 37 | +# Read one of the files to show it is there |
| 38 | +list.files('~') |
| 39 | +readLines("~/abc.txt") |
| 40 | +``` |
24 | 41 |
|
| 42 | +If you run this in the [WebR test app](https://webr.r-wasm.org/latest/) you can also view the file in the WebUI under `/home/web_user`. |
25 | 43 |
|
26 | | -## Test with a local proxy server |
| 44 | +## Test a local proxy server |
27 | 45 |
|
28 | 46 | On your local machine start the proxy server with: |
29 | 47 |
|
30 | 48 | ```sh |
31 | 49 | docker run -it -p7777:7777 ghcr.io/r-wasm/ws-proxy |
32 | 50 | ``` |
33 | 51 |
|
34 | | -Now open the WebR test app on https://webr.r-wasm.org/latest/ run this code to test: |
| 52 | +Now open the [WebR test app](https://webr.r-wasm.org/latest/) and run: |
35 | 53 |
|
36 | 54 | ```r |
37 | | -# Need to use non-https ws:// for local testing |
| 55 | +# Need to use non-https ws:// for local testing! |
38 | 56 | webr::eval_js("SOCKFS.websocketArgs.url = 'ws://'") |
39 | | -Sys.setenv(ALL_PROXY="socks5h://test:yolo@localhost:7777") |
40 | 57 |
|
| 58 | +# Same as before, but with localhost |
| 59 | +Sys.setenv(ALL_PROXY="socks5h://test:yolo@localhost:7777") |
41 | 60 | install.packages("curl") |
42 | | -library(curl) |
43 | | - |
44 | | -example(curl_fetch_memory) |
| 61 | +req <- curl::curl_fetch_memory('https://hb.cran.dev/get') |
| 62 | +cat(rawToChar(req$content)) |
45 | 63 | ``` |
46 | 64 |
|
47 | 65 | You should be able to see the proxy traffic in the docker terminal session. |
48 | 66 |
|
49 | | -NB the first line is needed because the your local proxy server does not have properly signed https certificates by default. Hence we connect over a HTTP instead of HTTPS websocket. |
50 | | - |
| 67 | +NB: the first line is needed because the your local proxy server does not have properly signed https certificates by default. Hence we proxy using a HTTP instead of HTTPS websocket. |
0 commit comments