#stun

rusturn

A Rust implementation of TURN server and client

6 releases

0.0.7 Feb 23, 2024
0.0.5 Jan 17, 2024
0.0.4 Mar 19, 2022
0.0.3 Jan 30, 2019
0.0.1 Sep 18, 2018

#297 in Asynchronous

Download history 21/week @ 2024-07-28 3/week @ 2024-09-22 4/week @ 2024-09-29

60 downloads per month

MIT license

83KB
2K SLoC

rusturn

rusturn Documentation Actions Status Coverage Status License: MIT

A Rust implementation of TURN server and client.

Documentation

Examples

use futures::Future;
use rustun::message::Request;
use rustun::transport::StunUdpTransporter;
use rusturn::auth::AuthParams;
use rusturn::transport::UdpOverTurnTransporter;
use stun_codec::{rfc5389, MessageDecoder, MessageEncoder};

let client_auth_params = AuthParams::new("foo", "bar")?;
let server_auth_params =
    AuthParams::with_realm_and_nonce("foo", "bar", "baz", "qux")?;

// STUN server (peer)
let stun_server = fibers_global::execute(rustun::server::UdpServer::start(
    fibers_global::handle(),
    "127.0.0.1:0".parse().unwrap(),
    rustun::server::BindingHandler,
))?;
let stun_server_addr = stun_server.local_addr();
fibers_global::spawn(stun_server.map(|_| ()).map_err(|e| panic!("{}", e)));

// TURN server
let turn_server = fibers_global::execute(rusturn::server::UdpServer::start(
    "127.0.0.1:0".parse().unwrap(),
    server_auth_params,
))?;
let turn_server_addr = turn_server.local_addr();
fibers_global::spawn(turn_server.map_err(|e| panic!("{}", e)));

// TURN client
let turn_client = fibers_global::execute(rusturn::client::UdpClient::allocate(
    turn_server_addr,
    client_auth_params
))?;
let transporter =
    UdpOverTurnTransporter::<_, MessageEncoder<_>, MessageDecoder<_>>::new(turn_client);

// STUN client (over TURN)
let stun_channel = rustun::channel::Channel::new(StunUdpTransporter::new(transporter));
let stun_client = rustun::client::Client::new(&fibers_global::handle(), stun_channel);

// BINDING request
let request = Request::<rfc5389::Attribute>::new(rfc5389::methods::BINDING);
let response = fibers_global::execute(
    stun_client.call(stun_server_addr, request)
)?;
assert!(response.is_ok(), "{:?}", response);

References

Dependencies

~4.5MB
~87K SLoC