IpxeScriptManager.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using Utils;
  6. namespace HttpServer.App
  7. {
  8. internal static class IpxeScriptManager
  9. {
  10. private static readonly string ScriptDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "IpxeScripts");
  11. private static readonly HashSet<char> InvalidFileNameChars = new HashSet<char>(Path.GetInvalidFileNameChars());
  12. private const string DefaultScript =
  13. "#!ipxe\r\n" +
  14. "echo Error: Boot script is no configured, please edit script file.";
  15. public static string GetInitScript(string url) =>
  16. "#!ipxe\r\n" +
  17. "chain " + url + "boot/ipxe/script?mac=${mac:hexhyp}";
  18. public static string GetScript(string mac)
  19. {
  20. var path = Path.Combine(ScriptDir, "MAC-" + FilterMac(mac) + ".txt");
  21. if (File.Exists(path)) return File.ReadAllText(path);
  22. if (false == Directory.Exists(ScriptDir)) Directory.CreateDirectory(ScriptDir);
  23. File.WriteAllText(path, DefaultScript);
  24. return File.ReadAllText(path);
  25. }
  26. private static string FilterMac(string mac)
  27. {
  28. var sb = new StringBuilder(mac);
  29. sb.FilterChars(InvalidFileNameChars);
  30. return sb.ToString();
  31. }
  32. }
  33. }