Skip to content

Commit

Permalink
给Desktop增加生成排故调试快照功能
Browse files Browse the repository at this point in the history
  • Loading branch information
CHKZL committed Sep 17, 2024
1 parent aebd514 commit 4ff2372
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 5 deletions.
10 changes: 10 additions & 0 deletions Core/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,16 @@ public string _RecFileDirectory
}
}

private static string DebugFileDirectory = "./Debug/";
/// <summary>
/// 用于生成排查文件的文件夹(字符串)
/// 默认值:./Debug/
/// </summary>
public string _DebugFileDirectory
{
get => DebugFileDirectory;
}


private static string DefaultLiverFolderName = "{ROOMID}_{NAME}";
/// <summary>
Expand Down
5 changes: 4 additions & 1 deletion Core/Init.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ private static void InitDirectoryAndFile()
{
Directory.CreateDirectory(Config.Core_RunConfig._RecFileDirectory);
}

if (!Directory.Exists(Config.Core_RunConfig._DebugFileDirectory))
{
Directory.CreateDirectory(Config.Core_RunConfig._DebugFileDirectory);
}
DeleteUnexpectedFiles();


Expand Down
3 changes: 3 additions & 0 deletions Core/LogModule/LogDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ internal class LogDB
internal static string ErrorFilePath = string.Empty;
internal static StreamWriter streamWriter = default;




public static class Config
{
#region 初始化数据库
Expand Down
112 changes: 112 additions & 0 deletions Core/Tools/DebuggingRecord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Core.Tools
{

public class DebuggingRecord
{
private static string publicKey = "MIIBCgKCAQEAvVfJfvg0QbH0dJtocE+Cm2RWyT1/hyTm2lebHljVghEzfI0BjiXspLyZBZojqRyeLqK+7A1KRnRJjSbE+pRdv7jkiEY6LjmjPCQBgFsJ0TgW2N4lxm0j4uTV1YxsnYNNslYW88Mv0kwTMfEtoas19wEeKFsjLJPS8qwpjnIX5buI8zDqyk4by+7xMdBFT12c9Jrp4AAizvrRIUVMbB95/YBZ16UjvMdCXhgDZEmrL6Dva8JbKaVY4NZ5S4u9NgrbtRBsIJAhSYLae/nIcoRsi/B755Jw0UxT1AePgaRwjo0uBYCJUlAoB9aykwfMML2sueYjk/dZxUPD27suhs1ZQQIDAQAB";
internal static string GetPublicKey { get { return publicKey; } }

internal static void EncryptFile(string inputFilePath, string outputFilePath)
{
byte[] fileBytes = File.ReadAllBytes(inputFilePath);
byte[] encryptedFileBytes;
byte[] encryptedKey;
byte[] iv;

using (var aes = Aes.Create())
{
aes.GenerateKey();
aes.GenerateIV();
iv = aes.IV;

using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV))
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
cs.Write(fileBytes, 0, fileBytes.Length);
cs.FlushFinalBlock();
encryptedFileBytes = ms.ToArray();
}
}

using (var rsa = RSA.Create())
{
rsa.ImportRSAPublicKey(Convert.FromBase64String(GetPublicKey), out _);
encryptedKey = rsa.Encrypt(aes.Key, RSAEncryptionPadding.OaepSHA256);
}
}

using (var ms = new MemoryStream())
{
ms.Write(iv, 0, iv.Length);
ms.Write(encryptedKey, 0, encryptedKey.Length);
ms.Write(encryptedFileBytes, 0, encryptedFileBytes.Length);
File.WriteAllBytes(outputFilePath, ms.ToArray());
}
}

/// <summary>
/// 生成Debug快照文件
/// </summary>
public static string GenerateReportSnapshot()
{
string GIDPath = string.Empty;
string ZipFilePath = string.Empty;
try
{
string GID = Guid.NewGuid().ToString();
GIDPath = $"{Config.Core_RunConfig._TemporaryFileDirectory}{GID}/";
if(!Directory.Exists(GIDPath))
{
Directory.CreateDirectory(GIDPath);
}
FileOperations.CopyAllFiles(Config.Core_RunConfig._ConfigDirectory, GIDPath);
Thread.Sleep(100);
if (File.Exists(LogModule.LogDB.ErrorFilePath))
{
FileInfo fileInfo = new(LogModule.LogDB.ErrorFilePath);
File.Copy(LogModule.LogDB.ErrorFilePath, $"{GIDPath}{fileInfo.Name}");
}
if (File.Exists(LogModule.LogDB.dbPath))
{
FileInfo fileInfo = new(LogModule.LogDB.dbPath);
File.Copy(LogModule.LogDB.dbPath, $"{GIDPath}{fileInfo.Name}");
}


ZipFilePath = Path.Combine($"{Config.Core_RunConfig._DebugFileDirectory}", $"{GID}.zip");
ZipFile.CreateFromDirectory(GIDPath, ZipFilePath);
FileInfo ZipFileInfo = new(ZipFilePath);
string EncryofFilaName = $"{Config.Core_RunConfig._DebugFileDirectory}{ZipFileInfo.Name}_Encryof.{ZipFileInfo.Extension}";
EncryptFile(ZipFilePath, EncryofFilaName);
return EncryofFilaName;
}
catch (Exception e)
{
return "";
}
finally
{
if (Directory.Exists(GIDPath))
{
Tools.FileOperations.DeletePathFile(GIDPath);
//Directory.Delete(GIDPath, true);
}
if (File.Exists(ZipFilePath))
{
Tools.FileOperations.Delete(ZipFilePath);
}
}
}

}
}
58 changes: 56 additions & 2 deletions Core/Tools/FileOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,31 @@ await Task.Run(() =>
{
Log.Warn(nameof(DeletePathFile), $"要删除的路径不存在");
}
dir.Delete(true);
string[] Files = GetAllFiles(targetDir);
foreach (var item in Files)
{
Delete(item);
}
});
}

