9 Commits

11 changed files with 218 additions and 64 deletions

View File

@@ -32,7 +32,6 @@ public partial class MainPage : ContentPage
KeyStorage.SavePublicKey(_username, keys.publicKey);
}
hybridWebView.SetInvokeJavaScriptTarget(this);
ServerAPI.setupClient();
_socket = new RelaySocketClient(_username);
@@ -44,6 +43,8 @@ public partial class MainPage : ContentPage
SafeSendRawToWebView
);
hybridWebView.SetInvokeJavaScriptTarget(_rtc);
_socket.Log += Console.WriteLine;
_socket.ChannelListReceived += HandleChannelList;
_socket.EncryptedChatReceived += HandleEncryptedChat;
@@ -297,26 +298,6 @@ public partial class MainPage : ContentPage
SwapView();
}
public Task JoinRtcChannel()
{
return _rtc.JoinRtcChannel();
}
public void LeaveRtcChannel()
{
_rtc.LeaveRtcChannel();
}
public void SendRtcSignal(string json)
{
_rtc.SendRtcSignal(json);
}
public Task<string> GetRtcParticipants()
{
return _rtc.GetRtcParticipants();
}
private async void OnHybridWebViewRawMessageReceived(object sender, HybridWebViewRawMessageReceivedEventArgs e)
{
if (e.Message == "rtc_page_ready")

View File

@@ -50,3 +50,22 @@ window.addEventListener("load", async () => {
await Media.loadDevices();
await Media.ensureLocalMedia();
});
function testIndex(rawJson)
{
const data = typeof rawJson === "string" ? JSON.parse(rawJson) : rawJson;
data.sdp = data.sdp.replaceAll("(rn)", "\r\n");
handleRtcSignal(JSON.stringify(data));
// if (data.type === "rtc_offer") {
// handleOffer(data)
// }
// if (data.type === "rtc_answer") {
// data.sdp = data.sdp.replaceAll("(rn)", "\r\n");
// handleAnswer(data)
// }
}
function noDataTest()
{
LogMessage("No Data Called!!");
}

View File

@@ -1,4 +1,4 @@
const peerConnections = {};
const peerConnections = {};
async function joinChannelCall() {
LogMessage("Current username: " + currentUsername);
@@ -24,7 +24,7 @@ async function joinChannelCall() {
}
for (const username of existingUsers) {
await sendOffer(username);
await sendOffer(username); //Creates an offer to each person in call for MESH RTC
}
}
@@ -34,6 +34,7 @@ async function sendOffer(username) {
await Media.applyLocalStreamToPeerConnection(pc, username);
const offer = await pc.createOffer();
// LogMessage(`Offer created: ${JSON.stringify(offer)}`);
await pc.setLocalDescription(offer);
await RelaySocket.sendRtcSignal({
@@ -88,11 +89,12 @@ async function handleRtcSignal(rawJson) {
}
async function handleOffer(msg) {
LogMessage(`Offer handler: ${msg}`);
const pc = await ensurePeerConnectionForUser(msg.from);
await Media.ensureLocalMedia();
await Media.applyLocalStreamToPeerConnection(pc, msg.from);
// const offer = JSON.parse(msg.offer);
await pc.setRemoteDescription({
type: "offer",
sdp: msg.sdp

View File

@@ -1,4 +1,5 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using RelayClient.Crypto;
using RelayShared.Rtc;
using RelayShared.Services;
@@ -13,12 +14,8 @@ public sealed class RtcBridgeService
private readonly Func<string?> _getCurrentChannelId;
private readonly Action<string> _sendRawToWebView;
public RtcBridgeService(
string username,
RelaySocketClient socket,
HybridWebView hybridWebView,
Func<string?> getCurrentChannelId,
Action<string> sendRawToWebView)
public RtcBridgeService(string username, RelaySocketClient socket, HybridWebView hybridWebView,
Func<string?> getCurrentChannelId, Action<string> sendRawToWebView)
{
_username = username;
_socket = socket;
@@ -74,6 +71,7 @@ public sealed class RtcBridgeService
rtcSignal.ChannelId ??= _getCurrentChannelId();
rtcSignal.From ??= _username;
// _sendRawToWebView($"RTC_SIGNAL file: {JsonSerializer.Serialize(rtcSignal)}");
if (string.IsNullOrWhiteSpace(rtcSignal.ChannelId))
{
_sendRawToWebView("SendRtcSignal failed: missing channel id.");
@@ -120,13 +118,20 @@ public sealed class RtcBridgeService
public async Task HandleIncomingRtcSignalAsync(SocketRtcSignalMessage payload)
{
// _sendRawToWebView("HandleIncomingRtcSignal called");
var currentChannelId = _getCurrentChannelId();
if (payload.ChannelId != currentChannelId)
{
_sendRawToWebView("Channel id does not match");
return;
}
if (payload.SenderUsername == _username)
{
_sendRawToWebView("Received own message");
return;
}
string decryptedJson;
@@ -156,6 +161,7 @@ public sealed class RtcBridgeService
try
{
rtcSignal = JsonSerializer.Deserialize<RtcSignalMessage>(decryptedJson);
// _sendRawToWebView($"Received Encrypted Signal: [{rtcSignal.From}]: {rtcSignal.Offer}");
}
catch (Exception ex)
{
@@ -164,7 +170,10 @@ public sealed class RtcBridgeService
}
if (rtcSignal is null)
{
_sendRawToWebView("rtcSignal is null");
return;
}
if (!string.IsNullOrWhiteSpace(rtcSignal.To) &&
!string.Equals(rtcSignal.To, _username, StringComparison.OrdinalIgnoreCase))
@@ -173,9 +182,9 @@ public sealed class RtcBridgeService
return;
}
_sendRawToWebView("Received encrypted RTC signal: " + decryptedJson);
// _sendRawToWebView("Received encrypted RTC signal: " + decryptedJson);
await SendRtcSignalToJsAsync(decryptedJson);
await SendRtcSignalToJsAsync(rtcSignal);
}
public Task PushRtcContextToJsAsync()
@@ -192,37 +201,55 @@ public sealed class RtcBridgeService
return Task.CompletedTask;
}
private Task SendRtcSignalToJsAsync(string rawJson)
private Task SendRtcSignalToJsAsync(RtcSignalMessage data)
{
if (data.Type == "rtc_offer" || data.Type == "rtc_answer")
{
data.Sdp = data.Sdp.Replace("\r\n", "(rn)");
}
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);
}}
");
// await _hybridWebView.InvokeJavaScriptAsync("testIndex", [JsonSerializer.Serialize(data)], [RtcJsType.Default.String]);
await _hybridWebView.InvokeJavaScriptAsync("testIndex", [data], [RtcJsType.Default.RtcSignalMessage]);
#region OldDebugger
// var jsArg = JsonSerializer.Serialize(data);
//
// 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);
// }}
// ");
#endregion
}
catch (Exception ex)
{
_sendRawToWebView("SendRtcSignalToJsAsync failed: " + ex.Message);
}
});
return Task.CompletedTask;
}
}
[JsonSourceGenerationOptions(WriteIndented = false)]
[JsonSerializable(typeof(RtcDescription))]
[JsonSerializable(typeof(List<RtcSignalMessage>))]
[JsonSerializable(typeof(RtcSignalMessage))]
[JsonSerializable(typeof(IceCandidate))]
[JsonSerializable(typeof(List<IceCandidate>))]
[JsonSerializable(typeof(string))]
internal partial class RtcJsType : JsonSerializerContext
{
}

