zlayer-tunnel
Secure tunneling for ZLayer services with token-based authentication, WebSocket control channels, and automatic reconnection.
Features
- Token-Based Authentication - Secure tunnel establishment with SHA256-hashed tokens
- TCP/UDP Tunneling - Protocol-agnostic service exposure through tunnels
- WebSocket Control Channel - Binary protocol over WebSocket for coordination and heartbeats
- Automatic Reconnection - Exponential backoff retry logic for resilient connections
- Node-to-Node Tunneling - Connect ZLayer nodes across different networks/datacenters
- On-Demand Access - Cloudflared-style temporary local access to remote services with TTL
- Session Management - Track active connections, bytes transferred, and session expiration
Installation
Add to your Cargo.toml:
[]
= "0.8"
Quick Start
Server Setup
use Arc;
use ;
// Create the registry with port range for service assignment
let registry = new;
// Configure the server
let config = default;
// Create handler with token validator
let validator = new;
let handler = new;
// Handle incoming WebSocket connections
async
Client Setup
use Arc;
use ;
// Configure the client
let config = new
.with_service
.with_service;
// Create the agent with connection callback
let agent = new
.on_connection;
// Run with automatic reconnection
agent.run.await?;
Node-to-Node Tunneling
use ;
// Create manager for this node
let config = default;
let manager = new;
// Define a tunnel from this node to another
let tunnel = new
.with_ports
.with_token;
manager.add_tunnel?;
// Start outbound connection
manager.start_outbound?;
// Check tunnel status
if let Some = manager.get_status
On-Demand Access
use Duration;
use AccessManager;
// Create access manager with default 1-hour TTL
let manager = new
.with_default_ttl;
// Start a session to access a remote service locally
let session = manager.start_session.await?;
println!;
// List active sessions
for info in manager.list_sessions
// Clean up expired sessions
manager.cleanup_expired;
Protocol Overview
The tunnel uses a compact binary message format over WebSocket:
+----------+----------+----------------------------------+
| Type(1) | Len(4) | Payload (variable) |
+----------+----------+----------------------------------+
Message Types
| Type | Code | Direction | Description |
|---|---|---|---|
| Auth | 0x01 | C->S | Client authentication request |
| AuthOk | 0x02 | S->C | Authentication success with tunnel ID |
| AuthFail | 0x03 | S->C | Authentication failure with reason |
| Register | 0x10 | C->S | Service registration request |
| RegisterOk | 0x11 | S->C | Registration success with service ID |
| RegisterFail | 0x12 | S->C | Registration failure |
| Connect | 0x20 | S->C | Incoming connection notification |
| ConnectAck | 0x21 | C->S | Connection accepted |
| ConnectFail | 0x22 | C->S | Connection rejected |
| Heartbeat | 0x30 | Bidirectional | Keepalive with timestamp |
| HeartbeatAck | 0x31 | Bidirectional | Heartbeat response |
| Unregister | 0x40 | C->S | Service unregistration |
| Disconnect | 0x41 | S->C | Server disconnect notification |
Connection Flow
- Client connects via WebSocket to server's control path
- Authentication: Client sends
Authwith token, server respondsAuthOk/AuthFail - Service registration: Client sends
Registerfor each service, server assigns ports - Heartbeat loop: Server sends periodic
Heartbeat, client responds withHeartbeatAck - Connection handling: Server sends
Connecton incoming connections, client acknowledges
API Reference
Core Types
| Type | Description |
|---|---|
TunnelRegistry |
Server-side registry for active tunnels and services |
ControlHandler |
WebSocket control channel handler |
TunnelAgent |
Client-side tunnel agent with auto-reconnect |
NodeTunnelManager |
Manager for node-to-node tunnel connections |
AccessManager |
On-demand access session manager |
Configuration
| Type | Description |
|---|---|
TunnelServerConfig |
Server settings (port range, heartbeat, limits) |
TunnelClientConfig |
Client settings (URL, token, services) |
ServiceConfig |
Service definition (name, protocol, ports) |
Constants
| Constant | Value | Description |
|---|---|---|
PROTOCOL_VERSION |
1 | Current protocol version |
MAX_MESSAGE_SIZE |
65536 | Maximum message size (64KB) |
HEADER_SIZE |
5 | Message header size in bytes |
License
Apache-2.0 - See LICENSE for details.