| 1 |
--- |
| 2 |
title: "nanonext - Configuration and Security" |
| 3 |
vignette: > |
| 4 |
%\VignetteIndexEntry{nanonext - Configuration and Security} |
| 5 |
%\VignetteEngine{litedown::vignette} |
| 6 |
%\VignetteEncoding{UTF-8} |
| 7 |
--- |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
``` r |
| 13 |
library(nanonext) |
| 14 |
``` |
| 15 |
|
| 16 |
### 1. TLS Secure Connections |
| 17 |
|
| 18 |
Secure connections use NNG and Mbed TLS libraries. Enable them by: |
| 19 |
|
| 20 |
1. Specifying a secure `tls+tcp://` or `wss://` URL |
| 21 |
2. Passing a TLS configuration object to the 'tls' argument of `listen()` or `dial()` |
| 22 |
|
| 23 |
Create TLS configurations with `tls_config()`: |
| 24 |
- Client configuration: requires PEM-encoded CA certificate to verify server identity |
| 25 |
- Server configuration: requires certificate and private key |
| 26 |
|
| 27 |
Certificates may be supplied as files or character vectors. Valid X.509 certificates from Certificate Authorities are supported. |
| 28 |
|
| 29 |
The convenience function `write_cert()` generates a 4096-bit RSA key pair and self-signed X.509 certificate. The 'cn' argument must match exactly the hostname/IP address of the URL (e.g., use '127.0.0.1' throughout, or 'localhost' throughout, not mixed). |
| 30 |
|
| 31 |
|
| 32 |
``` r |
| 33 |
cert <- write_cert(cn = "127.0.0.1") |
| 34 |
str(cert) |
| 35 |
#> List of 2 |
| 36 |
#> $ server: chr [1:2] "-----BEGIN CERTIFICATE-----\nMIIFOTCCAyGgAwIBAgIBATANBgkqhkiG9w0BAQsFADA0MRIwEAYDVQQDDAkxMjcu\nMC4wLjExETAPBgNV"| __truncated__ "-----BEGIN RSA PRIVATE KEY-----\nMIIJKAIBAAKCAgEA7bh7hshxv3wfY81Gkct1ffRlFB4XJj3vAH+wiM1l8Q9WAllX\nIfyEVwGdC665"| __truncated__ |
| 37 |
#> $ client: chr [1:2] "-----BEGIN CERTIFICATE-----\nMIIFOTCCAyGgAwIBAgIBATANBgkqhkiG9w0BAQsFADA0MRIwEAYDVQQDDAkxMjcu\nMC4wLjExETAPBgNV"| __truncated__ "" |
| 38 |
|
| 39 |
ser <- tls_config(server = cert$server) |
| 40 |
ser |
| 41 |
#> < TLS server config | auth mode: optional > |
| 42 |
|
| 43 |
cli <- tls_config(client = cert$client) |
| 44 |
cli |
| 45 |
#> < TLS client config | auth mode: required > |
| 46 |
|
| 47 |
s <- socket(listen = "tls+tcp://127.0.0.1:5558", tls = ser) |
| 48 |
s1 <- socket(dial = "tls+tcp://127.0.0.1:5558", tls = cli) |
| 49 |
|
| 50 |
# secure TLS connection established |
| 51 |
|
| 52 |
close(s1) |
| 53 |
close(s) |
| 54 |
|
| 55 |
``` |
| 56 |
|
| 57 |
### 2. Options |
| 58 |
|
| 59 |
Use `opt()` and `'opt<-'()` to get and set options on Sockets, Contexts, Streams, Listeners, or Dialers. See function documentation for available options. |
| 60 |
|
| 61 |
To configure dialers or listeners after creation, specify `autostart = FALSE` (configuration cannot be changed after starting). |
| 62 |
|
| 63 |
|
| 64 |
``` r |
| 65 |
s <- socket(listen = "inproc://options", autostart = FALSE) |
| 66 |
|
| 67 |
# no maximum message size |
| 68 |
opt(s$listener[[1]], "recv-size-max") |
| 69 |
#> [1] 0 |
| 70 |
|
| 71 |
# enforce maximum message size to protect against denial-of-service attacks |
| 72 |
opt(s$listener[[1]], "recv-size-max") <- 8192L |
| 73 |
|
| 74 |
opt(s$listener[[1]], "recv-size-max") |
| 75 |
#> [1] 8192 |
| 76 |
|
| 77 |
start(s$listener[[1]]) |
| 78 |
|
| 79 |
``` |
| 80 |
|
| 81 |
### 3. Custom Serialization |
| 82 |
|
| 83 |
The special write-only option 'serial' sets a serialization configuration via `serial_config()`. This registers custom functions for serializing/unserializing reference objects using R's 'refhook' system, enabling transparent send/receive with mode 'serial'. Configurations apply to the Socket and all Contexts created from it. |
| 84 |
|
| 85 |
|
| 86 |
``` r |
| 87 |
serial <- serial_config("obj_class", function(x) serialize(x, NULL), unserialize) |
| 88 |
opt(s, "serial") <- serial |
| 89 |
|
| 90 |
close(s) |
| 91 |
``` |
| 92 |
|
| 93 |
### 4. Statistics |
| 94 |
|
| 95 |
Use `stat()` to access NNG's statistics framework. Query Sockets, Listeners, or Dialers for statistics such as connection attempts and current connections. See function documentation for available statistics. |
| 96 |
|
| 97 |
|
| 98 |
``` r |
| 99 |
s <- socket(listen = "inproc://stat") |
| 100 |
|
| 101 |
# no active connections (pipes) |
| 102 |
stat(s, "pipes") |
| 103 |
#> [1] 0 |
| 104 |
|
| 105 |
s1 <- socket(dial = "inproc://stat") |
| 106 |
|
| 107 |
# one now that the dialer has connected |
| 108 |
stat(s, "pipes") |
| 109 |
#> [1] 1 |
| 110 |
|
| 111 |
close(s) |
| 112 |
``` |