Building on starmole

On this page

A program can be a starmole node itself and serve protocols of its own next to shell, forwards and the rest. It gets everything starmole daemon does: its own key and certificate, renewal, membership, revocations, reloading config.toml. Other machines reach it like any other node, by name or node id.

This is the way to build something like a sign-in service or a gateway for the network without changing starmole.

A node that says hello

starmole isn't published yet, so depend on a checkout of it:

[dependencies]
starmole = { path = "../starmole/crates/daemon" }
tokio = { version = "1", features = ["full"] }
anyhow = "1"
use starmole::iroh::endpoint::Connection;
use starmole::iroh::protocol::{AcceptError, ProtocolHandler};
use starmole::{Access, Builder, ConfigDir, Peers};

const ALPN: &[u8] = b"example/hello/1";

#[derive(Debug, Clone)]
struct Hello(Peers);

impl ProtocolHandler for Hello {
    async fn accept(&self, conn: Connection) -> Result<(), AcceptError> {
        let name = self
            .0
            .identity(&conn.remote_id())
            .map_or_else(|| conn.remote_id().fmt_short().to_string(), |id| id.name);
        let mut send = conn.open_uni().await.map_err(AcceptError::from_err)?;
        send.write_all(format!("hello {name}\n").as_bytes())
            .await
            .map_err(AcceptError::from_err)?;
        send.finish().map_err(AcceptError::from_err)?;
        conn.closed().await;
        Ok(())
    }
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let dir = ConfigDir::new("/var/lib/hello");
    // The daemon's socket, so `starmole status`, `join` and the rest work with this node.
    let socket = starmole::bind_socket(&dir.path().join("daemon.sock")).await?;
    let config = dir.load_config()?;
    let builder = Builder::new(dir, config);
    let peers = builder.peers();
    let node = builder.serve(ALPN, Access::Members, Hello(peers)).start().await?;
    tokio::select! {
        served = node.serve(socket) => served?,
        _ = tokio::signal::ctrl_c() => {}
    }
    node.shutdown().await
}

The node uses a config directory of its own, set up like the daemon's, and serves the same socket, so the starmole command works with it. To give it a key and join it to the network:

export STARMOLE_CONFIG_DIR=/var/lib/hello STARMOLE_SOCKET=/var/lib/hello/daemon.sock
starmole init
hello &                       # your program
starmole join starmole1:...  # an invite from your issuer

After that it renews its certificate and keeps up with the network on its own. The full example is in crates/daemon/examples/hello.rs.

Who gets in

serve takes who may connect:

Access::Members Machines with a certificate this node accepts, or listed under [[peer]] in its config. Everyone else is turned away before your code runs.
Access::Open Anyone. Your code decides everything. Use it only for something every machine has to reach first, like signing in.

Use Access::Members unless you have a reason not to.

Inside the handler, Peers says who's connecting:

A node built this way can't take over protocols starmole already serves. Trying to serve starmole/shell/1 stops the program when it starts.

Inviting and retagging from code

A node that holds network.key or an issuer certificate can do what starmole invite, retag and revoke do, from your program:

Together they cover machines that admit themselves through a program of yours: invite each new machine with a tag like pending that no rule matches, let it report to your node over your own protocol, and retag it once it's accepted.

Edit this page on GitHub