VoiceOptions is a struct of its own, no longer an alias of the TeamSpeak link's options: identity is an IdentityBlob and client_version a spec for ts::versions::resolve, which rides the address as the link parameter client_version and which the TeamSpeak connector resolves (a spec it refuses fails the connect in words). SessionLink builds the connect parameters (link/connect.rs: the bookmark's link, the hello, the credential of the identity's kind, now also a Voelin key's signer) and session.rs holds it directly: the ServerLink trait between them was redundant. The trait lives on in the TeamSpeak module (ts/ts_link.rs, with the TeamSpeak-typed ConnectOptions) for TsLink and the in-memory link of the tests. ServerLink, TsLink, LinkCtx, LinkCmd, LinkEvent and Remote are crate-private, Engine::start_with_link is gone, and the three tests that needed the inner link (tests/link_fake.rs) are unit tests of link/ now. The rg over the crate for tsclientlib, tsproto and voelin_query, outside ts/ and the three files that use them privately (gateway.rs, query.rs, stream.rs), is empty. The UI and voelinctl get compile fixes: the identity blob and the spec string. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
257 lines
8.8 KiB
Rust
257 lines
8.8 KiB
Rust
//! The inner link of the TeamSpeak side: [`ServerLink`], the trait that
|
|
//! [`TsLink`] implements, and the [`ConnectOptions`] it connects with.
|
|
//!
|
|
//! The engine does not know this vocabulary: it talks to a
|
|
//! `voelin_proto::link::Session`. The `ts_session` module makes a `Session`
|
|
//! of a [`ServerLink`] (`LinkSession`), and the engine's tests give it a link
|
|
//! of their own, an in-memory one. [`TsLink`] connects as a normal TeamSpeak 3
|
|
//! or 6 client with `tsclientlib` and
|
|
//! runs the connection ([`super::voice`]) with everything that rides on it:
|
|
//! chat, voice, files, pokes, offline messages and, on TeamSpeak 6, the
|
|
//! stream task. Which product the server is (and what it can do) is decided
|
|
//! there, from the version it sends, and reported in
|
|
//! [`LinkEvent::Connected`](crate::link::LinkEvent::Connected).
|
|
//!
|
|
//! Everything it uses (`voice.rs`, `book.rs`, `stream.rs`, `versions.rs`,
|
|
//! `identity.rs`) is TeamSpeak's and leaves this crate together with it:
|
|
//! the plan moves `TsLink` into the TeamSpeak gateway, `voelin-gateway-ts`
|
|
//! of VoelinGateWay.
|
|
|
|
use std::sync::Arc;
|
|
|
|
use tokio::sync::{mpsc, watch};
|
|
use voelin_stream::PeerConfig;
|
|
|
|
use super::voice;
|
|
use crate::link::{LinkCmd, LinkCtx, LinkEvent};
|
|
|
|
/// Starts the connection of a link: the contract of `crate::link` for the
|
|
/// vocabulary ([`LinkCmd`] in, [`LinkEvent`] out) holds for what a link does
|
|
/// with them, and the module documentation of `link/session_link.rs` states
|
|
/// it.
|
|
///
|
|
/// One link serves every session of an engine; [`spawn`](Self::spawn) starts
|
|
/// one connection.
|
|
pub(crate) trait ServerLink: Send + Sync + 'static {
|
|
/// Connect to the server `options` names and run the connection on its
|
|
/// own task: take commands from `cmds`, report through `events`, and end
|
|
/// the connection with [`LinkEvent::Disconnected`]. Does not block.
|
|
fn spawn(
|
|
&self,
|
|
options: ConnectOptions,
|
|
ctx: LinkCtx,
|
|
cmds: mpsc::UnboundedReceiver<LinkCmd>,
|
|
events: mpsc::UnboundedSender<LinkEvent>,
|
|
);
|
|
}
|
|
|
|
/// What a TeamSpeak connection needs to know: the server, who to be, where
|
|
/// to join. The reverse of what the engine's `ConnectParams` carry
|
|
/// (`ts_session::inner_options`).
|
|
#[derive(Clone, Debug)]
|
|
pub(crate) struct ConnectOptions {
|
|
pub address: String,
|
|
pub nickname: String,
|
|
/// The identity to connect with; none generates one.
|
|
pub identity: Option<tsclientlib::Identity>,
|
|
/// Signed compatibility version; `None` selects the native platform's tuple.
|
|
pub client_version: Option<tsclientlib::Version>,
|
|
pub server_password: Option<String>,
|
|
/// Channel path to join, e.g. `Lobby/Sub`, or `/<id>`.
|
|
pub channel: Option<String>,
|
|
/// The password of `channel`.
|
|
pub channel_password: Option<String>,
|
|
/// A privilege key to use when connecting.
|
|
pub token: Option<String>,
|
|
/// Open the audio devices (capture and playback).
|
|
pub audio: bool,
|
|
/// Network settings for stream peer connections.
|
|
pub stream_peer: PeerConfig,
|
|
}
|
|
|
|
impl ConnectOptions {
|
|
/// Options for `address` and `nickname`, the rest as a test leaves it.
|
|
#[cfg(test)]
|
|
pub(crate) fn new(address: impl Into<String>, nickname: impl Into<String>) -> Self {
|
|
Self {
|
|
address: address.into(),
|
|
nickname: nickname.into(),
|
|
identity: None,
|
|
client_version: None,
|
|
server_password: None,
|
|
channel: None,
|
|
channel_password: None,
|
|
token: None,
|
|
audio: false,
|
|
stream_peer: PeerConfig::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The main myTeamSpeak account of an application, as its connections show
|
|
/// it to the servers: the proof of the myTS ID, and what servers are shown
|
|
/// of it (its avatar and badges with their certificates, its User Tag).
|
|
///
|
|
/// The senders live here, one pair for every connection of a [`TsLink`]; each
|
|
/// connection watches the pair through [`Watches`], presents the account it
|
|
/// finds when it connects, and follows the changes.
|
|
pub(crate) struct Account {
|
|
identity: watch::Sender<Option<Arc<tsproto::myts::Identity>>>,
|
|
data: watch::Sender<Option<Arc<tsclientlib::MytsData>>>,
|
|
}
|
|
|
|
impl Default for Account {
|
|
/// No account is signed in.
|
|
fn default() -> Self {
|
|
Self { identity: watch::Sender::new(None), data: watch::Sender::new(None) }
|
|
}
|
|
}
|
|
|
|
impl Account {
|
|
/// The account proof for every current and future connection. Still no
|
|
/// account is no change: every notification restarts the handshakes and
|
|
/// sends `updatemytsid` on every connection. Any account is (each
|
|
/// connection's proof is signed anew).
|
|
pub(crate) fn set_identity(&self, identity: Option<Arc<tsproto::myts::Identity>>) {
|
|
self.identity.send_if_modified(|current| {
|
|
if current.is_none() && identity.is_none() {
|
|
return false;
|
|
}
|
|
*current = identity;
|
|
true
|
|
});
|
|
}
|
|
|
|
/// What the servers are shown of the account: sent to every connection
|
|
/// that presents the account's myTS ID, once connected and when it
|
|
/// changes; cleared on a sign-out. The same data is no change.
|
|
pub(crate) fn set_data(&self, data: Option<Arc<tsclientlib::MytsData>>) {
|
|
self.data.send_if_modified(|current| {
|
|
if *current == data {
|
|
return false;
|
|
}
|
|
*current = data;
|
|
true
|
|
});
|
|
}
|
|
|
|
/// What one connection watches. It keeps the senders alive for as long as
|
|
/// it runs: a connection ends with "account source closed" when its
|
|
/// watches have lost them, so the account outlives the connector that
|
|
/// made the connection.
|
|
pub(crate) fn watches(self: &Arc<Self>) -> Watches {
|
|
Watches {
|
|
identity: self.identity.subscribe(),
|
|
data: self.data.subscribe(),
|
|
_keep: Some(self.clone()),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The account a connection presents and follows ([`Account::watches`]).
|
|
pub(crate) struct Watches {
|
|
pub(super) identity: watch::Receiver<Option<Arc<tsproto::myts::Identity>>>,
|
|
pub(super) data: watch::Receiver<Option<Arc<tsclientlib::MytsData>>>,
|
|
_keep: Option<Arc<Account>>,
|
|
}
|
|
|
|
impl Watches {
|
|
/// Watches of senders that someone else keeps (a test's).
|
|
#[cfg(test)]
|
|
pub(super) fn of(
|
|
identity: watch::Receiver<Option<Arc<tsproto::myts::Identity>>>,
|
|
data: watch::Receiver<Option<Arc<tsclientlib::MytsData>>>,
|
|
) -> Self {
|
|
Self { identity, data, _keep: None }
|
|
}
|
|
}
|
|
|
|
/// Connects to TeamSpeak 3 and 6 servers. The link of [`TsConnector`](super::TsConnector).
|
|
#[derive(Clone, Default)]
|
|
pub(crate) struct TsLink {
|
|
account: Arc<Account>,
|
|
}
|
|
|
|
impl TsLink {
|
|
/// A link that presents no account until it is given one.
|
|
pub(crate) fn new() -> Self {
|
|
Self::default()
|
|
}
|
|
|
|
/// The account the link's connections present.
|
|
pub(crate) fn account(&self) -> &Arc<Account> {
|
|
&self.account
|
|
}
|
|
}
|
|
|
|
impl ServerLink for TsLink {
|
|
fn spawn(
|
|
&self,
|
|
options: ConnectOptions,
|
|
ctx: LinkCtx,
|
|
cmds: mpsc::UnboundedReceiver<LinkCmd>,
|
|
events: mpsc::UnboundedSender<LinkEvent>,
|
|
) {
|
|
tokio::spawn(voice::run(options, ctx, self.account.watches(), cmds, events));
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
/// A myTS ID's proof (the standard Ed25519 base point as the public key).
|
|
fn identity() -> Arc<tsproto::myts::Identity> {
|
|
let mut private = [0; 32];
|
|
private[0] = 1;
|
|
let mut public = [0x66; 32];
|
|
public[0] = 0x58;
|
|
Arc::new(
|
|
tsproto::myts::Identity::new(vec![1; 33], 42, public, private, vec![2; 65]).unwrap(),
|
|
)
|
|
}
|
|
|
|
/// An account sets what it holds only when it changes: every notification
|
|
/// of a watch restarts the handshake of a connection.
|
|
#[test]
|
|
fn a_watch_hears_of_a_change_and_not_of_the_same_account_again() {
|
|
let account = Arc::new(Account::default());
|
|
let mut watches = account.watches();
|
|
// No account, still none: nothing.
|
|
account.set_identity(None);
|
|
account.set_data(None);
|
|
assert!(!watches.identity.has_changed().unwrap());
|
|
assert!(!watches.data.has_changed().unwrap());
|
|
|
|
let proof = identity();
|
|
account.set_identity(Some(proof.clone()));
|
|
assert!(watches.identity.borrow_and_update().is_some());
|
|
// An account again is a change (each connection signs its proof anew), the
|
|
// same data is none.
|
|
account.set_identity(Some(proof));
|
|
assert!(watches.identity.has_changed().unwrap());
|
|
watches.identity.borrow_and_update();
|
|
let data = Arc::new(tsclientlib::MytsData::default());
|
|
account.set_data(Some(data.clone()));
|
|
assert!(watches.data.has_changed().unwrap());
|
|
watches.data.borrow_and_update();
|
|
account.set_data(Some(Arc::new(tsclientlib::MytsData::default())));
|
|
assert!(!watches.data.has_changed().unwrap(), "equal data is no change");
|
|
// A sign-out is a change.
|
|
account.set_identity(None);
|
|
assert!(watches.identity.has_changed().unwrap());
|
|
assert!(watches.identity.borrow_and_update().is_none());
|
|
}
|
|
|
|
/// A connection outlives the connector that made it (a session handed out
|
|
/// by a connector that was dropped): its watches keep their senders.
|
|
#[test]
|
|
fn a_connection_keeps_the_account_open_when_its_link_is_gone() {
|
|
let link = TsLink::new();
|
|
let watches = link.account().watches();
|
|
drop(link);
|
|
assert!(watches.identity.has_changed().is_ok(), "the identity watch lost its sender");
|
|
assert!(watches.data.has_changed().is_ok(), "the data watch lost its sender");
|
|
}
|
|
}
|