link::connect::credential sent a TeamSpeak IdentityBlob with an empty json as
an empty Credential::Foreign blob, which is what the TeamSpeak connector reads
as "make an identity up" (identity: None). An identity that holds nothing is no
identity: it is refused with SessionError::Protocol, as the Voelin branch
refuses a key that cannot be read, before any connector is asked. The comment
of ts::identity::to_blob ("a blob with no JSON is refused when it is used")
holds on both paths now: the gateway's from_blob already refused it.
Two tests: connect_params refuses "" and white space and still sends none as
the empty blob; an engine on the fake host answers ConnectVoice with the reason
and the connector is never asked and makes no session.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
262 lines
10 KiB
Rust
262 lines
10 KiB
Rust
//! What a session hands the connector of an address: the `ConnectParams` of
|
|
//! a [`VoiceOptions`] (`docs/session-seam.md` section 4.2).
|
|
//!
|
|
//! The options are the engine's own, and a connector reads them back: the
|
|
//! address is the link of the bookmark (a `ts3server://` link for a
|
|
//! bookmark that names a TeamSpeak server by `host[:port]`), the nickname,
|
|
//! the server password and the privilege key are in the hello, the identity
|
|
//! is the credential. What v1 has no field for yet (the channel to join and
|
|
//! its password, the client version a TeamSpeak server is shown) rides in
|
|
//! the address, as parameters of the link.
|
|
|
|
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
use base64::Engine as _;
|
|
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
|
|
use bytes::Bytes;
|
|
use voelin_identity::{IdentityBlob, KIND_TEAMSPEAK};
|
|
use voelin_model::Link;
|
|
use voelin_proto::link::{ConnectParams, Credential, IssuerTrust, MediaSink, SessionError};
|
|
use voelin_proto::v1;
|
|
|
|
use super::{Backends, VoiceOptions, without_query};
|
|
|
|
/// The link parameter that carries the client version to impersonate: the
|
|
/// user's spec (`VoiceOptions::client_version`), in unpadded URL-safe base64.
|
|
/// The connector for TeamSpeak resolves it; other connectors never read it.
|
|
pub(crate) const VERSION_PARAM: &str = "client_version";
|
|
|
|
/// The connect parameters of a connection, from the options the session was
|
|
/// asked to connect with.
|
|
///
|
|
/// The address is the link of the bookmark (the channel and its password
|
|
/// ride in it, and so does the client version to claim, until v1 has fields
|
|
/// for them), the server password is `Hello.password` and the privilege key
|
|
/// `Hello.invite_code`. The identity goes as the credential of its kind; no
|
|
/// account is used yet.
|
|
///
|
|
/// An address a link cannot carry is refused here ([`bookmark`]), and so is
|
|
/// an identity that is of no kind a connector can use.
|
|
pub(crate) fn connect_params(
|
|
options: &VoiceOptions,
|
|
media: Arc<dyn MediaSink>,
|
|
backends: &Backends,
|
|
) -> Result<ConnectParams, SessionError> {
|
|
let hello = v1::Hello {
|
|
version: 1,
|
|
client: Some(v1::ClientInfo {
|
|
name: "Voelin".into(),
|
|
version: voelin_platform::crash::app_version().to_owned(),
|
|
platform: std::env::consts::OS.into(),
|
|
kind: v1::ClientKind::User as i32,
|
|
}),
|
|
nickname: options.nickname.clone(),
|
|
password: options.server_password.clone().unwrap_or_default(),
|
|
invite_code: options.token.clone().unwrap_or_default(),
|
|
..Default::default()
|
|
};
|
|
Ok(ConnectParams {
|
|
address: address_link(options)?,
|
|
credential: credential(options.identity.as_ref())?,
|
|
linked: Vec::new(),
|
|
account: backends.account(),
|
|
issuer_trust: IssuerTrust::Unknown,
|
|
hello,
|
|
host_pin: None,
|
|
trust: backends.trust(),
|
|
media,
|
|
push_capacity: 1024,
|
|
timeout: Duration::from_secs(35),
|
|
})
|
|
}
|
|
|
|
/// The credential of an identity (`docs/session-seam.md` section 7). A
|
|
/// TeamSpeak identity goes as the JSON the key store keeps, and none at all as
|
|
/// an empty one, for which the TeamSpeak connector makes an identity up (the
|
|
/// protocol's convention for "generate one", follow-up P11); a Voelin key
|
|
/// signs. A TeamSpeak identity that holds no JSON is refused: sent as it is,
|
|
/// it would be that same empty blob and be taken for the request to make one
|
|
/// up, which only the absence of an identity may be.
|
|
fn credential(identity: Option<&IdentityBlob>) -> Result<Credential, SessionError> {
|
|
let Some(blob) = identity else {
|
|
return Ok(Credential::Foreign { kind: KIND_TEAMSPEAK, blob: Bytes::new() });
|
|
};
|
|
if blob.is_teamspeak() {
|
|
if blob.json.trim().is_empty() {
|
|
return Err(SessionError::Protocol(
|
|
"the TeamSpeak identity cannot be read: it is empty".into(),
|
|
));
|
|
}
|
|
let json = Bytes::copy_from_slice(blob.json.as_bytes());
|
|
Ok(Credential::Foreign { kind: KIND_TEAMSPEAK, blob: json })
|
|
} else if blob.is_voelin() {
|
|
let key = blob.voelin_key().map_err(|e| {
|
|
SessionError::Protocol(format!("the Voelin identity cannot be read: {e}"))
|
|
})?;
|
|
Ok(Credential::Voelin(Arc::new(key)))
|
|
} else {
|
|
Err(SessionError::Protocol(format!(
|
|
"an identity of kind {:?} cannot be used to connect",
|
|
blob.kind
|
|
)))
|
|
}
|
|
}
|
|
|
|
/// The `ts3server://` link of the options' address, with the channel to join
|
|
/// and the client version to claim.
|
|
fn address_link(options: &VoiceOptions) -> Result<String, SessionError> {
|
|
let mut link = match Link::parse(&options.address) {
|
|
Some(Link::Server(link)) => link,
|
|
// Not a TeamSpeak link, or none: a bookmark's `host[:port]`.
|
|
_ if !options.address.contains("://") => bookmark(&options.address)?,
|
|
// Some other scheme: as given, for the connector of that scheme.
|
|
_ => return Ok(options.address.clone()),
|
|
};
|
|
link.channel = options.channel.clone().filter(|c| !c.is_empty()).or(link.channel);
|
|
link.channel_password =
|
|
options.channel_password.clone().filter(|p| !p.is_empty()).or(link.channel_password);
|
|
let mut url = link.to_url();
|
|
if let Some(spec) = &options.client_version {
|
|
url.push(if url.contains('?') { '&' } else { '?' });
|
|
url.push_str(VERSION_PARAM);
|
|
url.push('=');
|
|
url.push_str(&URL_SAFE_NO_PAD.encode(spec));
|
|
}
|
|
Ok(url)
|
|
}
|
|
|
|
/// The link to a bookmark's `host[:port]`.
|
|
///
|
|
/// A link cannot say everything an address can: it drops a port that is no
|
|
/// port (`ServerLink::new` takes `host:99999` for the host `host` and joins
|
|
/// the default port), and it ends the host at a `?`, `#` or `/` and decodes a
|
|
/// `%` (so that a `?` in the host would hide the channel behind the next
|
|
/// one). The TeamSpeak library refuses such an address when it resolves it;
|
|
/// so does this, in words, instead of connecting to something else.
|
|
fn bookmark(address: &str) -> Result<voelin_model::ServerLink, SessionError> {
|
|
let address = address.trim();
|
|
// Without the query: a link keeps secrets in it.
|
|
let shown = without_query(address);
|
|
let not_an_address = || SessionError::Protocol(format!("not a server address: {shown}"));
|
|
if address.is_empty()
|
|
|| address
|
|
.chars()
|
|
.any(|c| matches!(c, '?' | '#' | '/' | '%') || c.is_whitespace() || c.is_control())
|
|
{
|
|
return Err(not_an_address());
|
|
}
|
|
// The text after the port's colon: `[v6]:port`, or `host:port` with the one
|
|
// colon (more colons are an IPv6 address without a port).
|
|
let port = if let Some(rest) = address.strip_prefix('[') {
|
|
let (_, after) = rest.split_once(']').ok_or_else(not_an_address)?;
|
|
(!after.is_empty()).then(|| after.strip_prefix(':').unwrap_or(after))
|
|
} else {
|
|
address.rsplit_once(':').filter(|(host, _)| !host.contains(':')).map(|(_, port)| port)
|
|
};
|
|
if let Some(port) = port
|
|
&& !port.parse::<u16>().is_ok_and(|port| port != 0)
|
|
{
|
|
return Err(SessionError::Protocol(format!(
|
|
"the port in {shown} is not a number from 1 to 65535"
|
|
)));
|
|
}
|
|
Ok(voelin_model::ServerLink::new(address))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use voelin_identity::IdentityKey;
|
|
|
|
fn params(options: &VoiceOptions) -> Result<ConnectParams, SessionError> {
|
|
let (media, _rx) = voelin_proto::test_util::media_channel();
|
|
connect_params(options, media, &Backends::empty())
|
|
}
|
|
|
|
/// The identity of the options is the credential of its kind: a TeamSpeak
|
|
/// identity as the JSON the key store keeps, none as the empty blob a
|
|
/// connector makes an identity up for, a Voelin key as its signer.
|
|
#[test]
|
|
fn an_identity_is_the_credential_of_its_kind() {
|
|
let mut options = VoiceOptions::new("ts.example.com", "nick");
|
|
let none = params(&options).unwrap();
|
|
assert!(matches!(
|
|
&none.credential,
|
|
Credential::Foreign { kind: "teamspeak", blob } if blob.is_empty()
|
|
));
|
|
|
|
options.identity = Some(IdentityBlob::teamspeak("uid=", 8, r#"{"key":"k"}"#));
|
|
let sent = params(&options).unwrap();
|
|
assert!(matches!(
|
|
&sent.credential,
|
|
Credential::Foreign { kind: "teamspeak", blob } if &blob[..] == br#"{"key":"k"}"#
|
|
));
|
|
|
|
let key = IdentityKey::generate().unwrap();
|
|
options.identity = Some(IdentityBlob::voelin(&key).unwrap());
|
|
let Credential::Voelin(signer) = params(&options).unwrap().credential else {
|
|
panic!("a Voelin credential")
|
|
};
|
|
assert_eq!(signer.public_key(), key.public_key());
|
|
assert_eq!(signer.sign(b"hello"), key.sign(b"hello"));
|
|
|
|
// A Voelin blob whose key cannot be read, and a kind that connects to
|
|
// nothing: refused in words, without the JSON.
|
|
options.identity = Some(IdentityBlob::new("voelin", "x", 0, "hunter2"));
|
|
let Err(SessionError::Protocol(text)) = params(&options) else { panic!("a refusal") };
|
|
assert!(
|
|
text.starts_with("the Voelin identity cannot be read") && !text.contains("hunter2")
|
|
);
|
|
options.identity = Some(IdentityBlob::new("account", "x", 0, "hunter2"));
|
|
let Err(SessionError::Protocol(text)) = params(&options) else { panic!("a refusal") };
|
|
assert_eq!(text, "an identity of kind \"account\" cannot be used to connect");
|
|
}
|
|
|
|
/// A TeamSpeak identity with no JSON is no identity. The empty blob is
|
|
/// what asks the TeamSpeak connector to make one up (`identity: None`), so
|
|
/// it is refused here instead of being sent as that request.
|
|
#[test]
|
|
fn a_teamspeak_identity_without_json_is_refused() {
|
|
let mut options = VoiceOptions::new("ts.example.com", "nick");
|
|
for json in ["", " \n\t"] {
|
|
options.identity = Some(IdentityBlob::teamspeak("uid=", 8, json));
|
|
let Err(SessionError::Protocol(text)) = params(&options) else {
|
|
panic!("a refusal for {json:?}")
|
|
};
|
|
assert_eq!(text, "the TeamSpeak identity cannot be read: it is empty");
|
|
}
|
|
// No identity at all is the request to make one up, as before.
|
|
options.identity = None;
|
|
assert!(matches!(
|
|
params(&options).unwrap().credential,
|
|
Credential::Foreign { kind: "teamspeak", blob } if blob.is_empty()
|
|
));
|
|
}
|
|
|
|
/// The client version spec rides the link as it was typed, whatever it
|
|
/// holds, and only for an address that is a TeamSpeak bookmark or link.
|
|
#[test]
|
|
fn the_client_version_spec_rides_the_link_unchanged() {
|
|
let spec = |address: &str, spec: &str| {
|
|
let mut options = VoiceOptions::new(address, "nick");
|
|
options.client_version = Some(spec.into());
|
|
params(&options).unwrap().address
|
|
};
|
|
let address = spec("ts.example.com:9988", "windows@3.6.2 [Build: 1]&x=#");
|
|
let (_, query) = address.split_once('?').unwrap();
|
|
let text = query
|
|
.split('&')
|
|
.find_map(|pair| pair.strip_prefix("client_version="))
|
|
.expect("the parameter");
|
|
let decoded = URL_SAFE_NO_PAD.decode(text).unwrap();
|
|
assert_eq!(decoded, b"windows@3.6.2 [Build: 1]&x=#");
|
|
// Another scheme is left as it is: its connector has no use for a TeamSpeak
|
|
// client's version.
|
|
assert_eq!(spec("voelin://host/i/code", "generic"), "voelin://host/i/code");
|
|
// No spec, no parameter.
|
|
let options = VoiceOptions::new("ts.example.com", "nick");
|
|
assert!(!params(&options).unwrap().address.contains(VERSION_PARAM));
|
|
}
|
|
}
|