From 073deb3b002dc61ae42eff453fc2b61748c954c5 Mon Sep 17 00:00:00 2001 From: JacobTech Date: Sun, 1 Oct 2023 16:14:42 -0400 Subject: [PATCH] Alt Servers. Started work on code for a server to list alternate servers to the client. --- LuskiServer/Classes/Luski.cs | 8 ++++ LuskiServer/Classes/TableDef/AltServers.cs | 15 ++++++++ LuskiServer/Classes/Tables.cs | 1 + .../Controllers/v1/SocketServerController.cs | 15 +++++++- LuskiServer/Enums/ServerPermissions.cs | 10 ++++- LuskiServer/LuskiServer.csproj | 2 +- LuskiServer/Program.cs | 37 ++++++++----------- 7 files changed, 62 insertions(+), 26 deletions(-) create mode 100644 LuskiServer/Classes/TableDef/AltServers.cs diff --git a/LuskiServer/Classes/Luski.cs b/LuskiServer/Classes/Luski.cs index 03dc816..58dc2ce 100644 --- a/LuskiServer/Classes/Luski.cs +++ b/LuskiServer/Classes/Luski.cs @@ -13,6 +13,14 @@ namespace LuskiServer.Classes; public static class Luski { public static Database Database = null!; + + public static HttpResponseMessage GetFromServer(string Domain, bool Secure, string ApiVersion, string Path, CancellationToken CancellationToken, params KeyValuePair[] Headers) + { + using HttpClient web = new(); + web.Timeout = TimeSpan.FromSeconds(10); + if (Headers is not null && Headers.Length > 0) foreach (KeyValuePair header in Headers) web.DefaultRequestHeaders.Add(header.Key, header.Value); + return web.GetAsync($"{(Secure ? "https" : "http" )}://{Domain}/{ApiVersion}/{Path}", cancellationToken: CancellationToken).Result; + } public static byte[] ToDB(this string s, EncoderType Encoder, long Encryption) { diff --git a/LuskiServer/Classes/TableDef/AltServers.cs b/LuskiServer/Classes/TableDef/AltServers.cs new file mode 100644 index 0000000..0ad8d94 --- /dev/null +++ b/LuskiServer/Classes/TableDef/AltServers.cs @@ -0,0 +1,15 @@ +using ServerDatabase; +using ServerDatabase.SourceGenerator; + +namespace LuskiServer.Classes.TableDef; + +public static class AltServers +{ + public static TableColumn Address { get; } = new("address", true); + public static TableColumn Secure { get; }= new("secure"); + public static TableColumn Key { get; } = new("key"); +} + +[TableRow(typeof(AltServers))] +public partial class AltServerRow +{} \ No newline at end of file diff --git a/LuskiServer/Classes/Tables.cs b/LuskiServer/Classes/Tables.cs index 8ef5b26..5945feb 100644 --- a/LuskiServer/Classes/Tables.cs +++ b/LuskiServer/Classes/Tables.cs @@ -6,6 +6,7 @@ namespace LuskiServer.Classes; public static class Tables { public static Table Users { get; } = new("users", null!); + public static Table AltServers { get; } = new("alt_servers", null!); public static Table Server { get; } = new("server", null!); public static Table Roles { get; } = new("roles", null!); public static Table Logs { get; } = new("logs", null!); diff --git a/LuskiServer/Controllers/v1/SocketServerController.cs b/LuskiServer/Controllers/v1/SocketServerController.cs index a8759e9..dbfcdcd 100644 --- a/LuskiServer/Controllers/v1/SocketServerController.cs +++ b/LuskiServer/Controllers/v1/SocketServerController.cs @@ -22,15 +22,26 @@ public class SocketServerController : ControllerBase [Route(Luski.Info.Routes.Default.Base)] public IActionResult Get() { - var sr = Tables.Server.ReadRow(Server.ID.CreateParameter(0)); + ServerRow sr = Tables.Server.ReadRow(Server.ID.CreateParameter(0)); return this.ResponseToResult(new ServerInfoJson() { - wssv4 = (Luski.Config.IPv4_Overode_WSS is null ? $"{(Luski.Config.IPv4SecureWSS ? "wss" : "ws" )}://{Luski.Config.IPv4WSS}:{Luski.Config.IPv4PortWSS}{Luski.Config.IPv4_URL_WSS}v1" : Luski.Config.IPv4_Overode_WSS + "v1"), + wssv4 = (Luski.Config.IPv4_Overode_WSS is null ? $"{(Luski.Config.IPv4SecureWSS ? "wss" : "ws" )}://{Luski.Config.IPv4WSS}:{Luski.Config.IPv4PortWSS}{Luski.Config.IPv4_URL_WSS}/v1" : Luski.Config.IPv4_Overode_WSS + "v1"), name = sr.Name, description = sr.Description, owner = sr.Owner }); } + + [HttpGet] + [Produces(MediaTypeNames.Application.Json)] + [Route(Luski.Info.Routes.Default.Base)] + public IActionResult ServerConnect([FromHeader(Name = "key")]string Key) + { + byte[] data = Convert.FromBase64String(Key); + if (!Tables.AltServers.TryReadRow(out var ser, AltServers.Key.CreateParameter(data))) + return this.ResponseCodeToResult(ErrorCode.Forbidden); + return null!; + } /// /// Returns the icon for the server. diff --git a/LuskiServer/Enums/ServerPermissions.cs b/LuskiServer/Enums/ServerPermissions.cs index 3992894..1388d30 100644 --- a/LuskiServer/Enums/ServerPermissions.cs +++ b/LuskiServer/Enums/ServerPermissions.cs @@ -2,6 +2,9 @@ namespace LuskiServer.Enums; public enum ServerPermissions : long { + /// + /// Internal permission for quick permission lookup + /// ViewThis, ViewChannels, MoveChannels, @@ -19,14 +22,17 @@ public enum ServerPermissions : long ManageRoles, ViewLogs, ManageServer, + AddServers, + RemoveServers, Invite, Nickname, - ManageNacknames, + ManageNicknames, Kick, Ban, SendMessages, SendFiles, - ChannelAndServerPings, + ChannelPings, + ServerPings, PingSomeone, ManageMessages, ReadMessageHistory, diff --git a/LuskiServer/LuskiServer.csproj b/LuskiServer/LuskiServer.csproj index fd8fe7e..82aaf99 100644 --- a/LuskiServer/LuskiServer.csproj +++ b/LuskiServer/LuskiServer.csproj @@ -23,7 +23,7 @@ - + diff --git a/LuskiServer/Program.cs b/LuskiServer/Program.cs index 1a73f78..4b73ab9 100644 --- a/LuskiServer/Program.cs +++ b/LuskiServer/Program.cs @@ -1,6 +1,7 @@ using System.Reflection; using System.Runtime.InteropServices.JavaScript; using System.Text; +using Asp.Versioning; using Asp.Versioning.ApiExplorer; using JacobTechEncryption; using JacobTechEncryption.Enums; @@ -27,19 +28,6 @@ Luski.Database = new Database(Luski.Config.Address, Luski.Config.Password, Luski.Config.CustomeName); -DirectoryInfo di = - new("/home/jacob/Downloads/matrix - Luski Project Discussion - Chat Export - 2023-08-08T01-13-19.480Z"); -foreach (DirectoryInfo d in di.GetDirectories()) -{ - foreach (FileInfo file in d.GetFiles()) - { - // Tables.Files.Insert( - // Files.ID.CreateParameter(0), - // Files.EncoderType.CreateParameter(EncoderType.UTF8), - // Files.Name); - } -} - Dictionary> fff = new(); @@ -210,7 +198,7 @@ if (!Tables.Roles.TryRead(Roles.ID, out _, Roles.ID.CreateParameter(0))) Roles.ID.CreateParameter(0), Roles.Name.CreateParameter("server"), Roles.DisplayName.CreateParameter("Members"), - Roles.Color.CreateParameter("5,5,5,5"), + Roles.Color.CreateParameter("ÿÿÿÿ"), Roles.Description.CreateParameter("The default role for the server. Everybody will have this role."), Roles.ServerPermissions.CreateParameter(new[] { @@ -219,7 +207,8 @@ if (!Tables.Roles.TryRead(Roles.ID, out _, Roles.ID.CreateParameter(0))) ServerPermissions.Nickname, ServerPermissions.SendMessages, ServerPermissions.SendFiles, - ServerPermissions.ChannelAndServerPings, + ServerPermissions.ChannelPings, + ServerPermissions.ServerPings, ServerPermissions.PingSomeone, ServerPermissions.ReadMessageHistory, ServerPermissions.UseServerCommands, @@ -269,21 +258,27 @@ if (!Tables.Categories.TryRead(Categories.ID, out _, Categories.ID.CreateParamet Tables.Sessions.DeleteRows(); + +foreach (AltServerRow Server in Tables.AltServers.ReadRows()) +{ + //TODO add code to open a live WSS connection with all other servers on the network +} + WSS.Init(Luski.Config.IPv4WSS, Luski.Config.IPv4PortWSS, Luski.Config.IPv4_URL_WSS!, Luski.Config.IPv4SecureWSS, Luski.Config.IPv6WSS, Luski.Config.IPv6_URL_WSS, Luski.Config.IPv6PortWSS, Luski.Config.IPv6SecureWSS); -var builder = WebApplication.CreateBuilder( args ); +WebApplicationBuilder builder = WebApplication.CreateBuilder( args ); // Add services to the container. builder.Services.AddControllers(); builder.Services.AddProblemDetails(); - builder.Services.AddApiVersioning( options => { // reporting api versions will return the headers // "api-supported-versions" and "api-deprecated-versions" options.ReportApiVersions = true; + /* Sunset example options.Policies.Sunset( 1 ) //.Effective( DateTimeOffset.Now.Subtract( new TimeSpan(9999999) ) ) @@ -341,8 +336,8 @@ builder.Services.AddSwaggerGen( // add a custom operation filter which sets default values options.OperationFilter(); - var fileName = typeof(Program).Assembly.GetName().Name + ".xml"; - var filePath = Path.Combine(AppContext.BaseDirectory, fileName); + string fileName = typeof(Program).Assembly.GetName().Name + ".xml"; + string filePath = Path.Combine(AppContext.BaseDirectory, fileName); // integrate xml comments options.IncludeXmlComments(filePath, true); @@ -359,7 +354,7 @@ app.UseSwaggerUI( IReadOnlyList descriptions = app.DescribeApiVersions(); // build a swagger endpoint for each discovered API version - foreach ( var description in descriptions ) + foreach ( ApiVersionDescription description in descriptions ) { string url = $"/swagger/{description.GroupName}/swagger.json"; string name = description.GroupName.ToUpperInvariant(); @@ -370,7 +365,7 @@ app.UseSwaggerUI( } ); app.MapGet("/www/css/SwaggerDark.css", async (CancellationToken t) => { - var css = await File.ReadAllBytesAsync("www/css/SwaggerDark.css", t); + byte[] css = await File.ReadAllBytesAsync("www/css/SwaggerDark.css", t); return Results.File(css, "text/css"); }).ExcludeFromDescription(); app.UseHttpsRedirection();