Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2024"

[dependencies]
anyhow = "1"
clap = "3"
env_logger = "0.11.8"
netdev = { version = "0.37.3", default-features = false }
Expand Down
45 changes: 39 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use std::collections::BTreeSet;
use std::net::IpAddr;
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;

use anyhow::{Context as _, Result};
use clap::{App, AppSettings, Arg};
use tokio::net::UdpSocket;
use tokio::signal;
use tokio::time::Duration;
use tokio::io::AsyncWriteExt;
use tokio::net::{UdpSocket, UnixListener};
use turn::Error;
use turn::auth::generate_long_term_credentials;
use turn::auth::*;
use turn::relay::relay_static::RelayAddressGeneratorStatic;
use turn::server::Server;
Expand All @@ -22,6 +25,25 @@ fn public_ips() -> BTreeSet<IpAddr> {
ip_set
}

/// Listens on the Unix socket,
/// returning valid credentials to any connecting client.
async fn socket_loop(path: &Path, shared_secret: &str) -> Result<()> {
let listener = UnixListener::bind(path).context("Failed to bind Unix socket")?;
loop {
match listener.accept().await {
Ok((mut stream, _addr)) => {
let duration = Duration::from_secs(5 * 24 * 3600);
let (username, password) = generate_long_term_credentials(shared_secret, duration)?;
let res = format!("{username}:{password}");
stream.write_all(res.as_bytes()).await?;
}
Err(err) => {
eprintln!("Unix connection failed: {err}.");
}
}
}
}

#[tokio::main]
async fn main() -> Result<(), Error> {
env_logger::init();
Expand All @@ -41,6 +63,13 @@ async fn main() -> Result<(), Error> {
.takes_value(true)
.long("realm")
.help("Realm (defaults to \"webrtc.rs\")"),
)
.arg(
Arg::with_name("socket")
.required(true)
.takes_value(true)
.long("socket")
.help("Unix socket path"),
);

let matches = app.clone().get_matches();
Expand All @@ -52,6 +81,7 @@ async fn main() -> Result<(), Error> {

let port = 3478;
let realm = matches.value_of("realm").unwrap();
let socket_path = Path::new(matches.value_of("socket").unwrap());

let mut conn_configs = Vec::new();
for public_ip in public_ips() {
Expand All @@ -68,7 +98,8 @@ async fn main() -> Result<(), Error> {
conn_configs.push(conn_config);
}

let auth_handler = LongTermAuthHandler::new("north".to_string());
let shared_secret = "north";
let auth_handler = LongTermAuthHandler::new(shared_secret.to_string());

let server = Server::new(ServerConfig {
conn_configs,
Expand All @@ -79,8 +110,10 @@ async fn main() -> Result<(), Error> {
})
.await?;

println!("Waiting for Ctrl-C...");
signal::ctrl_c().await.expect("failed to listen for event");
socket_loop(Path::new(socket_path), shared_secret)
.await
.unwrap();

server.close().await?;

Ok(())
Expand Down