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

ProtocolHandler.cs 3.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using Microsoft.Win32;
  2. using Splat;
  3. using System;
  4. namespace Shadowsocks.WPF.Utils
  5. {
  6. static class ProtocolHandler
  7. {
  8. const string ssURLRegKey = @"SOFTWARE\Classes\ss";
  9. public static bool Set(bool enabled)
  10. {
  11. RegistryKey? ssURLAssociation = null;
  12. try
  13. {
  14. ssURLAssociation = Registry.CurrentUser.CreateSubKey(ssURLRegKey, RegistryKeyPermissionCheck.ReadWriteSubTree);
  15. if (ssURLAssociation == null)
  16. {
  17. LogHost.Default.Error(@"Failed to create HKCU\SOFTWARE\Classes\ss to register ss:// association.");
  18. return false;
  19. }
  20. if (enabled)
  21. {
  22. ssURLAssociation.SetValue("", "URL:Shadowsocks");
  23. ssURLAssociation.SetValue("URL Protocol", "");
  24. var shellOpen = ssURLAssociation.CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command");
  25. shellOpen.SetValue("", $"{Utilities.ExecutablePath} --open-url %1");
  26. LogHost.Default.Info(@"Successfully added ss:// association.");
  27. }
  28. else
  29. {
  30. Registry.CurrentUser.DeleteSubKeyTree(ssURLRegKey);
  31. LogHost.Default.Info(@"Successfully removed ss:// association.");
  32. }
  33. return true;
  34. }
  35. catch (Exception e)
  36. {
  37. LogHost.Default.Error(e, "An error occurred while setting ss:// association registry entries.");
  38. return false;
  39. }
  40. finally
  41. {
  42. if (ssURLAssociation != null)
  43. {
  44. try
  45. {
  46. ssURLAssociation.Close();
  47. ssURLAssociation.Dispose();
  48. }
  49. catch (Exception e)
  50. {
  51. LogHost.Default.Error(e, "An error occurred while setting ss:// association registry entries.");
  52. }
  53. }
  54. }
  55. }
  56. public static bool Check()
  57. {
  58. RegistryKey? ssURLAssociation = null;
  59. try
  60. {
  61. ssURLAssociation = Registry.CurrentUser.OpenSubKey(ssURLRegKey, true);
  62. if (ssURLAssociation == null)
  63. {
  64. //logger.Info(@"ss:// links not associated.");
  65. return false;
  66. }
  67. var shellOpen = ssURLAssociation.OpenSubKey("shell")?.OpenSubKey("open")?.OpenSubKey("command");
  68. return shellOpen?.GetValue("") as string == $"{Utilities.ExecutablePath} --open-url %1";
  69. }
  70. catch (Exception e)
  71. {
  72. LogHost.Default.Error(e, "An error occurred while checking ss:// association registry entries.");
  73. return false;
  74. }
  75. finally
  76. {
  77. if (ssURLAssociation != null)
  78. {
  79. try
  80. {
  81. ssURLAssociation.Close();
  82. ssURLAssociation.Dispose();
  83. }
  84. catch (Exception e)
  85. {
  86. LogHost.Default.Error(e, "An error occurred while checking ss:// association registry entries.");
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }