feat: add E2EE DMs, group chats, local serversettings.json, and security/perf fixes
This commit is contained in:
parent
b60b5a0774
commit
3b52de20d1
33 changed files with 5107 additions and 366 deletions
|
|
@ -6,6 +6,7 @@ using System.Threading.Tasks;
|
|||
using GsDChat.Client.Network;
|
||||
using GsDChat.Client.Rendering;
|
||||
using GsDChat.Client.State;
|
||||
using GsDChat.Core.Models;
|
||||
|
||||
namespace GsDChat.Client.Screens;
|
||||
|
||||
|
|
@ -27,8 +28,53 @@ public class ChatScreen : IScreen
|
|||
public void Render(int width, int height, StringBuilder sb)
|
||||
{
|
||||
var ch = _state.ActiveChannel;
|
||||
string chName = ch != null ? $"#{ch.Name}" : "(no channel)";
|
||||
string topic = ch != null && !string.IsNullOrEmpty(ch.Topic) ? $" - {ch.Topic}" : "";
|
||||
string chName;
|
||||
string topic = "";
|
||||
|
||||
if (ch == null)
|
||||
{
|
||||
chName = "(no channel)";
|
||||
}
|
||||
else if (ch.Type == ChannelTypes.DirectMessage)
|
||||
{
|
||||
string e2eeTag;
|
||||
if (!_state.AllowDmE2ee)
|
||||
{
|
||||
e2eeTag = $"{Ansi.BrightYellow}[Plaintext / Server E2EE Off]{Ansi.Reset}";
|
||||
}
|
||||
else if (!_state.AccountE2eeEnabled || !ch.IsE2ee)
|
||||
{
|
||||
e2eeTag = $"{Ansi.BrightYellow}[⚠ Plaintext / E2EE Disabled]{Ansi.Reset}";
|
||||
}
|
||||
else if (!_state.HasUnlockedE2eeKey)
|
||||
{
|
||||
e2eeTag = $"{Ansi.BrightYellow}[🔑 E2EE Vault Locked — Enter PIN]{Ansi.Reset}";
|
||||
}
|
||||
else if (string.IsNullOrWhiteSpace(_state.GetPeerPublicKeyForDm(ch)))
|
||||
{
|
||||
e2eeTag = $"{Ansi.BrightYellow}[⚠ Plaintext — Peer E2EE Not Set Up]{Ansi.Reset}";
|
||||
}
|
||||
else
|
||||
{
|
||||
e2eeTag = $"{Ansi.BrightGreen}[🔒 Full E2EE]{Ansi.Reset}";
|
||||
}
|
||||
chName = $"DM @{ch.Name.TrimStart('@')}";
|
||||
topic = $" {e2eeTag}";
|
||||
}
|
||||
else if (ch.Type == ChannelTypes.Group)
|
||||
{
|
||||
int maxU = ch.MaxUsers > 0 ? ch.MaxUsers : _state.MaxGroupChatUsers;
|
||||
string membersList = ch.MemberUsernames.Count > 0 ? string.Join(", @", ch.MemberUsernames) : "";
|
||||
chName = $"[Group] {ch.Name} ({ch.MemberIds.Count}/{maxU})";
|
||||
topic = !string.IsNullOrEmpty(ch.Topic)
|
||||
? $" - {ch.Topic}"
|
||||
: (!string.IsNullOrEmpty(membersList) ? $" - @{membersList}" : "");
|
||||
}
|
||||
else
|
||||
{
|
||||
chName = $"#{ch.Name}";
|
||||
topic = !string.IsNullOrEmpty(ch.Topic) ? $" - {ch.Topic}" : "";
|
||||
}
|
||||
|
||||
// Header line
|
||||
int onlineCount = _state.Users.Count(u => u.IsOnline);
|
||||
|
|
@ -40,12 +86,10 @@ public class ChatScreen : IScreen
|
|||
// Messages area: height minus (Header: 2, Footer/Input: 3, Tab bar & status: 3) = height - 8
|
||||
int msgAreaHeight = Math.Max(3, height - 8);
|
||||
|
||||
List<Core.Models.ChatMessage> messages = new();
|
||||
if (!string.IsNullOrEmpty(_state.ActiveChannelId) &&
|
||||
_state.ChannelMessages.TryGetValue(_state.ActiveChannelId, out var msgs))
|
||||
{
|
||||
messages = msgs;
|
||||
}
|
||||
List<ChatMessage> messages = _state.GetMessagesForChannel(_state.ActiveChannelId);
|
||||
|
||||
bool hasMore = !string.IsNullOrEmpty(_state.ActiveChannelId) &&
|
||||
_state.ChannelHasMoreHistory.TryGetValue(_state.ActiveChannelId, out bool hm) && hm;
|
||||
|
||||
// Viewport calculation with scroll offset
|
||||
int totalMessages = messages.Count;
|
||||
|
|
@ -56,8 +100,14 @@ public class ChatScreen : IScreen
|
|||
|
||||
var visible = messages.Skip(startIndex).Take(countToTake).ToList();
|
||||
|
||||
// Print blank lines if fewer messages than viewport height
|
||||
int emptyLines = msgAreaHeight - visible.Count;
|
||||
if (startIndex == 0 && hasMore && emptyLines > 0)
|
||||
{
|
||||
sb.AppendLine($" {Ansi.Gray}[↑ Press PageUp or /history to load older messages from server ↑]{Ansi.Reset}");
|
||||
emptyLines--;
|
||||
}
|
||||
|
||||
// Print blank lines if fewer messages than viewport height
|
||||
for (int i = 0; i < emptyLines; i++)
|
||||
{
|
||||
sb.AppendLine();
|
||||
|
|
@ -70,17 +120,31 @@ public class ChatScreen : IScreen
|
|||
{
|
||||
"Owner" => Ansi.BrightYellow,
|
||||
"Admin" => Ansi.BrightMagenta,
|
||||
"System" => Ansi.BrightGreen,
|
||||
_ => Ansi.BrightCyan
|
||||
};
|
||||
|
||||
string line;
|
||||
if (!string.IsNullOrEmpty(msg.AttachmentId))
|
||||
string lockBadge = msg.IsEncrypted ? $"{Ansi.BrightGreen}🔒 {Ansi.Reset}" : "";
|
||||
string displayContent = msg.Content;
|
||||
if (displayContent.StartsWith("E2EE:v1:", StringComparison.Ordinal))
|
||||
{
|
||||
line = $" {Ansi.Gray}[{time}]{Ansi.Reset} <{roleColor}@{msg.SenderUsername}{Ansi.Reset}> {Ansi.BrightYellow}📎 [{msg.AttachmentName} - {FileStorageServiceFormat(msg.AttachmentSizeBytes ?? 0)}]{Ansi.Reset}";
|
||||
displayContent = !_state.HasUnlockedE2eeKey
|
||||
? $"{Ansi.BrightYellow}[🔒 Encrypted Message — Enter E2EE PIN or '/e2ee pin <pin>' to unlock]{Ansi.Reset}"
|
||||
: $"{Ansi.BrightYellow}[🔒 Encrypted with previous E2EE key]{Ansi.Reset}";
|
||||
}
|
||||
|
||||
string line;
|
||||
if (msg.SenderRole == "System")
|
||||
{
|
||||
line = $" {Ansi.Gray}[{time}]{Ansi.Reset} {Ansi.BrightGreen}*** {msg.Content}{Ansi.Reset}";
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(msg.AttachmentId))
|
||||
{
|
||||
line = $" {Ansi.Gray}[{time}]{Ansi.Reset} {lockBadge}<{roleColor}@{msg.SenderUsername}{Ansi.Reset}> {Ansi.BrightYellow}📎 [{msg.AttachmentName} - {FileStorageServiceFormat(msg.AttachmentSizeBytes ?? 0)}]{Ansi.Reset}";
|
||||
}
|
||||
else
|
||||
{
|
||||
line = $" {Ansi.Gray}[{time}]{Ansi.Reset} <{roleColor}@{msg.SenderUsername}{Ansi.Reset}> {msg.Content}";
|
||||
line = $" {Ansi.Gray}[{time}]{Ansi.Reset} {lockBadge}<{roleColor}@{msg.SenderUsername}{Ansi.Reset}> {displayContent}";
|
||||
}
|
||||
|
||||
sb.AppendLine(PadOrTruncate(line, width));
|
||||
|
|
@ -89,14 +153,68 @@ public class ChatScreen : IScreen
|
|||
// Divider
|
||||
sb.AppendLine(new string('─', Math.Max(0, width - 1)));
|
||||
|
||||
// Input Line
|
||||
string prompt = $" {Ansi.Cyan}[{chName}] > {Ansi.Reset}";
|
||||
string inputDisplay = _state.InputBuffer + "█";
|
||||
sb.AppendLine(prompt + inputDisplay);
|
||||
// Input Line or Lazy E2EE PIN Prompt
|
||||
if (_state.IsPromptingE2eePin)
|
||||
{
|
||||
string pinPromptLabel = _state.HasServerKeyVault
|
||||
? $"{Ansi.BrightYellow}[🔑 Unlock E2EE Key Vault] Enter E2EE PIN (Esc=Plaintext): {Ansi.Reset}"
|
||||
: $"{Ansi.BrightGreen}[🔑 Setup E2EE Key Vault] Create E2EE PIN (Esc=Plaintext): {Ansi.Reset}";
|
||||
string maskedPin = new string('*', _state.E2eePinInputBuffer.Length) + "█";
|
||||
sb.AppendLine($" {pinPromptLabel}{maskedPin}");
|
||||
}
|
||||
else
|
||||
{
|
||||
string prompt = $" {Ansi.Cyan}[{chName}] > {Ansi.Reset}";
|
||||
string inputDisplay = _state.InputBuffer + "█";
|
||||
sb.AppendLine(prompt + inputDisplay);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task HandleKeyAsync(ConsoleKeyInfo key)
|
||||
{
|
||||
if (_state.IsPromptingE2eePin)
|
||||
{
|
||||
if (key.Key == ConsoleKey.Enter)
|
||||
{
|
||||
string pin = _state.E2eePinInputBuffer.Trim();
|
||||
_state.E2eePinInputBuffer = string.Empty;
|
||||
if (string.IsNullOrEmpty(pin))
|
||||
{
|
||||
_state.SetStatus("PIN cannot be empty (Press Esc to skip and use plaintext).");
|
||||
return;
|
||||
}
|
||||
|
||||
var (ok, msg) = await _connection.UnlockOrInitializeE2eeWithPinAsync(pin, resetKeys: false);
|
||||
_state.SetStatus(msg, 5);
|
||||
if (ok)
|
||||
{
|
||||
_state.IsPromptingE2eePin = false;
|
||||
}
|
||||
_state.NeedsRedraw = true;
|
||||
}
|
||||
else if (key.Key == ConsoleKey.Escape)
|
||||
{
|
||||
_state.IsPromptingE2eePin = false;
|
||||
_state.E2eePinInputBuffer = string.Empty;
|
||||
_state.SetStatus("E2EE PIN prompt skipped. Use '/e2ee pin <pin>' anytime to unlock.");
|
||||
_state.NeedsRedraw = true;
|
||||
}
|
||||
else if (key.Key == ConsoleKey.Backspace)
|
||||
{
|
||||
if (_state.E2eePinInputBuffer.Length > 0)
|
||||
{
|
||||
_state.E2eePinInputBuffer = _state.E2eePinInputBuffer[..^1];
|
||||
_state.NeedsRedraw = true;
|
||||
}
|
||||
}
|
||||
else if (!char.IsControl(key.KeyChar))
|
||||
{
|
||||
_state.E2eePinInputBuffer += key.KeyChar;
|
||||
_state.NeedsRedraw = true;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (key.Key == ConsoleKey.Enter)
|
||||
{
|
||||
string text = _state.InputBuffer.Trim();
|
||||
|
|
@ -133,6 +251,13 @@ public class ChatScreen : IScreen
|
|||
else if (key.Key == ConsoleKey.PageUp)
|
||||
{
|
||||
_state.ChatScrollOffset += 5;
|
||||
if (!string.IsNullOrEmpty(_state.ActiveChannelId) &&
|
||||
_state.ChannelHasMoreHistory.TryGetValue(_state.ActiveChannelId, out bool hasMore) &&
|
||||
hasMore)
|
||||
{
|
||||
await _connection.FetchOlderHistoryAsync(_state.ActiveChannelId, 50);
|
||||
_state.SetStatus("Loading older messages from server...", 2);
|
||||
}
|
||||
_state.NeedsRedraw = true;
|
||||
}
|
||||
else if (key.Key == ConsoleKey.PageDown)
|
||||
|
|
@ -163,18 +288,21 @@ public class ChatScreen : IScreen
|
|||
case "join":
|
||||
if (string.IsNullOrEmpty(args))
|
||||
{
|
||||
_state.SetStatus("Usage: /join <channel_name>");
|
||||
_state.SetStatus("Usage: /join <channel_or_group_or_dm>");
|
||||
return;
|
||||
}
|
||||
var target = _state.Channels.FirstOrDefault(c => c.Name.Equals(args.TrimStart('#'), StringComparison.OrdinalIgnoreCase));
|
||||
string cleanTarget = args.TrimStart('#', '@');
|
||||
var target = _state.Channels.FirstOrDefault(c => c.Name.Equals(cleanTarget, StringComparison.OrdinalIgnoreCase))
|
||||
?? _state.GroupChats.FirstOrDefault(g => g.Name.Equals(cleanTarget, StringComparison.OrdinalIgnoreCase))
|
||||
?? _state.DirectMessages.FirstOrDefault(d => d.Name.TrimStart('@').Equals(cleanTarget, StringComparison.OrdinalIgnoreCase));
|
||||
if (target != null)
|
||||
{
|
||||
await _connection.JoinChannelAsync(target.Id);
|
||||
_state.SetStatus($"Joined #{target.Name}");
|
||||
_state.SetStatus($"Switched to {target.Name}");
|
||||
}
|
||||
else
|
||||
{
|
||||
_state.SetStatus($"Channel #{args} not found.");
|
||||
_state.SetStatus($"Conversation '{args}' not found.");
|
||||
}
|
||||
break;
|
||||
|
||||
|
|
@ -190,6 +318,117 @@ public class ChatScreen : IScreen
|
|||
await _connection.CreateChannelAsync(newName, newTopic);
|
||||
break;
|
||||
|
||||
case "dm":
|
||||
if (string.IsNullOrEmpty(args))
|
||||
{
|
||||
_state.SetStatus("Usage: /dm <username> [message]");
|
||||
return;
|
||||
}
|
||||
var dmParts = args.Split(' ', 2);
|
||||
string dmUser = dmParts[0].TrimStart('@');
|
||||
await _connection.StartDmAsync(dmUser);
|
||||
_state.SetStatus($"Opening DM with @{dmUser}...");
|
||||
if (dmParts.Length > 1 && !string.IsNullOrWhiteSpace(dmParts[1]))
|
||||
{
|
||||
await Task.Delay(100);
|
||||
var dmConv = _state.DirectMessages.FirstOrDefault(d => d.Name.TrimStart('@').Equals(dmUser, StringComparison.OrdinalIgnoreCase));
|
||||
if (dmConv != null)
|
||||
{
|
||||
await _connection.SendChatMessageAsync(dmConv.Id, dmParts[1]);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case "e2ee":
|
||||
var e2eeTokens = args.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (e2eeTokens.Length == 0)
|
||||
{
|
||||
_state.IsPromptingE2eePin = true;
|
||||
_state.NeedsRedraw = true;
|
||||
return;
|
||||
}
|
||||
string e2eeSub = e2eeTokens[0].ToLowerInvariant();
|
||||
string e2eeArg = e2eeTokens.Length > 1 ? e2eeTokens[1].Trim() : string.Empty;
|
||||
if ((e2eeSub == "pin" || e2eeSub == "unlock") && !string.IsNullOrEmpty(e2eeArg))
|
||||
{
|
||||
var (ok, msg) = await _connection.UnlockOrInitializeE2eeWithPinAsync(e2eeArg, resetKeys: false);
|
||||
_state.SetStatus(msg, 5);
|
||||
}
|
||||
else if (e2eeSub == "reset" && !string.IsNullOrEmpty(e2eeArg))
|
||||
{
|
||||
var (ok, msg) = await _connection.UnlockOrInitializeE2eeWithPinAsync(e2eeArg, resetKeys: true);
|
||||
_state.SetStatus(msg, 5);
|
||||
}
|
||||
else if (e2eeSub == "lock")
|
||||
{
|
||||
_connection.LockE2eeKeys();
|
||||
}
|
||||
else if ((e2eeSub == "dm" || e2eeSub == "account") && !string.IsNullOrEmpty(e2eeArg))
|
||||
{
|
||||
bool enable = e2eeArg.Equals("on", StringComparison.OrdinalIgnoreCase) ||
|
||||
e2eeArg.Equals("true", StringComparison.OrdinalIgnoreCase) ||
|
||||
e2eeArg.Equals("1", StringComparison.OrdinalIgnoreCase);
|
||||
await _connection.ToggleE2eeAsync(e2eeSub, enable, _state.ActiveChannelId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_state.SetStatus("Usage: /e2ee pin <pin> | /e2ee reset <new_pin> | /e2ee lock | /e2ee dm <on|off> | /e2ee account <on|off>", 6);
|
||||
}
|
||||
break;
|
||||
|
||||
case "history":
|
||||
case "more":
|
||||
if (!string.IsNullOrEmpty(_state.ActiveChannelId))
|
||||
{
|
||||
await _connection.FetchOlderHistoryAsync(_state.ActiveChannelId, 50);
|
||||
_state.SetStatus("Fetching older history from server...", 3);
|
||||
}
|
||||
break;
|
||||
|
||||
case "group":
|
||||
case "gcreate":
|
||||
string gArgs = cmd == "gcreate" ? $"create {args}" : args;
|
||||
var gTokens = gArgs.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
||||
if (gTokens.Length == 0)
|
||||
{
|
||||
_state.SetStatus("Usage: /group create <name> [user1,user2] | /group invite <user> | /group leave");
|
||||
return;
|
||||
}
|
||||
string sub = gTokens[0].ToLowerInvariant();
|
||||
if (sub == "create" && gTokens.Length >= 2)
|
||||
{
|
||||
string gName = gTokens[1];
|
||||
var members = gTokens.Skip(2).ToList();
|
||||
await _connection.CreateGroupChatAsync(gName, members);
|
||||
_state.SetStatus($"Creating group '{gName}'...");
|
||||
}
|
||||
else if (sub == "invite" && gTokens.Length >= 2)
|
||||
{
|
||||
await _connection.InviteToGroupAsync(_state.ActiveChannelId, gTokens[1]);
|
||||
}
|
||||
else if (sub == "leave")
|
||||
{
|
||||
await _connection.LeaveGroupAsync(_state.ActiveChannelId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_state.SetStatus("Usage: /group create <name> [user1,user2] | /group invite <user> | /group leave");
|
||||
}
|
||||
break;
|
||||
|
||||
case "invite":
|
||||
if (string.IsNullOrEmpty(args))
|
||||
{
|
||||
_state.SetStatus("Usage: /invite <username>");
|
||||
return;
|
||||
}
|
||||
await _connection.InviteToGroupAsync(_state.ActiveChannelId, args);
|
||||
break;
|
||||
|
||||
case "leave":
|
||||
await _connection.LeaveGroupAsync(_state.ActiveChannelId);
|
||||
break;
|
||||
|
||||
case "upload":
|
||||
if (string.IsNullOrEmpty(args))
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue