The engine's voice source is now a voelin_proto::link::Session obtained from a registry of connectors (docs/session-seam.md, slice 1): - link::Backends keeps the plain connectors of voelin_proto::link::Connectors and the in-process ones that also hand the engine a port for TeamSpeak 6 streams; the scheme of the address picks the connector, an address without one is a TeamSpeak bookmark. - link::SessionLink is the engine's ServerLink. session.rs is unchanged and still speaks LinkCmd and LinkEvent; SessionLink runs the connect sequence (List the server, subscribe HOME, SERVER and LIVE, take the seat), keeps a voelin_model::replica::Replica, projects it into the client's older model (link/project.rs, the fix-ups F1 to F10), frames voice as RTP and derives who talks from the media (link/media.rs), and turns every command into the Session call of its row. Transfers copy through read_file and write_file. - ts_session makes a TeamSpeak connection a Session: LinkSession over any ServerLink translates the link's commands, events and presence snapshots to v1 entities, pushes, answers and RTP; TsConnector makes them for ts3server:// and teamspeak:// addresses with TsLink inside, and LinkConnector for the link of Engine::start_with_link. - voice.rs opens the transfer connection itself for the new OpenRead and OpenWrite commands; files::download and upload are generic over the stream. - Engine::start_with_backends is new; start_with and start_with_link go through the registry. tests/link_fake.rs runs through both adapters; its fixtures are normalised as the design says (an order chain, database ids, talking from media, the server named, channel chat for the channel we are in), and the request id the link sees is the session's own. New unit tests cover every row of the mapping with a recording link, the TeamSpeak session on its own, media framing both ways, the talk tracker, the close table, the error texts, the backend and capability tables, the connect parameters, and the presence round trip on book fixtures and generated presences. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
116 lines
3.8 KiB
Rust
116 lines
3.8 KiB
Rust
//! Ids and unique ids as the TeamSpeak gateway writes them (protocol
|
|
//! section 11.1, "Ids"): TeamSpeak's numbers as they are, and a few
|
|
//! synthetic ones with bit 63 set.
|
|
//!
|
|
//! Both sides of the seam use these rules: the TeamSpeak `Session`
|
|
//! ([`super::LinkSession`]) writes entities with them, and the engine's
|
|
//! projection (`link/project.rs`) undoes the few that the client's older
|
|
//! model has no room for.
|
|
|
|
use base64::Engine as _;
|
|
use base64::engine::general_purpose::STANDARD;
|
|
|
|
/// The only server of a TeamSpeak connection (its virtual server).
|
|
pub(crate) const SERVER_ID: u64 = 1;
|
|
|
|
/// Marks the ids the gateway makes up: the server chat, direct-message
|
|
/// channels and users it knows by their unique id only.
|
|
pub(crate) const SYNTHETIC: u64 = 1 << 63;
|
|
|
|
/// The channel of the server chat, in the SERVER scope.
|
|
pub(crate) const SERVER_CHAT: u64 = SYNTHETIC;
|
|
|
|
/// Set on the id of a channel group, so that a role id does not mean a server
|
|
/// group and a channel group at once.
|
|
pub(crate) const CHANNEL_ROLE: u64 = 1 << 62;
|
|
|
|
/// The `Foreign.system` of what the protocol has no field for.
|
|
pub(crate) const FOREIGN_SYSTEM: &str = "teamspeak";
|
|
|
|
/// The role id of a channel group.
|
|
pub(crate) fn channel_role(group: u64) -> u64 {
|
|
group | CHANNEL_ROLE
|
|
}
|
|
|
|
/// The channel group a role id stands for (the reverse of [`channel_role`]).
|
|
pub(crate) fn channel_group(role: u64) -> u64 {
|
|
role & !CHANNEL_ROLE
|
|
}
|
|
|
|
/// FNV-1a, 64 bits.
|
|
fn fnv1a(bytes: &[u8]) -> u64 {
|
|
let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
|
|
for byte in bytes {
|
|
hash ^= u64::from(*byte);
|
|
hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
|
|
}
|
|
hash
|
|
}
|
|
|
|
/// A synthetic id for something known by a name only: bit 63 set and the
|
|
/// hash of `name` in the low 63 bits (1 if they come out 0, so it is never
|
|
/// [`SERVER_CHAT`]).
|
|
fn synthetic(name: &str) -> u64 {
|
|
let low = fnv1a(name.as_bytes()) & !SYNTHETIC;
|
|
SYNTHETIC | low.max(1)
|
|
}
|
|
|
|
/// The direct-message channel with the client of this unique id.
|
|
pub(crate) fn dm_channel(uid: &str) -> u64 {
|
|
synthetic(uid)
|
|
}
|
|
|
|
/// The user id of a client that has no database id (or that the snapshot
|
|
/// does not hold): by unique id, else by whatever names it.
|
|
pub(crate) fn synthetic_user(uid: &str) -> u64 {
|
|
synthetic(uid)
|
|
}
|
|
|
|
/// The bytes of a unique id, when it is canonical base64 (what TeamSpeak
|
|
/// writes): decoding and encoding again gives the same text. A ServerQuery
|
|
/// client's login name, or anything else, is not, and goes into
|
|
/// [`Foreign`](voelin_proto::v1::Foreign) instead.
|
|
pub(crate) fn uid_bytes(uid: &str) -> Option<Vec<u8>> {
|
|
let bytes = STANDARD.decode(uid).ok()?;
|
|
(STANDARD.encode(&bytes) == uid && !bytes.is_empty()).then_some(bytes)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn channel_groups_keep_their_number_below_the_flag() {
|
|
assert_eq!(channel_role(8), 8 | (1 << 62));
|
|
assert_eq!(channel_group(channel_role(8)), 8);
|
|
assert_eq!(channel_group(channel_role(0)), 0);
|
|
}
|
|
|
|
#[test]
|
|
fn synthetic_ids_have_bit_63_and_are_never_the_server_chat() {
|
|
for name in ["", "alice=", "client:7", "a-very-long-name-that-hashes-to-something"] {
|
|
let id = dm_channel(name);
|
|
assert_ne!(id & SYNTHETIC, 0, "{name}");
|
|
assert_ne!(id, SERVER_CHAT, "{name}");
|
|
}
|
|
assert_eq!(dm_channel("alice="), dm_channel("alice="));
|
|
assert_ne!(dm_channel("alice="), dm_channel("bob="));
|
|
}
|
|
|
|
#[test]
|
|
fn only_canonical_base64_is_a_unique_id() {
|
|
// A TeamSpeak 3 unique id (SHA-1) and a TeamSpeak 6 one (SHA-256).
|
|
assert_eq!(uid_bytes("uXDkxSZbv0IoA0z/toW3guGpUbI=").map(|b| b.len()), Some(20));
|
|
assert_eq!(
|
|
uid_bytes("xEZDkyeGdZ4bOSBCKqzTw8jVz2jsZCrJDRl9F8WWd3M=").map(|b| b.len()),
|
|
Some(32)
|
|
);
|
|
// A ServerQuery login name, a fixture's name, the empty string.
|
|
for not in ["serveradmin", "alice=", "me=", "", "Af4"] {
|
|
assert_eq!(uid_bytes(not), None, "{not:?}");
|
|
}
|
|
// Non-canonical trailing bits decode but do not round trip.
|
|
assert_eq!(uid_bytes("Af5="), None);
|
|
}
|
|
}
|