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.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using Microsoft.Win32;
  2. using NLog;
  3. using Shadowsocks.Util;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace Shadowsocks.Controller
  11. {
  12. static class ProtocolHandler
  13. {
  14. const string ssURLRegKey = @"SOFTWARE\Classes\ss";
  15. private static Logger logger = LogManager.GetCurrentClassLogger();
  16. // Don't use Application.ExecutablePath
  17. // see https://stackoverflow.com/questions/12945805/odd-c-sharp-path-issue
  18. private static readonly string ExecutablePath = Assembly.GetEntryAssembly().Location;
  19. // TODO: Elevate when necessary
  20. public static bool Set(bool enabled)
  21. {
  22. RegistryKey ssURLAssociation = null;
  23. try
  24. {
  25. ssURLAssociation = Registry.CurrentUser.CreateSubKey(ssURLRegKey, RegistryKeyPermissionCheck.ReadWriteSubTree);
  26. if (ssURLAssociation == null)
  27. {
  28. logger.Error(@"Failed to create HKCU\SOFTWARE\Classes\ss to register ss:// association.");
  29. return false;
  30. }
  31. if (enabled)
  32. {
  33. ssURLAssociation.SetValue("", "URL:Shadowsocks");
  34. ssURLAssociation.SetValue("URL Protocol", "");
  35. var shellOpen = ssURLAssociation.CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command");
  36. shellOpen.SetValue("", $"{ExecutablePath} --open-url %1");
  37. logger.Info(@"Successfully added ss:// association.");
  38. }
  39. else
  40. {
  41. Registry.CurrentUser.DeleteSubKeyTree(ssURLRegKey);
  42. logger.Info(@"Successfully removed ss:// association.");
  43. }
  44. return true;
  45. }
  46. catch (Exception e)
  47. {
  48. logger.LogUsefulException(e);
  49. return false;
  50. }
  51. finally
  52. {
  53. if (ssURLAssociation != null)
  54. {
  55. try
  56. {
  57. ssURLAssociation.Close();
  58. ssURLAssociation.Dispose();
  59. }
  60. catch (Exception e)
  61. { logger.LogUsefulException(e); }
  62. }
  63. }
  64. }
  65. public static bool Check()
  66. {
  67. RegistryKey ssURLAssociation = null;
  68. try
  69. {
  70. ssURLAssociation = Registry.CurrentUser.OpenSubKey(ssURLRegKey, true);
  71. if (ssURLAssociation == null)
  72. {
  73. //logger.Info(@"ss:// links not associated.");
  74. return false;
  75. }
  76. var shellOpen = ssURLAssociation.OpenSubKey("shell").OpenSubKey("open").OpenSubKey("command");
  77. return (string)shellOpen.GetValue("") == $"{ExecutablePath} --open-url %1";
  78. }
  79. catch (Exception e)
  80. {
  81. logger.LogUsefulException(e);
  82. return false;
  83. }
  84. finally
  85. {
  86. if (ssURLAssociation != null)
  87. {
  88. try
  89. {
  90. ssURLAssociation.Close();
  91. ssURLAssociation.Dispose();
  92. }
  93. catch (Exception e)
  94. { logger.LogUsefulException(e); }
  95. }
  96. }
  97. }
  98. }
  99. }