Repository navigation
Expand file tree
/
Copy pathUserInterface.Encryption.cs
More file actions
89 lines (88 loc) · 5.31 KB
/
Copy pathUserInterface.Encryption.cs
File metadata and controls
89 lines (88 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using TCPTunnel.Cryptography;
namespace TCPTunnel
{
public partial class UserInterface
{
private static SecureInvitation expectedInvitation;
internal static bool ConnectInvitation(string text)
{
try
{
var invitation = SecureInvitation.Parse(text);
expectedInvitation = invitation;
if (invitation.Port > 0) return DoConnect(invitation.Host, invitation.Port);
if (!EnsureNickname()) return false;
var connection = BluetoothConnector.Connect(invitation.BluetoothAddress, out string error);
if (connection == null) throw new System.IO.IOException(error);
return RunPreparedConnection(connection, "Bluetooth " + BluetoothSupport.FormatAddress(invitation.BluetoothAddress), false);
}
catch (Exception error) when (error is System.IO.IOException or FormatException or ArgumentException or System.Text.Json.JsonException)
{ ConsoleGraphic.WriteContentLine(error.Message); return false; }
finally { expectedInvitation = null; }
}
private static bool HandleEncryptionCommand(string input)
{
string[] parts = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 0) return false;
string command = parts[0].ToLowerInvariant();
if (command is not ("/invite" or "/pending" or "/approve" or "/identity")) return false;
bool validArguments = command == "/approve" ? parts.Length is 2 or 3 : command == "/pending" ? parts.Length is 1 or 2 : parts.Length == 1;
if (!validArguments)
{
CommandHelp.Show(command, (text, _) => WriteSystemChatLine(text));
return true;
}
try
{
if (command == "/identity" && parts.Length == 1)
{ using var identity = ApplicationSettings.OpenIdentity(); WriteSystemChatLine(identity.Fingerprint); CopyPublicText(identity.Fingerprint, TextId.PublicFingerprintCopied); return true; }
SecureHubService hub = ServerInterface.SecureHub;
if (!isLocalHubSession || hub == null) throw new InvalidOperationException(Lang.Current == AppLanguage.Russian ? "Команда доступна владельцу своего E2E Hub." : "This command requires your own E2E Hub.");
if (command == "/invite" && parts.Length == 1)
{
string invitation = hub.Invitation;
if (!CopyPublicText(invitation, TextId.InvitationCopied)) WriteSystemChatLine(invitation);
WriteSystemChatLine(hub.OwnerFingerprint);
}
else if (command == "/pending")
{
if (parts.Length == 2)
{
if (!TryApprovalNumber(parts[1], out long number)) { CommandHelp.Show(command, (text, _) => WriteSystemChatLine(text)); return true; }
var request = hub.GetPendingRequest(number);
WritePendingRequest(request); CopyPublicText(request.Fingerprint, TextId.PublicFingerprintCopied);
}
else
{
var requests = hub.PendingRequestsSnapshot();
if (requests.Length == 0) WriteSystemChatLine(Lang.Get(TextId.ApprovalNoPending));
foreach (var request in requests) WritePendingRequest(request);
if (requests.Length > 0) WriteSystemChatLine(Lang.Get(TextId.ApprovalVerifyHint));
}
}
else if (command == "/approve" && parts.Length == 2)
{
if (!TryApprovalNumber(parts[1], out long number)) { CommandHelp.Show(command, (text, _) => WriteSystemChatLine(text)); return true; }
var approved = hub.Approve(number); WriteSystemChatLine(Lang.Get(TextId.ApprovalSucceeded, approved.Nickname));
}
else if (command == "/approve" && parts.Length == 3)
{ hub.Approve(parts[1].TrimStart('@'), parts[2]); WriteSystemChatLine(Lang.Get(TextId.ApprovalSucceeded, parts[1].TrimStart('@'))); }
else CommandHelp.Show(command, (text, _) => WriteSystemChatLine(text));
}
catch (Exception error) when (error is ArgumentException or InvalidOperationException or UnauthorizedAccessException or System.IO.IOException)
{ WriteSystemChatLine(error.Message); }
return true;
}
private static bool TryApprovalNumber(string value, out long number) => Int64.TryParse(value, System.Globalization.NumberStyles.None, System.Globalization.CultureInfo.InvariantCulture, out number) && number > 0;
private static void WritePendingRequest(PendingRoomRequest request)
{
foreach (string line in request.DisplayLines()) WriteSystemChatLine(line);
}
private static bool CopyPublicText(string text, TextId success)
{
if (WindowsClipboard.TryCopy(text, out string error)) { WriteSystemChatLine(Lang.Get(success)); return true; }
WriteSystemChatLine(Lang.Get(TextId.ClipboardCopyFailed, error)); return false;
}
}
}