View File

@@ -0,0 +1,59 @@
using RelayCore.Services;
namespace RelayCore.Endpoints;
public static class AuthEndpoints
{
public static void MapAuthEndpoints(this WebApplication app)
{
app.MapPost("/user/signin", async (AuthSignin request, APIAuthService service, HttpContext context) =>
{
var ip = context.Connection.RemoteIpAddress?.MapToIPv4().ToString();
context.Request.Headers.TryGetValue("User-Agent", out var userAgent);
Console.WriteLine($"IP:{ip}\nUserAgent:{userAgent}");
// var token = await service.UserSigninAsync(request, ip, userAgent);
// return token != null ? Results.Ok(token) : Results.Unauthorized();
return Results.Ok();
});
app.MapPost("/user/register", async (AuthRegister request, APIAuthService service) =>
{
var token = await service.UserRegisterAsync(request);
return token != null ? Results.Ok(token) : Results.Unauthorized();
});
app.MapPost("/server/verify/user", async (AuthUserVerify request, APIAuthService service) =>
{
bool valid = await service.ServerVerifyUser(request);
return Results.Ok(valid);
});
app.MapPost("/server/verify/license", async (AuthServerLicense request, APIAuthService service) =>
{
throw new NotImplementedException();
});
}
}
public class AuthSignin
{
public string UserName { get; set; }
public string Password { get; set; }
}
public class AuthRegister
{
public string Username { get; set; }
public string Password { get; set; }
public string Email { get; set; }
}
public class AuthUserVerify
{
public string Username { get; set; }
public string Token { get; set; }
}
public class AuthServerLicense
{
public string License { get; set; }
}

View File

