#[cfg(any(feature = "rustls", feature = "dimpl"))]
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::net::SocketAddr;
pub use stun_proto::auth::Feature;
use stun_proto::types::{AddressFamily, message::IntegrityAlgorithm};
use crate::candidate::TransportType;
pub use turn_client_proto::types::TurnCredentials;
#[cfg(feature = "dimpl")]
use turn_client_dimpl::TurnClientDimpl;
#[cfg(feature = "openssl")]
use turn_client_openssl::TurnClientOpensslTls;
use turn_client_proto::tcp::TurnClientTcp;
use turn_client_proto::udp::TurnClientUdp;
#[cfg(feature = "rustls")]
use turn_client_rustls::TurnClientRustls;
#[derive(Debug, Clone)]
pub struct TurnConfig {
turn_config: turn_client_proto::api::TurnConfig,
client_transport: TransportType,
turn_server: SocketAddr,
tls_config: Option<TurnTlsConfig>,
}
impl TurnConfig {
pub fn new(
client_transport: TransportType,
server_addr: SocketAddr,
credentials: TurnCredentials,
) -> Self {
Self {
turn_config: turn_client_proto::api::TurnConfig::new(credentials),
client_transport,
turn_server: server_addr,
tls_config: None,
}
}
pub fn set_tls_config(&mut self, config: TurnTlsConfig) {
self.tls_config = Some(config);
}
pub fn tls_config(&self) -> Option<&TurnTlsConfig> {
self.tls_config.as_ref()
}
pub fn addr(&self) -> SocketAddr {
self.turn_server
}
pub fn client_transport(&self) -> TransportType {
self.client_transport
}
pub fn set_allocation_transport(&mut self, allocation_transport: TransportType) {
self.turn_config
.set_allocation_transport(allocation_transport);
}
pub fn allocation_transport(&self) -> TransportType {
self.turn_config.allocation_transport()
}
pub fn add_address_family(&mut self, family: AddressFamily) {
self.turn_config.add_address_family(family);
}
pub fn set_address_family(&mut self, family: AddressFamily) {
self.turn_config.set_address_family(family);
}
pub fn address_families(&self) -> &[AddressFamily] {
self.turn_config.address_families()
}
pub fn credentials(&self) -> &TurnCredentials {
self.turn_config.credentials()
}
pub fn add_supported_integrity(&mut self, integrity: IntegrityAlgorithm) {
self.turn_config.add_supported_integrity(integrity);
}
pub fn set_supported_integrity(&mut self, integrity: IntegrityAlgorithm) {
self.turn_config.set_supported_integrity(integrity);
}
pub fn supported_integrity(&self) -> &[IntegrityAlgorithm] {
self.turn_config.supported_integrity()
}
pub fn set_anonymous_username(&mut self, anon: Feature) {
self.turn_config.set_anonymous_username(anon);
}
pub fn anonymous_username(&self) -> Feature {
self.turn_config.anonymous_username()
}
pub(crate) fn turn_config(&self) -> &turn_client_proto::api::TurnConfig {
&self.turn_config
}
}
#[derive(Debug, Clone)]
pub enum TurnTlsConfig {
#[cfg(feature = "rustls")]
Rustls(RustlsTurnConfig),
#[cfg(feature = "openssl")]
Openssl(OpensslTurnConfig),
#[cfg(feature = "dimpl")]
Dimpl(DimplTurnConfig),
}
#[cfg(feature = "rustls")]
use rustls::{ClientConfig, pki_types::ServerName};
#[cfg(feature = "rustls")]
#[derive(Debug, Clone)]
pub struct RustlsTurnConfig {
config: Arc<ClientConfig>,
server_name: ServerName<'static>,
}
#[cfg(feature = "rustls")]
impl RustlsTurnConfig {
pub fn new(config: Arc<ClientConfig>, server_name: ServerName<'static>) -> Self {
Self {
config,
server_name,
}
}
pub fn client_config(&self) -> Arc<ClientConfig> {
self.config.clone()
}
pub fn server_name(&self) -> ServerName<'static> {
self.server_name.clone()
}
}
#[cfg(feature = "rustls")]
impl From<RustlsTurnConfig> for TurnTlsConfig {
fn from(value: RustlsTurnConfig) -> Self {
Self::Rustls(value)
}
}
#[cfg(feature = "openssl")]
#[derive(Debug, Clone)]
pub struct OpensslTurnConfig {
ssl: openssl::ssl::SslContext,
}
#[cfg(feature = "openssl")]
impl OpensslTurnConfig {
pub fn new(ssl: openssl::ssl::SslContext) -> Self {
Self { ssl }
}
pub fn ssl_context(&self) -> &openssl::ssl::SslContext {
&self.ssl
}
}
#[cfg(feature = "openssl")]
impl From<OpensslTurnConfig> for TurnTlsConfig {
fn from(value: OpensslTurnConfig) -> Self {
Self::Openssl(value)
}
}
#[cfg(feature = "dimpl")]
#[derive(Debug, Clone)]
pub struct DimplTurnConfig {
config: Arc<dimpl::Config>,
}
#[cfg(feature = "dimpl")]
impl DimplTurnConfig {
pub fn new(config: Arc<dimpl::Config>) -> Self {
Self { config }
}
pub fn config(&self) -> Arc<dimpl::Config> {
self.config.clone()
}
}
#[cfg(feature = "dimpl")]
impl From<DimplTurnConfig> for TurnTlsConfig {
fn from(value: DimplTurnConfig) -> Self {
Self::Dimpl(value)
}
}
#[cfg(all(feature = "openssl", feature = "rustls", feature = "dimpl"))]
turn_client_proto::impl_client!(
pub TurnClient,
(Udp, TurnClientUdp),
(Tcp, TurnClientTcp),
(Openssl, TurnClientOpensslTls),
(Rustls, TurnClientRustls),
(Dimpl, TurnClientDimpl)
);
#[cfg(all(feature = "openssl", not(feature = "rustls"), feature = "dimpl"))]
turn_client_proto::impl_client!(
pub TurnClient,
(Udp, TurnClientUdp),
(Tcp, TurnClientTcp),
(Openssl, TurnClientOpensslTls),
(Dimpl, TurnClientDimpl)
);
#[cfg(all(not(feature = "openssl"), feature = "rustls", feature = "dimpl"))]
turn_client_proto::impl_client!(
pub TurnClient,
(Udp, TurnClientUdp),
(Tcp, TurnClientTcp),
(Rustls, TurnClientRustls),
(Dimpl, TurnClientDimpl)
);
#[cfg(all(not(feature = "openssl"), not(feature = "rustls"), feature = "dimpl"))]
turn_client_proto::impl_client!(
pub TurnClient,
(Udp, TurnClientUdp),
(Tcp, TurnClientTcp),
(Dimpl, TurnClientDimpl)
);
#[cfg(all(feature = "openssl", feature = "rustls", not(feature = "dimpl")))]
turn_client_proto::impl_client!(
pub TurnClient,
(Udp, TurnClientUdp),
(Tcp, TurnClientTcp),
(Openssl, TurnClientOpensslTls),
(Rustls, TurnClientRustls)
);
#[cfg(all(feature = "openssl", not(feature = "rustls"), not(feature = "dimpl")))]
turn_client_proto::impl_client!(
pub TurnClient,
(Udp, TurnClientUdp),
(Tcp, TurnClientTcp),
(Openssl, TurnClientOpensslTls)
);
#[cfg(all(not(feature = "openssl"), feature = "rustls", not(feature = "dimpl")))]
turn_client_proto::impl_client!(
pub TurnClient,
(Udp, TurnClientUdp),
(Tcp, TurnClientTcp),
(Rustls, TurnClientRustls)
);
#[cfg(all(
not(feature = "openssl"),
not(feature = "rustls"),
not(feature = "dimpl")
))]
turn_client_proto::impl_client!(
pub TurnClient,
(Udp, TurnClientUdp),
(Tcp, TurnClientTcp)
);