Skip to content

Commit

Permalink
Merge pull request #68 from GeneralLibrary/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
JusterZhu authored Nov 17, 2024
2 parents a42bdc8 + fff595a commit 8447385
Show file tree
Hide file tree
Showing 252 changed files with 3,746 additions and 8,969 deletions.
Binary file added imgs/bowl.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions src/c#/GeneralUpdate.Bowl/Applications/Windows/export.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
@echo off
setlocal

if "%~1"=="" (
echo Please provide the export path as the first parameter.
exit /b 1
)

set exportDir=%~1

if not exist "%exportDir%" (
mkdir "%exportDir%"
)

set outputFile=%exportDir%\driverInfo.txt

:: 导出驱动信息
driverquery /v /fo table > "%outputFile%"
echo %outputFile% Export successfully.

:: 导出系统信息
set systemInfoFile=%exportDir%\systeminfo.txt
systeminfo > "%systemInfoFile%"
echo %systemInfoFile% Export successfully.

:: 获取当前日期
for /f "tokens=1-4 delims=/- " %%i in ('date /t') do (
set yyyy=%%i
set mm=%%j
set dd=%%k
)

:: 设置日志文件名
set logFile=%exportDir%\systemlog.evtx

:: 导出系统日志
wevtutil epl System "%logFile%" /q:"*[System[TimeCreated[timediff(@SystemTime) <= 86400000]]]"
echo %logFile% Export successfully.

endlocal
59 changes: 42 additions & 17 deletions src/c#/GeneralUpdate.Bowl/Bowl.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,64 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.Json;
using GeneralUpdate.Bowl.Strategys;
using GeneralUpdate.Common.AOT.JsonContext;
using GeneralUpdate.Common.Shared.Object;

namespace GeneralUpdate.Bowl;

public class Bowl
/// <summary>
/// Surveillance Main Program.
/// </summary>
public sealed class Bowl
{
private IStrategy _strategy;
private static IStrategy? _strategy;

public Bowl(MonitorParameter parameter = null)
{
CreateStrategy();
_strategy!.SetParameter(parameter);
}
private Bowl() { }

private void CreateStrategy()
private static void CreateStrategy()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
_strategy = new WindowStrategy();
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
/*else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
_strategy = new LinuxStrategy();
}
}*/

if (_strategy == null)
throw new PlatformNotSupportedException("Unsupported operating system");
}

public static void Launch(MonitorParameter? monitorParameter = null)
{
monitorParameter ??= CreateParameter();
CreateStrategy();
_strategy?.SetParameter(monitorParameter);
_strategy?.Launch();
}

public Bowl SetParameter(MonitorParameter parameter)
private static MonitorParameter CreateParameter()
{
if(parameter.Verify())
throw new ArgumentException("Parameter contains illegal values");
var json = Environment.GetEnvironmentVariable("ProcessInfo", EnvironmentVariableTarget.User);
if(string.IsNullOrWhiteSpace(json))
throw new ArgumentNullException("ProcessInfo environment variable not set !");

var processInfo = JsonSerializer.Deserialize<ProcessInfo>(json, ProcessInfoJsonContext.Default.ProcessInfo);
if(processInfo == null)
throw new ArgumentNullException("ProcessInfo json deserialize fail!");

_strategy.SetParameter(parameter);
return this;
return new MonitorParameter
{
ProcessNameOrId = processInfo.AppName,
DumpFileName = $"{processInfo.LastVersion}_fail.dmp",
FailFileName = $"{processInfo.LastVersion}_fail.json",
TargetPath = processInfo.InstallPath,
FailDirectory = Path.Combine(processInfo.InstallPath, "fail", processInfo.LastVersion),
BackupDirectory = Path.Combine(processInfo.InstallPath, processInfo.LastVersion),
ExtendedField = processInfo.LastVersion
};
}

