Compare commits
29 Commits
c3b8dc5061
...
RTC-Rewrit
| Author | SHA1 | Date | |
|---|---|---|---|
| 87ade75f1d | |||
| 798652cb4d | |||
| 6a650a282b | |||
| 5b10afcec2 | |||
| 1220654656 | |||
| be797c55c2 | |||
| 20e99fefec | |||
| f77a5eb823 | |||
| d6ecb63b5f | |||
| 687387b105 | |||
| 5486795f4d | |||
| b90144d335 | |||
| 9a3e435dd4 | |||
| c4dfb07627 | |||
| 92c432cd49 | |||
| a52ae2f4a4 | |||
| 0c9ff3b5d9 | |||
| 3b75c2b785 | |||
| 4f6bbcf6e2 | |||
| a2608ffab9 | |||
| b70189c619 | |||
| 88c5d597d3 | |||
| 4a8170c448 | |||
| a56e246095 | |||
| fba86881ec | |||
| 28be2ae6c3 | |||
| 627d67be39 | |||
| 085507519a | |||
| a67f94b08e |
@@ -62,8 +62,6 @@
|
||||
<!-- <WebView Source="test.html"/> -->
|
||||
<Grid RowDefinitions="Auto,*"
|
||||
ColumnDefinitions="*">
|
||||
<Button Text="Send message to JavaScript"
|
||||
Clicked="OnSendMessageButtonClicked" />
|
||||
<HybridWebView x:Name="hybridWebView"
|
||||
RawMessageReceived="OnHybridWebViewRawMessageReceived"
|
||||
Grid.Row="1" />
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
using RelayClient.Crypto;
|
||||
using WebSocketSharp;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json.Serialization.Metadata;
|
||||
using System.Text.Json.Serialization;
|
||||
using RelayClient.Crypto;
|
||||
using RelayClient.Services;
|
||||
using RelayShared.Rtc;
|
||||
using RelayShared.Services;
|
||||
|
||||
namespace RelayClient;
|
||||
|
||||
|
||||
public partial class MainPage : ContentPage
|
||||
{
|
||||
private readonly string _username;
|
||||
private readonly WebSocket _wsc;
|
||||
private string? _serverPublicKey;
|
||||
private readonly RelaySocketClient _socket;
|
||||
private readonly RtcBridgeService _rtc;
|
||||
|
||||
private string? _currentChannelId;
|
||||
private string? _currentChannelName;
|
||||
|
||||
@@ -33,19 +32,31 @@ public partial class MainPage : ContentPage
|
||||
KeyStorage.SavePublicKey(_username, keys.publicKey);
|
||||
}
|
||||
|
||||
_wsc = new WebSocket("ws://localhost:1337/");
|
||||
|
||||
_wsc.OnMessage += WscOnMessage;
|
||||
_wsc.Connect();
|
||||
|
||||
var publicKey = KeyStorage.LoadPublicKey(_username);
|
||||
_wsc.Send($"REGISTER_KEY|{_username}|{publicKey}");
|
||||
_wsc.Send("GET_SERVER_KEY");
|
||||
_wsc.Send("GET_CHANNELS");
|
||||
|
||||
hybridWebView.SetInvokeJavaScriptTarget(this);
|
||||
ServerAPI.setupClient();
|
||||
|
||||
_socket = new RelaySocketClient(_username);
|
||||
_rtc = new RtcBridgeService(
|
||||
_username,
|
||||
_socket,
|
||||
hybridWebView,
|
||||
() => _currentChannelId,
|
||||
SafeSendRawToWebView
|
||||
);
|
||||
|
||||
hybridWebView.SetInvokeJavaScriptTarget(_rtc);
|
||||
|
||||
_socket.Log += Console.WriteLine;
|
||||
_socket.ChannelListReceived += HandleChannelList;
|
||||
_socket.EncryptedChatReceived += HandleEncryptedChat;
|
||||
_socket.EncryptedRtcSignalReceived += payload =>
|
||||
{
|
||||
MainThread.BeginInvokeOnMainThread(async () =>
|
||||
{
|
||||
await _rtc.HandleIncomingRtcSignalAsync(payload);
|
||||
});
|
||||
};
|
||||
|
||||
_socket.Connect();
|
||||
}
|
||||
|
||||
private void SendButton_OnClicked(object? sender, EventArgs e)
|
||||
@@ -65,7 +76,7 @@ public partial class MainPage : ContentPage
|
||||
if (string.IsNullOrWhiteSpace(text))
|
||||
return;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(_serverPublicKey))
|
||||
if (string.IsNullOrWhiteSpace(_socket.ServerPublicKey))
|
||||
{
|
||||
Console.WriteLine("Server public key not loaded yet.");
|
||||
return;
|
||||
@@ -77,12 +88,12 @@ public partial class MainPage : ContentPage
|
||||
return;
|
||||
}
|
||||
|
||||
var encrypted = E2EeHelper.EncryptForRecipient(text, _serverPublicKey);
|
||||
var encrypted = E2EeHelper.EncryptForRecipient(text, _socket.ServerPublicKey);
|
||||
|
||||
var payload = new SocketEncryptedMessage
|
||||
{
|
||||
ChannelId = _currentChannelId!,
|
||||
Type = "client_encrypted_chat",
|
||||
Type = SignalType.ClientEncryptedChat,
|
||||
SenderUsername = _username,
|
||||
CipherText = encrypted.CipherText,
|
||||
Nonce = encrypted.Nonce,
|
||||
@@ -90,8 +101,7 @@ public partial class MainPage : ContentPage
|
||||
EncryptedKey = encrypted.EncryptedKey
|
||||
};
|
||||
|
||||
var json = JsonSerializer.Serialize(payload);
|
||||
_wsc.Send(json);
|
||||
_socket.SendJson(payload);
|
||||
|
||||
Console.WriteLine($"[{_username}] sent encrypted message.");
|
||||
|
||||
@@ -99,34 +109,8 @@ public partial class MainPage : ContentPage
|
||||
MessageEntry.Focus();
|
||||
}
|
||||
|
||||
private void WscOnMessage(object? sender, MessageEventArgs e)
|
||||
private void HandleChannelList(SocketChannelList channelList)
|
||||
{
|
||||
if (e.Data.StartsWith("SERVER:REGISTERED_KEY:"))
|
||||
{
|
||||
Console.WriteLine(e.Data);
|
||||
return;
|
||||
}
|
||||
|
||||
SafeSendRawToWebView($"[{_username}] RAW WS DATA: {e.Data}");
|
||||
|
||||
Console.WriteLine($"[{_username}] RAW WS DATA: {e.Data}");
|
||||
|
||||
try
|
||||
{
|
||||
using var doc = JsonDocument.Parse(e.Data);
|
||||
var root = doc.RootElement;
|
||||
|
||||
if (!root.TryGetProperty("Type", out var typeElement))
|
||||
return;
|
||||
|
||||
var type = typeElement.GetString();
|
||||
|
||||
if (type == "channel_list")
|
||||
{
|
||||
var channelList = JsonSerializer.Deserialize<SocketChannelList>(e.Data);
|
||||
if (channelList is null)
|
||||
return;
|
||||
|
||||
_channels.Clear();
|
||||
_channels.AddRange(channelList.Channels.OrderBy(c => c.CreatedAt));
|
||||
|
||||
@@ -136,8 +120,8 @@ public partial class MainPage : ContentPage
|
||||
.FirstOrDefault()
|
||||
?? _channels.OrderBy(c => c.CreatedAt).FirstOrDefault();
|
||||
|
||||
if (defaultChannel is not null)
|
||||
{
|
||||
if (defaultChannel is null) return;
|
||||
|
||||
_currentChannelId = defaultChannel.ChannelId;
|
||||
_currentChannelName = defaultChannel.Name;
|
||||
|
||||
@@ -145,42 +129,23 @@ public partial class MainPage : ContentPage
|
||||
{
|
||||
ChannelLabel.Text = $"#{_currentChannelName}";
|
||||
RenderChannelList();
|
||||
await PushRtcContextToJsAsync();
|
||||
await _rtc.PushRtcContextToJsAsync();
|
||||
});
|
||||
|
||||
_wsc.Send($"GET_HISTORY|{_username}|{_currentChannelId}");
|
||||
_socket.SendRaw($"GET_HISTORY|{_username}|{_currentChannelId}");
|
||||
}
|
||||
|
||||
private void HandleEncryptedChat(SocketEncryptedMessage payload) {
|
||||
if (payload.RecipientUsername != _username)
|
||||
return;
|
||||
}
|
||||
|
||||
if (type == "server_public_key")
|
||||
string decryptedText;
|
||||
|
||||
try
|
||||
{
|
||||
var serverKeyMessage = JsonSerializer.Deserialize<ServerPublicKeyMessage>(e.Data);
|
||||
if (serverKeyMessage is not null)
|
||||
{
|
||||
_serverPublicKey = serverKeyMessage.PublicKey;
|
||||
Console.WriteLine($"[{_username}] loaded server public key.");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (type == "encrypted_rtc_signal")
|
||||
{
|
||||
var payload = JsonSerializer.Deserialize<SocketRtcSignalMessage>(e.Data);
|
||||
if (payload is null)
|
||||
return;
|
||||
|
||||
if (payload.ChannelId != _currentChannelId)
|
||||
return;
|
||||
|
||||
if (payload.SenderUsername == _username)
|
||||
return;
|
||||
|
||||
var privateKey = KeyStorage.LoadPrivateKey(_username);
|
||||
|
||||
var decryptedJson = E2EeHelper.DecryptForRecipient(
|
||||
decryptedText = E2EeHelper.DecryptForRecipient(
|
||||
new EncryptedPayload
|
||||
{
|
||||
CipherText = payload.CipherText,
|
||||
@@ -190,116 +155,26 @@ public partial class MainPage : ContentPage
|
||||
},
|
||||
privateKey
|
||||
);
|
||||
|
||||
MainThread.BeginInvokeOnMainThread(async () =>
|
||||
{
|
||||
await SendRtcSignalToJsAsync(decryptedJson);
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (type == "rtc_offer_updated" || type == "rtc_answer_updated" || type == "rtc_candidate_added" || type == "rtc_call_left")
|
||||
{
|
||||
var rtcNotification = JsonSerializer.Deserialize<RtcNotificationMessage>(e.Data);
|
||||
if (rtcNotification is null)
|
||||
return;
|
||||
|
||||
var notificationType = rtcNotification.Type ?? string.Empty;
|
||||
var notificationChannelId = rtcNotification.ChannelId ?? string.Empty;
|
||||
|
||||
if (notificationChannelId != _currentChannelId)
|
||||
return;
|
||||
|
||||
SafeSendRawToWebView("RTC notification received: " + notificationType + " for " + notificationChannelId);
|
||||
|
||||
MainThread.BeginInvokeOnMainThread(async () =>
|
||||
{
|
||||
switch (notificationType)
|
||||
{
|
||||
case "rtc_offer_updated":
|
||||
{
|
||||
var offer = await GetRtcOffer();
|
||||
await SendRtcSignalToJsAsync(offer);
|
||||
break;
|
||||
}
|
||||
case "rtc_answer_updated":
|
||||
{
|
||||
var answer = await ServerAPI.GetAnswerForChannelAsync(_currentChannelId);
|
||||
if (answer is not null)
|
||||
{
|
||||
await AnswerCallback(answer);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "rtc_candidate_added":
|
||||
{
|
||||
try
|
||||
{
|
||||
IceCandidate? iceCandidate = JsonSerializer.Deserialize<IceCandidate>(rtcNotification.Direction);
|
||||
IceCandidateCallback(iceCandidate);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SafeSendRawToWebView($"Candidate rejected: {ex.Message}");
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case "rtc_call_left":
|
||||
{
|
||||
SafeSendRawToWebView("RTC call left notification received.");
|
||||
RtcLeaveCallback();
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Console.WriteLine($"[{_username}] failed to decrypt chat: {ex.Message}");
|
||||
return;
|
||||
}
|
||||
|
||||
if (type != "encrypted_chat")
|
||||
return;
|
||||
|
||||
var pyload = JsonSerializer.Deserialize<SocketEncryptedMessage>(e.Data);
|
||||
if (pyload is null)
|
||||
return;
|
||||
|
||||
if (pyload.RecipientUsername == _username)
|
||||
return;
|
||||
|
||||
Console.WriteLine($"[{_username}] received encrypted payload for {pyload.RecipientUsername}");
|
||||
|
||||
var privKey = KeyStorage.LoadPrivateKey(_username);
|
||||
|
||||
var decryptedText = E2EeHelper.DecryptForRecipient(
|
||||
new EncryptedPayload
|
||||
{
|
||||
CipherText = pyload.CipherText,
|
||||
Nonce = pyload.Nonce,
|
||||
Tag = pyload.Tag,
|
||||
EncryptedKey = pyload.EncryptedKey
|
||||
},
|
||||
privKey
|
||||
);
|
||||
|
||||
Console.WriteLine($"[{_username}] decrypted message from {pyload.SenderUsername}: {decryptedText}");
|
||||
|
||||
var message = new ChatMessage
|
||||
{
|
||||
SenderUsername = pyload.SenderUsername,
|
||||
SenderUsername = payload.SenderUsername,
|
||||
Text = decryptedText,
|
||||
Timestamp = DateTime.Now
|
||||
};
|
||||
|
||||
if (!_messagesByChannel.ContainsKey(pyload.ChannelId))
|
||||
{
|
||||
_messagesByChannel[pyload.ChannelId] = [];
|
||||
}
|
||||
if (!_messagesByChannel.ContainsKey(payload.ChannelId))
|
||||
_messagesByChannel[payload.ChannelId] = [];
|
||||
|
||||
_messagesByChannel[pyload.ChannelId].Add(message);
|
||||
_messagesByChannel[payload.ChannelId].Add(message);
|
||||
|
||||
if (pyload.ChannelId == _currentChannelId)
|
||||
if (payload.ChannelId == _currentChannelId)
|
||||
{
|
||||
MainThread.BeginInvokeOnMainThread(() =>
|
||||
{
|
||||
@@ -307,16 +182,10 @@ public partial class MainPage : ContentPage
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[{_username}] failed to process websocket message: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnDisappearing()
|
||||
{
|
||||
_wsc.OnMessage -= WscOnMessage;
|
||||
_wsc.Close();
|
||||
_socket.Disconnect();
|
||||
base.OnDisappearing();
|
||||
}
|
||||
|
||||
@@ -326,9 +195,11 @@ public partial class MainPage : ContentPage
|
||||
|
||||
foreach (var channel in _channels.OrderBy(c => c.CreatedAt))
|
||||
{
|
||||
var button = new Button
|
||||
var button = new ChannelButton
|
||||
{
|
||||
Text = $"#{channel.Name}"
|
||||
Text = $"#{channel.Name}",
|
||||
Type = channel.Type,
|
||||
Group = channel.Group
|
||||
};
|
||||
|
||||
button.Clicked += (_, _) =>
|
||||
@@ -338,16 +209,22 @@ public partial class MainPage : ContentPage
|
||||
|
||||
MainThread.BeginInvokeOnMainThread(async () =>
|
||||
{
|
||||
await PushRtcContextToJsAsync();
|
||||
await _rtc.PushRtcContextToJsAsync();
|
||||
|
||||
if (channel.Type == ChannelType.Voice)
|
||||
{
|
||||
if (!RtcView.IsVisible)
|
||||
SwapView();
|
||||
|
||||
_ = _rtc.JoinRtcChannel();
|
||||
}
|
||||
});
|
||||
|
||||
ChannelLabel.Text = $"#{_currentChannelName}";
|
||||
RenderCurrentChannelMessages();
|
||||
|
||||
if (!_messagesByChannel.ContainsKey(channel.ChannelId))
|
||||
{
|
||||
_wsc.Send($"GET_HISTORY|{_username}|{channel.ChannelId}");
|
||||
}
|
||||
_socket.SendRaw($"GET_HISTORY|{_username}|{channel.ChannelId}");
|
||||
};
|
||||
|
||||
SidebarList.Children.Add(button);
|
||||
@@ -372,7 +249,7 @@ public partial class MainPage : ContentPage
|
||||
|
||||
private async void RenderSingleMessage(ChatMessage message)
|
||||
{
|
||||
bool isOwnMessage = message.SenderUsername == _username;
|
||||
var isOwnMessage = message.SenderUsername == _username;
|
||||
|
||||
var bubble = new Border
|
||||
{
|
||||
@@ -400,14 +277,13 @@ public partial class MainPage : ContentPage
|
||||
await MessagesScrollView.ScrollToAsync(MessagesLayout, ScrollToPosition.End, true);
|
||||
}
|
||||
|
||||
private void SwapView_OnClicked(object? sender, EventArgs e)
|
||||
private void SwapView()
|
||||
{
|
||||
if (RtcView.IsVisible)
|
||||
{
|
||||
MessagesScrollView.IsVisible = true;
|
||||
RtcView.IsVisible = false;
|
||||
ViewSwapped.Text = "Swap to Web View";
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -417,206 +293,20 @@ public partial class MainPage : ContentPage
|
||||
}
|
||||
}
|
||||
|
||||
#region RTC Functions
|
||||
public async Task<bool> JoinRtcChannel()
|
||||
private void SwapView_OnClicked(object? sender, EventArgs e)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_currentChannelId))
|
||||
return false;
|
||||
|
||||
_wsc.Send($"RTC_JOIN_CHANNEL|{_username}|{_currentChannelId}");
|
||||
|
||||
SafeSendRawToWebView($"Attempting to join RTC Channel {_currentChannelName} | {_currentChannelId} ");
|
||||
|
||||
bool active = await ServerAPI.GetIsChannelActiveAsync(_currentChannelId);
|
||||
|
||||
// SafeSendRawToWebView($"Rtc Channel {_currentChannelName} | {_currentChannelId} is active: {active}");
|
||||
|
||||
return active;
|
||||
}
|
||||
|
||||
public void LeaveRtcChannel()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_currentChannelId))
|
||||
return;
|
||||
|
||||
_wsc.Send($"RTC_LEAVE_CHANNEL|{_username}|{_currentChannelId}");
|
||||
}
|
||||
|
||||
public async void WriteRtcOffer(string json)
|
||||
{
|
||||
try
|
||||
{
|
||||
RtcDescription? description = JsonSerializer.Deserialize<RtcDescription>(json);
|
||||
DBOffer offer = new DBOffer
|
||||
{
|
||||
ChannelId = _currentChannelId,
|
||||
Username = _username,
|
||||
SessionDescription = description
|
||||
};
|
||||
await ServerAPI.PostOfferAsync(offer);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SafeSendRawToWebView(ex.Message);
|
||||
}
|
||||
|
||||
}
|
||||
public async Task<string> GetRtcOffer()
|
||||
{
|
||||
RtcDescription? offer = await ServerAPI.GetOffersForChannelAsync(_currentChannelId);
|
||||
return JsonSerializer.Serialize(offer);
|
||||
}
|
||||
|
||||
public async void WriteRtcAnswer(string json)
|
||||
{
|
||||
// SafeSendRawToWebView("WriteRtcAnswer entered with: " + json);
|
||||
|
||||
try
|
||||
{
|
||||
RtcDescription? description = JsonSerializer.Deserialize<RtcDescription>(json);
|
||||
DBOffer answer = new DBOffer
|
||||
{
|
||||
ChannelId = _currentChannelId,
|
||||
Username = _username,
|
||||
SessionDescription = description
|
||||
};
|
||||
await ServerAPI.PostAnswerAsync(answer);
|
||||
SafeSendRawToWebView("WriteRtcAnswer posted successfully");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SafeSendRawToWebView("WriteRtcAnswer failed: " + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async void WriteIceCandidate(string json)
|
||||
{
|
||||
try
|
||||
{
|
||||
IceCandidate? candidate = JsonSerializer.Deserialize<IceCandidate>(json);
|
||||
DBIceCandidate DBCandidate = new DBIceCandidate
|
||||
{
|
||||
ChannelId = _currentChannelId,
|
||||
Username = _username,
|
||||
Candidate = candidate
|
||||
};
|
||||
if (candidate == null) return;
|
||||
await ServerAPI.PostIceCandidateAsync(DBCandidate);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SafeSendRawToWebView("WriteIceCandidate failed: " + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async void IceCandidateCallback(IceCandidate candidate)
|
||||
{
|
||||
try
|
||||
{
|
||||
await hybridWebView.InvokeJavaScriptAsync("IceCandidateAdded", [candidate], [HybridJSType.Default.IceCandidate]);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SafeSendRawToWebView("WriteIceCandidate failed: " + ex.Message);
|
||||
}
|
||||
}
|
||||
public async Task AnswerCallback(RtcDescription answer)
|
||||
{
|
||||
answer.sdp = answer.sdp.Replace("\r\n", "(rn)");
|
||||
try
|
||||
{
|
||||
await hybridWebView.InvokeJavaScriptAsync("AnswerCallbackJS", [answer], [HybridJSType.Default.RtcDescription]);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SafeSendRawToWebView("AnswerCallback failed: " + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async void RtcLeaveCallback()
|
||||
{
|
||||
try
|
||||
{
|
||||
await hybridWebView.InvokeJavaScriptAsync("RtcLeaveCall", [], []);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SafeSendRawToWebView("RtcLeaveCallback failed: " + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SendRtcSignalToJsAsync(string rawJson)
|
||||
{
|
||||
var jsArg = JsonSerializer.Serialize(rawJson);
|
||||
await hybridWebView.EvaluateJavaScriptAsync($"window.handleRtcSignal({jsArg})");
|
||||
} //Remove?
|
||||
|
||||
private async Task PushRtcContextToJsAsync()
|
||||
{
|
||||
var usernameJson = JsonSerializer.Serialize(_username);
|
||||
var channelIdJson = JsonSerializer.Serialize(_currentChannelId);
|
||||
|
||||
await hybridWebView.EvaluateJavaScriptAsync($"window.setUsername({usernameJson})");
|
||||
await hybridWebView.EvaluateJavaScriptAsync($"window.setChannelId({channelIdJson})");
|
||||
|
||||
Console.WriteLine($"[{_username}] pushed RTC context into HybridWebView.");
|
||||
} //Remove?
|
||||
|
||||
public void SendRtcSignal(string json)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_serverPublicKey))
|
||||
{
|
||||
Console.WriteLine("Server public key not loaded yet.");
|
||||
return;
|
||||
}
|
||||
|
||||
RtcSignalMessage? rtcSignal;
|
||||
try
|
||||
{
|
||||
rtcSignal = JsonSerializer.Deserialize<RtcSignalMessage>(json);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Failed to parse RTC signal from JS: {ex.Message}");
|
||||
return;
|
||||
}
|
||||
|
||||
if (rtcSignal is null)
|
||||
return;
|
||||
|
||||
var encrypted = E2EeHelper.EncryptForRecipient(json, _serverPublicKey);
|
||||
|
||||
var payload = new SocketRtcSignalMessage
|
||||
{
|
||||
Type = "encrypted_rtc_signal",
|
||||
SenderUsername = _username,
|
||||
ChannelId = rtcSignal.ChannelId,
|
||||
CipherText = encrypted.CipherText,
|
||||
Nonce = encrypted.Nonce,
|
||||
Tag = encrypted.Tag,
|
||||
EncryptedKey = encrypted.EncryptedKey
|
||||
};
|
||||
|
||||
_wsc.Send(JsonSerializer.Serialize(payload));
|
||||
Console.WriteLine($"[{_username}] sent RTC signal: {rtcSignal.Type} -> {rtcSignal.ChannelId}");
|
||||
} //Remove?
|
||||
|
||||
|
||||
#endregion
|
||||
private void OnSendMessageButtonClicked(object sender, EventArgs e)
|
||||
{
|
||||
SafeSendRawToWebView($"Hello from C#!");
|
||||
SwapView();
|
||||
}
|
||||
|
||||
private async void OnHybridWebViewRawMessageReceived(object sender, HybridWebViewRawMessageReceivedEventArgs e)
|
||||
{
|
||||
if (e.Message == "rtc_page_ready")
|
||||
{
|
||||
await PushRtcContextToJsAsync();
|
||||
await _rtc.PushRtcContextToJsAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
await DisplayAlertAsync("Raw Message Received", e.Message, "OK");
|
||||
SafeSendRawToWebView($"JS RAW -> C#: {e.Message}");
|
||||
}
|
||||
|
||||
private void SafeSendRawToWebView(string message)
|
||||
@@ -634,6 +324,12 @@ public partial class MainPage : ContentPage
|
||||
});
|
||||
}
|
||||
|
||||
public class ChannelButton : Button
|
||||
{
|
||||
public ChannelType Type { get; set; }
|
||||
public string Group { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = false)]
|
||||
[JsonSerializable(typeof(RtcDescription))]
|
||||
[JsonSerializable(typeof(List<RtcSignalMessage>))]
|
||||
@@ -642,8 +338,5 @@ public partial class MainPage : ContentPage
|
||||
[JsonSerializable(typeof(string))]
|
||||
internal partial class HybridJSType : JsonSerializerContext
|
||||
{
|
||||
// This type's attributes specify JSON serialization info to preserve type structure
|
||||
// for trimmed builds.
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,11 +5,8 @@ namespace RelayClient;
|
||||
|
||||
public static class MauiProgram
|
||||
{
|
||||
// public static event Action<ChatMessage>? MessageSent;
|
||||
public static MauiApp CreateMauiApp()
|
||||
{
|
||||
//wsc.OnMessage += (sender, e) => OnWebSocketRecieved(sender, e);
|
||||
//wsc.Connect();
|
||||
var builder = MauiApp.CreateBuilder();
|
||||
builder.UseMauiApp<App>().ConfigureFonts(fonts =>
|
||||
{
|
||||
@@ -19,8 +16,6 @@ public static class MauiProgram
|
||||
fonts.AddFont("AnonymousPro-Regular.ttf", "AnonymousProRegular");
|
||||
});
|
||||
|
||||
|
||||
|
||||
#if DEBUG
|
||||
builder.Services.AddHybridWebViewDeveloperTools();
|
||||
builder.Logging.AddDebug();
|
||||
@@ -28,19 +23,4 @@ public static class MauiProgram
|
||||
|
||||
return builder.Build();
|
||||
}
|
||||
|
||||
//public static void OnWebSocketRecieved(object? sender, MessageEventArgs e)
|
||||
//{
|
||||
// Console.WriteLine(sender.ToString());
|
||||
//
|
||||
// ChatSimulator.Send(e.Data.Split(":")[0], e.Data.Split(":")[1]);
|
||||
// // var message = new ChatMessage
|
||||
// // {
|
||||
// // SenderUsername = e.Data.Split(":")[0],
|
||||
// // Text = e.Data.Split(":")[1],
|
||||
// // Timestamp = DateTime.Now
|
||||
// // };
|
||||
// //
|
||||
// // MessageSent?.Invoke(message);
|
||||
//}
|
||||
}
|
||||
@@ -87,3 +87,28 @@ textarea::-webkit-scrollbar-thumb {
|
||||
border-radius: 10px;
|
||||
padding: 12px;
|
||||
}
|
||||
|
||||
.remote-media-container {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
gap: 16px;
|
||||
align-items: flex-start;
|
||||
flex-wrap: nowrap;
|
||||
overflow-x: auto;
|
||||
padding: 8px 0;
|
||||
}
|
||||
|
||||
.remote-media-tile,
|
||||
.remote-tile {
|
||||
flex: 0 0 auto;
|
||||
width: 320px;
|
||||
}
|
||||
|
||||
.remote-media-tile video,
|
||||
.remote-tile video {
|
||||
width: 320px;
|
||||
height: 240px;
|
||||
background: #111;
|
||||
border-radius: 8px;
|
||||
object-fit: cover;
|
||||
}
|
||||
@@ -8,6 +8,9 @@
|
||||
<link rel="stylesheet" href="styles/app.css">
|
||||
<link rel="stylesheet" href="index.css">
|
||||
<script src="_framework/hybridwebview.js"></script>
|
||||
<script src="media.js"></script>
|
||||
<script src="relaySocket.js"></script>
|
||||
<script src="rtc.js"></script>
|
||||
<script src="index.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
@@ -16,8 +19,8 @@
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<button onclick="refreshDevicesAndPreview()">Refresh Devices</button>
|
||||
<button onclick="joinChannelCall()">Join Call</button>
|
||||
<button onclick="Media.refreshDevicesAndPreview()">Refresh Devices</button>
|
||||
<button onclick="RelayRtc.joinChannelCall()">Join Call</button>
|
||||
</div>
|
||||
|
||||
<div style="margin-top: 8px;">
|
||||
@@ -35,11 +38,7 @@
|
||||
<div id="localMediaStatus">Waiting for local media...</div>
|
||||
</div>
|
||||
|
||||
<div style="display: inline-block; vertical-align: top;">
|
||||
<video id="remoteVideo" autoplay playsinline style="width: 320px; height: 240px; background: #111;"></video>
|
||||
<div id="remoteVideoStatus">Remote video: waiting...</div>
|
||||
<div id="remoteMediaStatus">Remote media: waiting...</div>
|
||||
</div>
|
||||
<div id="remoteMediaContainer" class="remote-media-container"></div>
|
||||
</div>
|
||||
|
||||
<div style="margin-top: 10px;">
|
||||
|
||||
@@ -1,26 +1,23 @@
|
||||
let peerConnection = null;
|
||||
let localStream = null;
|
||||
let currentUsername = null;
|
||||
let currentUsername = null;
|
||||
let currentChannelId = null;
|
||||
let availableCameras = [];
|
||||
let availableMics = [];
|
||||
let candidateQueue = [];
|
||||
|
||||
const configuration = {
|
||||
iceServers: [
|
||||
{
|
||||
urls: [
|
||||
'stun:stun1.l.google.com:19302',
|
||||
'stun:stun2.l.google.com:19302',
|
||||
],
|
||||
},
|
||||
],
|
||||
iceCandidatePoolSize: 10,
|
||||
"stun:stun1.l.google.com:19302",
|
||||
"stun:stun2.l.google.com:19302"
|
||||
]
|
||||
}
|
||||
],
|
||||
iceCandidatePoolSize: 10
|
||||
};
|
||||
|
||||
window.setUsername = function (name) {
|
||||
currentUsername = name;
|
||||
LogMessage("Username set to: " + currentUsername);
|
||||
};
|
||||
|
||||
window.setChannelId = function (channelId) {
|
||||
currentChannelId = channelId;
|
||||
LogMessage("Channel set to: " + currentChannelId);
|
||||
@@ -28,510 +25,17 @@ window.setChannelId = function(channelId) {
|
||||
|
||||
function LogMessage(msg) {
|
||||
const messageLog = document.getElementById("messageLog");
|
||||
messageLog.value += '\r\n' + msg;
|
||||
|
||||
if (!messageLog) {
|
||||
console.log(msg);
|
||||
return;
|
||||
}
|
||||
|
||||
messageLog.value += "\r\n" + msg;
|
||||
messageLog.scrollTop = messageLog.scrollHeight;
|
||||
}
|
||||
|
||||
function hasVideoTrack() {
|
||||
return !!localStream && localStream.getVideoTracks().length > 0;
|
||||
}
|
||||
|
||||
function hasAudioTrack() {
|
||||
return !!localStream && localStream.getAudioTracks().length > 0;
|
||||
}
|
||||
|
||||
async function ensurePeerConnection() {
|
||||
if (peerConnection) return;
|
||||
|
||||
peerConnection = new RTCPeerConnection({
|
||||
iceServers: [{ urls: "stun:stun.l.google.com:19302" }]
|
||||
});
|
||||
|
||||
peerConnection.onicecandidate = (event) => {
|
||||
if (event.candidate) {
|
||||
LogMessage("ICE candidate gathered");
|
||||
}
|
||||
};
|
||||
|
||||
peerConnection.ontrack = (event) => {
|
||||
LogMessage("Remote track received");
|
||||
|
||||
const remoteVideo = document.getElementById("remoteVideo");
|
||||
const remoteVideoStatus = document.getElementById("remoteVideoStatus");
|
||||
const remoteMediaStatus = document.getElementById("remoteMediaStatus");
|
||||
|
||||
const stream = event.streams[0];
|
||||
const hasVideo = stream.getVideoTracks().length > 0;
|
||||
const hasAudio = stream.getAudioTracks().length > 0;
|
||||
|
||||
if (hasVideo) {
|
||||
remoteVideo.srcObject = stream;
|
||||
} else {
|
||||
remoteVideo.srcObject = null;
|
||||
}
|
||||
|
||||
if (remoteVideoStatus) {
|
||||
remoteVideoStatus.textContent = hasVideo
|
||||
? "Remote video: active"
|
||||
: "Remote video: unavailable";
|
||||
}
|
||||
|
||||
if (remoteMediaStatus) {
|
||||
remoteMediaStatus.textContent = `Remote media: audio=${hasAudio} video=${hasVideo}`;
|
||||
}
|
||||
};
|
||||
|
||||
peerConnection.onconnectionstatechange = () => {
|
||||
LogMessage("Connection state: " + peerConnection.connectionState);
|
||||
const remoteMediaStatus = document.getElementById("remoteMediaStatus");
|
||||
if (remoteMediaStatus && peerConnection.connectionState === "connected") {
|
||||
remoteMediaStatus.textContent += " | connected";
|
||||
}
|
||||
};
|
||||
|
||||
peerConnection.oniceconnectionstatechange = () => {
|
||||
LogMessage("ICE connection state: " + peerConnection.iceConnectionState);
|
||||
};
|
||||
|
||||
peerConnection.onicegatheringstatechange = () => {
|
||||
LogMessage("ICE gathering state: " + peerConnection.iceGatheringState);
|
||||
};
|
||||
} //Remove?
|
||||
async function ensureLocalMedia(forceReload = false) {
|
||||
const localMediaStatus = document.getElementById("localMediaStatus");
|
||||
const localVideoStatus = document.getElementById("localVideoStatus");
|
||||
const localVideo = document.getElementById("localVideo");
|
||||
const cameraSelect = document.getElementById("cameraSelect");
|
||||
const micSelect = document.getElementById("micSelect");
|
||||
|
||||
if (localStream && !forceReload) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (localStream) {
|
||||
localStream.getTracks().forEach(track => track.stop());
|
||||
localStream = null;
|
||||
}
|
||||
|
||||
let selectedCameraId = cameraSelect ? cameraSelect.value : "";
|
||||
let selectedMicId = micSelect ? micSelect.value : "";
|
||||
|
||||
const videoConstraint = selectedCameraId
|
||||
? { deviceId: { exact: selectedCameraId } }
|
||||
: false;
|
||||
|
||||
const audioConstraint = selectedMicId
|
||||
? { deviceId: { exact: selectedMicId } }
|
||||
: true;
|
||||
|
||||
try {
|
||||
localStream = await navigator.mediaDevices.getUserMedia({
|
||||
video: videoConstraint,
|
||||
audio: audioConstraint
|
||||
});
|
||||
|
||||
LogMessage("Local media initialized");
|
||||
} catch (err) {
|
||||
LogMessage("selected media failed: " + err);
|
||||
|
||||
try {
|
||||
localStream = await navigator.mediaDevices.getUserMedia({
|
||||
video: false,
|
||||
audio: audioConstraint
|
||||
});
|
||||
|
||||
LogMessage("Local media initialized with audio only fallback");
|
||||
} catch (audioErr) {
|
||||
LogMessage("audio-only failed: " + audioErr);
|
||||
|
||||
if (localMediaStatus) localMediaStatus.textContent = "Local media failed";
|
||||
if (localVideoStatus) localVideoStatus.textContent = "Local video: unavailable";
|
||||
if (localVideo) localVideo.srcObject = null;
|
||||
|
||||
throw audioErr;
|
||||
}
|
||||
}
|
||||
|
||||
const hasVideo = localStream.getVideoTracks().length > 0;
|
||||
const hasAudio = localStream.getAudioTracks().length > 0;
|
||||
|
||||
localVideo.srcObject = hasVideo ? localStream : null;
|
||||
|
||||
if (localVideoStatus) {
|
||||
localVideoStatus.textContent = hasVideo
|
||||
? "Local video: active"
|
||||
: "Local video: unavailable";
|
||||
}
|
||||
|
||||
if (localMediaStatus) {
|
||||
localMediaStatus.textContent = `Local media: audio=${hasAudio} video=${hasVideo}`;
|
||||
}
|
||||
|
||||
if (!hasVideo) {
|
||||
LogMessage("No camera available, continuing without video");
|
||||
}
|
||||
}
|
||||
|
||||
async function applyLocalStreamToPeerConnection() {
|
||||
if (!peerConnection || !localStream) return;
|
||||
|
||||
const senders = peerConnection.getSenders();
|
||||
|
||||
const audioTrack = localStream.getAudioTracks()[0] || null;
|
||||
const videoTrack = localStream.getVideoTracks()[0] || null;
|
||||
|
||||
const audioSender = senders.find(s => s.track && s.track.kind === "audio");
|
||||
const videoSender = senders.find(s => s.track && s.track.kind === "video");
|
||||
|
||||
if (audioSender) {
|
||||
await audioSender.replaceTrack(audioTrack);
|
||||
LogMessage("Replaced audio track on peer connection");
|
||||
} else if (audioTrack) {
|
||||
peerConnection.addTrack(audioTrack, localStream);
|
||||
LogMessage("Added audio track to peer connection");
|
||||
}
|
||||
|
||||
if (videoSender) {
|
||||
await videoSender.replaceTrack(videoTrack);
|
||||
LogMessage("Replaced video track on peer connection");
|
||||
} else if (videoTrack) {
|
||||
peerConnection.addTrack(videoTrack, localStream);
|
||||
LogMessage("Added video track to peer connection");
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshDevicesAndPreview() {
|
||||
await loadDevices();
|
||||
await ensureLocalMedia(true);
|
||||
|
||||
if (peerConnection) {
|
||||
await applyLocalStreamToPeerConnection();
|
||||
}
|
||||
}
|
||||
|
||||
async function joinChannelCall() {
|
||||
LogMessage("Current username: " + currentUsername);
|
||||
LogMessage("Current channel: " + currentChannelId);
|
||||
// LogMessage("Joining RTCChannel");
|
||||
let active = await window.HybridWebView.InvokeDotNet("JoinRtcChannel");
|
||||
await channelCallJoin(active);
|
||||
// LogMessage("Joined RTCChannel");
|
||||
// return;
|
||||
// try {
|
||||
// if (!currentChannelId) {
|
||||
// LogMessage("No current channel set.");
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// await ensurePeerConnection();
|
||||
// await ensureLocalMedia();
|
||||
//
|
||||
// LogMessage(`Joining call with media: audio=${hasAudioTrack()} video=${hasVideoTrack()}`);
|
||||
//
|
||||
// const payload = {
|
||||
// type: "rtc_join",
|
||||
// from: currentUsername,
|
||||
// channelId: currentChannelId
|
||||
// };
|
||||
//
|
||||
// LogMessage("Requesting join for channel " + currentChannelId);
|
||||
// await window.HybridWebView.InvokeDotNet("SendRtcSignal", [JSON.stringify(payload)]);
|
||||
// } catch (err) {
|
||||
// LogMessage("joinChannelCall failed: " + err);
|
||||
// }
|
||||
} //Combine with channelCallJoin
|
||||
|
||||
async function ensurePeerConnection2()
|
||||
{
|
||||
if (peerConnection) return;
|
||||
peerConnection = new RTCPeerConnection(configuration);
|
||||
|
||||
peerConnection.onicegatheringstatechange = () => {
|
||||
console.log(`ICE gathering state changed: ${peerConnection.iceGatheringState}`);
|
||||
};
|
||||
peerConnection.onconnectionstatechange = () => {
|
||||
console.log(`Connection state change: ${peerConnection.connectionState}`);
|
||||
};
|
||||
peerConnection.onsignalingstatechange = () => {
|
||||
console.log(`Signaling state change: ${peerConnection.signalingState}`);
|
||||
};
|
||||
peerConnection.oniceconnectionstatechange = () => {
|
||||
console.log(`ICE connection state change: ${peerConnection.iceConnectionState}`);
|
||||
};
|
||||
|
||||
peerConnection.onicecandidate = async (event) => {
|
||||
console.log(`Ice Candidate: ${JSON.stringify(event.candidate)}`);
|
||||
LogMessage(`Ice Candidate: ${JSON.stringify(event.candidate)}`);
|
||||
await window.HybridWebView.InvokeDotNet("WriteIceCandidate", [JSON.stringify(event.candidate)]);
|
||||
await IceCandidateAdded(event.candidate);
|
||||
};
|
||||
|
||||
peerConnection.ontrack = (event) => {
|
||||
LogMessage("Remote track received");
|
||||
|
||||
const remoteVideo = document.getElementById("remoteVideo");
|
||||
const remoteVideoStatus = document.getElementById("remoteVideoStatus");
|
||||
const remoteMediaStatus = document.getElementById("remoteMediaStatus");
|
||||
|
||||
const stream = event.streams[0];
|
||||
const hasVideo = stream.getVideoTracks().length > 0;
|
||||
const hasAudio = stream.getAudioTracks().length > 0;
|
||||
|
||||
if (hasVideo) {
|
||||
remoteVideo.srcObject = stream;
|
||||
} else {
|
||||
remoteVideo.srcObject = null;
|
||||
}
|
||||
|
||||
if (remoteVideoStatus) {
|
||||
remoteVideoStatus.textContent = hasVideo
|
||||
? "Remote video: active"
|
||||
: "Remote video: unavailable";
|
||||
}
|
||||
|
||||
if (remoteMediaStatus) {
|
||||
remoteMediaStatus.textContent = `Remote media: audio=${hasAudio} video=${hasVideo}`;
|
||||
}
|
||||
};
|
||||
}
|
||||
async function channelCallJoin(activeCall)
|
||||
{
|
||||
// LogMessage("Active call: " + activeCall);
|
||||
await ensurePeerConnection2();
|
||||
await ensureLocalMedia();
|
||||
await applyLocalStreamToPeerConnection();
|
||||
|
||||
if (activeCall)
|
||||
{
|
||||
const rawJson = await window.HybridWebView.InvokeDotNet("GetRtcOffer");
|
||||
const offer = typeof rawJson === "string" ? JSON.parse(rawJson) : rawJson;
|
||||
await peerConnection.setRemoteDescription(offer);
|
||||
const answer = await peerConnection.createAnswer();
|
||||
await peerConnection.setLocalDescription(answer);
|
||||
|
||||
// LogMessage("Joining call with media answer: " + JSON.stringify(answer));
|
||||
// LogMessage("Calling C# WriteRtcAnswer with: " + JSON.stringify(answer));
|
||||
await window.HybridWebView.InvokeDotNet("WriteRtcAnswer", [JSON.stringify(answer)]);
|
||||
LogMessage("C# WriteRtcAnswer invoked");
|
||||
//TODO: Update offer in SurrealDB to include answer
|
||||
}
|
||||
else
|
||||
{
|
||||
const offer = await peerConnection.createOffer();
|
||||
await peerConnection.setLocalDescription(offer);
|
||||
|
||||
await window.HybridWebView.InvokeDotNet("WriteRtcOffer", [JSON.stringify(offer)]);
|
||||
LogMessage(`Joining call with media offer: ${JSON.stringify(offer)}`);
|
||||
}
|
||||
}
|
||||
async function AnswerCallbackJS(answer)
|
||||
{
|
||||
answer.sdp = answer.sdp.replaceAll("(rn)", "\r\n");
|
||||
// LogMessage("Answer: " + JSON.stringify(answer));
|
||||
|
||||
// LogMessage("RemoteDescription: " + peerConnection.currentRemoteDescription);
|
||||
|
||||
if (!peerConnection.currentRemoteDescription && answer)
|
||||
{
|
||||
LogMessage("Current answer: " + JSON.stringify(answer));
|
||||
const desc = new RTCSessionDescription(answer);
|
||||
await peerConnection.setRemoteDescription(desc);
|
||||
for (const candidate of candidateQueue) {
|
||||
await peerConnection.addIceCandidate(candidate);
|
||||
}
|
||||
}
|
||||
}
|
||||
async function IceCandidateAdded(candidate)
|
||||
{
|
||||
if (peerConnection.currentRemoteDescription) {
|
||||
await peerConnection.addIceCandidate(candidate);
|
||||
LogMessage("ICE CANDIDATE ADDED: " + JSON.stringify(candidate));
|
||||
}
|
||||
else {
|
||||
LogMessage("RemoteDescription Missing")
|
||||
candidateQueue.push(candidate);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
async function RtcLeaveCall()
|
||||
{}
|
||||
async function handleRtcSignal(rawJson) {
|
||||
try {
|
||||
const msg = typeof rawJson === "string" ? JSON.parse(rawJson) : rawJson;
|
||||
|
||||
LogMessage("Received signal: " + msg.type + " from " + msg.from + " in " + msg.channelId);
|
||||
|
||||
await ensurePeerConnection();
|
||||
|
||||
if (msg.type === "rtc_join_state") {
|
||||
if (msg.isInitiator) {
|
||||
LogMessage("No active call found. Becoming initiator.");
|
||||
|
||||
const offer = await peerConnection.createOffer();
|
||||
await peerConnection.setLocalDescription(offer);
|
||||
// await waitForIceGatheringComplete(peerConnection);
|
||||
|
||||
const payload = {
|
||||
type: "rtc_offer",
|
||||
from: currentUsername,
|
||||
channelId: currentChannelId,
|
||||
sdp: peerConnection.localDescription.sdp
|
||||
};
|
||||
|
||||
LogMessage("Sending offer to channel " + currentChannelId);
|
||||
await window.HybridWebView.InvokeDotNet("SendRtcSignal", [JSON.stringify(payload)]);
|
||||
} else {
|
||||
LogMessage("Active call exists. Waiting for stored offer.");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.type === "rtc_offer") {
|
||||
LogMessage("Incoming channel call offer from " + msg.from);
|
||||
await ensureLocalMedia();
|
||||
|
||||
LogMessage(`Answering call with media: audio=${hasAudioTrack()} video=${hasVideoTrack()}`);
|
||||
LogMessage("Applying remote offer");
|
||||
|
||||
await peerConnection.setRemoteDescription({
|
||||
type: "offer",
|
||||
sdp: msg.sdp
|
||||
});
|
||||
|
||||
const answer = await peerConnection.createAnswer();
|
||||
await peerConnection.setLocalDescription(answer);
|
||||
// await waitForIceGatheringComplete(peerConnection);
|
||||
|
||||
const payload = {
|
||||
type: "rtc_answer",
|
||||
from: currentUsername,
|
||||
channelId: msg.channelId,
|
||||
sdp: peerConnection.localDescription.sdp
|
||||
};
|
||||
|
||||
LogMessage("Sending answer to channel " + msg.channelId);
|
||||
await window.HybridWebView.InvokeDotNet("SendRtcSignal", [JSON.stringify(payload)]);
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.type === "rtc_answer") {
|
||||
LogMessage("Applying remote answer");
|
||||
|
||||
await peerConnection.setRemoteDescription({
|
||||
type: "answer",
|
||||
sdp: msg.sdp
|
||||
});
|
||||
|
||||
LogMessage("Remote answer applied");
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.type === "rtc_ice_candidate") {
|
||||
LogMessage("Applying remote ICE candidate");
|
||||
|
||||
await peerConnection.addIceCandidate({
|
||||
candidate: msg.candidate,
|
||||
sdpMid: msg.sdpMid,
|
||||
sdpMLineIndex: msg.sdpMLineIndex
|
||||
});
|
||||
|
||||
LogMessage("Remote ICE candidate applied");
|
||||
return;
|
||||
}
|
||||
|
||||
LogMessage("Unhandled signal type: " + msg.type);
|
||||
} catch (err) {
|
||||
LogMessage("handleRtcSignal failed: " + err);
|
||||
}
|
||||
} //Remove?
|
||||
|
||||
async function loadDevices() {
|
||||
try {
|
||||
const devices = await navigator.mediaDevices.enumerateDevices();
|
||||
|
||||
availableCameras = devices.filter(d => d.kind === "videoinput");
|
||||
availableMics = devices.filter(d => d.kind === "audioinput");
|
||||
|
||||
const cameraSelect = document.getElementById("cameraSelect");
|
||||
const micSelect = document.getElementById("micSelect");
|
||||
|
||||
if (!cameraSelect || !micSelect) {
|
||||
LogMessage("Device dropdowns not found.");
|
||||
return;
|
||||
}
|
||||
|
||||
cameraSelect.innerHTML = "";
|
||||
micSelect.innerHTML = "";
|
||||
|
||||
const noCameraOption = document.createElement("option");
|
||||
noCameraOption.value = "";
|
||||
noCameraOption.text = "No camera / audio-only";
|
||||
cameraSelect.appendChild(noCameraOption);
|
||||
|
||||
const noMicOption = document.createElement("option");
|
||||
noMicOption.value = "";
|
||||
noMicOption.text = "Default microphone";
|
||||
micSelect.appendChild(noMicOption);
|
||||
|
||||
for (const cam of availableCameras) {
|
||||
const option = document.createElement("option");
|
||||
option.value = cam.deviceId;
|
||||
option.text = cam.label || `Camera ${cameraSelect.options.length}`;
|
||||
cameraSelect.appendChild(option);
|
||||
}
|
||||
|
||||
for (const mic of availableMics) {
|
||||
const option = document.createElement("option");
|
||||
option.value = mic.deviceId;
|
||||
option.text = mic.label || `Microphone ${micSelect.options.length + 1}`;
|
||||
micSelect.appendChild(option);
|
||||
}
|
||||
|
||||
LogMessage(`Loaded devices: ${availableCameras.length} cameras, ${availableMics.length} mics`);
|
||||
} catch (err) {
|
||||
LogMessage("loadDevices failed: " + err);
|
||||
}
|
||||
}
|
||||
|
||||
function wireDeviceSelectors() {
|
||||
const cameraSelect = document.getElementById("cameraSelect");
|
||||
const micSelect = document.getElementById("micSelect");
|
||||
|
||||
if (cameraSelect) {
|
||||
cameraSelect.onchange = async () => {
|
||||
LogMessage("Camera changed");
|
||||
await ensureLocalMedia(true);
|
||||
await applyLocalStreamToPeerConnection();
|
||||
};
|
||||
}
|
||||
|
||||
if (micSelect) {
|
||||
micSelect.onchange = async () => {
|
||||
LogMessage("Microphone changed");
|
||||
await ensureLocalMedia(true);
|
||||
await applyLocalStreamToPeerConnection();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function waitForIceGatheringComplete(pc) {
|
||||
if (pc.iceGatheringState === "complete") return;
|
||||
|
||||
await new Promise(resolve => {
|
||||
function checkState() {
|
||||
if (pc.iceGatheringState === "complete") {
|
||||
pc.removeEventListener("icegatheringstatechange", checkState);
|
||||
resolve();
|
||||
}
|
||||
}
|
||||
|
||||
pc.addEventListener("icegatheringstatechange", checkState);
|
||||
});
|
||||
} //Remove?
|
||||
|
||||
// window.handleRtcSignal = handleRtcSignal;
|
||||
window.LogMessage = LogMessage;
|
||||
|
||||
window.addEventListener("HybridWebViewMessageReceived", function (e) {
|
||||
LogMessage("Raw message: " + e.detail.message);
|
||||
@@ -539,8 +43,10 @@ window.addEventListener("HybridWebViewMessageReceived", function (e) {
|
||||
|
||||
window.addEventListener("load", async () => {
|
||||
LogMessage("RTC page loaded");
|
||||
|
||||
window.HybridWebView.SendRawMessage("rtc_page_ready");
|
||||
await loadDevices();
|
||||
wireDeviceSelectors();
|
||||
await ensureLocalMedia(true);
|
||||
|
||||
Media.wireDeviceSelectors();
|
||||
await Media.loadDevices();
|
||||
await Media.ensureLocalMedia();
|
||||
});
|
||||
261
RelayClient/Resources/Raw/wwwroot/media.js
Normal file
261
RelayClient/Resources/Raw/wwwroot/media.js
Normal file
@@ -0,0 +1,261 @@
|
||||
let localStream = null;
|
||||
const remoteStreams = {};
|
||||
|
||||
const Media = {
|
||||
async loadDevices() {
|
||||
const devices = await navigator.mediaDevices.enumerateDevices();
|
||||
|
||||
const cameras = devices.filter(d => d.kind === "videoinput");
|
||||
const mics = devices.filter(d => d.kind === "audioinput");
|
||||
|
||||
const cameraSelect = document.getElementById("cameraSelect");
|
||||
const micSelect = document.getElementById("micSelect");
|
||||
|
||||
if (!cameraSelect || !micSelect) return;
|
||||
|
||||
const selectedCamera = cameraSelect.value;
|
||||
const selectedMic = micSelect.value;
|
||||
|
||||
cameraSelect.innerHTML = "";
|
||||
micSelect.innerHTML = "";
|
||||
|
||||
const noCamera = document.createElement("option");
|
||||
noCamera.value = "";
|
||||
noCamera.textContent = "No camera / audio only";
|
||||
cameraSelect.appendChild(noCamera);
|
||||
|
||||
const defaultMic = document.createElement("option");
|
||||
defaultMic.value = "";
|
||||
defaultMic.textContent = "Default microphone";
|
||||
micSelect.appendChild(defaultMic);
|
||||
|
||||
for (const camera of cameras) {
|
||||
const option = document.createElement("option");
|
||||
option.value = camera.deviceId;
|
||||
option.textContent = camera.label || `Camera ${cameraSelect.length}`;
|
||||
cameraSelect.appendChild(option);
|
||||
}
|
||||
|
||||
for (const mic of mics) {
|
||||
const option = document.createElement("option");
|
||||
option.value = mic.deviceId;
|
||||
option.textContent = mic.label || `Microphone ${micSelect.length}`;
|
||||
micSelect.appendChild(option);
|
||||
}
|
||||
|
||||
cameraSelect.value = [...cameraSelect.options].some(o => o.value === selectedCamera)
|
||||
? selectedCamera
|
||||
: "";
|
||||
|
||||
micSelect.value = [...micSelect.options].some(o => o.value === selectedMic)
|
||||
? selectedMic
|
||||
: "";
|
||||
|
||||
LogMessage(`Loaded devices: ${cameras.length} cameras, ${mics.length} mics`);
|
||||
},
|
||||
|
||||
async ensureLocalMedia() {
|
||||
const cameraSelect = document.getElementById("cameraSelect");
|
||||
const micSelect = document.getElementById("micSelect");
|
||||
|
||||
if (localStream) {
|
||||
return localStream;
|
||||
}
|
||||
|
||||
const audioDeviceId = micSelect?.value || "";
|
||||
const videoDeviceId = cameraSelect?.value || "";
|
||||
|
||||
const constraints = {
|
||||
audio: audioDeviceId
|
||||
? { deviceId: { exact: audioDeviceId } }
|
||||
: true,
|
||||
video: videoDeviceId
|
||||
? { deviceId: { exact: videoDeviceId } }
|
||||
: false
|
||||
};
|
||||
|
||||
try {
|
||||
localStream = await navigator.mediaDevices.getUserMedia(constraints);
|
||||
} catch (err) {
|
||||
LogMessage("Selected media failed: " + err);
|
||||
|
||||
localStream = await navigator.mediaDevices.getUserMedia({
|
||||
audio: audioDeviceId
|
||||
? { deviceId: { exact: audioDeviceId } }
|
||||
: true,
|
||||
video: false
|
||||
});
|
||||
|
||||
LogMessage("No camera available, continuing without video");
|
||||
}
|
||||
|
||||
this.attachLocalStream(localStream);
|
||||
LogMessage("Local media initialized");
|
||||
|
||||
return localStream;
|
||||
},
|
||||
|
||||
attachLocalStream(stream) {
|
||||
const localVideo = document.getElementById("localVideo");
|
||||
const localMediaStatus = document.getElementById("localMediaStatus");
|
||||
const localVideoStatus = document.getElementById("localVideoStatus");
|
||||
|
||||
const audioTracks = stream.getAudioTracks();
|
||||
const videoTracks = stream.getVideoTracks();
|
||||
|
||||
if (localVideo) {
|
||||
localVideo.srcObject = videoTracks.length > 0 ? stream : null;
|
||||
}
|
||||
|
||||
if (localMediaStatus) {
|
||||
localMediaStatus.textContent =
|
||||
audioTracks.length > 0
|
||||
? "Microphone active"
|
||||
: "No microphone";
|
||||
}
|
||||
|
||||
if (localVideoStatus) {
|
||||
localVideoStatus.textContent =
|
||||
videoTracks.length > 0
|
||||
? "Local video active"
|
||||
: "Local video unavailable";
|
||||
}
|
||||
},
|
||||
|
||||
async restartLocalMedia() {
|
||||
if (localStream) {
|
||||
localStream.getTracks().forEach(track => track.stop());
|
||||
localStream = null;
|
||||
}
|
||||
|
||||
await this.ensureLocalMedia();
|
||||
|
||||
if (window.RelayRtc?.applyLocalStreamToAllPeerConnections) {
|
||||
await window.RelayRtc.applyLocalStreamToAllPeerConnections();
|
||||
}
|
||||
},
|
||||
|
||||
async refreshDevicesAndPreview() {
|
||||
if (localStream) {
|
||||
localStream.getTracks().forEach(track => track.stop());
|
||||
localStream = null;
|
||||
}
|
||||
|
||||
await this.loadDevices();
|
||||
await this.ensureLocalMedia();
|
||||
|
||||
if (window.RelayRtc?.applyLocalStreamToAllPeerConnections) {
|
||||
await window.RelayRtc.applyLocalStreamToAllPeerConnections();
|
||||
}
|
||||
},
|
||||
|
||||
async applyLocalStreamToPeerConnection(pc, username) {
|
||||
const stream = await this.ensureLocalMedia();
|
||||
const existingSenders = pc.getSenders();
|
||||
|
||||
for (const track of stream.getTracks()) {
|
||||
const existingSender = existingSenders.find(sender =>
|
||||
sender.track && sender.track.kind === track.kind
|
||||
);
|
||||
|
||||
if (existingSender) {
|
||||
await existingSender.replaceTrack(track);
|
||||
LogMessage(`Replaced local ${track.kind} track for ${username}`);
|
||||
} else {
|
||||
pc.addTrack(track, stream);
|
||||
LogMessage(`Added local ${track.kind} track for ${username}`);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
async applyLocalStreamToAllPeerConnections() {
|
||||
if (!window.RelayRtc?.peerConnections) return;
|
||||
|
||||
for (const [username, pc] of Object.entries(window.RelayRtc.peerConnections)) {
|
||||
await this.applyLocalStreamToPeerConnection(pc, username);
|
||||
}
|
||||
},
|
||||
|
||||
attachRemoteStream(username, stream) {
|
||||
remoteStreams[username] = stream;
|
||||
|
||||
const tile = this.ensureRemoteTile(username);
|
||||
const video = tile.querySelector("video");
|
||||
const status = tile.querySelector(".remote-media-status");
|
||||
|
||||
if (video) {
|
||||
video.srcObject = stream;
|
||||
}
|
||||
|
||||
const audioTracks = stream.getAudioTracks();
|
||||
const videoTracks = stream.getVideoTracks();
|
||||
|
||||
if (status) {
|
||||
status.textContent =
|
||||
`${audioTracks.length > 0 ? "Audio" : "No audio"} / ` +
|
||||
`${videoTracks.length > 0 ? "Video" : "No video"}`;
|
||||
}
|
||||
},
|
||||
|
||||
ensureRemoteTile(username) {
|
||||
const container = document.getElementById("remoteMediaContainer");
|
||||
if (!container) return null;
|
||||
|
||||
let tile = document.getElementById(`remote-tile-${username}`);
|
||||
if (tile) return tile;
|
||||
|
||||
tile = document.createElement("div");
|
||||
tile.id = `remote-tile-${username}`;
|
||||
tile.className = "remote-media-tile";
|
||||
|
||||
const title = document.createElement("div");
|
||||
title.className = "remote-media-title";
|
||||
title.textContent = username;
|
||||
|
||||
const video = document.createElement("video");
|
||||
video.autoplay = true;
|
||||
video.playsInline = true;
|
||||
|
||||
const status = document.createElement("div");
|
||||
status.className = "remote-media-status";
|
||||
status.textContent = "Remote media: waiting...";
|
||||
|
||||
tile.appendChild(title);
|
||||
tile.appendChild(video);
|
||||
tile.appendChild(status);
|
||||
|
||||
container.appendChild(tile);
|
||||
|
||||
return tile;
|
||||
},
|
||||
|
||||
removeRemoteStream(username) {
|
||||
delete remoteStreams[username];
|
||||
|
||||
const tile = document.getElementById(`remote-tile-${username}`);
|
||||
if (tile) {
|
||||
tile.remove();
|
||||
}
|
||||
},
|
||||
|
||||
wireDeviceSelectors() {
|
||||
const cameraSelect = document.getElementById("cameraSelect");
|
||||
const micSelect = document.getElementById("micSelect");
|
||||
|
||||
if (cameraSelect) {
|
||||
cameraSelect.addEventListener("change", async () => {
|
||||
LogMessage("Camera changed");
|
||||
await this.restartLocalMedia();
|
||||
});
|
||||
}
|
||||
|
||||
if (micSelect) {
|
||||
micSelect.addEventListener("change", async () => {
|
||||
LogMessage("Microphone changed");
|
||||
await this.restartLocalMedia();
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
window.Media = Media;
|
||||
46
RelayClient/Resources/Raw/wwwroot/relaySocket.js
Normal file
46
RelayClient/Resources/Raw/wwwroot/relaySocket.js
Normal file
@@ -0,0 +1,46 @@
|
||||
const RelaySocket = {
|
||||
async joinRtcChannel() {
|
||||
await window.HybridWebView.InvokeDotNet("JoinRtcChannel");
|
||||
},
|
||||
|
||||
async leaveRtcChannel() {
|
||||
await window.HybridWebView.InvokeDotNet("LeaveRtcChannel");
|
||||
},
|
||||
|
||||
async getRtcParticipants() {
|
||||
const raw = await window.HybridWebView.InvokeDotNet("GetRtcParticipants");
|
||||
|
||||
if (!raw) return [];
|
||||
|
||||
return typeof raw === "string"
|
||||
? JSON.parse(raw)
|
||||
: raw;
|
||||
},
|
||||
|
||||
async sendRtcSignal(signal) {
|
||||
if (!signal.channelId) signal.channelId = currentChannelId;
|
||||
if (!signal.from) signal.from = currentUsername;
|
||||
|
||||
await window.HybridWebView.InvokeDotNet("SendRtcSignal", [
|
||||
JSON.stringify(signal)
|
||||
]);
|
||||
},
|
||||
|
||||
receiveRtcSignal(rawJson) {
|
||||
LogMessage("RelaySocket.receiveRtcSignal hit");
|
||||
|
||||
if (window.RelayRtc?.handleRtcSignal) {
|
||||
LogMessage("Forwarding RTC signal to RelayRtc.handleRtcSignal");
|
||||
return window.RelayRtc.handleRtcSignal(rawJson);
|
||||
}
|
||||
|
||||
if (typeof window.handleRtcSignal === "function") {
|
||||
LogMessage("Forwarding RTC signal to window.handleRtcSignal");
|
||||
return window.handleRtcSignal(rawJson);
|
||||
}
|
||||
|
||||
LogMessage("No RTC signal handler registered.");
|
||||
}
|
||||
};
|
||||
|
||||
window.RelaySocket = RelaySocket;
|
||||
225
RelayClient/Resources/Raw/wwwroot/rtc.js
Normal file
225
RelayClient/Resources/Raw/wwwroot/rtc.js
Normal file
@@ -0,0 +1,225 @@
|
||||
const peerConnections = {};
|
||||
|
||||
async function joinChannelCall() {
|
||||
LogMessage("Current username: " + currentUsername);
|
||||
LogMessage("Current channel: " + currentChannelId);
|
||||
|
||||
if (!currentUsername || !currentChannelId) {
|
||||
LogMessage("Cannot join RTC: missing username or channel.");
|
||||
return;
|
||||
}
|
||||
|
||||
await RelaySocket.joinRtcChannel();
|
||||
await Media.ensureLocalMedia();
|
||||
|
||||
const participants = await RelaySocket.getRtcParticipants();
|
||||
|
||||
LogMessage("Participants: " + JSON.stringify(participants));
|
||||
|
||||
const existingUsers = participants.filter(x => x !== currentUsername);
|
||||
|
||||
if (existingUsers.length === 0) {
|
||||
LogMessage("Joined call as first participant. Waiting for others...");
|
||||
return;
|
||||
}
|
||||
|
||||
for (const username of existingUsers) {
|
||||
await sendOffer(username);
|
||||
}
|
||||
}
|
||||
|
||||
async function sendOffer(username) {
|
||||
const pc = await ensurePeerConnectionForUser(username);
|
||||
|
||||
await Media.applyLocalStreamToPeerConnection(pc, username);
|
||||
|
||||
const offer = await pc.createOffer();
|
||||
await pc.setLocalDescription(offer);
|
||||
|
||||
await RelaySocket.sendRtcSignal({
|
||||
type: "rtc_offer",
|
||||
channelId: currentChannelId,
|
||||
from: currentUsername,
|
||||
to: username,
|
||||
sdp: offer.sdp
|
||||
});
|
||||
|
||||
LogMessage(`Sent offer to ${username}`);
|
||||
}
|
||||
|
||||
async function handleRtcSignal(rawJson) {
|
||||
try {
|
||||
const msg = typeof rawJson === "string" ? JSON.parse(rawJson) : rawJson;
|
||||
|
||||
if (!msg || !msg.type) return;
|
||||
if (msg.from === currentUsername) return;
|
||||
|
||||
if (msg.to && msg.to !== currentUsername) {
|
||||
LogMessage(`Ignoring RTC signal meant for ${msg.to}`);
|
||||
return;
|
||||
}
|
||||
|
||||
LogMessage(`Received signal: ${msg.type} from ${msg.from}`);
|
||||
|
||||
if (msg.type === "rtc_offer") {
|
||||
await handleOffer(msg);
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.type === "rtc_answer") {
|
||||
await handleAnswer(msg);
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.type === "rtc_ice") {
|
||||
await handleIce(msg);
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.type === "rtc_leave") {
|
||||
closePeerConnection(msg.from);
|
||||
return;
|
||||
}
|
||||
|
||||
LogMessage("Unhandled RTC signal type: " + msg.type);
|
||||
} catch (err) {
|
||||
LogMessage("handleRtcSignal failed: " + err);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleOffer(msg) {
|
||||
const pc = await ensurePeerConnectionForUser(msg.from);
|
||||
|
||||
await Media.ensureLocalMedia();
|
||||
await Media.applyLocalStreamToPeerConnection(pc, msg.from);
|
||||
|
||||
await pc.setRemoteDescription({
|
||||
type: "offer",
|
||||
sdp: msg.sdp
|
||||
});
|
||||
|
||||
const answer = await pc.createAnswer();
|
||||
await pc.setLocalDescription(answer);
|
||||
|
||||
await RelaySocket.sendRtcSignal({
|
||||
type: "rtc_answer",
|
||||
channelId: currentChannelId,
|
||||
from: currentUsername,
|
||||
to: msg.from,
|
||||
sdp: answer.sdp
|
||||
});
|
||||
|
||||
LogMessage(`Sent answer to ${msg.from}`);
|
||||
}
|
||||
|
||||
async function handleAnswer(msg) {
|
||||
const pc = peerConnections[msg.from];
|
||||
|
||||
if (!pc) {
|
||||
LogMessage(`No peer connection found for answer from ${msg.from}`);
|
||||
return;
|
||||
}
|
||||
|
||||
await pc.setRemoteDescription({
|
||||
type: "answer",
|
||||
sdp: msg.sdp
|
||||
});
|
||||
|
||||
LogMessage(`Applied answer from ${msg.from}`);
|
||||
}
|
||||
|
||||
async function handleIce(msg) {
|
||||
const pc = peerConnections[msg.from];
|
||||
|
||||
if (!pc) {
|
||||
LogMessage(`No peer connection found for ICE from ${msg.from}`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!msg.candidate) return;
|
||||
|
||||
await pc.addIceCandidate(msg.candidate);
|
||||
|
||||
LogMessage(`Applied ICE from ${msg.from}`);
|
||||
}
|
||||
|
||||
async function ensurePeerConnectionForUser(username) {
|
||||
if (peerConnections[username]) {
|
||||
return peerConnections[username];
|
||||
}
|
||||
|
||||
const pc = new RTCPeerConnection(configuration);
|
||||
peerConnections[username] = pc;
|
||||
|
||||
pc.onicecandidate = async event => {
|
||||
if (!event.candidate) return;
|
||||
|
||||
await RelaySocket.sendRtcSignal({
|
||||
type: "rtc_ice",
|
||||
channelId: currentChannelId,
|
||||
from: currentUsername,
|
||||
to: username,
|
||||
candidate: JSON.stringify(event.candidate)
|
||||
});
|
||||
};
|
||||
|
||||
pc.ontrack = event => {
|
||||
LogMessage(`Remote track received from ${username}`);
|
||||
|
||||
const stream = event.streams[0];
|
||||
if (!stream) return;
|
||||
|
||||
Media.attachRemoteStream(username, stream);
|
||||
};
|
||||
|
||||
pc.onconnectionstatechange = () => {
|
||||
LogMessage(`Connection ${username}: ${pc.connectionState}`);
|
||||
|
||||
if (
|
||||
pc.connectionState === "failed" ||
|
||||
pc.connectionState === "closed" ||
|
||||
pc.connectionState === "disconnected"
|
||||
) {
|
||||
closePeerConnection(username);
|
||||
}
|
||||
};
|
||||
|
||||
return pc;
|
||||
}
|
||||
|
||||
async function leaveChannelCall() {
|
||||
await RelaySocket.sendRtcSignal({
|
||||
type: "rtc_leave",
|
||||
channelId: currentChannelId,
|
||||
from: currentUsername
|
||||
});
|
||||
|
||||
for (const username of Object.keys(peerConnections)) {
|
||||
closePeerConnection(username);
|
||||
}
|
||||
|
||||
await RelaySocket.leaveRtcChannel();
|
||||
|
||||
LogMessage("Left RTC channel");
|
||||
}
|
||||
|
||||
function closePeerConnection(username) {
|
||||
const pc = peerConnections[username];
|
||||
if (!pc) return;
|
||||
|
||||
pc.close();
|
||||
delete peerConnections[username];
|
||||
|
||||
Media.removeRemoteStream(username);
|
||||
|
||||
LogMessage(`Closed RTC connection with ${username}`);
|
||||
}
|
||||
|
||||
window.RelayRtc = {
|
||||
joinChannelCall,
|
||||
leaveChannelCall,
|
||||
handleRtcSignal,
|
||||
peerConnections
|
||||
};
|
||||
|
||||
window.handleRtcSignal = handleRtcSignal;
|
||||
@@ -111,6 +111,19 @@ public class ServerAPI
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonSerializer.Deserialize<RtcDescription>(json);
|
||||
}
|
||||
|
||||
public static async Task<List<string>> GetRtcParticipantsAsync(string? channelId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(channelId))
|
||||
return new List<string>();
|
||||
|
||||
HttpResponseMessage response = await client.GetAsync($"api/rtc/participants/{channelId}");
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
|
||||
return JsonSerializer.Deserialize<List<string>>(json) ?? new List<string>();
|
||||
}
|
||||
}
|
||||
|
||||
public class RtcDescription
|
||||
|
||||
127
RelayClient/Services/RelaySocketClient.cs
Normal file
127
RelayClient/Services/RelaySocketClient.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
using System.Text.Json;
|
||||
using RelayClient.Crypto;
|
||||
using RelayShared.Services;
|
||||
using WebSocketSharp;
|
||||
|
||||
namespace RelayClient.Services;
|
||||
|
||||
public sealed class RelaySocketClient
|
||||
{
|
||||
private readonly string _username;
|
||||
private readonly WebSocket _socket;
|
||||
|
||||
public string? ServerPublicKey { get; private set; }
|
||||
|
||||
public event Action<string>? RawMessageReceived;
|
||||
public event Action<SocketChannelList>? ChannelListReceived;
|
||||
public event Action<SocketEncryptedMessage>? EncryptedChatReceived;
|
||||
public event Action<SocketRtcSignalMessage>? EncryptedRtcSignalReceived;
|
||||
public event Action<string>? ServerPublicKeyReceived;
|
||||
public event Action<string>? Log;
|
||||
|
||||
public RelaySocketClient(string username, string url = "ws://localhost:1337/")
|
||||
{
|
||||
_username = username;
|
||||
_socket = new WebSocket(url);
|
||||
_socket.OnMessage += OnMessage;
|
||||
}
|
||||
|
||||
public void Connect()
|
||||
{
|
||||
_socket.Connect();
|
||||
|
||||
var publicKey = KeyStorage.LoadPublicKey(_username);
|
||||
|
||||
SendRaw($"REGISTER_KEY|{_username}|{publicKey}");
|
||||
SendRaw("GET_SERVER_KEY");
|
||||
SendRaw("GET_CHANNELS");
|
||||
}
|
||||
|
||||
public void SendRaw(string message)
|
||||
{
|
||||
if (_socket.ReadyState == WebSocketState.Open)
|
||||
_socket.Send(message);
|
||||
}
|
||||
|
||||
public void SendJson<T>(T payload)
|
||||
{
|
||||
SendRaw(JsonSerializer.Serialize(payload));
|
||||
}
|
||||
|
||||
public void Disconnect()
|
||||
{
|
||||
_socket.OnMessage -= OnMessage;
|
||||
|
||||
if (_socket.ReadyState == WebSocketState.Open)
|
||||
_socket.Close();
|
||||
}
|
||||
|
||||
private void OnMessage(object? sender, MessageEventArgs e)
|
||||
{
|
||||
if (e.Data.StartsWith("SERVER:REGISTERED_KEY:"))
|
||||
{
|
||||
Log?.Invoke(e.Data);
|
||||
return;
|
||||
}
|
||||
|
||||
RawMessageReceived?.Invoke(e.Data);
|
||||
Log?.Invoke($"[{_username}] RAW WS DATA: {e.Data}");
|
||||
|
||||
try
|
||||
{
|
||||
using var doc = JsonDocument.Parse(e.Data);
|
||||
var root = doc.RootElement;
|
||||
|
||||
if (!root.TryGetProperty("Type", out var typeElement))
|
||||
return;
|
||||
|
||||
var type = (SignalType)typeElement.GetInt32();
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case SignalType.ChannelList:
|
||||
{
|
||||
var channelList = JsonSerializer.Deserialize<SocketChannelList>(e.Data);
|
||||
if (channelList is not null)
|
||||
ChannelListReceived?.Invoke(channelList);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
case SignalType.ServerPublicKey:
|
||||
{
|
||||
var serverKeyMessage = JsonSerializer.Deserialize<ServerPublicKeyMessage>(e.Data);
|
||||
if (serverKeyMessage is not null)
|
||||
{
|
||||
ServerPublicKey = serverKeyMessage.PublicKey;
|
||||
ServerPublicKeyReceived?.Invoke(serverKeyMessage.PublicKey);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
case SignalType.EncryptedSignal:
|
||||
{
|
||||
var payload = JsonSerializer.Deserialize<SocketRtcSignalMessage>(e.Data);
|
||||
if (payload is not null)
|
||||
EncryptedRtcSignalReceived?.Invoke(payload);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
case SignalType.EncryptedChat:
|
||||
{
|
||||
var payload = JsonSerializer.Deserialize<SocketEncryptedMessage>(e.Data);
|
||||
if (payload is not null)
|
||||
EncryptedChatReceived?.Invoke(payload);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log?.Invoke($"[{_username}] failed to process websocket message: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
224
RelayClient/Services/RtcBridgeService.cs
Normal file
224
RelayClient/Services/RtcBridgeService.cs
Normal file
@@ -0,0 +1,224 @@
|
||||
using System.Text.Json;
|
||||
using RelayClient.Crypto;
|
||||
using RelayShared.Rtc;
|
||||
using RelayShared.Services;
|
||||
|
||||
namespace RelayClient.Services;
|
||||
|
||||
public sealed class RtcBridgeService
|
||||
{
|
||||
private readonly string _username;
|
||||
private readonly RelaySocketClient _socket;
|
||||
private readonly HybridWebView _hybridWebView;
|
||||
private readonly Func<string?> _getCurrentChannelId;
|
||||
private readonly Action<string> _sendRawToWebView;
|
||||
|
||||
public RtcBridgeService(string username, RelaySocketClient socket, HybridWebView hybridWebView,
|
||||
Func<string?> getCurrentChannelId, Action<string> sendRawToWebView)
|
||||
{
|
||||
_username = username;
|
||||
_socket = socket;
|
||||
_hybridWebView = hybridWebView;
|
||||
_getCurrentChannelId = getCurrentChannelId;
|
||||
_sendRawToWebView = sendRawToWebView;
|
||||
}
|
||||
|
||||
public Task JoinRtcChannel()
|
||||
{
|
||||
var channelId = _getCurrentChannelId();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(channelId))
|
||||
return Task.CompletedTask;
|
||||
|
||||
_socket.SendRaw($"RTC_JOIN_CHANNEL|{_username}|{channelId}");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void LeaveRtcChannel()
|
||||
{
|
||||
var channelId = _getCurrentChannelId();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(channelId))
|
||||
return;
|
||||
|
||||
_socket.SendRaw($"RTC_LEAVE_CHANNEL|{_username}|{channelId}");
|
||||
}
|
||||
|
||||
public void SendRtcSignal(string json)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_socket.ServerPublicKey))
|
||||
{
|
||||
_sendRawToWebView("SendRtcSignal failed: server public key not loaded.");
|
||||
return;
|
||||
}
|
||||
|
||||
RtcSignalMessage? rtcSignal;
|
||||
|
||||
try
|
||||
{
|
||||
rtcSignal = JsonSerializer.Deserialize<RtcSignalMessage>(json);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_sendRawToWebView("SendRtcSignal failed to parse RTC signal: " + ex.Message);
|
||||
return;
|
||||
}
|
||||
|
||||
if (rtcSignal is null)
|
||||
return;
|
||||
|
||||
rtcSignal.ChannelId ??= _getCurrentChannelId();
|
||||
rtcSignal.From ??= _username;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(rtcSignal.ChannelId))
|
||||
{
|
||||
_sendRawToWebView("SendRtcSignal failed: missing channel id.");
|
||||
return;
|
||||
}
|
||||
|
||||
var outgoingJson = JsonSerializer.Serialize(rtcSignal);
|
||||
|
||||
try
|
||||
{
|
||||
var encrypted = E2EeHelper.EncryptForRecipient(outgoingJson, _socket.ServerPublicKey);
|
||||
|
||||
var payload = new SocketRtcSignalMessage
|
||||
{
|
||||
Type = SignalType.EncryptedSignal,
|
||||
SenderUsername = _username,
|
||||
ChannelId = rtcSignal.ChannelId,
|
||||
CipherText = encrypted.CipherText,
|
||||
Nonce = encrypted.Nonce,
|
||||
Tag = encrypted.Tag,
|
||||
EncryptedKey = encrypted.EncryptedKey
|
||||
};
|
||||
|
||||
_socket.SendJson(payload);
|
||||
|
||||
_sendRawToWebView($"SendRtcSignal sent: {rtcSignal.Type} -> {rtcSignal.To}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_sendRawToWebView("SendRtcSignal failed: " + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string> GetRtcParticipants()
|
||||
{
|
||||
var channelId = _getCurrentChannelId();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(channelId))
|
||||
return "[]";
|
||||
|
||||
var participants = await ServerAPI.GetRtcParticipantsAsync(channelId);
|
||||
return JsonSerializer.Serialize(participants ?? []);
|
||||
}
|
||||
|
||||
public async Task HandleIncomingRtcSignalAsync(SocketRtcSignalMessage payload)
|
||||
{
|
||||
var currentChannelId = _getCurrentChannelId();
|
||||
|
||||
if (payload.ChannelId != currentChannelId)
|
||||
return;
|
||||
|
||||
if (payload.SenderUsername == _username)
|
||||
return;
|
||||
|
||||
string decryptedJson;
|
||||
|
||||
try
|
||||
{
|
||||
var privateKey = KeyStorage.LoadPrivateKey(_username);
|
||||
|
||||
decryptedJson = E2EeHelper.DecryptForRecipient(
|
||||
new EncryptedPayload
|
||||
{
|
||||
CipherText = payload.CipherText,
|
||||
Nonce = payload.Nonce,
|
||||
Tag = payload.Tag,
|
||||
EncryptedKey = payload.EncryptedKey
|
||||
},
|
||||
privateKey
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_sendRawToWebView("RTC decrypt failed: " + ex.Message);
|
||||
return;
|
||||
}
|
||||
|
||||
RtcSignalMessage? rtcSignal;
|
||||
|
||||
try
|
||||
{
|
||||
rtcSignal = JsonSerializer.Deserialize<RtcSignalMessage>(decryptedJson);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_sendRawToWebView("RTC signal parse failed: " + ex.Message);
|
||||
return;
|
||||
}
|
||||
|
||||
if (rtcSignal is null)
|
||||
return;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(rtcSignal.To) &&
|
||||
!string.Equals(rtcSignal.To, _username, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
_sendRawToWebView($"Ignoring RTC signal meant for {rtcSignal.To}");
|
||||
return;
|
||||
}
|
||||
|
||||
_sendRawToWebView("Received encrypted RTC signal: " + decryptedJson);
|
||||
|
||||
await SendRtcSignalToJsAsync(decryptedJson);
|
||||
}
|
||||
|
||||
public Task PushRtcContextToJsAsync()
|
||||
{
|
||||
MainThread.BeginInvokeOnMainThread(async () =>
|
||||
{
|
||||
var usernameJson = JsonSerializer.Serialize(_username);
|
||||
var channelIdJson = JsonSerializer.Serialize(_getCurrentChannelId());
|
||||
|
||||
await _hybridWebView.EvaluateJavaScriptAsync($"window.setUsername({usernameJson})");
|
||||
await _hybridWebView.EvaluateJavaScriptAsync($"window.setChannelId({channelIdJson})");
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task SendRtcSignalToJsAsync(string rawJson)
|
||||
{
|
||||
MainThread.BeginInvokeOnMainThread(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var jsArg = JsonSerializer.Serialize(rawJson);
|
||||
|
||||
await _hybridWebView.EvaluateJavaScriptAsync($@"
|
||||
try {{
|
||||
window.HybridWebView.SendRawMessage('C# eval entered');
|
||||
|
||||
if (!window.RelaySocket) {{
|
||||
window.HybridWebView.SendRawMessage('window.RelaySocket missing');
|
||||
}} else if (typeof window.RelaySocket.receiveRtcSignal !== 'function') {{
|
||||
window.HybridWebView.SendRawMessage('RelaySocket.receiveRtcSignal missing');
|
||||
}} else {{
|
||||
window.HybridWebView.SendRawMessage('Calling RelaySocket.receiveRtcSignal');
|
||||
window.RelaySocket.receiveRtcSignal({jsArg});
|
||||
}}
|
||||
}} catch (err) {{
|
||||
window.HybridWebView.SendRawMessage('RTC JS dispatch failed: ' + err);
|
||||
}}
|
||||
");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_sendRawToWebView("SendRtcSignalToJsAsync failed: " + ex.Message);
|
||||
}
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -1,170 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Maui.Dispatching;
|
||||
using Microsoft.AspNetCore.SignalR.Client;
|
||||
|
||||
namespace RelayClient;
|
||||
|
||||
public static class NativeWebRtc
|
||||
{
|
||||
[DllImport("webrtc_wrapper.dll")]
|
||||
public static extern IntPtr CreatePeerConnection();
|
||||
|
||||
[DllImport("webrtc_wrapper.dll")]
|
||||
public static extern string CreateOffer(IntPtr pc);
|
||||
|
||||
[DllImport("webrtc_wrapper.dll")]
|
||||
public static extern string CreateAnswer(IntPtr pc);
|
||||
|
||||
[DllImport("webrtc_wrapper.dll")]
|
||||
public static extern void SetLocalDescription(IntPtr pc, string type, string sdp);
|
||||
|
||||
[DllImport("webrtc_wrapper.dll")]
|
||||
public static extern void SetRemoteDescription(IntPtr pc, string type, string sdp);
|
||||
|
||||
[DllImport("webrtc_wrapper.dll")]
|
||||
public static extern void AddIceCandidate(IntPtr pc, string candidate);
|
||||
}
|
||||
|
||||
public enum RTCSdpType { Offer, Answer }
|
||||
|
||||
public class RTCSessionDescription
|
||||
{
|
||||
public RTCSdpType Type { get; set; }
|
||||
public string Sdp { get; set; }
|
||||
}
|
||||
|
||||
public class RTCIceCandidate
|
||||
{
|
||||
public string Candidate { get; set; }
|
||||
}
|
||||
|
||||
public class PeerConnection
|
||||
{
|
||||
private readonly IntPtr _nativeHandle;
|
||||
public string RemoteId { get; set; }
|
||||
|
||||
public PeerConnection()
|
||||
{
|
||||
_nativeHandle = NativeWebRtc.CreatePeerConnection();
|
||||
}
|
||||
|
||||
public Task CreateOffer(Action<RTCSessionDescription> onOfferCreated)
|
||||
{
|
||||
var sdp = NativeWebRtc.CreateOffer(_nativeHandle);
|
||||
onOfferCreated?.Invoke(new RTCSessionDescription { Type = RTCSdpType.Offer, Sdp = sdp });
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task CreateAnswer(Action<RTCSessionDescription> onAnswerCreated)
|
||||
{
|
||||
var sdp = NativeWebRtc.CreateAnswer(_nativeHandle);
|
||||
onAnswerCreated?.Invoke(new RTCSessionDescription { Type = RTCSdpType.Answer, Sdp = sdp });
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task SetLocalDescription(RTCSessionDescription desc)
|
||||
{
|
||||
NativeWebRtc.SetLocalDescription(_nativeHandle, desc.Type.ToString(), desc.Sdp);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task SetRemoteDescription(RTCSessionDescription desc)
|
||||
{
|
||||
NativeWebRtc.SetRemoteDescription(_nativeHandle, desc.Type.ToString(), desc.Sdp);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task AddIceCandidate(RTCIceCandidate candidate)
|
||||
{
|
||||
NativeWebRtc.AddIceCandidate(_nativeHandle, candidate.Candidate);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
public class WebRtcClient
|
||||
{
|
||||
private readonly PeerConnection _peerConnection = new();
|
||||
private readonly HubConnection _signal;
|
||||
private string _myId;
|
||||
|
||||
public WebRtcClient(string serverUrl)
|
||||
{
|
||||
_signal = new HubConnectionBuilder()
|
||||
.WithUrl($"{serverUrl}/webrtc")
|
||||
.WithAutomaticReconnect()
|
||||
.Build();
|
||||
|
||||
_signal.On<string, string>("ReceiveOffer", (fromId, sdp) =>
|
||||
{
|
||||
MainThread.BeginInvokeOnMainThread(async () =>
|
||||
{
|
||||
await HandleOffer(fromId, sdp);
|
||||
});
|
||||
});
|
||||
|
||||
_signal.On<string, string>("ReceiveAnswer", (fromId, sdp) =>
|
||||
{
|
||||
MainThread.BeginInvokeOnMainThread(async () =>
|
||||
{
|
||||
await HandleAnswer(sdp);
|
||||
});
|
||||
});
|
||||
|
||||
_signal.On<string, string>("ReceiveIceCandidate", (fromId, candidate) =>
|
||||
{
|
||||
MainThread.BeginInvokeOnMainThread(async () =>
|
||||
{
|
||||
await HandleIceCandidate(candidate);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public async Task ConnectAsync()
|
||||
{
|
||||
await _signal.StartAsync();
|
||||
_myId = _signal.ConnectionId;
|
||||
}
|
||||
|
||||
public async Task CallAsync(string targetId)
|
||||
{
|
||||
_peerConnection.RemoteId = targetId;
|
||||
await _peerConnection.CreateOffer(async offer =>
|
||||
{
|
||||
await _peerConnection.SetLocalDescription(offer);
|
||||
await _signal.InvokeAsync("SendOffer", targetId, offer.Sdp);
|
||||
});
|
||||
}
|
||||
|
||||
public async Task HandleOffer(string fromId, string sdp)
|
||||
{
|
||||
_peerConnection.RemoteId = fromId;
|
||||
var remoteDesc = new RTCSessionDescription { Type = RTCSdpType.Offer, Sdp = sdp };
|
||||
await _peerConnection.SetRemoteDescription(remoteDesc);
|
||||
|
||||
await _peerConnection.CreateAnswer(async answer =>
|
||||
{
|
||||
await _peerConnection.SetLocalDescription(answer);
|
||||
await _signal.InvokeAsync("SendAnswer", fromId, answer.Sdp);
|
||||
});
|
||||
}
|
||||
|
||||
public async Task HandleAnswer(string sdp)
|
||||
{
|
||||
var remoteDesc = new RTCSessionDescription { Type = RTCSdpType.Answer, Sdp = sdp };
|
||||
await _peerConnection.SetRemoteDescription(remoteDesc);
|
||||
}
|
||||
|
||||
public async Task HandleIceCandidate(string candidate)
|
||||
{
|
||||
await _peerConnection.AddIceCandidate(new RTCIceCandidate { Candidate = candidate });
|
||||
}
|
||||
|
||||
public async Task SendIceCandidate(string candidate)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_peerConnection.RemoteId))
|
||||
{
|
||||
await _signal.InvokeAsync("SendIceCandidate", _peerConnection.RemoteId, candidate);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,12 +20,12 @@ var kira = await CreateUserAsync(db, "Ru_Kira", "jduesling13@gmail.com", "passwo
|
||||
var test = await CreateUserAsync(db, "Test", "test@gmail.com", "password");
|
||||
|
||||
var server = new Program();
|
||||
server.Main(db);
|
||||
|
||||
Console.WriteLine($"Keeper created: {ToJsonString(keeper)}");
|
||||
Console.WriteLine($"Kira created: {ToJsonString(kira)}");
|
||||
Console.WriteLine($"Test created: {ToJsonString(test)}");
|
||||
|
||||
await server.Main(db);
|
||||
Console.ReadKey(true);
|
||||
return;
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Text.Json;
|
||||
using RelayShared.Rtc;
|
||||
using RelayServer.Services.Rtc;
|
||||
using RelayShared.Services;
|
||||
|
||||
namespace RelayServer.Endpoints;
|
||||
|
||||
@@ -20,7 +21,7 @@ public static class RtcEndpoints
|
||||
|
||||
RtcNotificationService.BroadcastToChannel(new RtcNotificationMessage
|
||||
{
|
||||
Type = "rtc_offer_updated",
|
||||
Type = SignalType.OfferUpdated,
|
||||
ChannelId = request.ChannelId,
|
||||
Username = request.Username
|
||||
});
|
||||
@@ -58,7 +59,7 @@ public static class RtcEndpoints
|
||||
|
||||
RtcNotificationService.BroadcastToChannel(new RtcNotificationMessage
|
||||
{
|
||||
Type = "rtc_answer_updated",
|
||||
Type = SignalType.AnswerUpdated,
|
||||
ChannelId = request.ChannelId
|
||||
});
|
||||
|
||||
@@ -71,6 +72,11 @@ public static class RtcEndpoints
|
||||
return Results.Ok(await rtcCallService.GetAnswersAsync(channelId));
|
||||
});
|
||||
|
||||
app.MapGet("/api/rtc/participants/{channelId}", (string channelId) =>
|
||||
{
|
||||
return Results.Ok(RtcChannelPresenceService.GetUsersInChannel(channelId));
|
||||
});
|
||||
|
||||
// Return the latest answer stored for the specified channel.
|
||||
app.MapGet("/api/rtc/answer/{channelId}", async (string channelId, RtcCallService rtcCallService) =>
|
||||
{
|
||||
@@ -79,7 +85,7 @@ public static class RtcEndpoints
|
||||
});
|
||||
|
||||
// Store a new ICE candidate for the specified channel call.
|
||||
app.MapPost("/api/rtc/candidate", async (DBIceCandidate request, RtcCallService rtcCallService) =>
|
||||
app.MapPost("/api/rtc/candidate", async (RtcIceCandidate request, RtcCallService rtcCallService) =>
|
||||
{
|
||||
await rtcCallService.WriteIceCandidateAsync(
|
||||
request.ChannelId,
|
||||
@@ -91,7 +97,7 @@ public static class RtcEndpoints
|
||||
|
||||
RtcNotificationService.BroadcastToChannel(new RtcNotificationMessage
|
||||
{
|
||||
Type = "rtc_candidate_added",
|
||||
Type = SignalType.CandidateAdded,
|
||||
ChannelId = request.ChannelId,
|
||||
Username = request.Username,
|
||||
Direction = JsonSerializer.Serialize(request.Candidate)
|
||||
@@ -124,7 +130,7 @@ public static class RtcEndpoints
|
||||
|
||||
RtcNotificationService.BroadcastToChannel(new RtcNotificationMessage
|
||||
{
|
||||
Type = "rtc_call_left",
|
||||
Type = SignalType.CallLeft,
|
||||
ChannelId = request.ChannelId,
|
||||
Username = request.Username
|
||||
});
|
||||
|
||||
@@ -4,6 +4,7 @@ using RelayServer.Services.Core;
|
||||
using RelayServer.Services.Data;
|
||||
using RelayServer.Services.Rtc;
|
||||
using RelayShared.Rtc;
|
||||
using RelayShared.Services;
|
||||
using WebSocketSharp.Server;
|
||||
|
||||
var surrealService = new SurrealService();
|
||||
@@ -33,15 +34,14 @@ var wssv = new WebSocketServer("ws://localhost:1337");
|
||||
wssv.AddWebSocketService<ChatSocketBehavior>("/");
|
||||
RtcNotificationService.Server = wssv;
|
||||
|
||||
//TODO: Use AnswerCallback as a test on client and use the /rtc/answer endpoint call as a test on server
|
||||
|
||||
wssv.Start();
|
||||
Console.WriteLine("WebSocket server started");
|
||||
|
||||
await app.StartAsync();
|
||||
Console.WriteLine("HTTP API started");
|
||||
|
||||
Console.ReadKey(true); // TODO: Make program stop be a console command
|
||||
ConsoleCommandService.Start();
|
||||
await Task.Delay(Timeout.Infinite, ConsoleCommandService.ShutdownTokenSource.Token);
|
||||
|
||||
wssv.Stop();
|
||||
await app.StopAsync();
|
||||
|
||||
@@ -17,8 +17,4 @@
|
||||
<ProjectReference Include="..\RelayShared\RelayShared.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Models\Rtc\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -6,7 +6,7 @@ using RelayServer.Services.Rtc;
|
||||
using WebSocketSharp;
|
||||
using WebSocketSharp.Server;
|
||||
using ErrorEventArgs = WebSocketSharp.ErrorEventArgs;
|
||||
using RelayShared.Rtc;
|
||||
using RelayShared.Services;
|
||||
|
||||
namespace RelayServer.Services.Chat;
|
||||
|
||||
@@ -69,9 +69,114 @@ public class ChatSocketBehavior : WebSocketBehavior
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsEncryptedRtcSignal(msg))
|
||||
{
|
||||
HandleEncryptedRtcSignal(msg);
|
||||
return;
|
||||
}
|
||||
|
||||
HandleEncryptedChatMessage(msg);
|
||||
}
|
||||
|
||||
private static bool IsEncryptedRtcSignal(string msg)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var doc = JsonDocument.Parse(msg);
|
||||
var root = doc.RootElement;
|
||||
|
||||
if (!root.TryGetProperty("Type", out var typeProp))
|
||||
return false;
|
||||
|
||||
var type = (SignalType)typeProp.GetInt32();
|
||||
|
||||
return type == SignalType.EncryptedSignal;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleEncryptedRtcSignal(string msg)
|
||||
{
|
||||
Console.WriteLine("RTC SIGNAL HIT");
|
||||
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 != SignalType.EncryptedSignal)
|
||||
return;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(clientPayload.ChannelId))
|
||||
{
|
||||
Console.WriteLine("Encrypted RTC signal missing channel id.");
|
||||
return;
|
||||
}
|
||||
|
||||
string plainText;
|
||||
|
||||
try
|
||||
{
|
||||
plainText = 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: {ex.Message}");
|
||||
return;
|
||||
}
|
||||
|
||||
var sessionIds = RtcChannelPresenceService.GetSessionsInChannel(clientPayload.ChannelId);
|
||||
|
||||
foreach (var sessionId in sessionIds)
|
||||
{
|
||||
if (sessionId == ID)
|
||||
continue;
|
||||
|
||||
var username = RtcChannelPresenceService.GetUsernameForSession(sessionId);
|
||||
if (string.IsNullOrWhiteSpace(username))
|
||||
continue;
|
||||
|
||||
var clientKey = GetClientPublicKeyByUsernameSync(username);
|
||||
if (clientKey is null)
|
||||
continue;
|
||||
|
||||
var encrypted = E2EeHelper.EncryptForRecipient(plainText, clientKey.PublicKey);
|
||||
|
||||
var outbound = new SocketRtcSignalMessage
|
||||
{
|
||||
Type = SignalType.EncryptedSignal,
|
||||
SenderUsername = clientPayload.SenderUsername,
|
||||
ChannelId = clientPayload.ChannelId,
|
||||
CipherText = encrypted.CipherText,
|
||||
Nonce = encrypted.Nonce,
|
||||
Tag = encrypted.Tag,
|
||||
EncryptedKey = encrypted.EncryptedKey
|
||||
};
|
||||
|
||||
Sessions.SendTo(JsonSerializer.Serialize(outbound), sessionId);
|
||||
}
|
||||
|
||||
Console.WriteLine($"Forwarded encrypted RTC signal from {clientPayload.SenderUsername} to channel {clientPayload.ChannelId}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
@@ -143,7 +248,7 @@ public class ChatSocketBehavior : WebSocketBehavior
|
||||
Console.WriteLine("Db is not initialized.");
|
||||
return;
|
||||
}
|
||||
|
||||
//TODO: Update to include ChannelType and Group String on channels
|
||||
var channels = GetChannelsSync()
|
||||
.OrderBy(c => c.CreatedAt)
|
||||
.Select(c => new ChannelItem()
|
||||
@@ -156,7 +261,7 @@ public class ChatSocketBehavior : WebSocketBehavior
|
||||
|
||||
var payload = new SocketChannelList
|
||||
{
|
||||
Type = "channel_list",
|
||||
Type = SignalType.ChannelList,
|
||||
Channels = channels
|
||||
};
|
||||
|
||||
@@ -176,7 +281,7 @@ public class ChatSocketBehavior : WebSocketBehavior
|
||||
|
||||
var payload = new ServerPublicKeyMessage
|
||||
{
|
||||
Type = "server_public_key",
|
||||
Type = SignalType.ServerPublicKey,
|
||||
PublicKey = ServerPublicKey
|
||||
};
|
||||
|
||||
@@ -202,7 +307,7 @@ public class ChatSocketBehavior : WebSocketBehavior
|
||||
return;
|
||||
}
|
||||
|
||||
if (clientPayload is null || clientPayload.Type != "client_encrypted_chat")
|
||||
if (clientPayload is null || clientPayload.Type != SignalType.ClientEncryptedChat)
|
||||
return;
|
||||
|
||||
if (!EnsureCoreReady() || !EnsureCryptoReady())
|
||||
@@ -262,7 +367,7 @@ public class ChatSocketBehavior : WebSocketBehavior
|
||||
|
||||
var outbound = new SocketEncryptedMessage
|
||||
{
|
||||
Type = "encrypted_chat",
|
||||
Type = SignalType.EncryptedChat,
|
||||
SenderUsername = clientPayload.SenderUsername,
|
||||
RecipientUsername = client.Username,
|
||||
ChannelId = clientPayload.ChannelId,
|
||||
@@ -340,7 +445,7 @@ public class ChatSocketBehavior : WebSocketBehavior
|
||||
|
||||
var outbound = new SocketEncryptedMessage
|
||||
{
|
||||
Type = "encrypted_chat",
|
||||
Type = SignalType.EncryptedChat,
|
||||
SenderUsername = ExtractUsernameFromUserId(dbMessage.SenderUserId),
|
||||
RecipientUsername = username,
|
||||
ChannelId = channelId,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using RelayShared.Rtc;
|
||||
using SurrealDb.Net;
|
||||
using RelayShared.Rtc;
|
||||
|
||||
namespace RelayServer.Services.Rtc;
|
||||
|
||||
@@ -21,13 +22,13 @@ public sealed class RtcCallService
|
||||
/// </returns>
|
||||
public async Task<bool> HasActiveCallAsync(string channelId)
|
||||
{
|
||||
var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls");
|
||||
var activeCalls = await _db.Select<DBActiveCall>("rtc_active_calls");
|
||||
return activeCalls.Any(x => x.ChannelId == channelId && x.IsActive);
|
||||
}
|
||||
|
||||
public async Task<RtcActiveCall?> GetActiveCallAsync(string channelId)
|
||||
public async Task<DBActiveCall?> GetActiveCallAsync(string channelId)
|
||||
{
|
||||
var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls");
|
||||
var activeCalls = await _db.Select<DBActiveCall>("rtc_active_calls");
|
||||
return activeCalls
|
||||
.Where(x => x.ChannelId == channelId && x.IsActive)
|
||||
.OrderByDescending(x => x.UpdatedAt)
|
||||
@@ -45,12 +46,12 @@ public sealed class RtcCallService
|
||||
/// <param name="sdp">The SDP offer payload.</param>
|
||||
public async Task WriteOfferAsync(string channelId, string username, RtcSessionDescription sessionDescription)
|
||||
{
|
||||
var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls");
|
||||
var activeCalls = await _db.Select<DBActiveCall>("rtc_active_calls");
|
||||
var activeCall = activeCalls.FirstOrDefault(x => x.ChannelId == channelId && x.IsActive);
|
||||
|
||||
if (activeCall is null)
|
||||
{
|
||||
await _db.Create("rtc_active_calls", new RtcActiveCall
|
||||
await _db.Create("rtc_active_calls", new DBActiveCall
|
||||
{
|
||||
ChannelId = channelId,
|
||||
OfferUser = username,
|
||||
@@ -76,7 +77,7 @@ public sealed class RtcCallService
|
||||
};
|
||||
activeCall.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await _db.Merge<RtcActiveCall, RtcActiveCall>(activeCall);
|
||||
await _db.Merge<DBActiveCall, DBActiveCall>(activeCall);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -101,7 +102,7 @@ public sealed class RtcCallService
|
||||
/// <param name="sessionDescription">The SDP and type answer payload.</param>
|
||||
public async Task WriteAnswerAsync(string channelId, RtcSessionDescription sessionDescription)
|
||||
{
|
||||
var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls");
|
||||
var activeCalls = await _db.Select<DBActiveCall>("rtc_active_calls");
|
||||
var activeCall = activeCalls.FirstOrDefault(x => x.ChannelId == channelId && x.IsActive);
|
||||
|
||||
if (activeCall is null)
|
||||
@@ -114,7 +115,7 @@ public sealed class RtcCallService
|
||||
};
|
||||
activeCall.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await _db.Merge<RtcActiveCall, RtcActiveCall>(activeCall);
|
||||
await _db.Merge<DBActiveCall, DBActiveCall>(activeCall);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -166,7 +167,7 @@ public sealed class RtcCallService
|
||||
int? sdpMLineIndex/*,
|
||||
string direction*/)
|
||||
{
|
||||
await _db.Create("rtc_ice_candidates", new RtcIceCandidate
|
||||
await _db.Create("rtc_ice_candidates", new DBIceCandidate
|
||||
{
|
||||
ChannelId = channelId,
|
||||
Username = username,
|
||||
@@ -185,9 +186,9 @@ public sealed class RtcCallService
|
||||
/// <returns>
|
||||
/// A list of ICE candidates for the channel ordered from oldest to newest.
|
||||
/// </returns>
|
||||
public async Task<List<RtcIceCandidate>> GetIceCandidatesAsync(string channelId)
|
||||
public async Task<List<DBIceCandidate>> GetIceCandidatesAsync(string channelId)
|
||||
{
|
||||
var candidates = await _db.Select<RtcIceCandidate>("rtc_ice_candidates");
|
||||
var candidates = await _db.Select<DBIceCandidate>("rtc_ice_candidates");
|
||||
return candidates
|
||||
.Where(x => x.ChannelId == channelId)
|
||||
.OrderBy(x => x.CreatedAt)
|
||||
@@ -204,9 +205,9 @@ public sealed class RtcCallService
|
||||
/// <returns>
|
||||
/// A list of matching ICE candidates ordered from oldest to newest.
|
||||
/// </returns>
|
||||
public async Task<List<RtcIceCandidate>> GetIceCandidatesForOthersAsync(string channelId, string username, string direction)
|
||||
public async Task<List<DBIceCandidate>> GetIceCandidatesForOthersAsync(string channelId, string username, string direction)
|
||||
{
|
||||
var candidates = await _db.Select<RtcIceCandidate>("rtc_ice_candidates");
|
||||
var candidates = await _db.Select<DBIceCandidate>("rtc_ice_candidates");
|
||||
return candidates
|
||||
.Where(x => x.ChannelId == channelId && x.Username != username /*&& x.Direction == direction*/)
|
||||
.OrderBy(x => x.CreatedAt)
|
||||
@@ -221,7 +222,7 @@ public sealed class RtcCallService
|
||||
/// <param name="username">The user leaving the call.</param>
|
||||
public async Task LeaveCallAsync(string channelId, string username)
|
||||
{
|
||||
var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls");
|
||||
var activeCalls = await _db.Select<DBActiveCall>("rtc_active_calls");
|
||||
var activeCall = activeCalls.FirstOrDefault(x => x.ChannelId == channelId && x.IsActive);
|
||||
|
||||
if (activeCall is null)
|
||||
@@ -231,7 +232,7 @@ public sealed class RtcCallService
|
||||
{
|
||||
activeCall.IsActive = false;
|
||||
activeCall.UpdatedAt = DateTime.UtcNow;
|
||||
await _db.Merge<RtcActiveCall, RtcActiveCall>(activeCall);
|
||||
await _db.Merge<DBActiveCall, DBActiveCall>(activeCall);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -241,9 +242,9 @@ public sealed class RtcCallService
|
||||
/// <returns>
|
||||
/// A list of active calls with offers, ordered from newest to oldest.
|
||||
/// </returns>
|
||||
public async Task<List<RtcActiveCall>> GetOffersAsync()
|
||||
public async Task<List<DBActiveCall>> GetOffersAsync()
|
||||
{
|
||||
var activeCalls = await _db.Select<RtcActiveCall>("rtc_active_calls");
|
||||
var activeCalls = await _db.Select<DBActiveCall>("rtc_active_calls");
|
||||
return activeCalls
|
||||
.Where(x => x.Offer is not null)
|
||||
.OrderByDescending(x => x.UpdatedAt)
|
||||
|
||||
@@ -36,6 +36,11 @@ public static class RtcChannelPresenceService
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public static List<string> GetUsernamesInChannel(string channelId)
|
||||
{
|
||||
return GetUsersInChannel(channelId).ToList();
|
||||
}
|
||||
|
||||
public static IReadOnlyList<string> GetUsersInChannel(string channelId)
|
||||
{
|
||||
var sessionIds = GetSessionsInChannel(channelId);
|
||||
@@ -52,4 +57,11 @@ public static class RtcChannelPresenceService
|
||||
return SessionToChannel.TryGetValue(sessionId, out var currentChannel) &&
|
||||
string.Equals(currentChannel, channelId, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
public static string? GetUsernameForSession(string sessionId)
|
||||
{
|
||||
return SessionToUsername.TryGetValue(sessionId, out var username)
|
||||
? username
|
||||
: null;
|
||||
}
|
||||
}
|
||||
@@ -25,22 +25,4 @@ public static class RtcNotificationService
|
||||
host.Sessions.SendTo(json, sessionId);
|
||||
}
|
||||
}
|
||||
|
||||
public static void BroadcastToChannel(RtcIceNotificationMessage message)
|
||||
{
|
||||
if (Server is null)
|
||||
return;
|
||||
|
||||
var host = Server.WebSocketServices["/"];
|
||||
if (host is null)
|
||||
return;
|
||||
|
||||
var json = JsonSerializer.Serialize(message);
|
||||
var sessionIds = RtcChannelPresenceService.GetSessionsInChannel(message.ChannelId);
|
||||
|
||||
foreach (var sessionId in sessionIds)
|
||||
{
|
||||
host.Sessions.SendTo(json, sessionId);
|
||||
}
|
||||
}
|
||||
}
|
||||
40
RelayShared/Rtc/RTCDatabase.cs
Normal file
40
RelayShared/Rtc/RTCDatabase.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using SurrealDb.Net.Models;
|
||||
|
||||
namespace RelayShared.Rtc;
|
||||
|
||||
public sealed class DBActiveCall : Record
|
||||
{
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string? OfferUser { get; set; }
|
||||
public RtcSessionDescription? Offer { get; set; }
|
||||
public RtcSessionDescription? Answer { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public DateTime UpdatedAt { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
public string[] IceCandidates { get; set; } = []; //TODO: Should be array of DBIceCandidates IDs
|
||||
}
|
||||
|
||||
public sealed class DBOffer : Record
|
||||
{
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string Username { get; set; } = string.Empty;
|
||||
public string Type { get; set; } = string.Empty;
|
||||
public string Sdp { get; set; } = string.Empty;
|
||||
}
|
||||
public sealed class DBAnswer : Record
|
||||
{
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string Username { get; set; } = string.Empty;
|
||||
public string Type { get; set; } = string.Empty;
|
||||
public string Sdp { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class DBIceCandidate : Record
|
||||
{
|
||||
public required string ChannelId { get; set; }
|
||||
public required string Username { get; set; }
|
||||
public required string Candidate { get; set; }
|
||||
public string? SdpMid { get; set; }
|
||||
public int? SdpMLineIndex { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
}
|
||||
93
RelayShared/Rtc/RTCTransmissions.cs
Normal file
93
RelayShared/Rtc/RTCTransmissions.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using RelayShared.Services;
|
||||
|
||||
namespace RelayShared.Rtc;
|
||||
|
||||
public sealed class RtcSessionDescription
|
||||
{
|
||||
public string Type { get; set; } = string.Empty;
|
||||
public string Sdp { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public sealed class RtcOffer
|
||||
{
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string Username { get; set; } = string.Empty;
|
||||
public RtcSessionDescription SessionDescription { get; set; } = new();
|
||||
}
|
||||
|
||||
public sealed class RtcAnswer
|
||||
{
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string Username { get; set; } = string.Empty;
|
||||
public RtcSessionDescription SessionDescription { get; set; } = new();
|
||||
}
|
||||
public class RtcIceCandidate
|
||||
{
|
||||
public required string ChannelId { get; set; }
|
||||
public required string Username { get; set; }
|
||||
public required IceCandidate Candidate { get; set; }
|
||||
}
|
||||
|
||||
public class IceCandidate
|
||||
{
|
||||
public required string candidate { get; set; }
|
||||
public required string sdpMid { get; set; }
|
||||
public required int sdpMLineIndex { get; set; }
|
||||
public required string usernameFragment { get; set; }
|
||||
|
||||
}
|
||||
|
||||
public sealed class RtcJoinRequest
|
||||
{
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string Username { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public sealed class RtcJoinResponse
|
||||
{
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string[] Participants { get; set; } = [];
|
||||
}
|
||||
|
||||
public sealed class RtcLeaveRequest
|
||||
{
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string Username { get; set; } = string.Empty;
|
||||
}
|
||||
public sealed class RtcNotificationMessage //TODO: Review for removal
|
||||
{
|
||||
public SignalType? Type { get; set; }
|
||||
public string? ChannelId { get; set; }
|
||||
public string? Username { get; set; }
|
||||
public string? Direction { get; set; }
|
||||
}
|
||||
public sealed class RtcSignalMessage //TODO: Review for removal.
|
||||
{
|
||||
[JsonPropertyName("type")]
|
||||
public string Type { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("from")]
|
||||
public string From { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("to")]
|
||||
public string To { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("channelId")]
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("sdp")]
|
||||
public string? Sdp { get; set; }
|
||||
|
||||
[JsonPropertyName("candidate")]
|
||||
public string? Candidate { get; set; }
|
||||
|
||||
[JsonPropertyName("sdpMid")]
|
||||
public string? SdpMid { get; set; }
|
||||
|
||||
[JsonPropertyName("sdpMLineIndex")]
|
||||
public int? SdpMLineIndex { get; set; }
|
||||
|
||||
[JsonPropertyName("isInitiator")]
|
||||
public bool IsInitiator { get; set; }
|
||||
}
|
||||
@@ -1,155 +0,0 @@
|
||||
using SurrealDb.Net.Models;
|
||||
|
||||
namespace RelayShared.Rtc;
|
||||
|
||||
public static class RtcSignalTypes
|
||||
{
|
||||
public const string Offer = "rtc_offer";
|
||||
public const string Answer = "rtc_answer";
|
||||
public const string Candidate = "rtc_candidate";
|
||||
public const string OfferUpdated = "rtc_offer_updated";
|
||||
public const string AnswerUpdated = "rtc_answer_updated";
|
||||
public const string CandidateAdded = "rtc_candidate_added";
|
||||
public const string CallLeft = "rtc_call_left";
|
||||
}
|
||||
|
||||
public sealed class RtcSignalMessage
|
||||
{
|
||||
public string Type { get; set; } = string.Empty;
|
||||
public string From { get; set; } = string.Empty;
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string? Sdp { get; set; }
|
||||
public string? Candidate { get; set; }
|
||||
public string? SdpMid { get; set; }
|
||||
public int? SdpMLineIndex { get; set; }
|
||||
public bool IsInitiator { get; set; }
|
||||
}
|
||||
|
||||
public sealed class RtcNotificationMessage
|
||||
{
|
||||
public string? Type { get; set; }
|
||||
public string? ChannelId { get; set; }
|
||||
public string? Username { get; set; }
|
||||
public string? Direction { get; set; }
|
||||
}
|
||||
|
||||
public sealed class ServerPublicKeyMessage
|
||||
{
|
||||
public string Type { get; set; } = string.Empty;
|
||||
public string PublicKey { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public sealed class SocketRtcSignalMessage
|
||||
{
|
||||
public string Type { get; set; } = string.Empty;
|
||||
public string SenderUsername { get; set; } = string.Empty;
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string CipherText { get; set; } = string.Empty;
|
||||
public string Nonce { get; set; } = string.Empty;
|
||||
public string Tag { get; set; } = string.Empty;
|
||||
public string EncryptedKey { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public sealed class SocketEncryptedMessage
|
||||
{
|
||||
public string Type { get; set; } = string.Empty;
|
||||
public string SenderUsername { get; set; } = string.Empty;
|
||||
public string RecipientUsername { get; set; } = string.Empty;
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string CipherText { get; set; } = string.Empty;
|
||||
public string Nonce { get; set; } = string.Empty;
|
||||
public string Tag { get; set; } = string.Empty;
|
||||
public string EncryptedKey { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public sealed class ChannelItem
|
||||
{
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public DateTime CreatedAt { get; set; }
|
||||
}
|
||||
|
||||
public sealed class SocketChannelList
|
||||
{
|
||||
public string Type { get; set; } = string.Empty;
|
||||
public List<ChannelItem> Channels { get; set; } = [];
|
||||
}
|
||||
|
||||
public sealed class RtcJoinRequest
|
||||
{
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string Username { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public sealed class RtcJoinResponse
|
||||
{
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public bool HasActiveCall { get; set; }
|
||||
public bool IsOfferer { get; set; }
|
||||
public string? OfferUser { get; set; }
|
||||
public RtcSessionDescription? OfferSdp { get; set; }
|
||||
}
|
||||
|
||||
public sealed class RtcLeaveRequest
|
||||
{
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string Username { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public sealed class RtcSessionDescription
|
||||
{
|
||||
public string Type { get; set; } = string.Empty;
|
||||
public string Sdp { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public sealed class RtcOffer
|
||||
{
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string Username { get; set; } = string.Empty;
|
||||
public RtcSessionDescription SessionDescription { get; set; } = new();
|
||||
}
|
||||
|
||||
public sealed class RtcAnswer
|
||||
{
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string Username { get; set; } = string.Empty;
|
||||
public RtcSessionDescription SessionDescription { get; set; } = new();
|
||||
}
|
||||
|
||||
public class RtcIceCandidate : Record
|
||||
{
|
||||
public required string ChannelId { get; set; }
|
||||
public required string Username { get; set; }
|
||||
public required string Candidate { get; set; }
|
||||
public string? SdpMid { get; set; }
|
||||
public int? SdpMLineIndex { get; set; }
|
||||
// public required string Direction { get; set; } // "offer" or "answer"
|
||||
public DateTime CreatedAt { get; set; }
|
||||
}
|
||||
|
||||
public class DBIceCandidate
|
||||
{
|
||||
public required string ChannelId { get; set; }
|
||||
public required string Username { get; set; }
|
||||
public required IceCandidate Candidate { get; set; }
|
||||
}
|
||||
|
||||
public class IceCandidate
|
||||
{
|
||||
public required string candidate { get; set; }
|
||||
public required string sdpMid { get; set; }
|
||||
public required int sdpMLineIndex { get; set; }
|
||||
public required string usernameFragment { get; set; }
|
||||
|
||||
}
|
||||
|
||||
public sealed class RtcActiveCall : Record
|
||||
{
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string? OfferUser { get; set; }
|
||||
public RtcSessionDescription? Offer { get; set; }
|
||||
public RtcSessionDescription? Answer { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public DateTime UpdatedAt { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
}
|
||||
10
RelayShared/Services/ChannelEnums.cs
Normal file
10
RelayShared/Services/ChannelEnums.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace RelayShared.Services;
|
||||
|
||||
public enum ChannelType
|
||||
{
|
||||
Text, //Default channel type, handles text, links, files*, all in a linear live chat format
|
||||
Voice, //Used for general voice and video calls, utilizes WebRTC in its intended use
|
||||
File, //File browser for connected text channels, used for browsing files rather than scrolling through text channel
|
||||
Forum, //Specific forum posts, meant to keep conversations grouped and on topic while keeping all in an easy to find place
|
||||
Stage //Used for announcements and presentations, voice/video call utilizing a modified WebRTC protocol through server
|
||||
}
|
||||
18
RelayShared/Services/ChannelTransmissions.cs
Normal file
18
RelayShared/Services/ChannelTransmissions.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
namespace RelayShared.Services;
|
||||
|
||||
public sealed class ChannelItem
|
||||
{
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
public ChannelType Type { get; set; }
|
||||
|
||||
public string Group { get; set; } = string.Empty;
|
||||
public DateTime CreatedAt { get; set; }
|
||||
}
|
||||
|
||||
public sealed class SocketChannelList
|
||||
{
|
||||
public SignalType Type { get; set; } = SignalType.ChannelList;
|
||||
public List<ChannelItem> Channels { get; set; } = [];
|
||||
}
|
||||
38
RelayShared/Services/ConsoleCommandService.cs
Normal file
38
RelayShared/Services/ConsoleCommandService.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
namespace RelayShared.Services;
|
||||
|
||||
public static class ConsoleCommandService
|
||||
{
|
||||
public static CancellationTokenSource ShutdownTokenSource { get; } = new();
|
||||
|
||||
public static void Start()
|
||||
{
|
||||
Task.Run(() =>
|
||||
{
|
||||
while (!ShutdownTokenSource.IsCancellationRequested)
|
||||
{
|
||||
var input = Console.ReadLine();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
continue;
|
||||
|
||||
HandleCommand(input.Trim().ToLower());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void HandleCommand(string command)
|
||||
{
|
||||
switch (command)
|
||||
{
|
||||
case "exit":
|
||||
case "stop":
|
||||
Console.WriteLine("Shutting down...");
|
||||
ShutdownTokenSource.Cancel();
|
||||
break;
|
||||
|
||||
default:
|
||||
Console.WriteLine($"Unknown command: {command}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
48
RelayShared/Services/SocketTransmissions.cs
Normal file
48
RelayShared/Services/SocketTransmissions.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
namespace RelayShared.Services;
|
||||
|
||||
//TODO: review name of file, potentially rename for Encryption services rather than sockets
|
||||
|
||||
public sealed class SocketRtcSignalMessage
|
||||
{
|
||||
public SignalType Type { get; set; }
|
||||
public string SenderUsername { get; set; } = string.Empty;
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string CipherText { get; set; } = string.Empty;
|
||||
public string Nonce { get; set; } = string.Empty;
|
||||
public string Tag { get; set; } = string.Empty;
|
||||
public string EncryptedKey { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public sealed class SocketEncryptedMessage
|
||||
{
|
||||
public SignalType Type { get; set; } = SignalType.EncryptedChat;
|
||||
public string SenderUsername { get; set; } = string.Empty;
|
||||
public string RecipientUsername { get; set; } = string.Empty;
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string CipherText { get; set; } = string.Empty;
|
||||
public string Nonce { get; set; } = string.Empty;
|
||||
public string Tag { get; set; } = string.Empty;
|
||||
public string EncryptedKey { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public sealed class ServerPublicKeyMessage
|
||||
{
|
||||
public SignalType Type { get; set; } = SignalType.ServerPublicKey;
|
||||
public string PublicKey { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public enum SignalType
|
||||
{
|
||||
Offer,
|
||||
Answer,
|
||||
Candidate,
|
||||
OfferUpdated,
|
||||
AnswerUpdated,
|
||||
CandidateAdded,
|
||||
CallLeft,
|
||||
ChannelList,
|
||||
ServerPublicKey,
|
||||
EncryptedSignal,
|
||||
EncryptedChat,
|
||||
ClientEncryptedChat
|
||||
}
|
||||
@@ -64,18 +64,18 @@ Start-Sleep -Seconds 5
|
||||
& '$clientExe' --user Ru_Kira
|
||||
"@
|
||||
|
||||
# $testScript = New-TabScript -Name "Test" -Content @"
|
||||
# Set-Location '$root'
|
||||
# Start-Sleep -Seconds 25
|
||||
# & '$clientExe' --user Test
|
||||
# "@
|
||||
$testScript = New-TabScript -Name "Test" -Content @"
|
||||
Set-Location '$root'
|
||||
Start-Sleep -Seconds 25
|
||||
& '$clientExe' --user Test
|
||||
"@
|
||||
|
||||
$wtArgs = @(
|
||||
"new-tab --title `"SurrealDB`" `"$ps`" -NoExit -ExecutionPolicy Bypass -File `"$dockerScript`"",
|
||||
"new-tab --title `"RelayCore`" `"$ps`" -NoExit -ExecutionPolicy Bypass -File `"$coreScript`"",
|
||||
"new-tab --title `"RelayServer`" `"$ps`" -NoExit -ExecutionPolicy Bypass -File `"$serverScript`"",
|
||||
"new-tab --title `"Keeper317`" `"$ps`" -NoExit -ExecutionPolicy Bypass -File `"$keeperScript`"",
|
||||
# "new-tab --title `"Test`" `"$ps`" -NoExit -ExecutionPolicy Bypass -File `"$testScript`"",
|
||||
"new-tab --title `"Test`" `"$ps`" -NoExit -ExecutionPolicy Bypass -File `"$testScript`"",
|
||||
"new-tab --title `"Ru_Kira`" `"$ps`" -NoExit -ExecutionPolicy Bypass -File `"$kiraScript`""
|
||||
) -join " ; "
|
||||
|
||||
|
||||
Reference in New Issue
Block a user