From d7355c5eb943f49d75038ff8c9654c0f5688e22e Mon Sep 17 00:00:00 2001 From: Eauldane Date: Sat, 23 Aug 2025 19:56:21 +0100 Subject: [PATCH] Fixes --- .../Services/RemoteConfigurationService.cs | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/MareSynchronos/Services/RemoteConfigurationService.cs b/MareSynchronos/Services/RemoteConfigurationService.cs index d61fbd6..5a69e8d 100644 --- a/MareSynchronos/Services/RemoteConfigurationService.cs +++ b/MareSynchronos/Services/RemoteConfigurationService.cs @@ -12,6 +12,14 @@ namespace MareSynchronos.Services; public sealed class RemoteConfigurationService { + // private readonly static Dictionary ConfigPublicKeys = new(StringComparer.Ordinal) + // { + // { "", "" }, + // }; + + private readonly static string[] ConfigSources = [ + "https://snowcloak-sync.com/config.json", + ]; private readonly ILogger _logger; private readonly RemoteConfigCacheService _configService; @@ -23,7 +31,28 @@ public sealed class RemoteConfigurationService _configService = configService; _initTask = Task.Run(DownloadConfig); } - + + public async Task GetConfigAsync(string sectionName) + { + await _initTask.ConfigureAwait(false); + if (!_configService.Current.Configuration.TryGetPropertyValue(sectionName, out var section)) + section = null; + return (section as JsonObject) ?? new(); + } + + public async Task GetConfigAsync(string sectionName) + { + try + { + var json = await GetConfigAsync(sectionName).ConfigureAwait(false); + return JsonSerializer.Deserialize(json); + } + catch (JsonException ex) + { + _logger.LogWarning(ex, "Invalid JSON in remote config: {sectionName}", sectionName); + return default; + } + } private async Task DownloadConfig() { @@ -32,12 +61,21 @@ public sealed class RemoteConfigurationService } + private static bool VerifySignature(string message, ulong ts, string signature, string pubKey) + { + byte[] msg = [.. BitConverter.GetBytes(ts), .. Encoding.UTF8.GetBytes(message)]; + byte[] sig = Convert.FromBase64String(signature); + byte[] pub = Convert.FromBase64String(pubKey); + return Ed25519.Verify(sig, msg, pub); + } + private void LoadConfig() { ulong ts = 1755859494; var configString = "{\"mainServer\":{\"api_url\":\"wss://hub.snowcloak-sync.com/\",\"hub_url\":\"wss://hub.snowcloak-sync.com/mare\"},\"repoChange\":{\"current_repo\":\"https://hub.snowcloak-sync.com/repo.json\",\"valid_repos\":[\"https://hub.snowcloak-sync.com/repo.json\"]},\"noSnap\":{\"listOfPlugins\":[\"Snapper\",\"Snappy\",\"Meddle.Plugin\"]}}"; + _configService.Current.Configuration = JsonNode.Parse(configString)!.AsObject(); _configService.Current.Timestamp = ts; _configService.Save();