Compare commits
9 Commits
CoreAuth
...
b62ceb1949
| Author | SHA1 | Date | |
|---|---|---|---|
| b62ceb1949 | |||
| 1ed3efcc68 | |||
| 9fbe795660 | |||
| 63d3806936 | |||
| a9d2fd64de | |||
| f8b595f609 | |||
| 885db41ba9 | |||
| 4974663128 | |||
| 3901542141 |
@@ -15,7 +15,8 @@ public partial class App : Application
|
||||
|
||||
if (string.IsNullOrWhiteSpace(username))
|
||||
{
|
||||
throw new Exception("Missing required --user argument. Example: --user Keeper317");
|
||||
username = "Test";
|
||||
// throw new Exception("Missing required --user argument. Example: --user Keeper317");
|
||||
}
|
||||
|
||||
ClientSession.Username = username;
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
|
||||
xmlns:local="clr-namespace:RelayClient"
|
||||
Title="RelayClient">
|
||||
Title="RelayClient"
|
||||
FlyoutBehavior="Flyout">
|
||||
|
||||
<ShellContent
|
||||
Title="Home"
|
||||
|
||||
@@ -8,10 +8,12 @@ namespace RelayClient;
|
||||
|
||||
public partial class MainPage : ContentPage
|
||||
{
|
||||
private readonly string _username;
|
||||
public static string _username;
|
||||
private readonly RelaySocketClient _socket;
|
||||
private readonly RtcBridgeService _rtc;
|
||||
|
||||
public static string? _userToken;
|
||||
|
||||
private string? _currentChannelId;
|
||||
private string? _currentChannelName;
|
||||
|
||||
@@ -23,6 +25,7 @@ public partial class MainPage : ContentPage
|
||||
InitializeComponent();
|
||||
|
||||
_username = username;
|
||||
|
||||
UserLabel.Text = $"Logged in as: {_username}";
|
||||
|
||||
if (!KeyStorage.HasKeys(_username))
|
||||
@@ -32,7 +35,7 @@ public partial class MainPage : ContentPage
|
||||
KeyStorage.SavePublicKey(_username, keys.publicKey);
|
||||
}
|
||||
|
||||
ServerAPI.setupClient();
|
||||
var waitFor = ServerAPI.setupClient();
|
||||
|
||||
_socket = new RelaySocketClient(_username);
|
||||
_rtc = new RtcBridgeService(
|
||||
@@ -56,6 +59,8 @@ public partial class MainPage : ContentPage
|
||||
});
|
||||
};
|
||||
|
||||
// while(!waitFor.IsCompleted){}
|
||||
|
||||
_socket.Connect();
|
||||
}
|
||||
|
||||
@@ -132,11 +137,11 @@ public partial class MainPage : ContentPage
|
||||
await _rtc.PushRtcContextToJsAsync();
|
||||
});
|
||||
|
||||
_socket.SendRaw($"GET_HISTORY|{_username}|{_currentChannelId}");
|
||||
_socket.SendGetHistory(_currentChannelId);
|
||||
}
|
||||
|
||||
private void HandleEncryptedChat(SocketEncryptedMessage payload) {
|
||||
if (payload.RecipientUsername != _username)
|
||||
if (!payload.RecipientUsername.Equals(_username, StringComparison.OrdinalIgnoreCase))
|
||||
return;
|
||||
|
||||
string decryptedText;
|
||||
@@ -224,7 +229,7 @@ public partial class MainPage : ContentPage
|
||||
RenderCurrentChannelMessages();
|
||||
|
||||
if (!_messagesByChannel.ContainsKey(channel.ChannelId))
|
||||
_socket.SendRaw($"GET_HISTORY|{_username}|{channel.ChannelId}");
|
||||
_socket.SendGetHistory(channel.ChannelId);
|
||||
};
|
||||
|
||||
SidebarList.Children.Add(button);
|
||||
|
||||
@@ -54,7 +54,9 @@ window.addEventListener("load", async () => {
|
||||
function testIndex(rawJson)
|
||||
{
|
||||
const data = typeof rawJson === "string" ? JSON.parse(rawJson) : rawJson;
|
||||
if (data.sdp) {
|
||||
data.sdp = data.sdp.replaceAll("(rn)", "\r\n");
|
||||
}
|
||||
handleRtcSignal(JSON.stringify(data));
|
||||
// if (data.type === "rtc_offer") {
|
||||
// handleOffer(data)
|
||||
|
||||
@@ -140,7 +140,13 @@ async function handleIce(msg) {
|
||||
|
||||
if (!msg.candidate) return;
|
||||
|
||||
await pc.addIceCandidate(msg.candidate);
|
||||
const candidateInit = {
|
||||
candidate: msg.candidate,
|
||||
sdpMid: msg.sdpMid,
|
||||
sdpMLineIndex: msg.sdpMLineIndex
|
||||
};
|
||||
|
||||
await pc.addIceCandidate(candidateInit);
|
||||
|
||||
LogMessage(`Applied ICE from ${msg.from}`);
|
||||
}
|
||||
@@ -161,7 +167,9 @@ async function ensurePeerConnectionForUser(username) {
|
||||
channelId: currentChannelId,
|
||||
from: currentUsername,
|
||||
to: username,
|
||||
candidate: JSON.stringify(event.candidate)
|
||||
candidate: event.candidate.candidate,
|
||||
sdpMid: event.candidate.sdpMid,
|
||||
sdpMLineIndex: event.candidate.sdpMLineIndex
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -1,18 +1,51 @@
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using RelayShared.Services;
|
||||
|
||||
namespace RelayClient;
|
||||
|
||||
public class ServerAPI
|
||||
{
|
||||
static HttpClient client = new HttpClient { BaseAddress = new Uri("http://localhost:5000/") };
|
||||
static HttpClient client = new HttpClient { BaseAddress = new Uri("http://127.0.0.1:5000/") };
|
||||
static HttpClient core = new HttpClient { BaseAddress = new Uri("http://127.0.0.1:1337/") };
|
||||
// static HttpClient client = new HttpClient { BaseAddress = new Uri("http://192.168.1.92:5000/") };
|
||||
// static HttpClient core = new HttpClient { BaseAddress = new Uri("http://192.168.1.92:1337/") };
|
||||
|
||||
public static void setupClient()
|
||||
public static async Task setupClient()
|
||||
{
|
||||
client.DefaultRequestHeaders.Accept.Clear();
|
||||
client.DefaultRequestHeaders.Accept.Add(
|
||||
new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
core.DefaultRequestHeaders.Accept.Clear();
|
||||
core.DefaultRequestHeaders.Accept.Add(
|
||||
new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
core.DefaultRequestHeaders.Add("User-Agent", "RelayClient");
|
||||
MainPage._userToken = await CoreUserSignin(new AuthSignin
|
||||
{
|
||||
UserName = MainPage._username,
|
||||
Password = "password"
|
||||
});
|
||||
|
||||
await CoreUserAlive(new AuthSignin
|
||||
{
|
||||
UserName = MainPage._username,
|
||||
Password = MainPage._userToken
|
||||
});
|
||||
}
|
||||
|
||||
public static async Task<Uri> CoreUserAlive(AuthSignin data)
|
||||
{
|
||||
HttpResponseMessage response = await core.PostAsJsonAsync("user/isAlive", data);
|
||||
response.EnsureSuccessStatusCode();
|
||||
return response.Headers.Location;
|
||||
}
|
||||
|
||||
public static async Task<string> CoreUserSignin(AuthSignin data)
|
||||
{
|
||||
HttpResponseMessage response = await core.PostAsJsonAsync("user/signin", data);
|
||||
response.EnsureSuccessStatusCode();
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
|
||||
public static async Task<Uri> PostOfferAsync(DBOffer offer)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json;
|
||||
using RelayClient.Crypto;
|
||||
using RelayShared.Services;
|
||||
using WebSocketSharp;
|
||||
@@ -19,7 +19,7 @@ public sealed class RelaySocketClient
|
||||
public event Action<string>? ServerPublicKeyReceived;
|
||||
public event Action<string>? Log;
|
||||
|
||||
public RelaySocketClient(string username, string url = "ws://localhost:1337/")
|
||||
public RelaySocketClient(string username, string url = "ws://127.0.0.1:5001/")
|
||||
{
|
||||
_username = username;
|
||||
_socket = new WebSocket(url);
|
||||
@@ -32,9 +32,57 @@ public sealed class RelaySocketClient
|
||||
|
||||
var publicKey = KeyStorage.LoadPublicKey(_username);
|
||||
|
||||
SendRaw($"REGISTER_KEY|{_username}|{publicKey}");
|
||||
SendRaw("GET_SERVER_KEY");
|
||||
SendRaw("GET_CHANNELS");
|
||||
SendControlMessage(new WsControlMessage
|
||||
{
|
||||
Action = WsAction.Authenticate,
|
||||
Username = _username,
|
||||
Token = MainPage._userToken
|
||||
});
|
||||
|
||||
SendControlMessage(new WsControlMessage
|
||||
{
|
||||
Action = WsAction.RegisterKey,
|
||||
Username = _username,
|
||||
PublicKey = publicKey
|
||||
});
|
||||
|
||||
SendControlMessage(new WsControlMessage { Action = WsAction.GetServerKey });
|
||||
SendControlMessage(new WsControlMessage { Action = WsAction.GetChannels });
|
||||
}
|
||||
|
||||
public void SendControlMessage(WsControlMessage message)
|
||||
{
|
||||
SendRaw(JsonSerializer.Serialize(message));
|
||||
}
|
||||
|
||||
public void SendGetHistory(string channelId)
|
||||
{
|
||||
SendControlMessage(new WsControlMessage
|
||||
{
|
||||
Action = WsAction.GetHistory,
|
||||
Username = _username,
|
||||
ChannelId = channelId
|
||||
});
|
||||
}
|
||||
|
||||
public void SendRtcJoinChannel(string channelId)
|
||||
{
|
||||
SendControlMessage(new WsControlMessage
|
||||
{
|
||||
Action = WsAction.RtcJoin,
|
||||
Username = _username,
|
||||
ChannelId = channelId
|
||||
});
|
||||
}
|
||||
|
||||
public void SendRtcLeaveChannel(string channelId)
|
||||
{
|
||||
SendControlMessage(new WsControlMessage
|
||||
{
|
||||
Action = WsAction.RtcLeave,
|
||||
Username = _username,
|
||||
ChannelId = channelId
|
||||
});
|
||||
}
|
||||
|
||||
public void SendRaw(string message)
|
||||
@@ -58,12 +106,6 @@ public sealed class RelaySocketClient
|
||||
|
||||
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}");
|
||||
|
||||
@@ -72,6 +114,31 @@ public sealed class RelaySocketClient
|
||||
using var doc = JsonDocument.Parse(e.Data);
|
||||
var root = doc.RootElement;
|
||||
|
||||
// Control event responses (WsEvent)
|
||||
if (root.TryGetProperty("Event", out var eventElement))
|
||||
{
|
||||
var wsEvent = (WsEvent)eventElement.GetInt32();
|
||||
|
||||
switch (wsEvent)
|
||||
{
|
||||
case WsEvent.KeyRegistered:
|
||||
Log?.Invoke($"[{_username}] Key registered on server.");
|
||||
return;
|
||||
|
||||
case WsEvent.Authenticated:
|
||||
Log?.Invoke($"[{_username}] Authenticated with server.");
|
||||
return;
|
||||
|
||||
case WsEvent.Error:
|
||||
var detail = root.TryGetProperty("Detail", out var d) ? d.GetString() : null;
|
||||
Log?.Invoke($"[{_username}] Server error: {detail}");
|
||||
return;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Data messages (SignalType)
|
||||
if (!root.TryGetProperty("Type", out var typeElement))
|
||||
return;
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ public sealed class RtcBridgeService
|
||||
if (string.IsNullOrWhiteSpace(channelId))
|
||||
return Task.CompletedTask;
|
||||
|
||||
_socket.SendRaw($"RTC_JOIN_CHANNEL|{_username}|{channelId}");
|
||||
_socket.SendRtcJoinChannel(channelId);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ public sealed class RtcBridgeService
|
||||
if (string.IsNullOrWhiteSpace(channelId))
|
||||
return;
|
||||
|
||||
_socket.SendRaw($"RTC_LEAVE_CHANNEL|{_username}|{channelId}");
|
||||
_socket.SendRtcLeaveChannel(channelId);
|
||||
}
|
||||
|
||||
public void SendRtcSignal(string json)
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using RelayCore.Services;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using RelayCore.Services;
|
||||
using RelayShared.Services;
|
||||
|
||||
namespace RelayCore.Endpoints;
|
||||
|
||||
@@ -7,53 +9,55 @@ public static class AuthEndpoints
|
||||
public static void MapAuthEndpoints(this WebApplication app)
|
||||
{
|
||||
app.MapPost("/user/signin", async (AuthSignin request, APIAuthService service, HttpContext context) =>
|
||||
{
|
||||
string ip = "";
|
||||
StringValues userAgent = "";
|
||||
if (context != null)
|
||||
{
|
||||
ip = context.Connection.RemoteIpAddress?.MapToIPv4().ToString();
|
||||
context.Request.Headers.TryGetValue("User-Agent", out userAgent);
|
||||
}
|
||||
|
||||
var token = await service.UserSigninAsync(request, ip, userAgent.ToString());
|
||||
|
||||
return token != null ? Results.Ok(token) : Results.Unauthorized();
|
||||
});
|
||||
app.MapGet("/users", async (APIAuthService service) =>
|
||||
{
|
||||
return Results.Ok(await service.GetUsersAsync());
|
||||
});
|
||||
app.MapPost("/user/register", async (AuthRegister 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();
|
||||
var token = await service.UserRegisterAsync(request, ip, userAgent);
|
||||
return token != null ? Results.Ok(token) : Results.Ok("Username or Email already exists!");
|
||||
});
|
||||
app.MapPost("/user/register", async (AuthRegister request, APIAuthService service) =>
|
||||
app.MapPost("/user/isAlive", async (AuthSignin request, HttpContext context) =>
|
||||
{
|
||||
var token = await service.UserRegisterAsync(request);
|
||||
return token != null ? Results.Ok(token) : Results.Unauthorized();
|
||||
var ip = context.Connection.RemoteIpAddress?.MapToIPv4().ToString();
|
||||
context.Request.Headers.TryGetValue("User-Agent", out var userAgent);
|
||||
|
||||
Console.WriteLine($"UN: {request.UserName}\nToken: {request.Password}\nIP: {ip}\nUserAgent: {userAgent}");
|
||||
return Results.Ok();
|
||||
});
|
||||
app.MapPost("/server/verify/user", async (AuthUserVerify request, APIAuthService service) =>
|
||||
{
|
||||
bool valid = await service.ServerVerifyUser(request);
|
||||
Console.WriteLine($"UN: {request.Username}\nToken: {request.Token}");
|
||||
return Results.Ok(valid);
|
||||
});
|
||||
app.MapPost("/server/verify/license", async (AuthServerLicense request, APIAuthService service) =>
|
||||
app.MapPost("/server/license/generate", async (AuthServerLicenseGenerate request, APIAuthService service) =>
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var license = await service.ServerLicenseGenerate(request);
|
||||
|
||||
return license != null ? Results.Ok(license) : Results.BadRequest();
|
||||
});
|
||||
app.MapPost("/server/license/verify", async (AuthServerLicenseVerify request, APIAuthService service) =>
|
||||
{
|
||||
bool valid = await service.ServerVerifyLicense(request);
|
||||
return Results.Ok(valid);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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; }
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using SurrealDb.Net.Models;
|
||||
|
||||
namespace RelayCore.Models;
|
||||
|
||||
@@ -26,6 +26,7 @@ Console.WriteLine($"Test created: {ToJsonString(test)}");
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.WebHost.UseUrls("http://127.0.0.1:1337/");
|
||||
// builder.WebHost.UseUrls("http://192.168.1.92:1337");
|
||||
builder.Services.AddSingleton(db);
|
||||
builder.Services.AddScoped<APIAuthService>();
|
||||
|
||||
@@ -87,9 +88,9 @@ partial class Program
|
||||
{
|
||||
// Set up listener
|
||||
using var listener = new HttpListener();
|
||||
listener.Prefixes.Add("http://localhost:8080/");
|
||||
listener.Prefixes.Add("http://127.0.0.1:8080/");
|
||||
listener.Start();
|
||||
Console.WriteLine("API Started: http://localhost:8080/");
|
||||
Console.WriteLine("API Started: http://127.0.0.1:8080/");
|
||||
|
||||
while (true)
|
||||
{
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
using RelayCore.Endpoints;
|
||||
using Microsoft.AspNetCore.WebUtilities;
|
||||
using Newtonsoft.Json;
|
||||
using RelayCore.Endpoints;
|
||||
using RelayCore.Enums;
|
||||
using RelayCore.Models;
|
||||
using RelayShared.Services;
|
||||
using SurrealDb.Net;
|
||||
using SurrealDb.Net.Models;
|
||||
|
||||
@@ -7,22 +11,31 @@ namespace RelayCore.Services;
|
||||
|
||||
public class APIAuthService(SurrealDbClient _db)
|
||||
{
|
||||
|
||||
public async Task<string> UserSigninAsync(AuthSignin request)
|
||||
public async Task<List<Users>> GetUsersAsync()
|
||||
{
|
||||
var users = await _db.Select<Users>("auth_users");
|
||||
return users.Where(x => x.Username is not null).OrderByDescending(x=>x.CreatedAt).ToList();
|
||||
}
|
||||
public async Task<string?> UserSigninAsync(AuthSignin request, string ip, string userAgent)
|
||||
{
|
||||
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 user = users.FirstOrDefault(x => (x.Username.ToLower() == request.UserName.ToLower() ||
|
||||
x.Email.ToLower() == request.UserName.ToLower()) &&
|
||||
hasher.VerifyPassword(x.Id + request.Password, x.Password));
|
||||
if (user == null)
|
||||
return null;
|
||||
var tokens = await _db.Select<Sessions>("auth_sessions");
|
||||
var token = tokens.Where(x => x.UserId == user.Id && !x.Revoked).OrderByDescending(x => x.ExpiresAt).FirstOrDefault();
|
||||
var token = tokens.Where(x => x.UserId == user.Id && x.IpAddress == ip && x.UserAgent == userAgent && !x.Revoked)
|
||||
.OrderByDescending(x => x.ExpiresAt).FirstOrDefault();
|
||||
if (token != null)
|
||||
if (token.ExpiresAt > DateTime.UtcNow)
|
||||
return token.TokenHash;
|
||||
|
||||
//TODO: Generate TOKEN
|
||||
var newToken = hasher.HashPassword($"{user.Email}{user.Username}{user.Password}");
|
||||
var newToken = hasher.HashPassword($"{request.UserName}{userAgent}");
|
||||
//TODO: Store TOKEN and Username for verification
|
||||
var sessionId = await _db.Create<Sessions>(new Sessions
|
||||
var sessionId = await _db.Create("auth_sessions", new Sessions
|
||||
{
|
||||
UserId = user.Id,
|
||||
TokenHash = newToken,
|
||||
@@ -30,20 +43,84 @@ public class APIAuthService(SurrealDbClient _db)
|
||||
ExpiresAt = DateTime.UtcNow.AddDays(30),
|
||||
DeviceName = "",
|
||||
Revoked = false,
|
||||
IpAddress = "",
|
||||
UserAgent = ""
|
||||
IpAddress = ip,
|
||||
UserAgent = userAgent
|
||||
});
|
||||
//TODO: Add invalidation to TOKENs
|
||||
return newToken;
|
||||
}
|
||||
|
||||
public async Task<string> UserRegisterAsync(AuthRegister request)
|
||||
public async Task<string?> UserRegisterAsync(AuthRegister request, string ip, string userAgent)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var hasher = new PasswordHasher();
|
||||
var users = await _db.Select<Users>("auth_users");
|
||||
var user = users.FirstOrDefault(x => x.Username.ToLower() == request.Username.ToLower() || x.Email.ToLower() == request.Email.ToLower());
|
||||
if (user == null)
|
||||
{
|
||||
var now = DateTime.Now;
|
||||
var created = await _db.Create("auth_users", new Users
|
||||
{
|
||||
Username = request.Username,
|
||||
Email = request.Email,
|
||||
CreatedAt = now,
|
||||
UpdatedAt = now,
|
||||
LastLogin = now,
|
||||
TwoFactorEnabled = false,
|
||||
EmailVerified = false,
|
||||
AccountStatus = (int)AccountStatuses.Active,
|
||||
OnlineStatus = (int)OnlineStatuses.Online,
|
||||
|
||||
});
|
||||
var passwordHash = hasher.HashPassword(created.Id + request.Password);
|
||||
await _db.Merge<PasswordHash, Users>(new PasswordHash
|
||||
{
|
||||
Id = created.Id,
|
||||
Password = passwordHash
|
||||
});
|
||||
|
||||
return await UserSigninAsync(new AuthSignin{UserName=request.Username, Password = request.Password}, ip, userAgent);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<bool> ServerVerifyUser(AuthUserVerify request)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var users = await _db.Select<Users>("auth_users");
|
||||
var user = users.FirstOrDefault(x => x.Username == request.Username);
|
||||
|
||||
if (user == null)
|
||||
return false;
|
||||
|
||||
var sessions = await _db.Select<Sessions>("auth_sessions");
|
||||
var session = sessions.FirstOrDefault(x => x.TokenHash == request.Token && x.UserId == user.Id);
|
||||
if (session == null)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<string?> ServerLicenseGenerate(AuthServerLicenseGenerate request)
|
||||
{
|
||||
var hasher = new PasswordHasher();
|
||||
string token = null;
|
||||
token = hasher.HashPassword(DateTime.Now.ToString("yyyyMMddHHmmss"));
|
||||
var created = await _db.Create("auth_licenses", new DBLicense
|
||||
{
|
||||
Token = token,
|
||||
IsClient = false,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ExpiresAt = DateTime.UtcNow.AddDays(365),
|
||||
IsExpired = false,
|
||||
});
|
||||
return token;
|
||||
}
|
||||
|
||||
public async Task<bool> ServerVerifyLicense(AuthServerLicenseVerify request)
|
||||
{
|
||||
var tokens = await _db.Select<DBLicense>("auth_licenses");
|
||||
var token = tokens.FirstOrDefault(x => x.Token == request.License);
|
||||
if (token != null)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ await bootstrapService.InitializeAsync();
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.WebHost.UseUrls("http://127.0.0.1:5000/");
|
||||
// builder.WebHost.UseUrls("http://192.168.1.92:5000/");
|
||||
|
||||
builder.Services.AddSingleton(db);
|
||||
builder.Services.AddScoped<RtcCallService>();
|
||||
@@ -31,7 +32,8 @@ var app = builder.Build();
|
||||
app.MapGet("/", () => "Server Running!");
|
||||
app.MapRtcEndpoints();
|
||||
|
||||
var wssv = new WebSocketServer("ws://localhost:1337");
|
||||
var wssv = new WebSocketServer("ws://127.0.0.1:5001");
|
||||
// var wssv = new WebSocketServer("ws://192.168.1.92:5001");
|
||||
wssv.AddWebSocketService<ChatSocketBehavior>("/");
|
||||
RtcNotificationService.Server = wssv;
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Text.Json;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text.Json;
|
||||
using RelayServer.Models;
|
||||
using RelayServer.Services.Crypto;
|
||||
using RelayServer.Services.Data;
|
||||
@@ -11,9 +12,9 @@ using RelayShared.Services;
|
||||
namespace RelayServer.Services.Chat;
|
||||
|
||||
/// <summary>
|
||||
/// Handles websocket-based chat operations including client key registration,
|
||||
/// server key retrieval, channel listing, channel history loading, and encrypted
|
||||
/// channel message relay.
|
||||
/// Handles websocket-based chat operations including authentication, client key
|
||||
/// registration, server key retrieval, channel listing, channel history loading,
|
||||
/// and encrypted channel message relay.
|
||||
/// </summary>
|
||||
public class ChatSocketBehavior : WebSocketBehavior
|
||||
{
|
||||
@@ -25,82 +26,328 @@ public class ChatSocketBehavior : WebSocketBehavior
|
||||
public static SurrealDb.Net.SurrealDbClient? Db { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Routes incoming websocket messages to the appropriate chat handler.
|
||||
/// Routes incoming websocket messages to the appropriate handler via JSON dispatch.
|
||||
/// Control messages carry an <c>Action</c> property; data messages carry a <c>Type</c> property.
|
||||
/// </summary>
|
||||
/// <param name="e">The websocket message event arguments.</param>
|
||||
protected override void OnMessage(MessageEventArgs e)
|
||||
{
|
||||
var msg = e.Data;
|
||||
Console.WriteLine(msg);
|
||||
|
||||
if (msg.StartsWith("REGISTER_KEY|"))
|
||||
{
|
||||
HandleRegisterKey(msg);
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg == "GET_SERVER_KEY")
|
||||
{
|
||||
HandleGetServerKey();
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg == "GET_CHANNELS")
|
||||
{
|
||||
HandleGetChannels();
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.StartsWith("GET_HISTORY|"))
|
||||
{
|
||||
HandleGetHistory(msg);
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.StartsWith("RTC_JOIN_CHANNEL|"))
|
||||
{
|
||||
HandleRtcJoinChannel(msg);
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.StartsWith("RTC_LEAVE_CHANNEL|"))
|
||||
{
|
||||
HandleRtcLeaveChannel(msg);
|
||||
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;
|
||||
if (root.TryGetProperty("Action", out var actionProp))
|
||||
{
|
||||
var action = (WsAction)actionProp.GetInt32();
|
||||
var control = JsonSerializer.Deserialize<WsControlMessage>(msg)!;
|
||||
DispatchControl(action, control);
|
||||
return;
|
||||
}
|
||||
|
||||
if (root.TryGetProperty("Type", out var typeProp))
|
||||
{
|
||||
var type = (SignalType)typeProp.GetInt32();
|
||||
|
||||
return type == SignalType.EncryptedSignal;
|
||||
}
|
||||
catch
|
||||
switch (type)
|
||||
{
|
||||
return false;
|
||||
case SignalType.EncryptedSignal:
|
||||
HandleEncryptedRtcSignal(msg);
|
||||
return;
|
||||
|
||||
case SignalType.ClientEncryptedChat:
|
||||
HandleEncryptedChatMessage(msg);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine($"Unrecognised WebSocket message from session={ID}: {msg[..Math.Min(200, msg.Length)]}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"WebSocket message error: session={ID}, error={ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dispatches a control message to the correct handler based on its action.
|
||||
/// </summary>
|
||||
private void DispatchControl(WsAction action, WsControlMessage control)
|
||||
{
|
||||
switch (action)
|
||||
{
|
||||
case WsAction.Authenticate:
|
||||
HandleAuthenticate(control);
|
||||
break;
|
||||
|
||||
case WsAction.RegisterKey:
|
||||
HandleRegisterKey(control);
|
||||
break;
|
||||
|
||||
case WsAction.GetServerKey:
|
||||
HandleGetServerKey();
|
||||
break;
|
||||
|
||||
case WsAction.GetChannels:
|
||||
HandleGetChannels();
|
||||
break;
|
||||
|
||||
case WsAction.GetHistory:
|
||||
HandleGetHistory(control);
|
||||
break;
|
||||
|
||||
case WsAction.RtcJoin:
|
||||
HandleRtcJoinChannel(control);
|
||||
break;
|
||||
|
||||
case WsAction.RtcLeave:
|
||||
HandleRtcLeaveChannel(control);
|
||||
break;
|
||||
|
||||
default:
|
||||
Console.WriteLine($"Unknown WsAction {action} from session={ID}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Control handlers
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/// <summary>
|
||||
/// Verifies a user token with the Core service. The HTTP call is wrapped in
|
||||
/// a try-catch so that a network failure never crashes the WebSocket session.
|
||||
/// </summary>
|
||||
private async void HandleAuthenticate(WsControlMessage control)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(control.Username) || string.IsNullOrWhiteSpace(control.Token))
|
||||
{
|
||||
Console.WriteLine("Invalid Authenticate payload.");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using var core = new HttpClient { BaseAddress = new Uri("http://127.0.0.1:1337") };
|
||||
core.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
core.DefaultRequestHeaders.Add("User-Agent", "RelayServer");
|
||||
|
||||
var response = await core.PostAsJsonAsync("/server/verify/user", new AuthUserVerify
|
||||
{
|
||||
Username = control.Username,
|
||||
Token = control.Token
|
||||
});
|
||||
|
||||
Console.WriteLine($"Auth response for {control.Username}: {await response.Content.ReadAsStringAsync()}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Auth verification failed for {control.Username}: {ex.Message}");
|
||||
}
|
||||
|
||||
var result = new WsEventMessage { Event = WsEvent.Authenticated, Detail = control.Username };
|
||||
Send(JsonSerializer.Serialize(result));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stores (or updates) the client's public key and registers the session in
|
||||
/// <see cref="ConnectedClientService"/> so targeted delivery can resolve session ids.
|
||||
/// </summary>
|
||||
private void HandleRegisterKey(WsControlMessage control)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(control.Username) || string.IsNullOrWhiteSpace(control.PublicKey))
|
||||
{
|
||||
Console.WriteLine("Invalid RegisterKey payload.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (ClientKeyService is null)
|
||||
{
|
||||
Console.WriteLine("ClientKeyService is not initialized.");
|
||||
return;
|
||||
}
|
||||
|
||||
RegisterOrUpdateClientKeySync(control.Username, control.PublicKey);
|
||||
ConnectedClientService.Register(ID, control.Username);
|
||||
|
||||
Console.WriteLine($"Registered key and session for {control.Username} (session={ID})");
|
||||
|
||||
var result = new WsEventMessage { Event = WsEvent.KeyRegistered, Detail = control.Username };
|
||||
Send(JsonSerializer.Serialize(result));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends the server's public key to the requesting client.
|
||||
/// </summary>
|
||||
private void HandleGetServerKey()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(ServerPublicKey))
|
||||
{
|
||||
Console.WriteLine("Server public key is not initialized.");
|
||||
return;
|
||||
}
|
||||
|
||||
var payload = new ServerPublicKeyMessage
|
||||
{
|
||||
Type = SignalType.ServerPublicKey,
|
||||
PublicKey = ServerPublicKey
|
||||
};
|
||||
|
||||
Send(JsonSerializer.Serialize(payload));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends the current list of channels to the connected client.
|
||||
/// </summary>
|
||||
private void HandleGetChannels()
|
||||
{
|
||||
if (Db is null)
|
||||
{
|
||||
Console.WriteLine("Db is not initialized.");
|
||||
return;
|
||||
}
|
||||
|
||||
var channels = GetChannelsSync()
|
||||
.OrderBy(c => c.CreatedAt)
|
||||
.Select(c => new ChannelItem
|
||||
{
|
||||
ChannelId = GetRecordId(c.Id),
|
||||
Name = c.Name,
|
||||
CreatedAt = c.CreatedAt
|
||||
})
|
||||
.ToList();
|
||||
|
||||
var payload = new SocketChannelList
|
||||
{
|
||||
Type = SignalType.ChannelList,
|
||||
Channels = channels
|
||||
};
|
||||
|
||||
Send(JsonSerializer.Serialize(payload));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads stored channel history for a specific channel, decrypts it from
|
||||
/// database storage format, and sends it back encrypted for the requesting client.
|
||||
/// </summary>
|
||||
private void HandleGetHistory(WsControlMessage control)
|
||||
{
|
||||
var username = control.Username;
|
||||
var channelId = control.ChannelId;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(channelId))
|
||||
{
|
||||
Console.WriteLine("Invalid GetHistory payload.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!EnsureCoreReady() || ChannelCryptoService is null || string.IsNullOrWhiteSpace(ChannelDbKey))
|
||||
{
|
||||
Console.WriteLine("History dependencies are not initialized.");
|
||||
return;
|
||||
}
|
||||
|
||||
var targetClient = GetClientPublicKeyByUsernameSync(username);
|
||||
|
||||
if (targetClient is null)
|
||||
{
|
||||
Console.WriteLine($"No public key found for history request user {username}");
|
||||
return;
|
||||
}
|
||||
|
||||
var allMessages = GetChannelMessagesSync();
|
||||
|
||||
var channelMessages = allMessages
|
||||
.Where(m => m.ChannelId == channelId)
|
||||
.OrderBy(m => m.CreatedAt)
|
||||
.ToList();
|
||||
|
||||
Console.WriteLine($"Sending {channelMessages.Count} history messages to {username}");
|
||||
|
||||
foreach (var dbMessage in channelMessages)
|
||||
{
|
||||
string plainText;
|
||||
|
||||
try
|
||||
{
|
||||
plainText = ChannelCryptoService.Decrypt(
|
||||
dbMessage.CipherText,
|
||||
dbMessage.Nonce,
|
||||
dbMessage.Tag,
|
||||
ChannelDbKey
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Failed to decrypt DB history row {dbMessage.Id}: {ex.Message}");
|
||||
continue;
|
||||
}
|
||||
|
||||
var encrypted = E2EeHelper.EncryptForRecipient(plainText, targetClient.PublicKey);
|
||||
|
||||
var outbound = new SocketEncryptedMessage
|
||||
{
|
||||
Type = SignalType.EncryptedChat,
|
||||
SenderUsername = ExtractUsernameFromUserId(dbMessage.SenderUserId),
|
||||
RecipientUsername = username,
|
||||
ChannelId = channelId,
|
||||
CipherText = encrypted.CipherText,
|
||||
Nonce = encrypted.Nonce,
|
||||
Tag = encrypted.Tag,
|
||||
EncryptedKey = encrypted.EncryptedKey
|
||||
};
|
||||
|
||||
Send(JsonSerializer.Serialize(outbound));
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleRtcJoinChannel(WsControlMessage control)
|
||||
{
|
||||
var username = control.Username;
|
||||
var channelId = control.ChannelId;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(channelId))
|
||||
{
|
||||
Console.WriteLine("Invalid RtcJoin payload.");
|
||||
return;
|
||||
}
|
||||
|
||||
RtcChannelPresenceService.SetUser(ID, username);
|
||||
RtcChannelPresenceService.JoinChannel(ID, channelId);
|
||||
|
||||
Console.WriteLine($"RTC presence joined: session={ID}, user={username}, channel={channelId}");
|
||||
}
|
||||
|
||||
private void HandleRtcLeaveChannel(WsControlMessage control)
|
||||
{
|
||||
var username = control.Username;
|
||||
var channelId = control.ChannelId;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(channelId))
|
||||
{
|
||||
Console.WriteLine("Invalid RtcLeave payload.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (RtcChannelPresenceService.IsInChannel(ID, channelId))
|
||||
RtcChannelPresenceService.LeaveChannel(ID);
|
||||
|
||||
Console.WriteLine($"RTC presence left: session={ID}, user={username}, channel={channelId}");
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Data message handlers
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/// <summary>
|
||||
/// Decrypts an incoming encrypted RTC signal and re-encrypts it for every
|
||||
/// other session in the same RTC channel.
|
||||
/// </summary>
|
||||
private void HandleEncryptedRtcSignal(string msg)
|
||||
{
|
||||
Console.WriteLine("RTC SIGNAL HIT");
|
||||
|
||||
SocketRtcSignalMessage? clientPayload;
|
||||
|
||||
try
|
||||
@@ -178,121 +425,10 @@ public class ChatSocketBehavior : WebSocketBehavior
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// Decrypts an incoming encrypted chat message, stores it in the database,
|
||||
/// then re-encrypts and delivers it individually to every connected server member.
|
||||
/// Messages are never broadcast — each recipient receives their own encrypted copy.
|
||||
/// </summary>
|
||||
/// <param name="e"></param>
|
||||
protected override void OnClose(CloseEventArgs e)
|
||||
{
|
||||
RtcChannelPresenceService.RemoveSession(ID);
|
||||
Console.WriteLine($"WebSocket closed: session={ID}, code={e.Code}, reason={e.Reason}");
|
||||
base.OnClose(e);
|
||||
}
|
||||
|
||||
protected override void OnError(ErrorEventArgs e)
|
||||
{
|
||||
Console.WriteLine($"WebSocket error: session={ID}, message={e.Message}");
|
||||
base.OnError(e);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts a display username from a stored user record id value.
|
||||
/// </summary>
|
||||
/// <param name="senderUserId">The stored sender user id.</param>
|
||||
/// <returns>
|
||||
/// The extracted username when possible; otherwise, a fallback value.
|
||||
/// </returns>
|
||||
private static string ExtractUsernameFromUserId(string senderUserId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(senderUserId))
|
||||
return "Unknown";
|
||||
|
||||
var parts = senderUserId.Split(':', 2);
|
||||
return parts.Length == 2 ? parts[1] : senderUserId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers or updates a client's public key from a websocket registration payload.
|
||||
/// </summary>
|
||||
/// <param name="msg">The raw websocket registration message.</param>
|
||||
private void HandleRegisterKey(string msg)
|
||||
{
|
||||
var parts = msg.Split('|', 3);
|
||||
|
||||
if (parts.Length < 3)
|
||||
{
|
||||
Console.WriteLine("Invalid REGISTER_KEY payload.");
|
||||
return;
|
||||
}
|
||||
|
||||
var username = parts[1];
|
||||
var publicKey = parts[2];
|
||||
|
||||
if (ClientKeyService is null)
|
||||
{
|
||||
Console.WriteLine("ClientKeyService is not initialized.");
|
||||
return;
|
||||
}
|
||||
|
||||
RegisterOrUpdateClientKeySync(username, publicKey);
|
||||
|
||||
Send($"SERVER:REGISTERED_KEY:{username}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends the current list of channels to the connected websocket client.
|
||||
/// </summary>
|
||||
private void HandleGetChannels()
|
||||
{
|
||||
if (Db is null)
|
||||
{
|
||||
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()
|
||||
{
|
||||
ChannelId = GetRecordId(c.Id),
|
||||
Name = c.Name,
|
||||
CreatedAt = c.CreatedAt
|
||||
})
|
||||
.ToList();
|
||||
|
||||
var payload = new SocketChannelList
|
||||
{
|
||||
Type = SignalType.ChannelList,
|
||||
Channels = channels
|
||||
};
|
||||
|
||||
Send(JsonSerializer.Serialize(payload));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends the server's public key to the connected websocket client.
|
||||
/// </summary>
|
||||
private void HandleGetServerKey()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(ServerPublicKey))
|
||||
{
|
||||
Console.WriteLine("Server public key is not initialized.");
|
||||
return;
|
||||
}
|
||||
|
||||
var payload = new ServerPublicKeyMessage
|
||||
{
|
||||
Type = SignalType.ServerPublicKey,
|
||||
PublicKey = ServerPublicKey
|
||||
};
|
||||
|
||||
Send(JsonSerializer.Serialize(payload));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decrypts an incoming encrypted chat payload, stores it in the database,
|
||||
/// and rebroadcasts it to connected clients encrypted with each client's public key.
|
||||
/// </summary>
|
||||
/// <param name="msg">The raw encrypted chat websocket message.</param>
|
||||
private void HandleEncryptedChatMessage(string msg)
|
||||
{
|
||||
SocketEncryptedMessage? clientPayload;
|
||||
@@ -335,9 +471,10 @@ public class ChatSocketBehavior : WebSocketBehavior
|
||||
}
|
||||
|
||||
Console.WriteLine($"Server decrypted message from {clientPayload.SenderUsername}: {plainText}");
|
||||
|
||||
try
|
||||
{
|
||||
var dbEncrypted = ChannelCryptoService.Encrypt(plainText, ChannelDbKey);
|
||||
var dbEncrypted = ChannelCryptoService!.Encrypt(plainText, ChannelDbKey);
|
||||
|
||||
var savedMessage = CreateChannelMessageSync(new ChannelMessages
|
||||
{
|
||||
@@ -357,19 +494,40 @@ public class ChatSocketBehavior : WebSocketBehavior
|
||||
return;
|
||||
}
|
||||
|
||||
var allKeys = GetAllClientPublicKeysSync();
|
||||
// Deliver to every connected server member individually.
|
||||
var members = GetServerMembersSync();
|
||||
|
||||
foreach (var client in allKeys)
|
||||
foreach (var member in members)
|
||||
{
|
||||
var encrypted = E2EeHelper.EncryptForRecipient(plainText, client.PublicKey);
|
||||
// Derive the lowercase username from the stored record id (e.g. "users:keeper317").
|
||||
var rawUsername = ExtractUsernameFromUserId(member.UserId);
|
||||
|
||||
Console.WriteLine($"Encrypting outbound message from {clientPayload.SenderUsername} for {client.Username}");
|
||||
// Find all active sessions for this member (supports multi-device).
|
||||
var sessionIds = ConnectedClientService.GetSessionsForUser(rawUsername);
|
||||
if (sessionIds.Count == 0)
|
||||
continue;
|
||||
|
||||
// Resolve the correctly-cased username as the client registered it.
|
||||
var properUsername = sessionIds
|
||||
.Select(ConnectedClientService.GetUsernameForSession)
|
||||
.FirstOrDefault(u => u is not null) ?? rawUsername;
|
||||
|
||||
var clientKey = GetClientPublicKeyByUsernameSync(properUsername);
|
||||
if (clientKey is null)
|
||||
{
|
||||
Console.WriteLine($"No public key for {properUsername}, skipping.");
|
||||
continue;
|
||||
}
|
||||
|
||||
var encrypted = E2EeHelper.EncryptForRecipient(plainText, clientKey.PublicKey);
|
||||
|
||||
Console.WriteLine($"Encrypting outbound message from {clientPayload.SenderUsername} for {properUsername}");
|
||||
|
||||
var outbound = new SocketEncryptedMessage
|
||||
{
|
||||
Type = SignalType.EncryptedChat,
|
||||
SenderUsername = clientPayload.SenderUsername,
|
||||
RecipientUsername = client.Username,
|
||||
RecipientUsername = properUsername,
|
||||
ChannelId = clientPayload.ChannelId,
|
||||
CipherText = encrypted.CipherText,
|
||||
Nonce = encrypted.Nonce,
|
||||
@@ -377,117 +535,35 @@ public class ChatSocketBehavior : WebSocketBehavior
|
||||
EncryptedKey = encrypted.EncryptedKey
|
||||
};
|
||||
|
||||
Sessions.Broadcast(JsonSerializer.Serialize(outbound));
|
||||
var json = JsonSerializer.Serialize(outbound);
|
||||
|
||||
foreach (var sessionId in sessionIds)
|
||||
Sessions.SendTo(json, sessionId);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads stored channel history for a specific user and channel, decrypts it from
|
||||
/// database storage format, and sends it back encrypted for the requesting client.
|
||||
/// </summary>
|
||||
/// <param name="msg">The raw history request websocket message.</param>
|
||||
private void HandleGetHistory(string msg)
|
||||
// -------------------------------------------------------------------------
|
||||
// Lifecycle
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
protected override void OnClose(CloseEventArgs e)
|
||||
{
|
||||
var parts = msg.Split('|', 3);
|
||||
|
||||
if (parts.Length < 3)
|
||||
{
|
||||
Console.WriteLine("Invalid GET_HISTORY payload.");
|
||||
return;
|
||||
ConnectedClientService.Unregister(ID);
|
||||
RtcChannelPresenceService.RemoveSession(ID);
|
||||
Console.WriteLine($"WebSocket closed: session={ID}, code={e.Code}, reason={e.Reason}");
|
||||
base.OnClose(e);
|
||||
}
|
||||
|
||||
var username = parts[1];
|
||||
var channelId = parts[2];
|
||||
|
||||
if (!EnsureCoreReady() || ChannelCryptoService is null || string.IsNullOrWhiteSpace(ChannelDbKey))
|
||||
protected override void OnError(ErrorEventArgs e)
|
||||
{
|
||||
Console.WriteLine("History dependencies are not initialized.");
|
||||
return;
|
||||
Console.WriteLine($"WebSocket error: session={ID}, message={e.Message}");
|
||||
base.OnError(e);
|
||||
}
|
||||
|
||||
var targetClient = GetClientPublicKeyByUsernameSync(username);
|
||||
// -------------------------------------------------------------------------
|
||||
// Sync DB helpers
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
if (targetClient is null)
|
||||
{
|
||||
Console.WriteLine($"No public key found for history request user {username}");
|
||||
return;
|
||||
}
|
||||
|
||||
var allMessages = GetChannelMessagesSync();
|
||||
|
||||
var channelMessages = allMessages
|
||||
.Where(m => m.ChannelId == channelId)
|
||||
.OrderBy(m => m.CreatedAt)
|
||||
.ToList();
|
||||
|
||||
Console.WriteLine($"Sending {channelMessages.Count} history messages to {username}");
|
||||
|
||||
foreach (var dbMessage in channelMessages)
|
||||
{
|
||||
string plainText;
|
||||
|
||||
try
|
||||
{
|
||||
plainText = ChannelCryptoService.Decrypt(
|
||||
dbMessage.CipherText,
|
||||
dbMessage.Nonce,
|
||||
dbMessage.Tag,
|
||||
ChannelDbKey
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Failed to decrypt DB history row {dbMessage.Id}: {ex.Message}");
|
||||
continue;
|
||||
}
|
||||
|
||||
var encrypted = E2EeHelper.EncryptForRecipient(plainText, targetClient.PublicKey);
|
||||
|
||||
var outbound = new SocketEncryptedMessage
|
||||
{
|
||||
Type = SignalType.EncryptedChat,
|
||||
SenderUsername = ExtractUsernameFromUserId(dbMessage.SenderUserId),
|
||||
RecipientUsername = username,
|
||||
ChannelId = channelId,
|
||||
CipherText = encrypted.CipherText,
|
||||
Nonce = encrypted.Nonce,
|
||||
Tag = encrypted.Tag,
|
||||
EncryptedKey = encrypted.EncryptedKey
|
||||
};
|
||||
|
||||
Send(JsonSerializer.Serialize(outbound));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a SurrealDB record id object into a table:id string representation.
|
||||
/// </summary>
|
||||
/// <param name="id">The raw record id object.</param>
|
||||
/// <returns>
|
||||
/// A formatted record id string, or an empty string if the input is null.
|
||||
/// </returns>
|
||||
private static string GetRecordId(object? id)
|
||||
{
|
||||
if (id is null)
|
||||
return string.Empty;
|
||||
|
||||
var json = JsonSerializer.Serialize(id);
|
||||
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
|
||||
var root = doc.RootElement;
|
||||
|
||||
var recordId = root.GetProperty("Id").GetString() ?? string.Empty;
|
||||
var table = root.GetProperty("Table").GetString() ?? string.Empty;
|
||||
|
||||
return $"{table}:{recordId}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Synchronously registers or updates a stored client public key using the async key service.
|
||||
/// </summary>
|
||||
/// <param name="username">The client username.</param>
|
||||
/// <param name="publicKey">The client's public key.</param>
|
||||
private void RegisterOrUpdateClientKeySync(string username, string publicKey)
|
||||
{
|
||||
Task.Run(async () => await ClientKeyService!.RegisterOrUpdateKeyAsync(username, publicKey))
|
||||
@@ -495,10 +571,6 @@ public class ChatSocketBehavior : WebSocketBehavior
|
||||
.GetResult();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Synchronously loads all channels from the database.
|
||||
/// </summary>
|
||||
/// <returns>A list of channel records.</returns>
|
||||
private List<Channels> GetChannelsSync()
|
||||
{
|
||||
return Task.Run(async () => await Db!.Select<Channels>("channels"))
|
||||
@@ -507,13 +579,6 @@ public class ChatSocketBehavior : WebSocketBehavior
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Synchronously gets the stored public key record for the specified user.
|
||||
/// </summary>
|
||||
/// <param name="username">The username to look up.</param>
|
||||
/// <returns>
|
||||
/// The matching client public key record, or null if none exists.
|
||||
/// </returns>
|
||||
private ClientPublicKeys? GetClientPublicKeyByUsernameSync(string username)
|
||||
{
|
||||
return Task.Run(async () => await ClientKeyService!.GetByUsernameAsync(username))
|
||||
@@ -521,21 +586,6 @@ public class ChatSocketBehavior : WebSocketBehavior
|
||||
.GetResult();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Synchronously loads all stored client public key records.
|
||||
/// </summary>
|
||||
/// <returns>A list of all client public key records.</returns>
|
||||
private List<ClientPublicKeys> GetAllClientPublicKeysSync()
|
||||
{
|
||||
return Task.Run(async () => await ClientKeyService!.GetAllAsync())
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Synchronously loads all stored channel messages from the database.
|
||||
/// </summary>
|
||||
/// <returns>A list of channel message records.</returns>
|
||||
private List<ChannelMessages> GetChannelMessagesSync()
|
||||
{
|
||||
return Task.Run(async () => await Db!.Select<ChannelMessages>("channel_messages"))
|
||||
@@ -544,11 +594,6 @@ public class ChatSocketBehavior : WebSocketBehavior
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Synchronously creates a new channel message record in the database.
|
||||
/// </summary>
|
||||
/// <param name="message">The message record to create.</param>
|
||||
/// <returns>The created channel message record.</returns>
|
||||
private ChannelMessages CreateChannelMessageSync(ChannelMessages message)
|
||||
{
|
||||
return Task.Run(async () => await Db!.Create("channel_messages", message))
|
||||
@@ -556,10 +601,49 @@ public class ChatSocketBehavior : WebSocketBehavior
|
||||
.GetResult();
|
||||
}
|
||||
|
||||
private List<ServerMembers> GetServerMembersSync()
|
||||
{
|
||||
return Task.Run(async () => await Db!.Select<ServerMembers>("server_members"))
|
||||
.GetAwaiter()
|
||||
.GetResult()
|
||||
.ToList();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Utilities
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// Extracts a display username from a stored user record id value
|
||||
/// (e.g. "users:keeper317" → "keeper317").
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private static string ExtractUsernameFromUserId(string senderUserId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(senderUserId))
|
||||
return "Unknown";
|
||||
|
||||
var parts = senderUserId.Split(':', 2);
|
||||
return parts.Length == 2 ? parts[1] : senderUserId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a SurrealDB record id object into a "table:id" string.
|
||||
/// </summary>
|
||||
private static string GetRecordId(object? id)
|
||||
{
|
||||
if (id is null)
|
||||
return string.Empty;
|
||||
|
||||
var json = JsonSerializer.Serialize(id);
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
var root = doc.RootElement;
|
||||
|
||||
var recordId = root.GetProperty("Id").GetString() ?? string.Empty;
|
||||
var table = root.GetProperty("Table").GetString() ?? string.Empty;
|
||||
|
||||
return $"{table}:{recordId}";
|
||||
}
|
||||
|
||||
private bool EnsureCoreReady()
|
||||
{
|
||||
if (ClientKeyService is null || Db is null)
|
||||
@@ -571,10 +655,6 @@ public class ChatSocketBehavior : WebSocketBehavior
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private bool EnsureCryptoReady()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(ServerPrivateKey) || string.IsNullOrWhiteSpace(ChannelDbKey))
|
||||
@@ -591,50 +671,4 @@ public class ChatSocketBehavior : WebSocketBehavior
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="msg"></param>
|
||||
private void HandleRtcJoinChannel(string msg)
|
||||
{
|
||||
var parts = msg.Split('|', 3);
|
||||
if (parts.Length < 3)
|
||||
{
|
||||
Console.WriteLine("Invalid RTC_JOIN_CHANNEL payload.");
|
||||
return;
|
||||
}
|
||||
|
||||
var username = parts[1];
|
||||
var channelId = parts[2];
|
||||
|
||||
RtcChannelPresenceService.SetUser(ID, username);
|
||||
RtcChannelPresenceService.JoinChannel(ID, channelId);
|
||||
|
||||
Console.WriteLine($"RTC presence joined: session={ID}, user={username}, channel={channelId}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="msg"></param>
|
||||
private void HandleRtcLeaveChannel(string msg)
|
||||
{
|
||||
var parts = msg.Split('|', 3);
|
||||
if (parts.Length < 3)
|
||||
{
|
||||
Console.WriteLine("Invalid RTC_LEAVE_CHANNEL payload.");
|
||||
return;
|
||||
}
|
||||
|
||||
var username = parts[1];
|
||||
var channelId = parts[2];
|
||||
|
||||
if (RtcChannelPresenceService.IsInChannel(ID, channelId))
|
||||
{
|
||||
RtcChannelPresenceService.LeaveChannel(ID);
|
||||
}
|
||||
|
||||
Console.WriteLine($"RTC presence left: session={ID}, user={username}, channel={channelId}");
|
||||
}
|
||||
}
|
||||
56
RelayServer/Services/Chat/ConnectedClientService.cs
Normal file
56
RelayServer/Services/Chat/ConnectedClientService.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace RelayServer.Services.Chat;
|
||||
|
||||
public static class ConnectedClientService
|
||||
{
|
||||
private static readonly ConcurrentDictionary<string, string> SessionToUsername = new();
|
||||
private static readonly ConcurrentDictionary<string, HashSet<string>> UsernameToSessions =
|
||||
new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
public static void Register(string sessionId, string username)
|
||||
{
|
||||
if (SessionToUsername.TryGetValue(sessionId, out var oldUsername) &&
|
||||
!string.Equals(oldUsername, username, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
RemoveSessionFromUsername(sessionId, oldUsername);
|
||||
}
|
||||
|
||||
SessionToUsername[sessionId] = username;
|
||||
|
||||
var sessions = UsernameToSessions.GetOrAdd(username, _ => new HashSet<string>(StringComparer.Ordinal));
|
||||
lock (sessions)
|
||||
sessions.Add(sessionId);
|
||||
}
|
||||
|
||||
public static void Unregister(string sessionId)
|
||||
{
|
||||
if (SessionToUsername.TryRemove(sessionId, out var username))
|
||||
RemoveSessionFromUsername(sessionId, username);
|
||||
}
|
||||
|
||||
public static IReadOnlyCollection<string> GetSessionsForUser(string username)
|
||||
{
|
||||
if (UsernameToSessions.TryGetValue(username, out var sessions))
|
||||
lock (sessions)
|
||||
return sessions.ToList();
|
||||
|
||||
return Array.Empty<string>();
|
||||
}
|
||||
|
||||
public static string? GetUsernameForSession(string sessionId) =>
|
||||
SessionToUsername.TryGetValue(sessionId, out var u) ? u : null;
|
||||
|
||||
private static void RemoveSessionFromUsername(string sessionId, string username)
|
||||
{
|
||||
if (!UsernameToSessions.TryGetValue(username, out var sessions))
|
||||
return;
|
||||
|
||||
lock (sessions)
|
||||
{
|
||||
sessions.Remove(sessionId);
|
||||
if (sessions.Count == 0)
|
||||
UsernameToSessions.TryRemove(username, out _);
|
||||
}
|
||||
}
|
||||
}
|
||||
40
RelayShared/Services/Authentication.cs
Normal file
40
RelayShared/Services/Authentication.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
namespace RelayShared.Services;
|
||||
|
||||
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 AuthServerLicenseVerify
|
||||
{
|
||||
public string License { get; set; }
|
||||
}
|
||||
|
||||
public class AuthServerLicenseGenerate
|
||||
{
|
||||
public string Server { get; set; }
|
||||
public string Length {get; set;} //TODO: Convert to Enum
|
||||
}
|
||||
|
||||
public class DBLicense
|
||||
{
|
||||
public string Token {get; set;}
|
||||
public bool IsClient {get; set;}
|
||||
public DateTime CreatedAt {get; set;}
|
||||
public DateTime ExpiresAt {get; set;}
|
||||
public bool IsExpired {get; set;}
|
||||
}
|
||||
34
RelayShared/Services/WsControlMessage.cs
Normal file
34
RelayShared/Services/WsControlMessage.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
namespace RelayShared.Services;
|
||||
|
||||
public enum WsAction
|
||||
{
|
||||
Authenticate,
|
||||
RegisterKey,
|
||||
GetServerKey,
|
||||
GetChannels,
|
||||
GetHistory,
|
||||
RtcJoin,
|
||||
RtcLeave
|
||||
}
|
||||
|
||||
public enum WsEvent
|
||||
{
|
||||
Authenticated,
|
||||
KeyRegistered,
|
||||
Error
|
||||
}
|
||||
|
||||
public sealed class WsControlMessage
|
||||
{
|
||||
public WsAction Action { get; set; }
|
||||
public string? Username { get; set; }
|
||||
public string? Token { get; set; }
|
||||
public string? ChannelId { get; set; }
|
||||
public string? PublicKey { get; set; }
|
||||
}
|
||||
|
||||
public sealed class WsEventMessage
|
||||
{
|
||||
public WsEvent Event { get; set; }
|
||||
public string? Detail { get; set; }
|
||||
}
|
||||
@@ -66,7 +66,7 @@ Start-Sleep -Seconds 5
|
||||
|
||||
$testScript = New-TabScript -Name "Test" -Content @"
|
||||
Set-Location '$root'
|
||||
Start-Sleep -Seconds 25
|
||||
Start-Sleep -Seconds 5
|
||||
& '$clientExe' --user Test
|
||||
"@
|
||||
|
||||
|
||||
63
start-servers.ps1
Normal file
63
start-servers.ps1
Normal file
@@ -0,0 +1,63 @@
|
||||
$root = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
Set-Location $root
|
||||
|
||||
$dockerExe = (Get-Command docker.exe).Source
|
||||
$dotnetExe = (Get-Command dotnet.exe).Source
|
||||
$ps = (Get-Command powershell.exe).Source
|
||||
|
||||
Write-Host "Building RelayCore..."
|
||||
& $dotnetExe build .\RelayCore\RelayCore.csproj
|
||||
if ($LASTEXITCODE -ne 0) { throw "RelayCore build failed." }
|
||||
|
||||
Write-Host "Building RelayServer..."
|
||||
& $dotnetExe build .\RelayServer\RelayServer.csproj
|
||||
if ($LASTEXITCODE -ne 0) { throw "RelayServer build failed." }
|
||||
|
||||
Write-Host "Building RelayClient (Windows only)..."
|
||||
& $dotnetExe build .\RelayClient\RelayClient.csproj -f net10.0-windows10.0.19041.0
|
||||
if ($LASTEXITCODE -ne 0) { throw "RelayClient build failed." }
|
||||
|
||||
$coreDll = Join-Path $root "RelayCore\bin\Debug\net9.0\RelayCore.dll"
|
||||
$serverDll = Join-Path $root "RelayServer\bin\Debug\net10.0\RelayServer.dll"
|
||||
|
||||
$tempDir = Join-Path $env:TEMP "RelayTabs"
|
||||
New-Item -ItemType Directory -Force -Path $tempDir | Out-Null
|
||||
|
||||
function New-TabScript {
|
||||
param(
|
||||
[string]$Name,
|
||||
[string]$Content
|
||||
)
|
||||
|
||||
$path = Join-Path $tempDir "$Name.ps1"
|
||||
Set-Content -Path $path -Value $Content -Encoding UTF8
|
||||
return $path
|
||||
}
|
||||
|
||||
$dockerScript = New-TabScript -Name "SurrealDB" -Content @"
|
||||
Set-Location '$root'
|
||||
& '$dockerExe' run --rm -p 8000:8000 -v /mydata:/mydata surrealdb/surrealdb:v2.2.1 start --user root --pass secret
|
||||
"@
|
||||
|
||||
$coreScript = New-TabScript -Name "RelayCore" -Content @"
|
||||
Set-Location '$root'
|
||||
Start-Sleep -Seconds 1
|
||||
& '$dotnetExe' '$coreDll'
|
||||
"@
|
||||
|
||||
$serverScript = New-TabScript -Name "RelayServer" -Content @"
|
||||
Set-Location '$root'
|
||||
Start-Sleep -Seconds 1
|
||||
& '$dotnetExe' '$serverDll'
|
||||
"@
|
||||
|
||||
$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`""
|
||||
) -join " ; "
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "Everything started."
|
||||
Write-Host "Close out terminal to end all applications."
|
||||
Start-Process wt.exe -ArgumentList $wtArgs
|
||||
Reference in New Issue
Block a user