FireWallController.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using WindowsFirewallHelper;
  9. namespace ClashDotNetFramework.Controllers
  10. {
  11. public static class FireWallController
  12. {
  13. public static string ClashDotNetFramework = "ClashDotNetFramework";
  14. public static void AddFireWallRules()
  15. {
  16. if (!FirewallWAS.IsSupported)
  17. {
  18. return;
  19. }
  20. var rule = FirewallManager.Instance.Rules.FirstOrDefault(r => r.Name == ClashDotNetFramework);
  21. if (rule != null)
  22. {
  23. if (rule.ApplicationName.StartsWith(Global.ClashDotNetFrameworkDir))
  24. return;
  25. }
  26. try
  27. {
  28. Process proc = new Process
  29. {
  30. StartInfo =
  31. {
  32. FileName = Path.GetFullPath($"bin\\FireWallHelper.exe"),
  33. WorkingDirectory = $"{Global.ClashDotNetFrameworkDir}\\bin",
  34. CreateNoWindow = true,
  35. WindowStyle = ProcessWindowStyle.Hidden,
  36. UseShellExecute = true,
  37. Verb = "runas"
  38. }
  39. };
  40. proc.Start();
  41. }
  42. catch
  43. {
  44. }
  45. }
  46. }
  47. }