using Chaos.NaCl; using MareSynchronos.MareConfiguration; using Microsoft.Extensions.Logging; using System.Net; using System.Net.Http.Headers; using System.Reflection; using System.Text; using System.Text.Json; using System.Text.Json.Nodes; 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; private readonly Task _initTask; public RemoteConfigurationService(ILogger logger, RemoteConfigCacheService configService) { _logger = logger; _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() { // Removed Lop's remote config code. Function exists purely to keep things clean. LoadConfig(); } 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://raw.githubusercontent.com/Eauldane/ElfShelf/refs/heads/main/repo.json\",\"valid_repos\":[\"https://raw.githubusercontent.com/Eauldane/ElfShelf/refs/heads/main/repo.json\"]},\"noSnap\":{\"listOfPlugins\":[\"Snapper\",\"Snappy\",\"Meddle.Plugin\"]}}"; _configService.Current.Configuration = JsonNode.Parse(configString)!.AsObject(); _configService.Current.Timestamp = ts; _configService.Save(); } }