@@ -22,7 +22,7 @@ namespace RelayCore.Models
/// <summary>
/// Number of threads to use for parallel computation
/// </summary>
private const int DegreeOfParallelism = 1;
private const int DegreeOfParallelism = 2;
/// <summary>
/// Number of iterations for the Argon2id algorithm

View File

@@ -4,7 +4,7 @@ namespace RelayCore.Models;
public class Sessions : Record
{
public required string UserId { get; set; }
public required RecordId UserId { get; set; }
public required string TokenHash { get; set; }
public required DateTime IssuedAt { get; set; }
public required DateTime ExpiresAt { get; set; }

View File

@@ -1,14 +1,13 @@
using SurrealDb.Net;
using SurrealDb.Net.Models.Auth;
using System.Text.Json;
using System;
using System.Net;
using System.Threading.Tasks;
using System.Text;
using System.Text.Json;
using RelayCore.Enums;
using RelayCore.Models;
using RelayCore.Endpoints;
using RelayCore.Services;
await using var db = new SurrealDbClient("ws://127.0.0.1:8000/rpc");
@@ -25,8 +24,25 @@ Console.WriteLine($"Keeper created: {ToJsonString(keeper)}");
Console.WriteLine($"Kira created: {ToJsonString(kira)}");
Console.WriteLine($"Test created: {ToJsonString(test)}");
await server.Main(db);
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseUrls("http://127.0.0.1:1337/");
builder.Services.AddSingleton(db);
builder.Services.AddScoped<APIAuthService>();
var app = builder.Build();
app.MapGet("/", () => "Auth Server Running!");
app.MapAuthEndpoints();
// await server.Main(db);
await app.StartAsync();
Console.WriteLine("API Started");
Console.WriteLine("\n\n\n");
Console.Write("Press any key to stop.");
Console.ReadKey(true);
await app.StopAsync();
return;
static string ToJsonString(object? o)
@@ -51,7 +67,7 @@ static async Task<Users> CreateUserAsync(SurrealDbClient db, string username, st
OnlineStatus = (int)OnlineStatuses.Online,
};
var created = await db.Create("users", user);
var created = await db.Create("auth_users", user);
var hasher = new PasswordHasher();
var passwordHash = hasher.HashPassword(created.Id.ToString() + rawPassword);
@@ -65,7 +81,6 @@ static async Task<Users> CreateUserAsync(SurrealDbClient db, string username, st
return updated;
}
partial class Program
{
public async Task Main(SurrealDbClient db)

View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<OutputType>Exe</OutputType>
@@ -10,11 +10,12 @@
<ItemGroup>
<PackageReference Include="Konscious.Security.Cryptography.Argon2" Version="1.3.1" />
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.2.9" />
<PackageReference Include="SurrealDb.Net" Version="0.9.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Services\" />
<ProjectReference Include="..\RelayShared\RelayShared.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,49 @@
using RelayCore.Endpoints;
using RelayCore.Models;
using SurrealDb.Net;
using SurrealDb.Net.Models;
namespace RelayCore.Services;
public class APIAuthService(SurrealDbClient _db)
{
public async Task<string> UserSigninAsync(AuthSignin request)
{
var hasher = new PasswordHasher();
var users = await _db.Select<Users>("auth_users");
var user = users.FirstOrDefault(x => (x.Username == request.UserName || x.Email == request.UserName)
&& hasher.VerifyPassword(request.Password, x.Password));
var tokens = await _db.Select<Sessions>("auth_sessions");
var token = tokens.Where(x => x.UserId == user.Id && !x.Revoked).OrderByDescending(x => x.ExpiresAt).FirstOrDefault();
if (token.ExpiresAt > DateTime.UtcNow)
return token.TokenHash;
//TODO: Generate TOKEN
var newToken = hasher.HashPassword($"{user.Email}{user.Username}{user.Password}");
//TODO: Store TOKEN and Username for verification
var sessionId = await _db.Create<Sessions>(new Sessions
{
UserId = user.Id,
TokenHash = newToken,
IssuedAt = DateTime.UtcNow,
ExpiresAt = DateTime.UtcNow.AddDays(30),
DeviceName = "",
Revoked = false,
IpAddress = "",
UserAgent = ""
});
//TODO: Add invalidation to TOKENs
return newToken;
}
public async Task<string> UserRegisterAsync(AuthRegister request)
{
throw new NotImplementedException();
}
public async Task<bool> ServerVerifyUser(AuthUserVerify request)
{
throw new NotImplementedException();
}
}

View File

@@ -21,6 +21,7 @@ var bootstrapService = new ServerBootstrapService(db, coreClient, cryptoService)
await bootstrapService.InitializeAsync();
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseUrls("http://127.0.0.1:5000/");
builder.Services.AddSingleton(db);
builder.Services.AddScoped<RtcCallService>();