Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

VRelay

The relay of the Veydan network, compatible with Nostr. One program, vrelay, without state of its own, in two modes:

  • vrelay serve holds the connections of the clients: sign-in (NIP-42), the read and write policy, the subscriptions. It hands every event to the queue.
  • vrelay write takes the events from the queue (NATS JetStream), stores them (Cassandra) and hands them to the subscribers.

It stands in place of the relay site of nginx, vguard, strfry, its router and its timers.

State: 0.1.0, the first release.

Layout

crates/               the crates of the relay; the binary is crates/vrelay
deploy/               the image (Dockerfile), the compose stack of one server
                      (compose.yaml, compose/), the systemd units, and the
                      files of a cluster across regions (cluster/, its README)
scripts/              build
spec/                 the design, the compatibility, the security, the
                      cluster, the tuning and the statistics endpoint
spec/config.md        the settings reference (environment of vrelay.env)

Working on it

make test            tests
make check           clippy, warnings are errors
make run             vrelay serve here with vrelay.env (MODE=write for the writer)

The Rust version is the one rust-toolchain.toml names. Settings come from the environment only: on a server from /opt/vrelay/etc/vrelay.env, under docker compose from an env file. The reference is spec/config.md.

Run it locally

One Cassandra and one NATS in docker, then the writer and the relay from this folder. The driver of Cassandra connects to the address the node advertises, so the native port is the same number inside and out; the datacenter is dc1, as in the compose stack (VRELAY_STORE_LOCAL_DC; a name no node reports stops the start and names those there are):

docker run -d --name vrelay-it-nats -p 127.0.0.1:24222:4222 nats:2.15.3 -js
docker run -d --init --name vrelay-it-cassandra -p 127.0.0.1:29042:29042 \
    -e MAX_HEAP_SIZE=1G -e CASSANDRA_BROADCAST_RPC_ADDRESS=127.0.0.1 \
    -e CASSANDRA_DC=dc1 -e CASSANDRA_ENDPOINT_SNITCH=GossipingPropertyFileSnitch \
    -e JVM_EXTRA_OPTS="-Dcassandra.native_transport_port=29042 -XX:+AlwaysPreTouch" cassandra:5.0.9
# about a minute: docker logs vrelay-it-cassandra | grep "Startup complete"

head -c 24 /dev/urandom | od -An -tx1 | tr -d ' \n' > /tmp/app.key
head -c 24 /dev/urandom | od -An -tx1 | tr -d ' \n' > /tmp/peer.key
cat > /tmp/vrelay.env <<EOF
VRELAY_NET_LISTEN=127.0.0.1:27778
VRELAY_NET_HTTP_LISTEN=127.0.0.1:27779
VRELAY_GATE_APP_KEY_FILE=/tmp/app.key
VRELAY_GATE_PEER_KEY_FILE=/tmp/peer.key
VRELAY_AUTH_RELAY_HOSTS=127.0.0.1,localhost
VRELAY_STORE_NODES=127.0.0.1:29042
VRELAY_BUS_SERVERS=nats://127.0.0.1:24222
EOF
export VRELAY_ENV_FILE=/tmp/vrelay.env
source scripts/env.sh && cargo build --release -p vrelay
B=$(cargo_target_dir)/release/vrelay       # or the path cargo printed
$B config check --mode write && $B migrate  # the tables and the stream
$B write &                                  # the writer
$B serve &                                  # the relay: ws://127.0.0.1:27778/?key=<app key>
$B probe                                    # 0: it answers

docker rm -f vrelay-it-cassandra vrelay-it-nats when done.

Run with Docker

