Files
MSCO-Dedicated-Server/LicenseCache.cs
T
MrMahoandClaude Opus 5.5 1a2431db3a License: running server keeps running until the next restart
The configurable LicenseGraceHours setting is gone. If the master is down or
rejects the key, a running server now only warns and keeps running until the
next restart; a (re)start still works up to 72 h after the last confirmation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 20:45:38 +02:00

59 lines
2.5 KiB
C#

using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
namespace MSCO.Server;
/// <summary>
/// Letzte erfolgreiche Lizenzprüfung beim Master (data/license.json). Ist der Master mal nicht erreichbar oder lehnt
/// den Schlüssel kurz ab (z.B. nach einem Umzug ohne keys.json), läuft bzw. startet der Server damit weiter - bis
/// GraceHours ohne Bestätigung vergangen sind (fest, nicht einstellbar). Gespeichert wird nur ein Hash des Schlüssels.
/// </summary>
public static class LicenseCache
{
/// <summary>So lange startet ein Server ohne Bestätigung des Masters noch (ein laufender läuft bis zum Neustart weiter).</summary>
public static readonly TimeSpan Grace = TimeSpan.FromHours(72);
public sealed class Entry
{
public string KeyHash { get; set; }
public string Owner { get; set; }
public string Label { get; set; }
public DateTime LastConfirmedUtc { get; set; }
}
static string PathOf(string dataDir) => Path.Combine(dataDir, "license.json");
static string Hash(string key) =>
Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(key ?? ""))).ToLowerInvariant();
/// <summary>Gemerkte Bestätigung für genau diesen Schlüssel (null = keine).</summary>
public static Entry Load(string dataDir, string key)
{
try
{
string path = PathOf(dataDir);
if (!File.Exists(path)) return null;
var entry = JsonSerializer.Deserialize<Entry>(File.ReadAllText(path));
return entry != null && entry.KeyHash == Hash(key) ? entry : null;
}
catch { return null; }
}
public static void Save(string dataDir, string key, string owner, string label, DateTime confirmedUtc)
{
try
{
var entry = new Entry { KeyHash = Hash(key), Owner = owner, Label = label, LastConfirmedUtc = confirmedUtc };
string path = PathOf(dataDir), tmp = path + ".tmp";
File.WriteAllText(tmp, JsonSerializer.Serialize(entry, new JsonSerializerOptions { WriteIndented = true }));
File.Move(tmp, path, true);
}
catch (Exception e) { GameServer.Log("Could not remember the license check: " + e.Message); }
}
/// <summary>"3 h", "2 d 4 h" ...</summary>
public static string Span(TimeSpan t) =>
t.TotalHours < 1 ? $"{Math.Max(1, (int)t.TotalMinutes)} min" : t.TotalDays < 1 ? $"{(int)t.TotalHours} h" : $"{(int)t.TotalDays} d {t.Hours} h";
}