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.

SystemProxy.cs 5.9 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System.Windows.Forms;
  2. using Microsoft.Win32;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.IO;
  8. using Shadowsocks.Model;
  9. namespace Shadowsocks.Controller
  10. {
  11. public class SystemProxy
  12. {
  13. [DllImport("wininet.dll")]
  14. public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
  15. public const int INTERNET_OPTION_SETTINGS_CHANGED = 39;
  16. public const int INTERNET_OPTION_REFRESH = 37;
  17. static bool _settingsReturn, _refreshReturn;
  18. public static void NotifyIE()
  19. {
  20. // These lines implement the Interface in the beginning of program
  21. // They cause the OS to refresh the settings, causing IP to realy update
  22. _settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
  23. _refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
  24. }
  25. public static void Update(Configuration config, bool forceDisable)
  26. {
  27. bool global = config.global;
  28. bool enabled = config.enabled;
  29. if (forceDisable)
  30. {
  31. enabled = false;
  32. }
  33. try
  34. {
  35. RegistryKey registry =
  36. Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
  37. true);
  38. if (enabled)
  39. {
  40. if (global)
  41. {
  42. registry.SetValue("ProxyEnable", 1);
  43. registry.SetValue("ProxyServer", "127.0.0.1:" + config.localPort.ToString());
  44. registry.SetValue("AutoConfigURL", "");
  45. }
  46. else
  47. {
  48. string pacUrl;
  49. if (config.useOnlinePac && !string.IsNullOrEmpty(config.pacUrl))
  50. pacUrl = config.pacUrl;
  51. else
  52. pacUrl = "http://127.0.0.1:" + config.localPort.ToString() + "/pac?t=" + GetTimestamp(DateTime.Now);
  53. registry.SetValue("ProxyEnable", 0);
  54. var readProxyServer = registry.GetValue("ProxyServer");
  55. registry.SetValue("ProxyServer", "");
  56. registry.SetValue("AutoConfigURL", pacUrl);
  57. }
  58. }
  59. else
  60. {
  61. registry.SetValue("ProxyEnable", 0);
  62. registry.SetValue("ProxyServer", "");
  63. registry.SetValue("AutoConfigURL", "");
  64. }
  65. //Set AutoDetectProxy Off
  66. IEAutoDetectProxy(false);
  67. SystemProxy.NotifyIE();
  68. //Must Notify IE first, or the connections do not chanage
  69. CopyProxySettingFromLan();
  70. }
  71. catch (Exception e)
  72. {
  73. Logging.LogUsefulException(e);
  74. // TODO this should be moved into views
  75. MessageBox.Show(I18N.GetString("Failed to update registry"));
  76. }
  77. }
  78. private static void CopyProxySettingFromLan()
  79. {
  80. RegistryKey registry =
  81. Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Connections",
  82. true);
  83. var defaultValue = registry.GetValue("DefaultConnectionSettings");
  84. try
  85. {
  86. var connections = registry.GetValueNames();
  87. foreach (String each in connections)
  88. {
  89. if (!(each.Equals("DefaultConnectionSettings")
  90. || each.Equals("LAN Connection")
  91. || each.Equals("SavedLegacySettings")))
  92. {
  93. //set all the connections's proxy as the lan
  94. registry.SetValue(each, defaultValue);
  95. }
  96. }
  97. SystemProxy.NotifyIE();
  98. } catch (IOException e) {
  99. Logging.LogUsefulException(e);
  100. }
  101. }
  102. private static String GetTimestamp(DateTime value)
  103. {
  104. return value.ToString("yyyyMMddHHmmssffff");
  105. }
  106. /// <summary>
  107. /// Checks or unchecks the IE Options Connection setting of "Automatically detect Proxy"
  108. /// </summary>
  109. /// <param name="set">Provide 'true' if you want to check the 'Automatically detect Proxy' check box. To uncheck, pass 'false'</param>
  110. private static void IEAutoDetectProxy(bool set)
  111. {
  112. RegistryKey registry =
  113. Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Connections",
  114. true);
  115. byte[] defConnection = (byte[])registry.GetValue("DefaultConnectionSettings");
  116. byte[] savedLegacySetting = (byte[])registry.GetValue("SavedLegacySettings");
  117. if (set)
  118. {
  119. defConnection[8] = Convert.ToByte(defConnection[8] & 8);
  120. savedLegacySetting[8] = Convert.ToByte(savedLegacySetting[8] & 8);
  121. }
  122. else
  123. {
  124. defConnection[8] = Convert.ToByte(defConnection[8] & ~8);
  125. savedLegacySetting[8] = Convert.ToByte(savedLegacySetting[8] & ~8);
  126. }
  127. registry.SetValue("DefaultConnectionSettings", defConnection);
  128. registry.SetValue("SavedLegacySettings", savedLegacySetting);
  129. }
  130. }
  131. }