Skip to content

Commit 2dec52d

Browse files
committed
Update example a bit
1 parent 5ab9999 commit 2dec52d

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

README.md

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,71 @@
1-
# WebR Proxy
1+
# WS Proxy
22

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.
44

55

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
97

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.
109

1110
```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
1512
install.packages("curl")
16-
library(curl)
1713

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))
1918
```
2019

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
2223

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 an PACKAGES index file from CRAN, and then 200 small text files in parallel over HTTP/2 with verbosity turned on.
25+
26+
```r
27+
# Install the R package
28+
install.packages('curl')
29+
30+
# Set the ws-proxy server
31+
Sys.setenv(ALL_PROXY='socks5h://test:yolo@ws.r-universe.dev:443')
32+
33+
# From here everything is normal R code:
34+
df <- read.dcf(curl::curl('https://cran.rstudio.com/src/contrib/PACKAGES'))
35+
pkgs <- df[1:200, 'Package']
36+
urls <- sprintf('https://cran.rstudio.com/web/packages/%s/DESCRIPTION', pkgs)
37+
destfiles <- sprintf('~/%s.txt', pkgs)
38+
results <- curl::multi_download(urls, destfiles, verbose = TRUE)
39+
all(results$status == 200)
40+
41+
# Read one of the files to show it is there
42+
list.files('~')
43+
readLines("~/abc.txt")
44+
```
2445

46+
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`.
2547

26-
## Test with a local proxy server
48+
## Test a local proxy server
2749

2850
On your local machine start the proxy server with:
2951

3052
```sh
3153
docker run -it -p7777:7777 ghcr.io/r-wasm/ws-proxy
3254
```
3355

34-
Now open the WebR test app on https://webr.r-wasm.org/latest/ run this code to test:
56+
Now open the [WebR test app](https://webr.r-wasm.org/latest/) and run:
3557

3658
```r
37-
# Need to use non-https ws:// for local testing
59+
# Need to use non-https ws:// for local testing!
3860
webr::eval_js("SOCKFS.websocketArgs.url = 'ws://'")
39-
Sys.setenv(ALL_PROXY="socks5h://test:yolo@localhost:7777")
4061

62+
# Same as before, but with localhost
63+
Sys.setenv(ALL_PROXY="socks5h://test:yolo@localhost:7777")
4164
install.packages("curl")
42-
library(curl)
43-
44-
example(curl_fetch_memory)
65+
req <- curl::curl_fetch_memory('https://hb.cran.dev/get')
66+
cat(rawToChar(req$content))
4567
```
4668

4769
You should be able to see the proxy traffic in the docker terminal session.
4870

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-
71+
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

Comments
 (0)