CLI tool for running Playbooks
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
5.3 KiB

1 year ago
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Windows;
  6. using TrustedUninstaller.Shared;
  7. namespace TrustedUninstaller.CLI
  8. {
  9. public class CLI
  10. {
  11. private static async System.Threading.Tasks.Task<int> Main(string[] args)
  12. {
  13. //Needed after defender removal's reboot, the "current directory" will be set to System32
  14. //After the auto start up.
  15. Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
  16. DualOut.Init();
  17. if (!WinUtil.IsAdministrator())
  18. {
  19. System.Console.Error.WriteLine("This program must be launched as an Administrator!");
  20. return -1;
  21. }
  22. #if !DEBUG
  23. /*
  24. if (!WinUtil.IsGenuineWindows())
  25. {
  26. System.Console.Error.WriteLine("This program only works on genuine Windows copies!");
  27. return -1;
  28. }
  29. */
  30. #endif
  31. if (args.Length < 1 || !Directory.Exists(args[0]))
  32. {
  33. Console.WriteLine("No Playbook selected: Use the GUI to select a playbook to run.");
  34. return -1;
  35. }
  36. AmeliorationUtil.Playbook = await AmeliorationUtil.DeserializePlaybook(args[0]);
  37. if (!Directory.Exists($"{AmeliorationUtil.Playbook.Path}\\Configuration"))
  38. {
  39. Console.WriteLine("Creating Configuration folder...");
  40. Directory.CreateDirectory($"{AmeliorationUtil.Playbook.Path}\\Configuration");
  41. }
  42. if (Directory.GetFiles($"{AmeliorationUtil.Playbook.Path}\\Configuration").Length == 0)
  43. {
  44. Console.WriteLine("Configuration folder is empty, put YAML files in it and restart the application.");
  45. Console.WriteLine($"Current directory: {Directory.GetCurrentDirectory()}");
  46. return -1;
  47. }
  48. ExtractResourceFolder("resources", Directory.GetCurrentDirectory());
  49. await AmeliorationUtil.StartAmelioration();
  50. return 0;
  51. }
  52. public static void ExtractResourceFolder(string resource, string dir, bool overwrite = false)
  53. {
  54. if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
  55. Assembly assembly = Assembly.GetExecutingAssembly();
  56. var resources = assembly.GetManifestResourceNames().Where(res => res.StartsWith($"TrustedUninstaller.CLI.Properties"));
  57. foreach (var obj in resources)
  58. {
  59. using (UnmanagedMemoryStream stream = (UnmanagedMemoryStream)assembly.GetManifestResourceStream(obj))
  60. {
  61. int MB = 1024 * 1024;
  62. int offset = -MB;
  63. var file = dir + $"\\{obj.Substring($"TrustedUninstaller.CLI.Properties.{resource}.".Length).Replace("---", "\\")}";
  64. if (file.EndsWith(".gitkeep")) continue;
  65. var fileDir = Path.GetDirectoryName(file);
  66. if (fileDir != null && !Directory.Exists(fileDir)) Directory.CreateDirectory(fileDir);
  67. if (File.Exists(file) && !overwrite) continue;
  68. if (File.Exists(file) && overwrite)
  69. {
  70. try
  71. {
  72. File.Delete(file);
  73. }
  74. catch (Exception e)
  75. {
  76. if (!Directory.Exists(Directory.GetCurrentDirectory() + "\\Logs"))
  77. Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\Logs");
  78. using (var writer = new StreamWriter(Path.Combine(Directory.GetCurrentDirectory(), "Logs\\ErrorLog.txt"), true))
  79. {
  80. writer.WriteLine($"Title: Could not delete existing resource file {file}.\r\nMessage: {e.Message}\r\n\r\nStackTrace: {e.StackTrace}");
  81. writer.WriteLine("\r\nDate/Time: " + DateTime.Now);
  82. writer.WriteLine("============================================");
  83. }
  84. continue;
  85. }
  86. }
  87. using (FileStream fsDlst = new FileStream(file, FileMode.CreateNew, FileAccess.Write))
  88. {
  89. while (offset + MB < stream.Length)
  90. {
  91. var buffer = new byte[MB];
  92. offset += MB;
  93. if (offset + MB > stream.Length)
  94. {
  95. var bytesLeft = stream.Length - offset;
  96. buffer = new byte[bytesLeft];
  97. }
  98. stream.Seek(offset, SeekOrigin.Begin);
  99. stream.Read(buffer, 0, buffer.Length);
  100. fsDlst.Seek(offset, SeekOrigin.Begin);
  101. fsDlst.Write(buffer, 0, buffer.Length);
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }