12345678910111213141516171819202122232425262728293031323334353637383940 |
- 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<char> InvalidFileNameChars = new HashSet<char>(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();
- }
- }
- }
|