public void Launch() => _strategy.Launch();
}
11 changes: 11 additions & 0 deletions src/c#/GeneralUpdate.Bowl/GeneralUpdate.Bowl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@
<None Update="Applications\Linux\procdump_3.3.0_amd64.deb">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Applications\Windows\export.bat">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json" Version="9.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\GeneralUpdate.Common\GeneralUpdate.Common.csproj" />
</ItemGroup>

</Project>
87 changes: 18 additions & 69 deletions src/c#/GeneralUpdate.Bowl/Strategys/AbstractStrategy.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using GeneralUpdate.Common.FileBasic;

namespace GeneralUpdate.Bowl.Strategys;

public abstract class AbstractStrategy : IStrategy
{
protected MonitorParameter _parameter;

private readonly IReadOnlyList<string> _sensitiveCharacter = new List<string>
{
"Exit",
"exit"
};
protected List<string> OutputList = new ();

public void SetParameter(MonitorParameter parameter) => _parameter = parameter;

public virtual void Launch()
{
Backup();
Startup(_parameter.ProcessNameOrId, _parameter.InnerArguments);
Startup(_parameter.InnerApp, _parameter.InnerArguments);
}

private void Startup(string appName, string arguments)
{
if (Directory.Exists(_parameter.FailDirectory))
{
Directory.Delete(_parameter.FailDirectory, true);
}
Directory.CreateDirectory(_parameter.FailDirectory);

var startInfo = new ProcessStartInfo
{
FileName = appName,
Arguments = arguments,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
Expand All @@ -35,71 +40,15 @@ private void Startup(string appName, string arguments)
process.OutputDataReceived += OutputHandler;
process.ErrorDataReceived += OutputHandler;
process.Start();
process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit(1000 * 10);
}

private void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
var data = outLine.Data;
if (!string.IsNullOrEmpty(data))
{
foreach (var sensitive in _sensitiveCharacter)
{
if (data.Contains(sensitive)){
Restore();
Process.Start(_parameter.ProcessNameOrId, _parameter.Arguments);
break;
}
}
}
OutputList.Add(data);
}

private void Backup()
{
var backupPath = _parameter.Target;
var sourcePath = _parameter.Source;

if (Directory.Exists(backupPath))
{
Directory.Delete(backupPath, true);
}

Directory.CreateDirectory(backupPath);

foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
{
Directory.CreateDirectory(dirPath.Replace(sourcePath, backupPath));
}

foreach (string newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories))
{
File.Copy(newPath, newPath.Replace(sourcePath, backupPath), true);
}
}

private void Restore()
{
var restorePath = _parameter.Target;
var backupPath = _parameter.Source;

if (Directory.Exists(restorePath))
{
Directory.Delete(restorePath, true);
}

Directory.CreateDirectory(restorePath);

foreach (string dirPath in Directory.GetDirectories(backupPath, "*", SearchOption.AllDirectories))
{
Directory.CreateDirectory(dirPath.Replace(backupPath, restorePath));
}

foreach (string newPath in Directory.GetFiles(backupPath, "*.*", SearchOption.AllDirectories))
{
File.Copy(newPath, newPath.Replace(backupPath, restorePath), true);
}
}

public void SetParameter(MonitorParameter parameter) => _parameter = parameter;
}
10 changes: 10 additions & 0 deletions src/c#/GeneralUpdate.Bowl/Strategys/Crash.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;

namespace GeneralUpdate.Bowl.Strategys;

public class Crash
{
public MonitorParameter Parameter { get; set; }

public List<string> ProcdumpOutPutLines { get; set; }
}
28 changes: 14 additions & 14 deletions src/c#/GeneralUpdate.Bowl/Strategys/LinuxStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ namespace GeneralUpdate.Bowl.Strategys;