/// <summary>
/// 获取目标文件夹的所有子文件结构树
/// </summary>
/// <param name="targetDirectory"></param>
/// <returns></returns>
public static string[] GetAllFiles(string targetDirectory)
{
if (Directory.Exists(targetDirectory))
{
return Directory.GetFiles(targetDirectory, "*", SearchOption.AllDirectories);
}
else
{
return new string[0]; // 如果目标文件夹不存在,返回一个空数组
}
}

/// <summary>
/// 在指定路径中创建所有目录
/// </summary>
Expand Down Expand Up @@ -64,6 +86,12 @@ public static string CreateAll(string path)

private static List<(string File, string Message)> _DelFileList = new();
private static bool DelState = false;

/// <summary>
/// 删除文件队列
/// </summary>
/// <param name="Path"></param>
/// <param name="Message"></param>
public static void Delete(string Path, string Message = "")
{
if (!DelState)
Expand Down Expand Up @@ -107,7 +135,7 @@ public static void Delete(string Path, string Message = "")
}





/// <summary>
Expand Down Expand Up @@ -152,6 +180,32 @@ private static DirectoryNode GetDirectoryStructure(DirectoryInfo directoryInfo,
}
}

/// <summary>
/// 复制源文件夹中的所有文件到目标文件夹
/// </summary>
/// <param name="sourceDirectory">源文件夹</param>
/// <param name="destinationDirectory">目标文件夹</param>
public static void CopyAllFiles(string sourceDirectory, string destinationDirectory)
{
// 确保目标文件夹存在
Directory.CreateDirectory(destinationDirectory);

// 获取源文件夹中的所有文件
string[] files = Directory.GetFiles(sourceDirectory);

foreach (string file in files)
{
// 获取文件名
string fileName = Path.GetFileName(file);

// 生成目标文件路径
string destFile = Path.Combine(destinationDirectory, fileName);

// 复制文件
File.Copy(file, destFile, true);
}
}

public class DirectoryNode
{
public string Name { get; set; }
Expand Down
6 changes: 4 additions & 2 deletions Desktop/Views/Pages/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<ui:Button Grid.Column="0" Margin="0,0,0,10" Content="保存全部" FontSize="20" HorizontalAlignment="Left" Icon="{ui:SymbolIcon Save20}" Click="Config_Save_Button_Click"/>
<ui:TextBlock Name="SaveTips" Grid.Column="1" Margin="10,8,0,0" FontSize="16" FontTypography="Body" HorizontalAlignment="Left" Text="请注意:未保存退出当前页面将丢失已修改内容" />
<ui:Button Grid.Column="2" Margin="0,0,0,10" Content="检查更新" FontSize="20" HorizontalAlignment="Right" Icon="{ui:SymbolIcon DocumentHeaderArrowDown20}" Click="CheckForUpdates_Click"/>
<ui:Button Grid.Column="2" Margin="30,0,0,10" Content="生成排故调试快照" FontSize="20" HorizontalAlignment="Right" Icon="{ui:SymbolIcon BugArrowCounterclockwise20}" Click="Generate_Debug_Click" ToolTipService.InitialShowDelay="100" ToolTipService.Placement="MousePoint" ToolTipService.ToolTip="点击后将会将现在的配置和本次启动产生的日志文件打包加密生成一个zip包"/>
<ui:Button Grid.Column="3" Margin="30,0,0,10" Content="检查更新" FontSize="20" HorizontalAlignment="Right" Icon="{ui:SymbolIcon DocumentHeaderArrowDown20}" Click="CheckForUpdates_Click"/>
</Grid>

<Grid>
Expand Down
19 changes: 19 additions & 0 deletions Desktop/Views/Pages/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using Core;
using Desktop.Views.Windows;
using SharpCompress.Common;
using System.Diagnostics;
using System.IO;
using System.Windows;
Expand Down Expand Up @@ -461,4 +462,22 @@ private async void ReLogin_Button_ClickAsync(object sender, RoutedEventArgs e)
});
}
}

private void Generate_Debug_Click(object sender, RoutedEventArgs e)
{
string DebugFilePath = Core.Tools.DebuggingRecord.GenerateReportSnapshot();
if(string.IsNullOrEmpty(DebugFilePath))
{
MainWindow.SnackbarService.Show("生成排故快照", "生成排故障快照文件失败...", ControlAppearance.Caution, new SymbolIcon(SymbolRegular.ArrowSync20), TimeSpan.FromSeconds(5));
}
else
{
if (File.Exists(DebugFilePath))
{
FileInfo fileInfo = new(DebugFilePath);
Process.Start("explorer.exe", $"/select,\"{fileInfo.FullName}\"");
}

}
}
}

0 comments on commit 4ff2372

Please sign in to comment.