diff --git a/connector/src/main.rs b/connector/src/main.rs index 621d90f..4d8c251 100644 --- a/connector/src/main.rs +++ b/connector/src/main.rs @@ -15,7 +15,7 @@ use tsclientlib::events::{Event as BookEvent, PropertyId, PropertyValue}; use tsclientlib::messages::c2s::{OutClientPokeRequestPart, OutSendTextMessagePart}; use tsclientlib::prelude::*; use tsclientlib::{ - data, ChannelId, ClientId, Connection, DisconnectOptions, MaxClients, MessageHandle, + data, ChannelId, ClientId, Connection, DisconnectOptions, Identity, MaxClients, MessageHandle, MessageTarget, Reason, StreamItem, TextMessageTargetMode, }; use tsproto_packets::packets::{AudioData, CodecType, OutAudio}; @@ -43,6 +43,12 @@ struct Args { /// instead of the server's actual default channel #[arg(long)] default_channel: Option, + /// Previously-issued identity (as emitted in the "connected" event's + /// `identity` field) to reconnect with, so the server sees the same + /// client UID across sessions. A fresh identity is generated - and + /// reported back the same way - when omitted or unparseable. + #[arg(long)] + identity: Option, } #[derive(Serialize)] @@ -102,6 +108,9 @@ enum Event { server_version: String, server_license: String, server_banner_url: String, + /// Opaque JSON blob the frontend can store and pass back as `--identity` + /// on a future connection to keep the same client UID. + identity: String, }, #[serde(rename = "channels")] Channels { channels: Vec, clients: Vec }, @@ -348,7 +357,13 @@ async fn run(args: Args) -> Result<()> { tracing_subscriber::fmt().with_env_filter("warn").with_writer(std::io::stderr).init(); let address = args.address.clone(); - let mut con_config = Connection::build(args.address).name(args.nickname); + let identity = args + .identity + .as_deref() + .and_then(|s| serde_json::from_str::(s).ok()) + .unwrap_or_else(Identity::create); + let identity_str = serde_json::to_string(&identity).unwrap(); + let mut con_config = Connection::build(args.address).name(args.nickname).identity(identity); if let Some(pwd) = args.server_password { con_config = con_config.password(pwd); } @@ -396,6 +411,7 @@ async fn run(args: Args) -> Result<()> { server_version, server_license, server_banner_url, + identity: identity_str, }); // Subscribe to all channels so we actually receive the full channel/client list. diff --git a/gateway/src/index.ts b/gateway/src/index.ts index d935bf8..5b03824 100644 --- a/gateway/src/index.ts +++ b/gateway/src/index.ts @@ -71,6 +71,7 @@ wss.on("connection", (socket: WebSocket) => { serverPassword: msg.serverPassword, channelPassword: msg.channelPassword, defaultChannel: msg.defaultChannel, + identity: msg.identity, }; connection = new Ts3Connection(options); connection.onEvent((event) => socket.send(JSON.stringify(event))); diff --git a/gateway/src/ts3/connection.ts b/gateway/src/ts3/connection.ts index a13084e..164d58c 100644 --- a/gateway/src/ts3/connection.ts +++ b/gateway/src/ts3/connection.ts @@ -54,6 +54,7 @@ export type Ts3ConnectionEvent = serverVersion: string; serverLicense: string; serverBannerUrl: string; + identity: string; } | { type: "channels"; channels: ChannelInfo[]; clients: ClientInfo[] } | { type: "chatMessage"; from: string; message: string } @@ -72,6 +73,9 @@ export interface Ts3ConnectOptions { serverPassword?: string; channelPassword?: string; defaultChannel?: string; + /** Previously-issued identity (from a prior "connected" event) to keep + * the same client UID across sessions. Omit to get a freshly generated one. */ + identity?: string; } const __dirname = path.dirname(fileURLToPath(import.meta.url)); @@ -102,6 +106,7 @@ export class Ts3Connection { if (this.options.serverPassword) args.push("--server-password", this.options.serverPassword); if (this.options.channelPassword) args.push("--channel-password", this.options.channelPassword); if (this.options.defaultChannel) args.push("--default-channel", this.options.defaultChannel); + if (this.options.identity) args.push("--identity", this.options.identity); this.child = spawn(CONNECTOR_BIN, args); createInterface({ input: this.child.stdout }).on("line", (line) => { @@ -139,6 +144,7 @@ export class Ts3Connection { server_version: string; server_license: string; server_banner_url: string; + identity: string; } | { type: "channels"; channels: RawChannelInfo[]; clients: RawClientInfo[] } | { type: "chatMessage"; from: string; message: string } @@ -160,6 +166,7 @@ export class Ts3Connection { serverVersion: event.server_version, serverLicense: event.server_license, serverBannerUrl: event.server_banner_url, + identity: event.identity, }); } else if (event.type === "channels") { this.emit({ diff --git a/web/src/App.tsx b/web/src/App.tsx index 1b393eb..a884b0c 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -65,6 +65,9 @@ const GATEWAY_URL = import.meta.env.DEV const LAST_HOST_KEY = "webspeak3:last-host"; const LAST_NICKNAME_KEY = "webspeak3:last-nickname"; +/** Identity persisted across sessions so the server sees the same client UID + * each time, instead of a fresh one being generated per connection. */ +const IDENTITY_KEY = "webspeak3:identity"; const FAVORITES_KEY = "webspeak3:favorites"; const INPUT_DEVICE_KEY = "webspeak3:input-device"; const PLAYBACK_VOLUME_KEY = "webspeak3:playback-volume"; @@ -1719,6 +1722,7 @@ function AppInner() { serverPassword: connectServerPassword || undefined, channelPassword: connectChannelPassword || undefined, defaultChannel: connectDefaultChannel || undefined, + identity: localStorage.getItem(IDENTITY_KEY) || undefined, }) ); }; @@ -1733,6 +1737,7 @@ function AppInner() { setConnected(true); localStorage.setItem(LAST_HOST_KEY, connectHost); localStorage.setItem(LAST_NICKNAME_KEY, connectNickname); + if (data.identity) localStorage.setItem(IDENTITY_KEY, data.identity); setServerName(data.serverName); setServerMaxClients(data.serverMaxClients); setServerVersion(data.serverVersion);