Hightower Client Library
A Rust library for building applications that connect to Hightower gateways with integrated WireGuard transport.
Overview
This library provides a complete solution for connecting to Hightower gateways. It handles everything internally:
- WireGuard transport creation and management
- Certificate/keypair generation
- Network discovery via STUN using actual bound ports
- Gateway registration
- Peer configuration
- Automatic deregistration on disconnect
- Connection persistence and automatic restoration
You only provide: gateway URL and auth token You get: a working transport, node ID, and assigned IP
Everything else is handled automatically!
Connection Persistence (New in 0.1.1)
By default, connections are automatically persisted to ~/.hightower/gateway/<gateway>/. When you reconnect to the same gateway:
- The library reuses your stored WireGuard keys (same identity)
- No re-registration needed with the gateway
- Same node ID across application restarts
- Only
disconnect()removes the stored connection
This makes your application's network identity stable across restarts!
Installation
Add this to your Cargo.toml:
[]
= "0.1.1"
Usage
Basic Connection
use HightowerConnection;
async
Custom Gateway URL
use HightowerConnection;
async
Examples
The library includes several examples:
Simple Connection
Connection Persistence Demo
# optional
Demonstrates how connections are automatically restored across application restarts.
Storage Modes
# optional
Shows all available storage modes: default persistence, ephemeral, custom directory, and forced fresh registration.
Custom Gateway URL
HTTPS Gateway
Auto Deregistration
API Documentation
HightowerConnection
The main connection struct with integrated WireGuard transport.
Methods
-
async fn connect(gateway_url: impl Into<String>, auth_token: impl Into<String>) -> Result<Self, ClientError>Connects to a Hightower gateway with a custom URL. The URL must include the scheme (http:// or https://).
With persistence (default):
- Checks for existing connection in
~/.hightower/gateway/<gateway>/ - If found, restores using stored keys (same identity)
- If not found, generates new keypair and registers
Handles everything internally:
- Generates or restores WireGuard keypair
- Creates transport server on 0.0.0.0:0
- Discovers network info via STUN using actual bound port
- Registers with gateway (or reuses existing registration)
- Adds gateway as peer
- Persists connection for future use
Returns a ready-to-use connection with working transport.
- Checks for existing connection in
-
async fn connect_with_auth_token(auth_token: impl Into<String>) -> Result<Self, ClientError>Connects using the default gateway (
http://127.0.0.1:8008). Includes automatic persistence. -
async fn connect_ephemeral(gateway_url: impl Into<String>, auth_token: impl Into<String>) -> Result<Self, ClientError>Connects without persistence. Always creates fresh registration, nothing stored to disk.
-
async fn connect_with_storage(gateway_url: impl Into<String>, auth_token: impl Into<String>, storage_dir: impl Into<PathBuf>) -> Result<Self, ClientError>Connects using a custom storage directory instead of the default location.
-
async fn connect_fresh(gateway_url: impl Into<String>, auth_token: impl Into<String>) -> Result<Self, ClientError>Forces a fresh registration even if a stored connection exists. Deletes any existing stored connection for this gateway.
-
fn node_id(&self) -> &strReturns the node ID assigned by the gateway.
-
fn assigned_ip(&self) -> &strReturns the IP address assigned by the gateway.
-
fn transport(&self) -> &TransportServerReturns the transport for sending/receiving data.
-
async fn disconnect(self) -> Result<(), ClientError>Disconnects from the gateway and automatically deregisters using the internal token.
TransportServer
Wrapper around the WireGuard transport.
Methods
-
fn server(&self) -> &ServerGet a reference to the underlying hightower-wireguard
Server. Use this to access the full transport API for sending/receiving data.
ClientError
Error types returned by the library.
Variants:
Configuration(String)- Invalid configuration (e.g., empty endpoint, invalid URL)Request(reqwest::Error)- HTTP request failedGatewayError { status: u16, message: String }- Gateway returned errorInvalidResponse(String)- Unexpected response formatNetworkDiscovery(String)- Failed to discover network info via STUNTransport(String)- Transport creation or operation failedStorage(String)- Storage operation failed (persistence)
Testing
Run the test suite:
License
MIT