|
@@ -1,39 +1,63 @@
|
|
|
-using System;
|
|
|
+using Newtonsoft.Json;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
using System.IO;
|
|
|
-using Newtonsoft.Json;
|
|
|
+using System.Text;
|
|
|
|
|
|
namespace DhcpServer
|
|
|
{
|
|
|
internal static class AppConfigs
|
|
|
{
|
|
|
- private const string DefaultConfigFileName = "Default.json";
|
|
|
+ private static readonly string ConfigFileDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configs");
|
|
|
|
|
|
- public static readonly string ConfigFileDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configs");
|
|
|
- public static readonly string DefaultConfigFilePath = Path.Combine(ConfigFileDir, DefaultConfigFileName);
|
|
|
+ private static readonly HashSet<char> InvalidFileNameChars = new HashSet<char>(Path.GetInvalidFileNameChars());
|
|
|
|
|
|
- public static string GetClientEntryPath(string mac)
|
|
|
+ private static string FilterUserClass(string input)
|
|
|
{
|
|
|
- return Path.Combine(ConfigFileDir, mac + ".json");
|
|
|
+ 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.Insert(0, '-');
|
|
|
+ return sb.ToString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static string GetClientEntryPath(string mac, string userClass = null)
|
|
|
+ {
|
|
|
+ return Path.Combine(ConfigFileDir, mac + FilterUserClass(userClass) + ".json");
|
|
|
+ }
|
|
|
+
|
|
|
+ private static string GetDefaultEntryPath(string userClass = null)
|
|
|
+ {
|
|
|
+ return Path.Combine(ConfigFileDir, "Default" + FilterUserClass(userClass) + ".json");
|
|
|
}
|
|
|
|
|
|
public static bool CreateDefaultClientEntryIfNoExist()
|
|
|
{
|
|
|
- if (File.Exists(DefaultConfigFilePath)) return false;
|
|
|
+ var path = GetDefaultEntryPath();
|
|
|
+ if (File.Exists(path)) return false;
|
|
|
if (false == Directory.Exists(ConfigFileDir)) Directory.CreateDirectory(ConfigFileDir);
|
|
|
- File.WriteAllText(DefaultConfigFilePath, JsonConvert.SerializeObject(new ClientEntry(), Formatting.Indented));
|
|
|
+ File.WriteAllText(path, JsonConvert.SerializeObject(new ClientEntry(), Formatting.Indented));
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- public static ClientEntry GetDefaultClientEntry()
|
|
|
+ public static ClientEntry GetDefaultClientEntry(string userClass = null)
|
|
|
{
|
|
|
- return File.Exists(DefaultConfigFilePath)
|
|
|
- ? JsonConvert.DeserializeObject<ClientEntry>(File.ReadAllText(DefaultConfigFilePath))
|
|
|
- : 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));
|
|
|
+ return createNew;
|
|
|
}
|
|
|
|
|
|
- public static ClientEntry GetClientEntry(string mac)
|
|
|
+ public static ClientEntry GetClientEntry(string mac, string userClass = null)
|
|
|
{
|
|
|
- var path = GetClientEntryPath(mac);
|
|
|
+ var path = GetClientEntryPath(mac, userClass);
|
|
|
if (File.Exists(path)) return JsonConvert.DeserializeObject<ClientEntry>(File.ReadAllText(path));
|
|
|
|
|
|
var createNew = new ClientEntry();
|