public class LinuxStrategy : AbstractStrategy
{
/*procdump-3.3.0-0.cm2.x86_64.rpm:
适合系统:此RPM包可能适用于基于CentOS或RHEL的某些派生版本,具体来说是CM2版本。CM2通常指的是ClearOS 7.x或类似的社区维护版本。
procdump-3.3.0-0.el8.x86_64.rpm:
适合系统:此RPM包适用于Red Hat Enterprise Linux 8 (RHEL 8)、CentOS 8及其他基于RHEL 8的发行版。
procdump_3.3.0_amd64.deb:
适合系统:此DEB包适用于Debian及其衍生发行版,如Ubuntu,适用于64位系统(amd64架构)。*/
/*procdump-3.3.0-0.cm2.x86_64.rpm:
Compatible Systems: This RPM package may be suitable for certain CentOS or RHEL-based derivatives, specifically the CM2 version. CM2 typically refers to ClearOS 7.x or similar community-maintained versions.
procdump-3.3.0-0.el8.x86_64.rpm:
Compatible Systems: This RPM package is suitable for Red Hat Enterprise Linux 8 (RHEL 8), CentOS 8, and other RHEL 8-based distributions.
procdump_3.3.0_amd64.deb:
Compatible Systems: This DEB package is suitable for Debian and its derivatives, such as Ubuntu, for 64-bit systems (amd64 architecture).*/

private IReadOnlyList<string> procdump_amd64 = new List<string> { "Ubuntu", "Debian" };
private IReadOnlyList<string> _rocdumpAmd64 = new List<string> { "Ubuntu", "Debian" };
private IReadOnlyList<string> procdump_el8_x86_64 = new List<string> { "Red Hat", "CentOS", "Fedora" };
private IReadOnlyList<string> procdump_cm2_x86_64 = new List<string> { "ClearOS" };

Expand Down Expand Up @@ -64,9 +66,9 @@ private void Install()

private string GetPacketName()
{
string packageFileName = string.Empty;
LinuxSystem system = GetSystem();
if (procdump_amd64.Contains(system.Name))
var packageFileName = string.Empty;
var system = GetSystem();
if (_rocdumpAmd64.Contains(system.Name))
{
packageFileName = $"procdump_3.3.0_amd64.deb";
}
Expand Down Expand Up @@ -105,9 +107,7 @@ private LinuxSystem GetSystem()

return new LinuxSystem(distro, version);
}
else
{
throw new FileNotFoundException("Cannot determine the Linux distribution. The /etc/os-release file does not exist.");
}

throw new FileNotFoundException("Cannot determine the Linux distribution. The /etc/os-release file does not exist.");
}
}
36 changes: 18 additions & 18 deletions src/c#/GeneralUpdate.Bowl/Strategys/MonitorParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@

public class MonitorParameter
{
public string Target { get; set; }

public string Source { get; set; }
public MonitorParameter() { }

public string ProcessNameOrId { get; set; }
public string TargetPath { get; set; }

public string DumpPath { get; set; }
public string FailDirectory { get; set; }

public string BackupDirectory { get; set; }

public string ProcessNameOrId { get; set; }

public string DumpFileName { get; set; }

public string Arguments { get; set; }
public string FailFileName { get; set; }

internal string InnerArguments => $"-e -ma {ProcessNameOrId} {DumpPath}";
internal string InnerArguments { get; set; }

internal string InnerAppName { get; set; }

public bool Verify()
{
return string.IsNullOrEmpty(Target) &&
string.IsNullOrEmpty(Source) &&
string.IsNullOrEmpty(ProcessNameOrId) &&
string.IsNullOrEmpty(DumpPath) &&
string.IsNullOrEmpty(DumpFileName) &&
string.IsNullOrEmpty(Arguments);
}
internal string InnerApp { get; set; }

/// <summary>
/// Upgrade: upgrade mode. This mode is primarily used in conjunction with GeneralUpdate for internal use. Please do not modify it arbitrarily when the default mode is activated.
/// Normal: Normal mode,This mode can be used independently to monitor a single program. If the program crashes, it will export the crash information.
/// </summary>
public string WorkModel { get; set; } = "Upgrade";

public string ExtendedField { get; set; }
}
Loading

0 comments on commit 8447385

Please sign in to comment.