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 4.6 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. namespace Shadowsocks.Controller
  9. {
  10. public class SystemProxy
  11. {
  12. [DllImport("wininet.dll")]
  13. public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
  14. public const int INTERNET_OPTION_SETTINGS_CHANGED = 39;
  15. public const int INTERNET_OPTION_REFRESH = 37;
  16. static bool _settingsReturn, _refreshReturn;
  17. public static void NotifyIE()
  18. {
  19. // These lines implement the Interface in the beginning of program
  20. // They cause the OS to refresh the settings, causing IP to realy update
  21. _settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
  22. _refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
  23. }
  24. public static void Enable(bool global)
  25. {
  26. try
  27. {
  28. RegistryKey registry =
  29. Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
  30. true);
  31. if (global)
  32. {
  33. registry.SetValue("ProxyEnable", 1);
  34. registry.SetValue("ProxyServer", "127.0.0.1:8123");
  35. registry.SetValue("AutoConfigURL", "");
  36. }
  37. else
  38. {
  39. registry.SetValue("ProxyEnable", 0);
  40. registry.SetValue("ProxyServer", "");
  41. registry.SetValue("AutoConfigURL", "http://127.0.0.1:8093/pac?t=" + GetTimestamp(DateTime.Now));
  42. }
  43. SystemProxy.NotifyIE();
  44. //Must Notify IE first, or the connections do not chanage
  45. CopyProxySettingFromLan();
  46. }
  47. catch (Exception e)
  48. {
  49. Logging.LogUsefulException(e);
  50. // TODO this should be moved into views
  51. MessageBox.Show(I18N.GetString("Failed to update registry"));
  52. }
  53. }
  54. public static void Disable()
  55. {
  56. try
  57. {
  58. Console.WriteLine("Disable");
  59. RegistryKey registry =
  60. Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
  61. true);
  62. Console.WriteLine("Disable1");
  63. registry.SetValue("ProxyEnable", 0);
  64. registry.SetValue("ProxyServer", "");
  65. registry.SetValue("AutoConfigURL", "");
  66. Console.WriteLine("Disable2");
  67. SystemProxy.NotifyIE();
  68. Console.WriteLine("Disable3");
  69. CopyProxySettingFromLan();
  70. Console.WriteLine("Disable4");
  71. }
  72. catch (Exception e)
  73. {
  74. Logging.LogUsefulException(e);
  75. // TODO this should be moved into views
  76. MessageBox.Show(I18N.GetString("Failed to update registry"));
  77. }
  78. }
  79. private static void CopyProxySettingFromLan()
  80. {
  81. RegistryKey registry =
  82. Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Connections",
  83. true);
  84. var defaultValue = registry.GetValue("DefaultConnectionSettings");
  85. try
  86. {
  87. var connections = registry.GetValueNames();
  88. foreach (String each in connections)
  89. {
  90. if (!(each.Equals("DefaultConnectionSettings")
  91. || each.Equals("LAN Connection")
  92. || each.Equals("SavedLegacySettings")))
  93. {
  94. //set all the connections's proxy as the lan
  95. registry.SetValue(each, defaultValue);
  96. }
  97. }
  98. SystemProxy.NotifyIE();
  99. } catch (IOException e) {
  100. Logging.LogUsefulException(e);
  101. }
  102. }
  103. private static String GetTimestamp(DateTime value)
  104. {
  105. return value.ToString("yyyyMMddHHmmssffff");
  106. }
  107. }
  108. }