using System.Text.Json; namespace XFEWarframe.Client; public sealed record ServerDescription(string Service, string Status, int ApiVersion, string ServerName, string GameUrl); public static class ServerConnection { public static async Task ReadAsync(string origin, CancellationToken cancellation = default) { origin = ClientProtocol.NormalizeServer(origin); using var http = new HttpClient(new HttpClientHandler { AllowAutoRedirect = false }) { Timeout = TimeSpan.FromSeconds(8) }; using var response = await http.GetAsync(origin + "/health", cancellation).ConfigureAwait(false); response.EnsureSuccessStatusCode(); var bytes = await response.Content.ReadAsByteArrayAsync(cancellation).ConfigureAwait(false); var identity = JsonSerializer.Deserialize(bytes, ClientJson.Default.ServerDescription) ?? throw new InvalidDataException("服务器没有返回状态。"); if (identity.Service != "XFEWarframe" || identity.ApiVersion < 3) throw new InvalidDataException("服务器版本过旧,尚未包含游戏登录修复。请关闭旧服务器并启动同一发行包中的新版 Server。"); if (identity.Status != "ok") throw new InvalidOperationException("服务器正在维护,请稍后再试。"); var game = new Uri(ClientProtocol.NormalizeServer(identity.GameUrl)); if (game.Scheme != "https") throw new InvalidDataException("游戏需要 HTTPS,请在服务端启用 GameListenUrl 和 GamePublicUrl。"); if (string.IsNullOrWhiteSpace(identity.ServerName) || identity.ServerName.Length > 64 || identity.ServerName.Any(char.IsControl)) throw new InvalidDataException("服务器名称无效。"); return identity; } }