#ip-address #compile #userspace #tailscale #listen #libtailscale #tailnet

tsnet

Compile Tailscale into your program and get an entirely userspace IP address on a tailnet

1 unstable release

0.1.0 Mar 12, 2023

#5 in #tailscale

40 downloads per month

BSD-3-Clause

35KB
875 lines

tsnet - bindings for libtailscale.

libtailscale is a C library that embeds Tailscale into a process. tsnet is a Rust crate wrapping libtailscale and exposing a Rust-y API on top.

Use this library to compile Tailscale into your program and get an IP address on a tailnet, entirely from userspace.

Requirements

  • Rust compiler & Cargo
  • Go v1.20 or higher

Getting started

After running cargo init add the following lines to your Cargo.toml file:

tsnet = "0.1.0"

Development

Build with

cargo build

Run tests with

cargo test

Run the examples with

cargo run --example echo_server
cargo run --example echo_client

Bugs

Please file any issues about this code or the hosted service on the issue tracker.

License

BSD 3-Clause for this repository, see LICENSE.


lib.rs:

Compile Tailscale into your program and get an entirely userspace IP address on a tailnet.

From here you can listen for other programs on your tailnet dialing you, or connect directly to other services.

Based on libtailscale, the C wrapper around the Tailscale Go package. See https://pkg.go.dev/tailscale.com/tsnet for Go module docs.

Examples

Server

use std::net::TcpStream;
use tsnet::{ServerBuilder, Network};

fn main() {
    let ts = ServerBuilder::new().ephemeral().redirect_log().build().unwrap();
    let ln = ts.listen(Network::Tcp, ":1999").unwrap();

    for conn in ln {
        match conn {
            Ok(conn) => handle_client(conn),
            Err(err) => panic!("{err}"),
        }
    }
}

fn handle_client(mut stream: TcpStream) {
  // ...
}

Client

use std::{env, io::Write};

use tsnet::{ServerBuilder, Network};

fn main() {
    let srv = ServerBuilder::new()
        .ephemeral()
        .build()
        .unwrap();

    let mut conn = srv.connect(Network::Tcp, "echo-server:1999").unwrap();
    write!(conn, "This is a test of the Tailscale connection service.\n").unwrap();
}

Dependencies

~2–13MB
~135K SLoC