using System.Security.Cryptography;
using System.Text.Json;
namespace XFEWarframe.Core;
public sealed class ServerOptions
{
public string[] ListenUrls { get; set; } = ["http://127.0.0.1:8444/"];
public string PublicUrl { get; set; } = "http://127.0.0.1:8444";
public string ServerName { get; set; } = "XFEWarframe 星际服务器";
public string? GameListenUrl { get; set; } = "https://127.0.0.1:8443/";
public string? GamePublicUrl { get; set; } = "https://127.0.0.1:8443";
public string AdminToken { get; set; } = Convert.ToHexStringLower(RandomNumberGenerator.GetBytes(32));
public bool AutoCreateAccount { get; set; } = true;
public bool AllowPlayerRegistration { get; set; } = true;
public bool AllowPlayerCurrencyGrant { get; set; } = true;
public bool AllowPlayerItemGrant { get; set; }
public string FallbackBuildLabel { get; set; } = "2026.05.13.13.07";
public string? TlsPfxPath { get; set; }
public string? TlsPasswordEnvironmentVariable { get; set; }
public string[] IrcAddresses { get; set; } = [];
public IrcServerOptions Irc { get; set; } = new();
public string[] NrsAddresses { get; set; } = [];
public HubServerOptions[] HubServers { get; set; } = [];
public bool NoHubDiscrimination { get; set; }
public int Dtls { get; set; }
public string[] GoogleClientIds { get; set; } = [];
public string? GameAssetRoot { get; set; }
public bool SyncOfficialEvents { get; set; } = true;
public int OfficialEventsIntervalSeconds { get; set; } = 60;
public static ServerOptions Load(string root)
{
Directory.CreateDirectory(root);
var file = Path.Combine(root, "server.json");
var options = File.Exists(file) ? JsonSerializer.Deserialize<ServerOptions>(File.ReadAllText(file))
?? throw new InvalidDataException("server.json is empty") : new ServerOptions();
options.Validate();
if (!File.Exists(file)) File.WriteAllText(file, JsonSerializer.Serialize(options, new JsonSerializerOptions { WriteIndented = true }));
options.GameAssetRoot = Path.GetFullPath(options.GameAssetRoot ?? "assets", root);
return options;
}
public void Validate()
{
if (OfficialEventsIntervalSeconds is < 30 or > 3600) throw new InvalidDataException("OfficialEventsIntervalSeconds must be between 30 and 3600");
if (Irc is null || IrcAddresses is null || IrcAddresses.Any(string.IsNullOrWhiteSpace)) throw new InvalidDataException("Invalid IRC configuration");
Irc.Validate();
_ = Services.ServerIdentity.ValidateName(ServerName);
foreach (var secure in new[] { GameListenUrl, GamePublicUrl }.Where(x => x is not null))
if (!Uri.TryCreate(secure, UriKind.Absolute, out var target) || target.Scheme != "https" || target.AbsolutePath != "/" || target.UserInfo.Length != 0 || target.Query.Length != 0 || target.Fragment.Length != 0)
throw new InvalidDataException("GameListenUrl / GamePublicUrl must be HTTPS origins");
if ((GameListenUrl is null) != (GamePublicUrl is null)) throw new InvalidDataException("Configure both GameListenUrl and GamePublicUrl, or set both to null");
if (AdminToken.Length < 32) throw new InvalidDataException("AdminToken must contain at least 32 characters");
if (ListenUrls.Length == 0) throw new InvalidDataException("At least one ListenUrls entry is required");
foreach (var url in ListenUrls)
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri) || uri.Scheme is not ("http" or "https") || uri.AbsolutePath != "/")
throw new InvalidDataException($"Invalid listening URL: {url}");
if (!Uri.TryCreate(PublicUrl, UriKind.Absolute, out var publicUri) || publicUri.Scheme is not ("http" or "https") || publicUri.AbsolutePath != "/")
throw new InvalidDataException("PublicUrl must be an absolute HTTP(S) origin");
}
public string[] GetIrcAddresses()
{
var host = new Uri(GamePublicUrl ?? PublicUrl).Host;
if (IrcAddresses.Length > 0) return IrcAddresses.Select(x => x.Replace("%THIS_MACHINE%", host, StringComparison.Ordinal)).ToArray();
if (!Irc.Enabled) return [];
var ports = Irc.TlsPorts.Length > 0 ? Irc.TlsPorts : Irc.Ports;
var standard = Enumerable.Range(Irc.TlsPorts.Length > 0 ? 6695 : 6665, 5);
return [ports.SequenceEqual(standard) ? host : $"{host}:{ports[0]}"];
}
}
public sealed class IrcServerOptions
{
public bool Enabled { get; set; } = true;
public string ListenAddress { get; set; } = "127.0.0.1";
public int[] Ports { get; set; } = [6665, 6666, 6667, 6668, 6669];
public int[] TlsPorts { get; set; } = [6695, 6696, 6697, 6698, 6699];
// Fallback for native builds whose challenge protocol is not yet recorded.
// Known builds always require their real proof; fallback is TLS + loopback only.
public bool AllowLegacyLoopbackClients { get; set; } = true;
public int MaxConnections { get; set; } = 256;
public int RegistrationTimeoutSeconds { get; set; } = 30;
public int PingIntervalSeconds { get; set; } = 60;
public int PingTimeoutSeconds { get; set; } = 120;
public void Validate()
{
if (!System.Net.IPAddress.TryParse(ListenAddress, out _)) throw new InvalidDataException("Irc.ListenAddress must be an IP address");
if (Ports is null || TlsPorts is null || Ports.Concat(TlsPorts).Any(p => p is < 1 or > 65535)
|| Ports.Concat(TlsPorts).Distinct().Count() != Ports.Length + TlsPorts.Length)
throw new InvalidDataException("Irc ports must be unique TCP ports between 1 and 65535");
if (Enabled && Ports.Length + TlsPorts.Length == 0) throw new InvalidDataException("At least one IRC port is required");
if (MaxConnections is < 1 or > 4096 || RegistrationTimeoutSeconds is < 1 or > 300
|| PingIntervalSeconds is < 1 or > 600 || PingTimeoutSeconds < PingIntervalSeconds || PingTimeoutSeconds > 1200)
throw new InvalidDataException("Invalid IRC connection limits or timeouts");
}
}
public sealed class HubServerOptions
{
public string Address { get; set; } = "%THIS_MACHINE%:6952";
public string[] Regions { get; set; } = [];
public bool DtlsUnsupported { get; set; }
}
using System.Security.Cryptography;
using System.Text.Json;
namespace XFEWarframe.Core;
public sealed class ServerOptions
{
public string[] ListenUrls { get; set; } = ["http://127.0.0.1:8444/"];
public string PublicUrl { get; set; } = "http://127.0.0.1:8444";
public string ServerName { get; set; } = "XFEWarframe 星际服务器";
public string? GameListenUrl { get; set; } = "https://127.0.0.1:8443/";
public string? GamePublicUrl { get; set; } = "https://127.0.0.1:8443";
public string AdminToken { get; set; } = Convert.ToHexStringLower(RandomNumberGenerator.GetBytes(32));
public bool AutoCreateAccount { get; set; } = true;
public bool AllowPlayerRegistration { get; set; } = true;
public bool AllowPlayerCurrencyGrant { get; set; } = true;
public bool AllowPlayerItemGrant { get; set; }
public string FallbackBuildLabel { get; set; } = "2026.05.13.13.07";
public string? TlsPfxPath { get; set; }
public string? TlsPasswordEnvironmentVariable { get; set; }
public string[] IrcAddresses { get; set; } = [];
public IrcServerOptions Irc { get; set; } = new();
public string[] NrsAddresses { get; set; } = [];
public HubServerOptions[] HubServers { get; set; } = [];
public bool NoHubDiscrimination { get; set; }
public int Dtls { get; set; }
public string[] GoogleClientIds { get; set; } = [];
public string? GameAssetRoot { get; set; }
public bool SyncOfficialEvents { get; set; } = true;
public int OfficialEventsIntervalSeconds { get; set; } = 60;
public static ServerOptions Load(string root)
{
Directory.CreateDirectory(root);
var file = Path.Combine(root, "server.json");
var options = File.Exists(file) ? JsonSerializer.Deserialize<ServerOptions>(File.ReadAllText(file))
?? throw new InvalidDataException("server.json is empty") : new ServerOptions();
options.Validate();
if (!File.Exists(file)) File.WriteAllText(file, JsonSerializer.Serialize(options, new JsonSerializerOptions { WriteIndented = true }));
options.GameAssetRoot = Path.GetFullPath(options.GameAssetRoot ?? "assets", root);
return options;
}
public void Validate()
{
if (OfficialEventsIntervalSeconds is < 30 or > 3600) throw new InvalidDataException("OfficialEventsIntervalSeconds must be between 30 and 3600");
if (Irc is null || IrcAddresses is null || IrcAddresses.Any(string.IsNullOrWhiteSpace)) throw new InvalidDataException("Invalid IRC configuration");
Irc.Validate();
_ = Services.ServerIdentity.ValidateName(ServerName);
foreach (var secure in new[] { GameListenUrl, GamePublicUrl }.Where(x => x is not null))
if (!Uri.TryCreate(secure, UriKind.Absolute, out var target) || target.Scheme != "https" || target.AbsolutePath != "/" || target.UserInfo.Length != 0 || target.Query.Length != 0 || target.Fragment.Length != 0)
throw new InvalidDataException("GameListenUrl / GamePublicUrl must be HTTPS origins");
if ((GameListenUrl is null) != (GamePublicUrl is null)) throw new InvalidDataException("Configure both GameListenUrl and GamePublicUrl, or set both to null");
if (AdminToken.Length < 32) throw new InvalidDataException("AdminToken must contain at least 32 characters");
if (ListenUrls.Length == 0) throw new InvalidDataException("At least one ListenUrls entry is required");
foreach (var url in ListenUrls)
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri) || uri.Scheme is not ("http" or "https") || uri.AbsolutePath != "/")
throw new InvalidDataException($"Invalid listening URL: {url}");
if (!Uri.TryCreate(PublicUrl, UriKind.Absolute, out var publicUri) || publicUri.Scheme is not ("http" or "https") || publicUri.AbsolutePath != "/")
throw new InvalidDataException("PublicUrl must be an absolute HTTP(S) origin");
}
public string[] GetIrcAddresses()
{
var host = new Uri(GamePublicUrl ?? PublicUrl).Host;
if (IrcAddresses.Length > 0) return IrcAddresses.Select(x => x.Replace("%THIS_MACHINE%", host, StringComparison.Ordinal)).ToArray();
if (!Irc.Enabled) return [];
var ports = Irc.TlsPorts.Length > 0 ? Irc.TlsPorts : Irc.Ports;
var standard = Enumerable.Range(Irc.TlsPorts.Length > 0 ? 6695 : 6665, 5);
return [ports.SequenceEqual(standard) ? host : $"{host}:{ports[0]}"];
}
}
public sealed class IrcServerOptions
{
public bool Enabled { get; set; } = true;
public string ListenAddress { get; set; } = "127.0.0.1";
public int[] Ports { get; set; } = [6665, 6666, 6667, 6668, 6669];
public int[] TlsPorts { get; set; } = [6695, 6696, 6697, 6698, 6699];
// Fallback for native builds whose challenge protocol is not yet recorded.
// Known builds always require their real proof; fallback is TLS + loopback only.
public bool AllowLegacyLoopbackClients { get; set; } = true;
public int MaxConnections { get; set; } = 256;
public int RegistrationTimeoutSeconds { get; set; } = 30;
public int PingIntervalSeconds { get; set; } = 60;
public int PingTimeoutSeconds { get; set; } = 120;
public void Validate()
{
if (!System.Net.IPAddress.TryParse(ListenAddress, out _)) throw new InvalidDataException("Irc.ListenAddress must be an IP address");
if (Ports is null || TlsPorts is null || Ports.Concat(TlsPorts).Any(p => p is < 1 or > 65535)
|| Ports.Concat(TlsPorts).Distinct().Count() != Ports.Length + TlsPorts.Length)
throw new InvalidDataException("Irc ports must be unique TCP ports between 1 and 65535");
if (Enabled && Ports.Length + TlsPorts.Length == 0) throw new InvalidDataException("At least one IRC port is required");
if (MaxConnections is < 1 or > 4096 || RegistrationTimeoutSeconds is < 1 or > 300
|| PingIntervalSeconds is < 1 or > 600 || PingTimeoutSeconds < PingIntervalSeconds || PingTimeoutSeconds > 1200)
throw new InvalidDataException("Invalid IRC connection limits or timeouts");
}
}
public sealed class HubServerOptions
{
public string Address { get; set; } = "%THIS_MACHINE%:6952";
public string[] Regions { get; set; } = [];
public bool DtlsUnsupported { get; set; }
}