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::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<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)]
@@ -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<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();
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 {
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.
+1
View File
@@ -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)));
+7
View File
@@ -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({
+5
View File
@@ -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);