Added WebRTC stuff - Needs Testing.

This commit is contained in:
2026-03-29 15:18:57 -04:00
parent 0bb3aa28b1
commit 8c6724038a
8 changed files with 417 additions and 170 deletions

View File

@@ -41,6 +41,22 @@ public class ChatTest : WebSocketBehavior
HandleGetHistory(msg);
return;
}
SocketRtcSignalMessage? rtcProbe = null;
try
{
rtcProbe = JsonSerializer.Deserialize<SocketRtcSignalMessage>(msg);
}
catch
{
// ignored
}
if (rtcProbe?.Type == "encrypted_rtc_signal")
{
HandleEncryptedRtcSignal(msg);
return;
}
HandleEncryptedClientMessage(msg);
}
@@ -323,4 +339,74 @@ public class ChatTest : WebSocketBehavior
return $"{table}:{recordId}";
}
private void HandleEncryptedRtcSignal(string msg)
{
SocketRtcSignalMessage? clientPayload;
try
{
clientPayload = JsonSerializer.Deserialize<SocketRtcSignalMessage>(msg);
}
catch
{
Console.WriteLine("Failed to parse encrypted RTC signal payload.");
return;
}
if (clientPayload is null || clientPayload.Type != "encrypted_rtc_signal")
return;
if (ClientKeyService is null || string.IsNullOrWhiteSpace(ServerPrivateKey))
{
Console.WriteLine("Server RTC crypto dependencies are not initialized.");
return;
}
string plainJson;
try
{
plainJson = E2EeHelper.DecryptForRecipient(
new EncryptedPayload
{
CipherText = clientPayload.CipherText,
Nonce = clientPayload.Nonce,
Tag = clientPayload.Tag,
EncryptedKey = clientPayload.EncryptedKey
},
ServerPrivateKey
);
}
catch (Exception ex)
{
Console.WriteLine($"Failed to decrypt RTC signal payload: {ex.Message}");
return;
}
var targetClient = Task.Run(async () => await ClientKeyService.GetByUsernameAsync(clientPayload.RecipientUsername))
.GetAwaiter()
.GetResult();
if (targetClient is null)
{
Console.WriteLine($"No target RTC client key found for {clientPayload.RecipientUsername}");
return;
}
var encrypted = E2EeHelper.EncryptForRecipient(plainJson, targetClient.PublicKey);
var outbound = new SocketRtcSignalMessage
{
Type = "encrypted_rtc_signal",
SenderUsername = clientPayload.SenderUsername,
RecipientUsername = clientPayload.RecipientUsername,
CipherText = encrypted.CipherText,
Nonce = encrypted.Nonce,
Tag = encrypted.Tag,
EncryptedKey = encrypted.EncryptedKey
};
Sessions.Broadcast(JsonSerializer.Serialize(outbound));
}
}