rtailcat lets R act as either end of a tailcat connection. A tailcat server registers with a public relay and gets a secret, shareable address; any client holding that address can open TCP streams or exchange UDP datagrams with the server, from anywhere, without accounts, port forwarding, or a VPN. Traffic is end-to-end encrypted and uses a direct path between the peers when one can be found.
The package embeds tailcat’s Go library through its versioned C ABI and wraps it in R6 classes: TailcatServer, TailcatListener, TailcatClient, and TailcatConnection. Every blocking method also has an _async() version that returns a promises promise, so servers can run alongside Shiny, httpuv, or other code driven by the later event loop.
Full documentation is at https://rundel.github.io/rtailcat/.
Installation
Building the package needs a Go toolchain (version 1.27.2 or newer, see src/go/go.mod), a C compiler, and GNU make; on Windows that means Rtools plus Go on the PATH. configure looks for Go in the RTAILCAT_GO environment variable, on the PATH, and in a few common install locations, and fails with an explanation if it cannot find a recent enough version. It never downloads Go itself.
The first build downloads the pinned, checksum-verified Go modules unless the source tree contains src/go/vendor/ (created by tools/vendor.sh), in which case it builds offline. A tarball built from a vendored tree is meant for offline or self-contained installs, not for CRAN: the vendored Go sources include file paths longer than 100 bytes, so R CMD build warns that such paths are not portable to every tar implementation. The warning is expected and does not affect installing with a current R.
Install the development version from GitHub with pak, which builds the package from source, so Go must be installed as described above:
pak::pak("rundel/rtailcat")Or, from a checkout of the source:
A server and a client
Run the server in one R session. It listens on port 8080 of its tailcat address and writes the address to a file that only you can read; give that file to the client by any private channel.
library(rtailcat)
srv = TailcatServer$new()
lst = srv$listen(8080)
tailcat_address_write(srv, "address.txt")
conn = lst$accept()
msg = conn$read_all(as = "text")
conn$write(paste0("server received: ", msg))$close_write()$close()
srv$close(drain = 5)Run the client in a second R session, on the same machine or anywhere else:
library(rtailcat)
cl = TailcatClient$new(tailcat_address_read("address.txt"))
conn = cl$dial(8080)
conn$write("hello from the client")$close_write()
conn$read_all(as = "text")
#> [1] "server received: hello from the client"
conn$close()
cl$close(drain = 5)Reads return raw vectors by default; as = "text" returns a UTF-8 string instead. Over TCP a single $read() can end in the middle of a multibyte character, so use $read_all(as = "text"), or read raw bytes and convert them once, for multibyte text. Data containing NUL bytes cannot be read as text and raises an error.
$read_all() keeps the whole stream in memory, so give it a limit when the peer is not trusted: conn$read_all(max_bytes = 1e6) raises a tailcat_failure error as soon as the stream turns out to be longer than a million bytes. The bytes read are discarded, but the connection stays open.
For a conversation with several messages in each direction, use $write_message() and $read_message(): each $read_message() returns exactly what one $write_message() sent, however TCP split it up on the way, and returns NULL once the other side has ended the stream. Give $read_message() a max_bytes when the peer is not trusted; a longer message is refused before it is read. $read_exact(n) reads exactly n bytes, for fixed-size fields of other binary protocols.
$write(), like the other methods that change a connection, returns the connection, so calls chain as above. $close_write() tells the other side that you have finished sending (it then reads end of stream, so $read_all() returns), while still letting you read the reply. $close(drain = 5) on a client or server first waits up to five seconds for TCP shutdown traffic to finish, so the peer sees a clean close; close the connections before that.
Use network = "udp" in $listen() and $dial() for datagrams: each $write() sends one datagram of at most 1232 bytes (tailcat_udp_max_payload()) and each $read() returns one. A UDP flow appears at the listener once the client has sent its first datagram.
A few helpers round this out: cl$ping() and cl$disco_ping() measure latency to the server in seconds (the latter also reports, as direct, whether the path is direct or through a relay), tailcat_address_parse() shows the public parts of an address without revealing its pre-shared key, and tailcat_build_info() reports the embedded tailcat and Go versions. To use your own relay for every server and client in a session, including ones created inside other packages, set the options rtailcat.region or rtailcat.relay_map_url (see ?TailcatServer).
Serving connections asynchronously
$serve() runs an accept loop on the later event loop and calls a handler for each connection, without blocking the R session. This echo server handles any number of clients, one after another or at the same time:
library(rtailcat)
srv = TailcatServer$new()
lst = srv$listen(7)
lst$serve(function(conn) {
promises::then(conn$read_all_async(timeout = 60), function(data) {
conn$write(data)$close_write()$close()
})
})
tailcat_address_write(srv, "address.txt")The loop only makes progress while the event loop runs, which happens automatically when the R console is idle and inside Shiny or httpuv apps. In a script run with Rscript, keep it running explicitly:
tailcat_run(until = function() lst$is_closed)A handler error, or a rejection of the promise the handler returns (as in the example above), is passed to on_error if you give one (lst$serve(handler, on_error = function(cond, conn) ...)), otherwise it becomes a warning; either way that connection is closed and the loop carries on. Closing the listener or the server ends the loop; lst$stop_serving() ends it but keeps the listener open, and lst$is_serving tells whether a loop is running. Blocking calls inside a handler hold up the event loop until they return, so prefer the _async() methods there.
conn$read_loop(callback) is the reading counterpart of $serve(): it calls callback with each chunk, UDP datagram, or (with messages = TRUE) message as it arrives, waits for a promise the callback returns before reading on, and stops at the end of the stream (calling on_end), on conn$stop_reading(), or on an error (calling on_error and closing the connection). conn$is_reading tells whether a loop is running.
In a script, tailcat_wait(promise, timeout) runs the event loop until a promise settles and returns its value, so conn = tailcat_wait(cl$dial_async(8080)) works like the blocking call.
The other asynchronous methods work the same way, for example cl$dial_async(8080) or srv$listen_async(0). Promises reject with the same error conditions the blocking methods raise. Reads, and writes, on one connection run in the order they are started, so several $write_async() calls send their data in call order, while a read and a write can run at the same time. A promise is settled on the later event loop that was current when the operation started, for example inside later::with_temp_loop(); if that loop is destroyed first, the result is delivered on the global loop instead.
Any promise returned by an _async() method or function can be canceled with tailcat_cancel(). The promise then rejects with a tailcat_canceled condition, and the listener, connection, client, or server stays usable, as after a timeout:
pending = lst$accept_async()
# ... later, nobody came
tailcat_cancel(pending)
promises::catch(pending, function(e) {
if (inherits(e, "tailcat_canceled")) message("accept canceled")
})Cancel the promise returned by the _async() call itself: promises derived from it with promises::then() or a promise pipe do not carry the cancellation handle. Canceling an operation that has already finished, or canceling twice, does nothing. For $read_all_async() the read in flight is canceled and no further reads start.
Errors and timeouts
Timeouts are in seconds and Inf means no limit. Starting, listening, dialing, pinging, and resolving default to 30 seconds, draining to 5, and accepting, reading, and writing wait indefinitely. A timeout only ends that call: the listener or connection stays usable.
timeout = 0 is a non-blocking poll: conn$read(timeout = 0) returns data that has already arrived, lst$accept(timeout = 0) returns a connection that is already waiting, and otherwise they raise tailcat_timeout at once. This differs from tailcat’s C ABI, where a zero timeout is an already expired deadline; see the Timeouts section of ?TailcatServer.
Errors are conditions of class tailcat_error with one subclass, and they carry the integer status and, for reads and writes, the number of bytes transferred before the error:
status |
Class | Meaning |
|---|---|---|
| 1 | tailcat_invalid_argument |
An argument is invalid |
| 2 | tailcat_closed |
The object, or the client or server that owns it, is closed |
| 3 | tailcat_timeout |
The timeout ran out |
| 4 | tailcat_canceled |
The operation was canceled with tailcat_cancel()
|
| 5 | tailcat_failure |
Anything else, including calls made in the wrong state |
Branch on the class or the status rather than on the message:
library(rtailcat)
srv = TailcatServer$new()
lst = srv$listen(8080)
tryCatch(
lst$accept(timeout = 2),
tailcat_timeout = function(e) message("nobody connected")
)
#> nobody connectedThe message starts with a one-line summary, followed by tailcat’s own description when it has one:
lst$accept(timeout = 2)
#> Error in `lst$accept()`:
#> ! Timed out after 2 seconds trying to accept a connection.
#> ℹ tailcat: context deadline exceededOnce a server, client, listener, or connection is closed (or the client or server that owns it is), its methods and fields raise tailcat_closed; only $close(), $format(), $print(), and $is_closed keep working, as do $is_serving and $is_reading (which are then FALSE), a listener’s $server, and a connection’s $client or $server, which give the object that owns it. Objects restored from a saved session (saveRDS(), a saved workspace) are closed, because the handles inside them do not survive serialization.
Blocking calls can be interrupted with Ctrl-C or Esc. If the operation completes at the same moment as the interrupt (for example a read whose data has just arrived), its result is returned and that interrupt is dropped, so no data is lost; press again to interrupt the code that follows.
Security
A tailcat address is a credential. It contains the server’s public key, relay details, and a pre-shared key, and anyone who has it can connect to every port the server listens on. Share it privately and do not commit it, log it, or paste it into chats or issues. Addresses, private keys, and pre-shared keys are returned as tailcat_secret strings: print(), format(), and str() show <redacted>, also after subsetting with [ or combining with c(), as do print() and str() of lists that contain them (format() of a plain list only redacts from R 4.6.0; the lists this package returns redact in every version), while writeLines(), paste(), as.character(), and the functions of this package use the full value. dput(), saveRDS(), and explicit conversions reveal the value, so store secrets deliberately: tailcat_reveal() returns the plain string where you mean to show an address, and tailcat_address_write() and tailcat_keys_save() write files that only you can read. Servers fetch their address from tailcat when asked instead of keeping it in R, so saving a server object does not write its address to disk.
By default each server gets a new key pair and address. To keep an address across restarts, create the keys with keys = tailcat_keygen("~/.config/myapp/keys.json"), which saves them to a file only you can read the first time and loads them from it afterwards, and pass them to TailcatServer$new(key = keys), which uses both the private key and the pre-shared key. To accept only particular clients, give them stable keys (TailcatClient$new(address, key = keys)) and pass those keys, or just their public keys, in allowed_clients or to $allow_client().
Caveats
- The package embeds a Go runtime in the R process. It cannot be unloaded, so the package’s shared object stays loaded until R exits.
- The Go runtime does not survive
fork(). rtailcat refuses to run in a forked child process, which includes the workers ofparallel::mclapply()andparallel::mcparallel(), and themulticorebackend of future. Use separate R processes (for exampleparallel::makePSOCKcluster(), callr, or mirai) instead. - Asynchronous results, including
$serve()handlers, are only delivered while the later event loop runs. - Each operation in flight that can block (every method or function with a
timeout, and fields such as$addressthat ask tailcat), synchronous or asynchronous, running or waiting its turn on a connection, uses its own operating system thread. The optionrtailcat.max_operationscaps how many may be in flight at once in the R process: a positive whole number, orInffor no cap, read each time an operation starts. The default of 1024 stays well below the per-process thread limits of macOS (about 6,000) and typical Linux systems and the 10,000 threads at which the Go runtime ends the process. An operation started at the cap fails at once with atailcat_failureerror that names the option and the limit; the operations already in flight are not affected. See the section on operations in flight in?TailcatServer. - One
$read()returns at most 65536 bytes, whatevernis; loop, or use$read_all(), to read more. - Printing a connection shows its endpoints only until
$close_write()succeeds on it;$info(),$local_address, and$remote_addresskeep working until the connection is closed.
Platform status
R CMD check, including the end-to-end tests against a local relay, runs on GitHub Actions for every push:
| Platform | R versions | Status |
|---|---|---|
| macOS (arm64) | release | Tested in CI and in development |
| Linux (Ubuntu, x86_64) | devel, release, oldrel-1 | Tested in CI |
| Windows (x86_64, Rtools) | release | Tested in CI, except the interrupt and fork tests |
| macOS (x86_64) | Expected to work, untested |
R CMD check reports one expected message about compiled code on every platform (a WARNING on macOS and Linux, a NOTE on Windows): the Go runtime in the package’s shared library refers to stderr, abort, and exit.
Development
devtools::test() runs the end-to-end tests against a local relay on 127.0.0.1, so they need Go but no internet access: a setup file builds upstream’s internal/capitest relay from the small Go module in tests/relay/ (pinned to the same tailcat commit as the package) into a temporary directory and stops it when the run ends. If the relay cannot be built or started, the tests fall back to tailcat’s public relays and say so. Set RTAILCAT_TEST_RELAY to local (fail instead of falling back), public, or auto (the default), or RTAILCAT_SKIP_ONLINE=1 to skip the network tests. On CRAN, and under R CMD check unless NOT_CRAN=true, nothing is built and the network tests are skipped. The GitHub Actions workflow installs Go with actions/setup-go and sets NOT_CRAN, so it runs the full suite with the local relay.