Persist client identity across sessions

Previously every connect generated a fresh tsclientlib identity, so
the server saw a new client UID each session. The connector now
accepts a saved identity via --identity and always reports the one
actually used (freshly generated or reused) back in the "connected"
event; the frontend round-trips it through localStorage so
reconnecting keeps the same UID.

Verified end-to-end against the real test server: two separate
connects with the identity carried over produced byte-identical
identity blobs.
This commit is contained in:
Moepchi committed 2026-08-02 14:29:00 +02:00
1 parent 249687d98a
commit 7217bc146a
4 files changed
+31 -2

No files matched your search

+18 -2
View File
@@ -15,7 +15,7 @@ use tsclientlib::events::{Event as BookEvent, PropertyId, PropertyValue};
use tsclientlib::messages::c2s::{OutClientPokeRequestPart, OutSendTextMessagePart}; use tsclientlib::messages::c2s::{OutClientPokeRequestPart, OutSendTextMessagePart};
use tsclientlib::prelude::*; use tsclientlib::prelude::*;
use tsclientlib::{ use tsclientlib::{
data, ChannelId, ClientId, Connection, DisconnectOptions, MaxClients, MessageHandle, data, ChannelId, ClientId, Connection, DisconnectOptions, Identity, MaxClients, MessageHandle,
MessageTarget, Reason, StreamItem, TextMessageTargetMode, MessageTarget, Reason, StreamItem, TextMessageTargetMode,
}; };
use tsproto_packets::packets::{AudioData, CodecType, OutAudio}; use tsproto_packets::packets::{AudioData, CodecType, OutAudio};
@@ -43,6 +43,12 @@ struct Args {
/// instead of the server's actual default channel /// instead of the server's actual default channel
#[arg(long)] #[arg(long)]
default_channel: Option<String>, default_channel: Option<String>,
/// 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<String>,
} }
#[derive(Serialize)] #[derive(Serialize)]
@@ -102,6 +108,9 @@ enum Event {
server_version: String, server_version: String,
server_license: String, server_license: String,
server_banner_url: 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")] #[serde(rename = "channels")]
Channels { channels: Vec<ChannelInfo>, clients: Vec<ClientInfo> }, Channels { channels: Vec<ChannelInfo>, clients: Vec<ClientInfo> },
@@ -348,7 +357,13 @@ async fn run(args: Args) -> Result<()> {
tracing_subscriber::fmt().with_env_filter("warn").with_writer(std::io::stderr).init(); tracing_subscriber::fmt().with_env_filter("warn").with_writer(std::io::stderr).init();
let address = args.address.clone(); 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::<Identity>(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 { if let Some(pwd) = args.server_password {
con_config = con_config.password(pwd); con_config = con_config.password(pwd);
} }
@@ -396,6 +411,7 @@ async fn run(args: Args) -> Result<()> {
server_version, server_version,
server_license, server_license,
server_banner_url, server_banner_url,
identity: identity_str,
}); });
// Subscribe to all channels so we actually receive the full channel/client list. // Subscribe to all channels so we actually receive the full channel/client list.
+1
View File
@@ -71,6 +71,7 @@ wss.on("connection", (socket: WebSocket) => {
serverPassword: msg.serverPassword, serverPassword: msg.serverPassword,
channelPassword: msg.channelPassword, channelPassword: msg.channelPassword,
defaultChannel: msg.defaultChannel, defaultChannel: msg.defaultChannel,
identity: msg.identity,
}; };
connection = new Ts3Connection(options); connection = new Ts3Connection(options);
connection.onEvent((event) => socket.send(JSON.stringify(event))); connection.onEvent((event) => socket.send(JSON.stringify(event)));
+7
View File
@@ -54,6 +54,7 @@ export type Ts3ConnectionEvent =
serverVersion: string; serverVersion: string;
serverLicense: string; serverLicense: string;
serverBannerUrl: string; serverBannerUrl: string;
identity: string;
} }
| { type: "channels"; channels: ChannelInfo[]; clients: ClientInfo[] } | { type: "channels"; channels: ChannelInfo[]; clients: ClientInfo[] }
| { type: "chatMessage"; from: string; message: string } | { type: "chatMessage"; from: string; message: string }
@@ -72,6 +73,9 @@ export interface Ts3ConnectOptions {
serverPassword?: string; serverPassword?: string;
channelPassword?: string; channelPassword?: string;
defaultChannel?: 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)); 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.serverPassword) args.push("--server-password", this.options.serverPassword);
if (this.options.channelPassword) args.push("--channel-password", this.options.channelPassword); 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.defaultChannel) args.push("--default-channel", this.options.defaultChannel);
if (this.options.identity) args.push("--identity", this.options.identity);
this.child = spawn(CONNECTOR_BIN, args); this.child = spawn(CONNECTOR_BIN, args);
createInterface({ input: this.child.stdout }).on("line", (line) => { createInterface({ input: this.child.stdout }).on("line", (line) => {
@@ -139,6 +144,7 @@ export class Ts3Connection {
server_version: string; server_version: string;
server_license: string; server_license: string;
server_banner_url: string; server_banner_url: string;
identity: string;
} }
| { type: "channels"; channels: RawChannelInfo[]; clients: RawClientInfo[] } | { type: "channels"; channels: RawChannelInfo[]; clients: RawClientInfo[] }
| { type: "chatMessage"; from: string; message: string } | { type: "chatMessage"; from: string; message: string }
@@ -160,6 +166,7 @@ export class Ts3Connection {
serverVersion: event.server_version, serverVersion: event.server_version,
serverLicense: event.server_license, serverLicense: event.server_license,
serverBannerUrl: event.server_banner_url, serverBannerUrl: event.server_banner_url,
identity: event.identity,
}); });
} else if (event.type === "channels") { } else if (event.type === "channels") {
this.emit({ this.emit({
+5
View File
@@ -65,6 +65,9 @@ const GATEWAY_URL = import.meta.env.DEV
const LAST_HOST_KEY = "webspeak3:last-host"; const LAST_HOST_KEY = "webspeak3:last-host";
const LAST_NICKNAME_KEY = "webspeak3:last-nickname"; 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 FAVORITES_KEY = "webspeak3:favorites";
const INPUT_DEVICE_KEY = "webspeak3:input-device"; const INPUT_DEVICE_KEY = "webspeak3:input-device";
const PLAYBACK_VOLUME_KEY = "webspeak3:playback-volume"; const PLAYBACK_VOLUME_KEY = "webspeak3:playback-volume";
@@ -1719,6 +1722,7 @@ function AppInner() {
serverPassword: connectServerPassword || undefined, serverPassword: connectServerPassword || undefined,
channelPassword: connectChannelPassword || undefined, channelPassword: connectChannelPassword || undefined,
defaultChannel: connectDefaultChannel || undefined, defaultChannel: connectDefaultChannel || undefined,
identity: localStorage.getItem(IDENTITY_KEY) || undefined,
}) })
); );
}; };
@@ -1733,6 +1737,7 @@ function AppInner() {
setConnected(true); setConnected(true);
localStorage.setItem(LAST_HOST_KEY, connectHost); localStorage.setItem(LAST_HOST_KEY, connectHost);
localStorage.setItem(LAST_NICKNAME_KEY, connectNickname); localStorage.setItem(LAST_NICKNAME_KEY, connectNickname);
if (data.identity) localStorage.setItem(IDENTITY_KEY, data.identity);
setServerName(data.serverName); setServerName(data.serverName);
setServerMaxClients(data.serverMaxClients); setServerMaxClients(data.serverMaxClients);
setServerVersion(data.serverVersion); setServerVersion(data.serverVersion);