feat: add E2EE DMs, group chats, local serversettings.json, and security/perf fixes
This commit is contained in:
parent
b60b5a0774
commit
3b52de20d1
33 changed files with 5107 additions and 366 deletions
|
|
@ -14,4 +14,6 @@ public class ServerOptions
|
|||
public string ServerName { get; set; } = "GsDChat Community Server";
|
||||
public string Motd { get; set; } = "Welcome to GsDChat! Enjoy private, modular messaging.";
|
||||
public bool AllowMemberChannelCreation { get; set; } = true;
|
||||
public bool AllowDmE2ee { get; set; } = true;
|
||||
public int MaxGroupChatUsers { get; set; } = 10;
|
||||
}
|
||||
|
|
|
|||
112
src/GsDChat.Server/Configuration/ServerSettingsManager.cs
Normal file
112
src/GsDChat.Server/Configuration/ServerSettingsManager.cs
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace GsDChat.Server.Configuration;
|
||||
|
||||
public class ServerSettingsManager
|
||||
{
|
||||
private static readonly JsonSerializerOptions s_readOptions = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
ReadCommentHandling = JsonCommentHandling.Skip,
|
||||
AllowTrailingCommas = true
|
||||
};
|
||||
|
||||
private static readonly JsonSerializerOptions s_writeOptions = new()
|
||||
{
|
||||
WriteIndented = true
|
||||
};
|
||||
|
||||
public string FilePath { get; }
|
||||
|
||||
public ServerSettingsManager(string? customFilePath = null)
|
||||
{
|
||||
FilePath = ResolveSettingsFilePath(customFilePath);
|
||||
}
|
||||
|
||||
public static string ResolveSettingsFilePath(string? customFilePath = null, string[]? args = null)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(customFilePath))
|
||||
{
|
||||
return Path.GetFullPath(customFilePath);
|
||||
}
|
||||
|
||||
if (args != null)
|
||||
{
|
||||
for (int i = 0; i < args.Length - 1; i++)
|
||||
{
|
||||
if (args[i] == "--settings" && !string.IsNullOrWhiteSpace(args[i + 1]))
|
||||
{
|
||||
return Path.GetFullPath(args[i + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
string? envPath = Environment.GetEnvironmentVariable("GSDCHAT_SETTINGS_FILE");
|
||||
if (!string.IsNullOrWhiteSpace(envPath))
|
||||
{
|
||||
return Path.GetFullPath(envPath);
|
||||
}
|
||||
|
||||
string cwdSettings = Path.Combine(Directory.GetCurrentDirectory(), "serversettings.json");
|
||||
if (File.Exists(cwdSettings))
|
||||
{
|
||||
return cwdSettings;
|
||||
}
|
||||
|
||||
return Path.Combine(AppContext.BaseDirectory, "serversettings.json");
|
||||
}
|
||||
|
||||
public ServerOptions LoadOrCreate()
|
||||
{
|
||||
var options = new ServerOptions();
|
||||
|
||||
if (File.Exists(FilePath))
|
||||
{
|
||||
try
|
||||
{
|
||||
string json = File.ReadAllText(FilePath);
|
||||
if (!string.IsNullOrWhiteSpace(json))
|
||||
{
|
||||
var loaded = JsonSerializer.Deserialize<ServerOptions>(json, s_readOptions);
|
||||
if (loaded != null)
|
||||
{
|
||||
options = loaded;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[Warning] Failed to read {FilePath}: {ex.Message}. Using defaults.");
|
||||
}
|
||||
}
|
||||
|
||||
if (options.MaxGroupChatUsers < 2)
|
||||
{
|
||||
options.MaxGroupChatUsers = 2;
|
||||
}
|
||||
|
||||
Save(options);
|
||||
return options;
|
||||
}
|
||||
|
||||
public void Save(ServerOptions options)
|
||||
{
|
||||
try
|
||||
{
|
||||
string? dir = Path.GetDirectoryName(FilePath);
|
||||
if (!string.IsNullOrEmpty(dir))
|
||||
{
|
||||
Directory.CreateDirectory(dir);
|
||||
}
|
||||
|
||||
string json = JsonSerializer.Serialize(options, s_writeOptions);
|
||||
File.WriteAllText(FilePath, json);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[Warning] Failed to persist settings to {FilePath}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue