|
@@ -3,12 +3,13 @@ using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.IO;
|
|
|
using System.Text;
|
|
|
+using Utils;
|
|
|
|
|
|
namespace DhcpServer
|
|
|
{
|
|
|
- internal static class AppConfigs
|
|
|
+ internal static class DhcpEntryManager
|
|
|
{
|
|
|
- private static readonly string ConfigFileDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configs");
|
|
|
+ private static readonly string EntriesDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DhcpEntries");
|
|
|
|
|
|
private static readonly HashSet<char> InvalidFileNameChars = new HashSet<char>(Path.GetInvalidFileNameChars());
|
|
|
|
|
@@ -17,51 +18,50 @@ namespace DhcpServer
|
|
|
if (input == null) return null;
|
|
|
|
|
|
var sb = new StringBuilder(input);
|
|
|
- for (int i = 0; i < sb.Length; i++)
|
|
|
- {
|
|
|
- if (InvalidFileNameChars.Contains(sb[i])) sb[i] = '_';
|
|
|
- }
|
|
|
+ sb.FilterChars(InvalidFileNameChars);
|
|
|
|
|
|
sb.Insert(0, '-');
|
|
|
return sb.ToString();
|
|
|
}
|
|
|
|
|
|
- private static string GetClientEntryPath(string mac, string userClass = null)
|
|
|
+
|
|
|
+
|
|
|
+ private static string GetEntryPath(string mac, string userClass = null)
|
|
|
{
|
|
|
- return Path.Combine(ConfigFileDir, mac + FilterUserClass(userClass) + ".json");
|
|
|
+ return Path.Combine(EntriesDir, "MAC-" + mac + FilterUserClass(userClass) + ".json");
|
|
|
}
|
|
|
|
|
|
private static string GetDefaultEntryPath(string userClass = null)
|
|
|
{
|
|
|
- return Path.Combine(ConfigFileDir, "Default" + FilterUserClass(userClass) + ".json");
|
|
|
+ return Path.Combine(EntriesDir, "Default" + FilterUserClass(userClass) + ".json");
|
|
|
}
|
|
|
|
|
|
- public static bool CreateDefaultClientEntryIfNoExist()
|
|
|
+ public static bool CreateDefaultEntryIfNoExist()
|
|
|
{
|
|
|
var path = GetDefaultEntryPath();
|
|
|
if (File.Exists(path)) return false;
|
|
|
- if (false == Directory.Exists(ConfigFileDir)) Directory.CreateDirectory(ConfigFileDir);
|
|
|
- File.WriteAllText(path, JsonConvert.SerializeObject(new ClientEntry(), Formatting.Indented));
|
|
|
+ if (false == Directory.Exists(EntriesDir)) Directory.CreateDirectory(EntriesDir);
|
|
|
+ File.WriteAllText(path, JsonConvert.SerializeObject(new DhcpEntry(), Formatting.Indented));
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- public static ClientEntry GetDefaultClientEntry(string userClass = null)
|
|
|
+ public static DhcpEntry GetDefaultEntry(string userClass = null)
|
|
|
{
|
|
|
var path = GetDefaultEntryPath(userClass);
|
|
|
- if (File.Exists(path)) return JsonConvert.DeserializeObject<ClientEntry>(File.ReadAllText(path));
|
|
|
-
|
|
|
- var createNew = new ClientEntry();
|
|
|
- File.WriteAllText(path, JsonConvert.SerializeObject(new ClientEntry(), Formatting.Indented));
|
|
|
+ if (File.Exists(path)) return JsonConvert.DeserializeObject<DhcpEntry>(File.ReadAllText(path));
|
|
|
+
|
|
|
+ var createNew = new DhcpEntry();
|
|
|
+ File.WriteAllText(path, JsonConvert.SerializeObject(new DhcpEntry(), Formatting.Indented));
|
|
|
return createNew;
|
|
|
}
|
|
|
|
|
|
- public static ClientEntry GetClientEntry(string mac, string userClass = null)
|
|
|
+ public static DhcpEntry GetClientEntry(string mac, string userClass = null)
|
|
|
{
|
|
|
- var path = GetClientEntryPath(mac, userClass);
|
|
|
- if (File.Exists(path)) return JsonConvert.DeserializeObject<ClientEntry>(File.ReadAllText(path));
|
|
|
+ var path = GetEntryPath(mac, userClass);
|
|
|
+ if (File.Exists(path)) return JsonConvert.DeserializeObject<DhcpEntry>(File.ReadAllText(path));
|
|
|
|
|
|
- var createNew = new ClientEntry();
|
|
|
- File.WriteAllText(path, JsonConvert.SerializeObject(new ClientEntry(), Formatting.Indented));
|
|
|
+ var createNew = new DhcpEntry();
|
|
|
+ File.WriteAllText(path, JsonConvert.SerializeObject(new DhcpEntry(), Formatting.Indented));
|
|
|
return createNew;
|
|
|
}
|
|
|
}
|