| Crates.io | turn-server-sdk |
| lib.rs | turn-server-sdk |
| version | 0.1.0-beta.5 |
| created_at | 2025-11-16 14:02:38.43959+00 |
| updated_at | 2025-12-06 12:21:14.107728+00 |
| description | Client SDK for interacting with the turn-server gRPC API. |
| homepage | |
| repository | https://github.com/mycrl/turn-rs |
| max_upload_size | |
| id | 1935536 |
| size | 60,124 |
A Rust client SDK for interacting with the turn-server gRPC API exposed by the turn-rs project. This crate provides both client and server utilities for TURN server integration.
Add this to your Cargo.toml:
[dependencies]
turn-server-sdk = "0.1.0-beta.1" # When published
turn-server-protos = "0.1.0-beta.1"
The TurnService client allows you to interact with a running TURN server's gRPC API:
use turn_server_sdk::{TurnServiceļ¼ tonic::transport::Channel};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Connect to the TURN server gRPC endpoint
let channel = Channel::from_static("http://127.0.0.1:3000")
.connect()
.await?;
// Create a client
let mut client = TurnService::new(channel);
// Get server information
let info = client.get_info().await?;
println!("Server version: {}", info.version);
// Query a session by ID
let session = client.get_session("session-id".to_string()).await?;
println!("Session username: {}", session.username);
// Get session statistics
let stats = client.get_session_statistics("session-id".to_string()).await?;
println!("Bytes sent: {}", stats.send_bytes);
// Destroy a session
client.destroy_session("session-id".to_string()).await?;
Ok(())
}
Implement the TurnHooksServer trait to provide custom authentication and handle TURN events:
use turn_server_sdk::{
Credential, TurnHooksServer,
protos::PasswordAlgorithm,
tonic::{Status, transport::Server},
};
struct MyHooksServer;
#[turn_server_sdk::tonic::async_trait]
impl TurnHooksServer for MyHooksServer {
async fn get_password(
&self,
realm: &str,
username: &str,
algorithm: PasswordAlgorithm,
) -> Result<Credential, Status> {
// Implement your authentication logic here
// For example, look up the user in a database
Ok(Credential {
password: "user-password".to_string(),
realm: realm.to_string(),
})
}
async fn on_allocated(&self, id: String, username: String, port: u16) {
println!("Session allocated: id={}, username={}, port={}", id, username, port);
// Handle allocation event (e.g., log to database, update metrics)
}
async fn on_channel_bind(&self, id: String, username: String, channel: u16) {
println!("Channel bound: id={}, username={}, channel={}", id, username, channel);
}
async fn on_create_permission(&self, id: String, username: String, ports: Vec<u16>) {
println!("Permission created: id={}, username={}, ports={:?}", id, username, ports);
}
async fn on_refresh(&self, id: String, username: String, lifetime: u32) {
println!("Session refreshed: id={}, username={}, lifetime={}", id, username, lifetime);
}
async fn on_destroy(&self, id: String, username: String) {
println!("Session destroyed: id={}, username={}", id, username);
// Handle session destruction (e.g., cleanup resources)
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Start the hooks server
let mut server = Server::builder();
let hooks = MyHooksServer;
hooks.start_with_server(
&mut server,
"127.0.0.1:3000".parse()?,
).await?;
Ok(())
}
Generate STUN/TURN authentication passwords for long-term credentials:
use turn_server_sdk::{generate_password, protos::PasswordAlgorithm};
// Generate MD5 password (RFC 5389)
let md5_password = generate_password(
"username",
"password",
"realm",
PasswordAlgorithm::Md5,
);
// Generate SHA256 password (RFC 8489)
let sha256_password = generate_password(
"username",
"password",
"realm",
PasswordAlgorithm::Sha256,
);
// Access the password bytes
match md5_password {
turn_server_sdk::Password::Md5(bytes) => {
println!("MD5 password: {:?}", bytes);
}
turn_server_sdk::Password::Sha256(bytes) => {
println!("SHA256 password: {:?}", bytes);
}
}
The TurnHooksServer trait provides hooks for various TURN server events:
on_allocated: Called when a client allocates a relay porton_channel_bind: Called when a channel is bound to a peeron_create_permission: Called when permissions are created for peerson_refresh: Called when a session is refreshedon_destroy: Called when a session is destroyedAll event handlers are optional and have default no-op implementations. You only need to implement the ones you care about.
Most operations return Result<T, Status> where Status is a gRPC status code. Common error scenarios:
Status::not_found: Session or resource not foundStatus::unavailable: Server is not availableStatus::unauthenticated: Authentication failedStatus::internal: Internal server errorThis crate re-exports:
tonic: The gRPC framework used for communicationprotos: The generated protobuf bindings for TURN server messagesYou can use these directly from turn_server_sdk:
use turn_server_sdk::{tonic, protos};
For more detailed API documentation, see:
This project is licensed under the MIT License - see the LICENSE file for details.