using System; using System.Collections.Generic; using System.IO; using System.Text; using Utils; namespace HttpServer.App { internal static class IpxeScriptManager { private static readonly string ScriptDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "IpxeScripts"); private static readonly HashSet InvalidFileNameChars = new HashSet(Path.GetInvalidFileNameChars()); private const string DefaultScript = "#!ipxe\r\n" + "echo Error: Boot script is no configured, please edit script file."; public static string GetInitScript(string url) => "#!ipxe\r\n" + "chain " + url + "boot/ipxe/script?mac=${mac:hexhyp}"; public static string GetScript(string mac) { var path = Path.Combine(ScriptDir, "MAC-" + FilterMac(mac) + ".txt"); if (File.Exists(path)) return File.ReadAllText(path); if (false == Directory.Exists(ScriptDir)) Directory.CreateDirectory(ScriptDir); File.WriteAllText(path, DefaultScript); return File.ReadAllText(path); } private static string FilterMac(string mac) { var sb = new StringBuilder(mac); sb.FilterChars(InvalidFileNameChars); return sb.ToString(); } } }