VNOX-Client/src/ui/bridge.rs
loki5512344 9a20ace6e6
feat(client): TLS 1.3 control connection with SPKI pinning and LNEx v3
Replace the custom X25519/HKDF/ChaCha20 TCP layer with rustls (TLS 1.3 only,
ALPN vnox-lnex/3, no plaintext or fallback). Add a trust store (pin, trust on
first use, public CA), block changed fingerprints and identities, bind the
Ed25519 auth signature to the TLS exporter, add RU/EN trust dialog and
update the interop test and e2e voice tool.

Claude-Session: https://claude.ai/code/session_01GDjxLvBcnJqniFQGUGb1V4
2026-10-24 22:05:19 +02:00

467 lines
16 KiB
Rust

use std::rc::Rc;
use slint::{Model, ModelRc, SharedString, VecModel};
use crate::ui::state::{ConnState, UiState};
fn presence_label(status: &str) -> String {
crate::ui::i18n::text(match status.to_ascii_lowercase().as_str() {
"online" => "Online",
"idle" | "away" => "Idle",
"dnd" | "busy" => "Do not disturb",
"pending" => "Pending",
_ => "Offline",
})
}
type MainWindow = crate::ui::slint::MainWindow;
type BookmarkItem = crate::ui::slint::BookmarkItem;
type ChannelItem = crate::ui::slint::ChannelItem;
type DmItem = crate::ui::slint::DmItem;
type GuildItem = crate::ui::slint::GuildItem;
type MessageItem = crate::ui::slint::MessageItem;
type MemberItem = crate::ui::slint::MemberItem;
type FriendItem = crate::ui::slint::FriendItem;
type VoiceMemberItem = crate::ui::slint::VoiceMemberItem;
fn replace_model<T: Clone + PartialEq + 'static>(
current: ModelRc<T>,
items: Vec<T>,
set: impl FnOnce(ModelRc<T>),
) {
if !current.iter().eq(items.iter().cloned()) {
set(Rc::new(VecModel::from(items)).into());
}
}
fn fmt_ts(ts: i64) -> String {
let secs = (ts / 1000).abs() % 86400;
format!("{:02}:{:02}", secs / 3600, (secs % 3600) / 60)
}
fn set_messages(
window: &MainWindow,
state: &UiState,
msgs: &[crate::ui::state::ChatMessage],
own_user_id: &str,
) {
let items: Vec<MessageItem> = msgs
.iter()
.enumerate()
.map(|(index, m)| MessageItem {
avatar_text: state
.nick_for(&m.sender_id)
.chars()
.take(2)
.collect::<String>()
.into(),
show_author: index == 0
|| msgs[index - 1].sender_id != m.sender_id
|| m.timestamp.saturating_sub(msgs[index - 1].timestamp) > 300_000,
message_id: SharedString::from(m.message_id.as_str()),
sender_id: SharedString::from(m.sender_id.as_str()),
sender_name: SharedString::from(state.nick_for(&m.sender_id)),
content: SharedString::from(m.content.as_str()),
timestamp: SharedString::from(fmt_ts(m.timestamp)),
is_own: m.sender_id == own_user_id,
edited: m.edited,
reply_to: SharedString::from(
m.reply_to
.as_ref()
.map(|id| {
msgs.iter()
.find(|other| &other.message_id == id)
.map(|other| {
format!(
"{}: {}",
state.nick_for(&other.sender_id),
other.content.chars().take(90).collect::<String>()
)
})
.unwrap_or_else(|| {
crate::ui::i18n::text("Original message unavailable")
})
})
.unwrap_or_default(),
),
})
.collect();
replace_model(window.get_messages(), items, |model| {
window.set_messages(model)
});
}
fn set_members_state(window: &MainWindow, state: &UiState, ch_id: &str) {
if let Some(ch) = state.channels.iter().find(|c| c.id == ch_id) {
let items: Vec<MemberItem> = ch
.members
.iter()
.map(|nick| {
let user_id = state
.user_names
.iter()
.find(|(_, n)| *n == nick)
.map(|(id, _)| id.clone())
.unwrap_or_else(|| nick.clone());
let status = state
.presences
.get(&user_id)
.map(|p| p.status.as_str())
.unwrap_or("offline");
MemberItem {
avatar_text: nick.chars().take(2).collect::<String>().into(),
user_id: SharedString::from(user_id),
nickname: SharedString::from(nick.as_str()),
status: presence_label(status).into(),
}
})
.collect();
replace_model(window.get_members(), items, |model| {
window.set_members(model)
});
}
}
fn sync_sidebar(window: &MainWindow, state: &UiState, own_nickname: &str) {
let guild_id = state.active_guild_id.as_deref();
let channels: Vec<ChannelItem> = state
.channels
.iter()
.filter(|c| match guild_id {
None => true,
Some(gid) => c.guild_id.as_deref() == Some(gid) || c.guild_id.is_none(),
})
.map(|c| ChannelItem {
channel_id: SharedString::from(c.id.as_str()),
name: SharedString::from(c.name.as_str()),
kind: SharedString::from(c.kind.as_str()),
})
.collect();
replace_model(window.get_channels(), channels, |model| {
window.set_channels(model)
});
let guilds: Vec<GuildItem> = state
.guilds
.iter()
.map(|g| {
let short: String = g.name.chars().take(2).collect();
GuildItem {
guild_id: SharedString::from(g.guild_id.as_str()),
name: SharedString::from(g.name.as_str()),
short_name: SharedString::from(short),
selected: Some(g.guild_id.as_str()) == guild_id,
}
})
.collect();
replace_model(window.get_guilds(), guilds, |model| {
window.set_guilds(model)
});
let dms: Vec<DmItem> = state
.dm_conversations
.iter()
.map(|c| DmItem {
dm_id: c.dm_id.as_str().into(),
name: c.other_nickname.as_str().into(),
unread: c.unread_count.min(i32::MAX as u32) as i32,
})
.collect();
replace_model(window.get_dm_conversations(), dms, |model| {
window.set_dm_conversations(model)
});
window.set_username(SharedString::from(own_nickname));
window.set_user_status(crate::ui::i18n::text("Online").into());
window.set_show_dms(state.browsing_dms);
window.set_active_dm_id(state.active_dm_id.as_deref().unwrap_or("").into());
window.set_replying_to_message(SharedString::from(
state.replying_to_message.as_deref().unwrap_or(""),
));
window.set_replying_to_sender(SharedString::from(state.replying_to_sender.as_str()));
window.set_replying_to_content(SharedString::from(state.replying_to_content.as_str()));
window.set_active_channel_id(SharedString::from(
state.active_channel.as_deref().unwrap_or(""),
));
window.set_active_guild_idx(state.active_guild_id.clone().map_or(-1, |id| {
state
.guilds
.iter()
.position(|g| g.guild_id == id)
.map_or(-1, |i| i as i32)
}));
}
fn sync_chat(window: &MainWindow, state: &UiState, own_user_id: &str) {
let dm = state
.active_dm_id
.as_ref()
.and_then(|id| state.dm_conversations.iter().find(|c| &c.dm_id == id));
let channel = state
.active_channel
.as_ref()
.and_then(|id| state.channels.iter().find(|c| &c.id == id));
window.set_is_dm(dm.is_some());
// Encryption is not advertised until the transport has a verified nonce/message-ID contract.
window.set_e2ee_enabled(false);
window.set_dm_partner_name(dm.map_or("", |c| c.other_nickname.as_str()).into());
window.set_show_voice(state.showing_voice);
window.set_show_chat(!state.showing_voice && (dm.is_some() || channel.is_some()));
window.set_show_friends(!state.showing_voice && dm.is_none() && channel.is_none());
if let Some(dm) = dm {
window.set_active_channel_name(dm.other_nickname.as_str().into());
set_messages(
window,
state,
state
.dm_messages
.get(&dm.dm_id)
.map(Vec::as_slice)
.unwrap_or_default(),
own_user_id,
);
replace_model(window.get_members(), Vec::<MemberItem>::new(), |m| {
window.set_members(m)
});
} else if let Some(channel) = channel {
window.set_active_channel_name(channel.name.as_str().into());
set_messages(
window,
state,
state
.messages
.get(&channel.id)
.map(Vec::as_slice)
.unwrap_or_default(),
own_user_id,
);
set_members_state(window, state, &channel.id);
}
// User edits are immediately written back through input-edited. Only a real
// state change (navigation/send/edit) should replace the editor contents.
if window.get_input_text().as_str() != state.chat_input {
window.set_input_text(state.chat_input.as_str().into());
}
}
fn sync_friends(window: &MainWindow, state: &UiState) {
let friends: Vec<FriendItem> = state
.friends
.iter()
.map(|f| FriendItem {
user_id: SharedString::from(f.user_id.as_str()),
nickname: SharedString::from(f.nickname.as_str()),
status_label: presence_label(
state
.presences
.get(&f.user_id)
.map(|p| p.status.as_str())
.unwrap_or("offline"),
)
.into(),
status: SharedString::from(
state
.presences
.get(&f.user_id)
.map(|p| p.status.as_str())
.unwrap_or("offline"),
),
})
.collect();
replace_model(window.get_friends(), friends, |model| {
window.set_friends(model)
});
let pending: Vec<FriendItem> = state
.pending_friend_requests
.iter()
.map(|f| FriendItem {
user_id: SharedString::from(f.user_id.as_str()),
nickname: SharedString::from(f.nickname.as_str()),
status: "pending".into(),
status_label: presence_label("pending").into(),
})
.collect();
replace_model(window.get_pending_requests(), pending, |model| {
window.set_pending_requests(model)
});
let blocked: Vec<SharedString> = state
.blocked_users
.iter()
.map(|u| SharedString::from(u.as_str()))
.collect();
replace_model(window.get_blocked_users(), blocked, |model| {
window.set_blocked_users(model)
});
window.set_friends_active_tab(SharedString::from(state.friends_tab.as_str()));
}
pub fn sync_voice(window: &MainWindow, state: &UiState) {
let members: Vec<VoiceMemberItem> = state
.channels
.iter()
.filter(|c| {
matches!(
&state.voice,
crate::ui::state::VoiceUiState::Active { channel } if channel == &c.id
)
})
.flat_map(|c| c.members.iter())
.map(|nick| {
let user_id = state
.user_names
.iter()
.find(|(_, n)| *n == nick)
.map(|(id, _)| id.clone())
.unwrap_or_else(|| nick.clone());
VoiceMemberItem {
avatar_text: nick.chars().take(2).collect::<String>().into(),
user_id: SharedString::from(user_id.clone()),
nickname: SharedString::from(nick.as_str()),
speaking: state.remote_speaking && state.last_remote_speaker_id == user_id,
}
})
.collect();
replace_model(window.get_voice_members(), members, |model| {
window.set_voice_members(model)
});
window.set_mic_enabled(state.mic_enabled);
window.set_deafened(state.deafen);
if let crate::ui::state::VoiceUiState::Active { channel } = &state.voice {
if let Some(ch) = state.channels.iter().find(|c| &c.id == channel) {
window.set_voice_channel_name(SharedString::from(ch.name.as_str()));
}
} else {
window.set_voice_channel_name("".into());
}
window.set_voice_connected(matches!(
state.voice,
crate::ui::state::VoiceUiState::Active { .. }
));
let banner = match (state.local_speaking, state.remote_speaking) {
(true, true) => crate::ui::i18n::text("You and others are talking"),
(true, false) => crate::ui::i18n::text("You are talking"),
(false, true) => crate::ui::i18n::format(
"{name} is talking",
&[(
"name",
state.nick_for(&state.last_remote_speaker_id).to_string(),
)],
),
_ => String::new(),
};
window.set_activity_banner(SharedString::from(banner));
}
pub fn sync_connect(window: &MainWindow, state: &UiState) {
let bookmarks: Vec<BookmarkItem> = state
.bookmarks
.iter()
.map(|bm| BookmarkItem {
name: SharedString::from(bm.label.as_str()),
address: SharedString::from(bm.address.as_str()),
})
.collect();
replace_model(window.get_bookmarks(), bookmarks, |model| {
window.set_bookmarks(model)
});
window.set_status_text(SharedString::from(
state
.status_msg
.as_ref()
.map(ToString::to_string)
.unwrap_or_default(),
));
window.set_connecting(matches!(
state.conn,
ConnState::Connecting | ConnState::Reconnecting { .. }
));
window.set_auto_reconnect(state.auto_reconnect);
{
use crate::ui::state::TrustPromptKind;
let (kind, address, received, expected) = match &state.trust_prompt {
None => (0, "", "", ""),
Some(prompt) => (
match prompt.kind {
TrustPromptKind::FirstUse => 1,
TrustPromptKind::FingerprintChanged => 2,
TrustPromptKind::IdentityChanged => 3,
},
prompt.address.as_str(),
prompt.received.as_str(),
prompt.expected.as_str(),
),
};
window.set_trust_kind(kind);
window.set_trust_address(SharedString::from(address));
window.set_trust_received(SharedString::from(
vnox_client::net::trust::display_fingerprint(received),
));
window.set_trust_expected(SharedString::from(
vnox_client::net::trust::display_fingerprint(expected),
));
}
if let ConnState::Connected { ref node_name, .. } = state.conn {
window.set_node_name(SharedString::from(node_name.as_str()));
} else if matches!(state.conn, ConnState::Disconnected) {
window.set_node_name(SharedString::from(""));
}
}
pub fn sync_settings(window: &MainWindow, state: &UiState, own_pubkey: &str) {
window.set_identity_pubkey(SharedString::from(own_pubkey));
window.set_vault_enabled(state.vault_enabled);
window.set_voice_bitrate(state.voice_bitrate);
window.set_vad_mode(state.vad_mode as i32);
window.set_volume(state.output_volume);
window.set_settings_open(state.settings_open);
window.set_input_device_index(state.mic_device as i32);
window.set_output_device_index(state.output_device as i32);
window.set_passphrase_error(SharedString::from(
state
.settings_passphrase_error
.as_ref()
.map(ToString::to_string)
.unwrap_or_default(),
));
window.set_export_output(SharedString::from(
state.settings_export_output.as_deref().unwrap_or(""),
));
window.set_export_error(SharedString::from(
state
.settings_export_error
.as_ref()
.map(ToString::to_string)
.unwrap_or_default(),
));
window.set_import_error(SharedString::from(
state
.settings_import_error
.as_ref()
.map(ToString::to_string)
.unwrap_or_default(),
));
}
pub fn sync_ui(
window: &MainWindow,
state: &UiState,
own_user_id: &str,
own_nickname: &str,
own_pubkey: &str,
) {
sync_sidebar(window, state, own_nickname);
sync_chat(window, state, own_user_id);
sync_friends(window, state);
sync_voice(window, state);
sync_connect(window, state);
sync_settings(window, state, own_pubkey);
}