using System.Net; using System.Reflection; using System.Text; namespace NginxMan; internal static class WebPortal { private const string ResourcePrefix = "NginxMan.WebPortal/"; private const string TemplateFileName = "index.template.html"; private const string OutputFileName = "index.html"; private const string ServiceCardsToken = "{{NGINXMAN_SERVICE_CARDS}}"; private const string TemplateVersionMarker = ""; private static readonly UTF8Encoding Utf8WithoutBom = new(false); public static void ExtractTo(string dataDirectory) { var assembly = typeof(WebPortal).Assembly; var resources = assembly.GetManifestResourceNames() .Where(name => name.StartsWith(ResourcePrefix, StringComparison.Ordinal)) .ToArray(); var targetDirectory = Path.Combine(dataDirectory, "www"); var targetRoot = Path.GetFullPath(targetDirectory) + Path.DirectorySeparatorChar; foreach (var resourceName in resources) { var relativePath = resourceName[ResourcePrefix.Length..] .Replace('/', Path.DirectorySeparatorChar) .Replace('\\', Path.DirectorySeparatorChar); if (relativePath.Equals("index.html", StringComparison.OrdinalIgnoreCase)) { relativePath = TemplateFileName; } var targetPath = Path.GetFullPath(Path.Combine(targetDirectory, relativePath)); if (!targetPath.StartsWith(targetRoot, StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException("Invalid embedded web resource path."); } // Preserve editable templates after the current template format has been // installed. Older bundled templates without the version marker are // upgraded once so legacy branding and layout do not remain in data/www. var isCurrentTemplate = relativePath.Equals(TemplateFileName, StringComparison.OrdinalIgnoreCase) && File.Exists(targetPath) && File.ReadAllText(targetPath, Encoding.UTF8).Contains(TemplateVersionMarker, StringComparison.Ordinal); if (File.Exists(targetPath) && (!relativePath.Equals(TemplateFileName, StringComparison.OrdinalIgnoreCase) || isCurrentTemplate)) { continue; } Directory.CreateDirectory(Path.GetDirectoryName(targetPath)!); using var input = assembly.GetManifestResourceStream(resourceName) ?? throw new InvalidOperationException($"Cannot read embedded resource: {resourceName}"); using var output = File.Create(targetPath); input.CopyTo(output); } } public static string RenderTo(string dataDirectory, IReadOnlyList routes) { var targetDirectory = Path.Combine(dataDirectory, "www"); var templatePath = Path.Combine(targetDirectory, TemplateFileName); if (!File.Exists(templatePath)) { ExtractTo(dataDirectory); } var template = File.ReadAllText(templatePath, Encoding.UTF8); if (!template.Contains(ServiceCardsToken, StringComparison.Ordinal)) { throw new InvalidOperationException( $"网页模板缺少入口占位符 {ServiceCardsToken}: {templatePath}"); } var content = template.Replace( ServiceCardsToken, BuildServiceCards(routes), StringComparison.Ordinal); var outputPath = Path.Combine(targetDirectory, OutputFileName); File.WriteAllText(outputPath, content, Utf8WithoutBom); return outputPath; } private static string BuildServiceCards(IReadOnlyList routes) { if (routes.Count == 0) { return "
暂时没有可用的网站入口。
"; } var cards = new StringBuilder(); for (var index = 0; index < routes.Count; index++) { var route = routes[index]; var domain = route.Domain.Trim().TrimEnd('.'); var websiteName = string.IsNullOrWhiteSpace(route.WebsiteName) ? domain : route.WebsiteName.Trim(); var description = string.IsNullOrWhiteSpace(route.Description) ? $"访问 {domain} 服务。" : route.Description.Trim(); var portSuffix = route.ListenPort == 80 ? string.Empty : $":{route.ListenPort}"; var href = $"http://{domain}{portSuffix}/"; var accentClass = $"accent-{index % 4}"; var delay = Math.Min(0.36 + index * 0.08, 1.2).ToString("0.00", System.Globalization.CultureInfo.InvariantCulture); cards.AppendLine($" "); cards.AppendLine("
"); cards.AppendLine($" {index + 1:D2} / {Encode(domain)}"); cards.AppendLine(" "); cards.AppendLine("
"); cards.AppendLine($"

{Encode(websiteName)}

"); cards.AppendLine($"

{Encode(description)}

"); cards.AppendLine($" 进入 {Encode(websiteName)}"); cards.Append("
"); if (index < routes.Count - 1) { cards.AppendLine(); cards.AppendLine(); } } return cards.ToString(); } private static string Encode(string value) => WebUtility.HtmlEncode(value); }