Alt Servers.

Started work on code for a server to list alternate servers to the client.
This commit is contained in:
JacobTech 2023-10-01 16:14:42 -04:00
parent e7fbda0cfb
commit 073deb3b00
7 changed files with 62 additions and 26 deletions

View File

@ -14,6 +14,14 @@ 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<string, string?>[] Headers)
{
using HttpClient web = new();
web.Timeout = TimeSpan.FromSeconds(10);
if (Headers is not null && Headers.Length > 0) foreach (KeyValuePair<string, string?> 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)
{
if (!Tables.Keys.TryReadRow(out KeyRow row, Keys.ID.CreateParameter(Encryption))) throw new Exception("No a key");

View File

@ -0,0 +1,15 @@
using ServerDatabase;
using ServerDatabase.SourceGenerator;
namespace LuskiServer.Classes.TableDef;
public static class AltServers
{
public static TableColumn<string> Address { get; } = new("address", true);
public static TableColumn<bool> Secure { get; }= new("secure");
public static TableColumn<byte[]> Key { get; } = new("key");
}
[TableRow(typeof(AltServers))]
public partial class AltServerRow
{}

View File

@ -6,6 +6,7 @@ namespace LuskiServer.Classes;
public static class Tables
{
public static Table<UserRow> Users { get; } = new("users", null!);
public static Table<AltServerRow> AltServers { get; } = new("alt_servers", null!);
public static Table<ServerRow> Server { get; } = new("server", null!);
public static Table<RoleRow> Roles { get; } = new("roles", null!);
public static Table<LogRow> Logs { get; } = new("logs", null!);

View File

@ -22,16 +22,27 @@ 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!;
}
/// <summary>
/// Returns the icon for the server.
/// </summary>

View File

@ -2,6 +2,9 @@ namespace LuskiServer.Enums;
public enum ServerPermissions : long
{
/// <summary>
/// Internal permission for quick permission lookup
/// </summary>
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,

View File

@ -23,7 +23,7 @@
<PackageReference Include="JacobTechEncryption" Version="1.0.2" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.3" />
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
<PackageReference Include="ServerDatabase" Version="2.8.8" />
<PackageReference Include="ServerDatabase" Version="2.8.9" />
<PackageReference Include="ServerDatabase.SourceGenerator" Version="1.0.2-alpha09" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />

View File

@ -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<string, List<Message>> 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<SwaggerDefaultValues>();
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<ApiVersionDescription> 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();