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

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