Your own relay on one machine: vrelay serve and vrelay write, NATS, Cassandra, and Caddy for TLS, from published images; nothing is built. You need Docker with Compose, 8 GB of memory (the limits of the containers add up to under 5 GB; Cassandra takes about 1.7 GB when idle), a domain name whose DNS record points at the machine, and ports 80 and 443 open to the internet: Caddy gets and renews the certificate of that name by itself (Let's Encrypt).

cd deploy
cp compose/.env.example .env      # set RELAY_HOST, and ADMIN_PUBKEY; read the rest
docker compose up -d --wait       # about 90 s the first time, 40 s after
docker compose logs relay-init    # the card: the address, the app key, the link for the app

The first start makes the passwords and the keys (secrets-init), turns the default login of Cassandra off and gives vrelay a role of its own (cassandra-init, cassandra-grant), makes the tables and the streams (vrelay-migrate), then starts the writer, the relay and Caddy. The card again, at any time: docker compose run --rm --no-deps relay-init card. The settings are in .env, one file with comments; every other setting of the relay (spec/config.md) can be added to environment: of vrelay in compose.yaml.

  • Open to the internet: 80 and 443 (Caddy) alone. Cassandra (9042), NATS (4222, 8222) and the plain socket of vrelay (7778, for a proxy of your own) listen on 127.0.0.1. No container runs as root but the two one-shots that make the passwords and own the folders; none sees the docker socket.
  • The data: docker volumes of the project veydan-relay: cassandra-data (the events), nats-data (the queue), secrets (the passwords and the keys), vrelay-access (members and invites), caddy-data (the certificate). docker compose down keeps them; docker compose down -v deletes them all, the keys too.
  • Backup: stop the stack and save the volumes, e.g. docker compose stop, then for each volume docker run --rm -v veydan-relay_<volume>:/v:ro -v "$PWD":/b nats:2.15.3-alpine tar -C /v -czf /b/<volume>.tgz ., and docker compose up -d. The events alone, while it runs: docker compose run --rm --no-deps -T vrelay-write export > events.jsonl.
  • Update: put the new version in RELAY_TAG of .env, then docker compose pull && docker compose up -d --wait; vrelay-migrate runs at every start and makes what a new version adds. To pin the image by its digest as well: RELAY_TAG=<version>@sha256:<digest> (docker buildx imagetools inspect rookbeam/vrelay:<version>).
  • A proxy of your own instead of Caddy: COMPOSE_PROFILES=relay in .env, the proxy to 127.0.0.1:7778, and its address in RELAY_TRUSTED_PROXIES.
  • Team or channel: ACCESS_MODE in .env; the members and the invites with docker compose exec vrelay /vrelay access --help; a card with an invite: docker compose run --rm --no-deps relay-init card <code>.

Moving from strfry and back

Before the switch the database of strfry is poured into the store; for a rollback, what the new relay holds is poured back into strfry. Both ways are JSON lines, one event a line. With the env file of vrelay write (config check --mode import checks what both read):

# strfry -> vrelay, after `vrelay migrate`
strfry export > events.jsonl
vrelay config check --mode import
vrelay import events.jsonl          # or: strfry export | vrelay import -

# vrelay -> strfry
vrelay export > events.jsonl        # --since <unix> / --until <unix>: by created_at
strfry import < events.jsonl

vrelay import puts each event as the writer puts a job of the queue, straight to Cassandra, with nothing published live. It checks the id and the signature (--trust: not), the size and the tags; the window of time of a fresh event does not apply. It passes over ephemeral kinds, the kinds kept in memory only (PRESENCE_KINDS), an event past its expiration and one of RETENTION_KINDS past its term; the others of those kinds are kept for what is left of the term, counted from created_at. The newest version of an address stays whatever the order of the lines. The deletions (kind 5) are put after every other event: a file is read twice, a stream (-) once, with its deletions held in memory. The regular events a deletion removes are told to NATS as the writer tells them, so a copy sent again later is refused (--no-bus: only the store knows them). A second run of the same file stores nothing. At the end it prints what became of each line: read, stored, duplicate, older, deleted, removed by deletions, skipped by reason, failed; the exit is 1 when any event failed.

vrelay export walks the whole event table, a page of STORE_PAGE_SIZE at a time: every row of the cluster is read once, so it costs a full read of the table, and --since/--until save only the writing. Deleted, replaced and expired events are not written.

Members and invites

A team or a channel server (VRELAY_ACCESS_MODE not open) keeps its members and invites in the files of VRELAY_ACCESS_MEMBERS_FILE, VRELAY_ACCESS_INVITES_FILE and VRELAY_ACCESS_SECRET_FILE, in the formats of vguard: a server moved from vguard keeps them, and a code of either is a code of the other (vi1.<id>.<mac>). vrelay access is vguard's command line for them, with the env file of vrelay serve (config check --mode access checks what it reads):

vrelay access mode                            # open, team or channel (also: vrelay mode)
vrelay access members list                    # one hex key a line
vrelay access members add <npub|hex>          # prints the hex key
vrelay access members remove <npub|hex>
vrelay access invite [--uses N] [--days D] [--label L]   # the code, one line; 1 use, 7 days
vrelay access invite new [--uses N] [--days D] [--label L]
vrelay access invite list                     # id, uses left, expires, made, label; tabs
vrelay access invite revoke <id>

Each change is made under access.lock beside the members file, the lock the access API takes for a join, and written as a new file renamed over the old one, with the owner and the mode of the file it replaces (so a run as root leaves the files readable by the relay). The running relay reads the members again within VRELAY_POLICY_REREAD_SECS, or at once on SIGHUP (systemctl reload vrelay); it reads the invites at every request of the API. Exits: 0 done, 1 it did not work, 2 the settings do not allow it (an open server, a setting missing).

On a server

make release builds one static binary, dist/vrelay, with its checksum and release name. By hand: deploy/vrelay.service and deploy/vrelay-write.service under /opt/vrelay.

Licence

VRelay is under the PolyForm Perimeter License 1.0.1, the licence of every part of Veydan: LICENSE is the text that counts, and LICENSE-SUMMARY.md says it in short. The crates it is built from are listed with their licences in THIRD-PARTY-LICENSES.md (make third-party).

Developed by Rookbeam Technologies LLC, USA

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages