feat: publish dbussmsforward refactor
This commit is contained in:
@@ -0,0 +1,208 @@
|
||||
using DbusSmsForward.SettingModel;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace DbusSmsForward.Helper;
|
||||
|
||||
public static class ConfigHelper
|
||||
{
|
||||
private static readonly JsonSerializerOptions JsonOptions = new(SourceGenerationContext.Default.Options)
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
WriteIndented = true
|
||||
};
|
||||
|
||||
public static string DefaultSettingsFileName => Path.Combine(AppContext.BaseDirectory, "appsettings.json");
|
||||
|
||||
public static string ResolveConfigPath(IConfiguration? configuration = null)
|
||||
{
|
||||
string[] candidates =
|
||||
[
|
||||
configuration?["DBUSSMSFORWARD_CONFIG_PATH"] ?? string.Empty,
|
||||
Environment.GetEnvironmentVariable("DBUSSMSFORWARD_CONFIG_PATH") ?? string.Empty,
|
||||
"/etc/dbussmsforward/appsettings.json",
|
||||
DefaultSettingsFileName
|
||||
];
|
||||
|
||||
foreach (string candidate in candidates)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(candidate) && File.Exists(candidate))
|
||||
{
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
return candidates.Last(static c => !string.IsNullOrWhiteSpace(c));
|
||||
}
|
||||
|
||||
public static DbusSmsForwardOptions LoadOptions(IConfiguration? configuration = null)
|
||||
{
|
||||
DbusSmsForwardOptions options = new();
|
||||
string configPath = ResolveConfigPath(configuration);
|
||||
if (File.Exists(configPath))
|
||||
{
|
||||
string json = File.ReadAllText(configPath);
|
||||
options = JsonSerializer.Deserialize(json, SourceGenerationContext.Default.DbusSmsForwardOptions) ?? new DbusSmsForwardOptions();
|
||||
}
|
||||
|
||||
if (configuration is not null)
|
||||
{
|
||||
configuration.GetSection("DbusSmsForward").Bind(options);
|
||||
configuration.GetSection("Daemon").Bind(options.Daemon);
|
||||
configuration.GetSection("Modem").Bind(options.Modem);
|
||||
configuration.GetSection("Forwarding").Bind(options.Forwarding);
|
||||
configuration.GetSection("Notifications").Bind(options.Notifications);
|
||||
configuration.GetSection("LegacyCompatibility").Bind(options.LegacyCompatibility);
|
||||
}
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
public static void SaveOptions(DbusSmsForwardOptions options, string? path = null)
|
||||
{
|
||||
string target = path ?? ResolveConfigPath();
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(target) ?? AppContext.BaseDirectory);
|
||||
string updatedJson = JsonSerializer.Serialize(options, SourceGenerationContext.Default.DbusSmsForwardOptions);
|
||||
File.WriteAllText(target, updatedJson);
|
||||
}
|
||||
|
||||
public static void SaveLegacySettings(appsettingsModel result, string? path = null)
|
||||
{
|
||||
string target = path ?? DefaultSettingsFileName;
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(target) ?? AppContext.BaseDirectory);
|
||||
string updatedJson = JsonSerializer.Serialize(result, SourceGenerationContext.Default.appsettingsModel);
|
||||
File.WriteAllText(target, updatedJson);
|
||||
}
|
||||
|
||||
public static void GetSettings(ref appsettingsModel result)
|
||||
{
|
||||
string settingsFileName = ResolveConfigPath();
|
||||
if (!File.Exists(settingsFileName))
|
||||
{
|
||||
result ??= new appsettingsModel();
|
||||
return;
|
||||
}
|
||||
|
||||
string config = File.ReadAllText(settingsFileName);
|
||||
|
||||
try
|
||||
{
|
||||
if (LooksLikeNewConfig(config))
|
||||
{
|
||||
DbusSmsForwardOptions options = JsonSerializer.Deserialize(config, SourceGenerationContext.Default.DbusSmsForwardOptions) ?? new DbusSmsForwardOptions();
|
||||
result = ConvertToLegacy(options);
|
||||
return;
|
||||
}
|
||||
|
||||
result = JsonSerializer.Deserialize(config, SourceGenerationContext.Default.appsettingsModel) ?? new appsettingsModel();
|
||||
}
|
||||
catch
|
||||
{
|
||||
result ??= new appsettingsModel();
|
||||
}
|
||||
}
|
||||
|
||||
public static void UpdateSettings(ref appsettingsModel result)
|
||||
{
|
||||
SaveLegacySettings(result);
|
||||
}
|
||||
|
||||
public static bool JudgeIsForwardIgnore(uint storage)
|
||||
{
|
||||
appsettingsModel result = new();
|
||||
GetSettings(ref result);
|
||||
string forwardStorageType = result.appSettings.ForwardIgnoreStorageType?.Trim().ToLowerInvariant() ?? string.Empty;
|
||||
|
||||
return (forwardStorageType, storage) switch
|
||||
{
|
||||
("unknown", 0) => true,
|
||||
("sm", 1) => true,
|
||||
("me", 2) => true,
|
||||
("mt", 3) => true,
|
||||
("sr", 4) => true,
|
||||
("bm", 5) => true,
|
||||
("ta", 6) => true,
|
||||
_ => false
|
||||
};
|
||||
}
|
||||
|
||||
public static string GetDeviceHostName()
|
||||
{
|
||||
try
|
||||
{
|
||||
return Dns.GetHostName();
|
||||
}
|
||||
catch
|
||||
{
|
||||
try
|
||||
{
|
||||
return Environment.MachineName;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static string ResolveDeviceName(DbusSmsForwardOptions options)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(options.Forwarding.DeviceName))
|
||||
{
|
||||
return options.Forwarding.DeviceName;
|
||||
}
|
||||
|
||||
return GetDeviceHostName();
|
||||
}
|
||||
|
||||
public static appsettingsModel ConvertToLegacy(DbusSmsForwardOptions options)
|
||||
{
|
||||
return new appsettingsModel
|
||||
{
|
||||
appSettings = new LegacyAppSettings
|
||||
{
|
||||
EmailConfig = options.Notifications.Email,
|
||||
PushPlusConfig = options.Notifications.PushPlus,
|
||||
DingTalkConfig = options.Notifications.DingTalk,
|
||||
BarkConfig = options.Notifications.Bark,
|
||||
WeComApplicationConfig = options.Notifications.WeComApplication,
|
||||
TGBotConfig = options.Notifications.TelegramBot,
|
||||
ShellConfig = options.Notifications.Shell,
|
||||
SmsCodeKey = options.Forwarding.SmsCodeKey,
|
||||
ForwardIgnoreStorageType = options.Forwarding.IgnoreStorageType,
|
||||
DeviceName = options.Forwarding.DeviceName
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static DbusSmsForwardOptions ConvertFromLegacy(appsettingsModel legacy)
|
||||
{
|
||||
return new DbusSmsForwardOptions
|
||||
{
|
||||
Forwarding = new ForwardingConfig
|
||||
{
|
||||
DeviceName = legacy.appSettings.DeviceName,
|
||||
SmsCodeKey = legacy.appSettings.SmsCodeKey,
|
||||
IgnoreStorageType = legacy.appSettings.ForwardIgnoreStorageType
|
||||
},
|
||||
Notifications = new NotificationChannels
|
||||
{
|
||||
Email = legacy.appSettings.EmailConfig,
|
||||
PushPlus = legacy.appSettings.PushPlusConfig,
|
||||
DingTalk = legacy.appSettings.DingTalkConfig,
|
||||
Bark = legacy.appSettings.BarkConfig,
|
||||
WeComApplication = legacy.appSettings.WeComApplicationConfig,
|
||||
TelegramBot = legacy.appSettings.TGBotConfig,
|
||||
Shell = legacy.appSettings.ShellConfig
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static bool LooksLikeNewConfig(string json)
|
||||
{
|
||||
using JsonDocument document = JsonDocument.Parse(json);
|
||||
JsonElement root = document.RootElement;
|
||||
return root.TryGetProperty("Daemon", out _) || root.TryGetProperty("Notifications", out _) || root.TryGetProperty("Forwarding", out _);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user