mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-01 00:42:49 +08:00
Pass TS6 stream signaling through connector and gateway
tsclientlib dropped every command missing from the message declarations, which silently swallowed TS6's whole Stream/Call signaling exchange: none of the stream commands are declared, so notifystreamsignaling never reached a consumer. That is why the protocol could only be captured with a standalone raw-tsproto tool. handle_command now splits the parse failure - ParseError::UnknownCommand hands the raw line up as StreamItem::UnknownCommand instead of warning and returning, everything else keeps the old behaviour. The connector unescapes the TS wire format and forwards the stream commands as a streamEvent, filtered to notifystream*/notifyrespondjoinstream* so unrelated undeclared commands stay dropped. The JSON payload stays opaque the whole way - SDP and ICE belong to the browser's RTCPeerConnection, not to the connector. Outbound, streamjoin/streamleave/streamsignal join the existing raw OutCommand handlers; parameter names and order are as recovered from TeamSpeak.dll and confirmed against a live TS6 client. The gateway serializes the payload itself so no literal newline can reach the line-based stdin protocol. Nothing renders video yet - the browser side is still missing. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
This commit is contained in:
1 parent
7776a71e84
commit
9f1a63c9d3
3 files changed
+155
-1
No files matched your search
+110
-1
@@ -1,5 +1,6 @@
|
||||
use std::borrow::Cow;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::collections::{BTreeMap, HashMap, HashSet};
|
||||
use std::str;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::Result;
|
||||
@@ -29,6 +30,7 @@ use tsclientlib::{
|
||||
LicenseType, MaxClients, MessageHandle, MessageTarget, Permission, Reason, ServerGroupId,
|
||||
ServerType, StreamItem, TextMessageTargetMode, TsError,
|
||||
};
|
||||
use tsproto_packets::commands::{CommandItem, CommandParser};
|
||||
use tsproto_packets::packets::{AudioData, CodecType, Direction, Flags, OutAudio, OutCommand, PacketType};
|
||||
|
||||
/// 20ms frames at 48kHz, which is what TeamSpeak's Opus voice codec expects.
|
||||
@@ -344,6 +346,16 @@ enum Event {
|
||||
/// anywhere), for populating an "add permission" picker.
|
||||
#[serde(rename = "permissionCatalog")]
|
||||
PermissionCatalog { entries: Vec<PermissionCatalogEntry> },
|
||||
/// A TS6 Stream/Call command the server sent us, forwarded verbatim.
|
||||
///
|
||||
/// `tsclientlib` has no declarations for these, so they arrive as
|
||||
/// `StreamItem::UnknownCommand` and are only unescaped here - the payload
|
||||
/// of `notifystreamsignaling` is an opaque JSON blob (SDP offer/answer,
|
||||
/// ICE candidates) that belongs to the browser's `RTCPeerConnection`, not
|
||||
/// to the connector. `name` is the raw command name, e.g.
|
||||
/// `notifystreamsignaling`.
|
||||
#[serde(rename = "streamEvent")]
|
||||
StreamEvent { name: String, args: BTreeMap<String, String> },
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
@@ -641,6 +653,57 @@ fn snapshot(con: &data::Connection) -> Event {
|
||||
}
|
||||
}
|
||||
|
||||
/// Splits a raw TS command line into its unescaped key/value pairs.
|
||||
///
|
||||
/// Only the first part of a `|`-separated multi-part command is read; none of
|
||||
/// the stream commands use multiple parts. A flag without `=` becomes an empty
|
||||
/// string, which is how TeamSpeak treats it too.
|
||||
fn parse_ts_args(content: &str) -> BTreeMap<String, String> {
|
||||
let (_, parser) = CommandParser::new(content.as_bytes());
|
||||
let mut args = BTreeMap::new();
|
||||
for item in parser {
|
||||
match item {
|
||||
CommandItem::Argument(arg) => {
|
||||
let (Ok(name), Ok(value)) = (str::from_utf8(arg.name()), arg.value().get_str())
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
args.insert(name.to_string(), value.into_owned());
|
||||
}
|
||||
// Stop at the first part boundary.
|
||||
CommandItem::NextCommand => break,
|
||||
}
|
||||
}
|
||||
args
|
||||
}
|
||||
|
||||
/// Sends `joinstreamrequest` for a `"<stream-id> <clid> [msg]"` argument line.
|
||||
///
|
||||
/// Parameter names and order are as recovered from TS6's `TeamSpeak.dll`; `msg`
|
||||
/// is written even when empty so the shape matches the native client.
|
||||
fn send_join_stream_request(con: &mut tsclientlib::Connection, rest: &str, is_remove: bool) {
|
||||
let mut parts = rest.splitn(3, ' ');
|
||||
let (Some(id), Some(Ok(clid))) = (parts.next(), parts.next().map(|c| c.parse::<u16>())) else {
|
||||
emit(&Event::Error { message: "Usage: streamjoin <stream-id> <clid> [msg]".into() });
|
||||
return;
|
||||
};
|
||||
if id.is_empty() {
|
||||
emit(&Event::Error { message: "Usage: streamjoin <stream-id> <clid> [msg]".into() });
|
||||
return;
|
||||
}
|
||||
let msg = parts.next().unwrap_or("");
|
||||
|
||||
let mut packet =
|
||||
OutCommand::new(Direction::C2S, Flags::empty(), PacketType::Command, "joinstreamrequest");
|
||||
packet.write_arg("id", &id);
|
||||
packet.write_arg("clid", &clid);
|
||||
packet.write_arg("msg", &msg);
|
||||
packet.write_arg("is_remove", &u8::from(is_remove));
|
||||
if let Err(e) = packet.send(con) {
|
||||
emit(&Event::Error { message: format!("joinstreamrequest failed: {e}") });
|
||||
}
|
||||
}
|
||||
|
||||
fn send_server_get_variables(con: &mut tsclientlib::Connection) {
|
||||
let packet = OutCommand::new(
|
||||
Direction::C2S,
|
||||
@@ -1401,6 +1464,42 @@ async fn run(args: Args) -> Result<()> {
|
||||
}
|
||||
Err(e) => emit(&Event::Error { message: e.to_string() }),
|
||||
}
|
||||
} else if let Some(rest) = l.strip_prefix("streamjoin ") {
|
||||
// "streamjoin <stream-id> <clid> [msg]" - ask the client
|
||||
// publishing a TS6 stream to let us in. Its reply is a
|
||||
// notifyrespondjoinstreamrequest carrying the SDP offer.
|
||||
send_join_stream_request(&mut con, rest, false);
|
||||
} else if let Some(rest) = l.strip_prefix("streamleave ") {
|
||||
// Same command with is_remove=1, which is how TS6 leaves
|
||||
// a stream it has joined.
|
||||
send_join_stream_request(&mut con, rest, true);
|
||||
} else if let Some(rest) = l.strip_prefix("streamsignal ") {
|
||||
// "streamsignal <stream-id> <clid> <json>" - the json is
|
||||
// the rest of the line, opaque to us. Escaping is done by
|
||||
// write_arg.
|
||||
let mut parts = rest.splitn(3, ' ');
|
||||
match (parts.next(), parts.next().and_then(|c| c.parse::<u16>().ok()), parts.next()) {
|
||||
(Some(id), Some(clid), Some(json)) if !id.is_empty() && !json.is_empty() => {
|
||||
let mut packet = OutCommand::new(
|
||||
Direction::C2S,
|
||||
Flags::empty(),
|
||||
PacketType::Command,
|
||||
"streamsignaling",
|
||||
);
|
||||
packet.write_arg("id", &id);
|
||||
packet.write_arg("clid", &clid);
|
||||
packet.write_arg("json", &json);
|
||||
// Fire and forget: ICE candidates arrive in bursts
|
||||
// and a return_code round trip per candidate would
|
||||
// only add latency to the handshake.
|
||||
if let Err(e) = packet.send(&mut con) {
|
||||
emit(&Event::Error { message: format!("streamsignaling failed: {e}") });
|
||||
}
|
||||
}
|
||||
_ => emit(&Event::Error {
|
||||
message: "Usage: streamsignal <stream-id> <clid> <json>".into(),
|
||||
}),
|
||||
}
|
||||
} else if l == "banlist" {
|
||||
// No typed builder exists for banlist, so this is built as a raw
|
||||
// command; the reply arrives via StreamItem::MessageEvent (see below).
|
||||
@@ -2370,6 +2469,16 @@ async fn run(args: Args) -> Result<()> {
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(Ok(StreamItem::UnknownCommand { name, content })) => {
|
||||
// TS6 Stream/Call signaling. Everything else undeclared is
|
||||
// dropped on purpose - forwarding every unknown command
|
||||
// would turn this into a firehose, and nothing downstream
|
||||
// wants it.
|
||||
if name.starts_with("notifystream") || name.starts_with("notifyrespondjoinstream")
|
||||
{
|
||||
emit(&Event::StreamEvent { args: parse_ts_args(&content), name });
|
||||
}
|
||||
}
|
||||
Some(Ok(StreamItem::MessageEvent(msg))) => {
|
||||
// Defense in depth: TeaSpeak/GTS may surface notifyclientpoke as an
|
||||
// unhandled MessageEvent if bookkeeping did not mark it handled.
|
||||
|
||||
@@ -415,6 +415,27 @@ wss.on("connection", (socket: WebSocket) => {
|
||||
await connection?.sendAudio(msg.pcm);
|
||||
break;
|
||||
}
|
||||
case "joinStream": {
|
||||
const clientId = Number(msg.clientId);
|
||||
if (Number.isFinite(clientId)) {
|
||||
await connection?.joinStream(String(msg.streamId ?? ""), clientId, msg.message ?? "");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "leaveStream": {
|
||||
const clientId = Number(msg.clientId);
|
||||
if (Number.isFinite(clientId)) {
|
||||
await connection?.leaveStream(String(msg.streamId ?? ""), clientId);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "streamSignal": {
|
||||
const clientId = Number(msg.clientId);
|
||||
if (Number.isFinite(clientId) && msg.payload !== undefined) {
|
||||
await connection?.sendStreamSignal(String(msg.streamId ?? ""), clientId, msg.payload);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "setAway": {
|
||||
await connection?.setAway(msg.away, msg.message ?? "");
|
||||
break;
|
||||
|
||||
@@ -774,6 +774,30 @@ export class Ts3Connection {
|
||||
this.child?.stdin.write(`audio ${pcmBase64}\n`);
|
||||
}
|
||||
|
||||
/** Ask the client publishing a TS6 stream to let us watch. The answer comes
|
||||
* back as a `streamEvent` of type `notifyrespondjoinstreamrequest`, which
|
||||
* carries the SDP offer. */
|
||||
async joinStream(streamId: string, clientId: number, message = ""): Promise<void> {
|
||||
const id = streamId.replace(/[\s]+/g, "");
|
||||
const sanitized = message.replace(/[\r\n]+/g, " ").trim();
|
||||
if (id) this.child?.stdin.write(`streamjoin ${id} ${clientId} ${sanitized}\n`);
|
||||
}
|
||||
|
||||
async leaveStream(streamId: string, clientId: number): Promise<void> {
|
||||
const id = streamId.replace(/[\s]+/g, "");
|
||||
if (id) this.child?.stdin.write(`streamleave ${id} ${clientId}\n`);
|
||||
}
|
||||
|
||||
/** Relays one signaling payload (SDP answer, ICE candidate) to a stream peer.
|
||||
* `payload` is passed straight through - the connector only escapes it for
|
||||
* the TS wire, nobody in this path interprets it. Serializing here rather
|
||||
* than accepting a string keeps literal newlines out of the line-based
|
||||
* stdin protocol. */
|
||||
async sendStreamSignal(streamId: string, clientId: number, payload: unknown): Promise<void> {
|
||||
const id = streamId.replace(/[\s]+/g, "");
|
||||
if (id) this.child?.stdin.write(`streamsignal ${id} ${clientId} ${JSON.stringify(payload)}\n`);
|
||||
}
|
||||
|
||||
async setAway(away: boolean, message: string): Promise<void> {
|
||||
if (away) {
|
||||
const sanitized = message.replace(/[\r\n]+/g, " ").trim();
|
||||
|
||||
Reference in new issue
Block a user