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.1 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
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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)
  48. {
  49. MessageBox.Show("can not change registry!");
  50. throw;
  51. }
  52. }
  53. public static void Disable()
  54. {
  55. try
  56. {
  57. RegistryKey registry =
  58. Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
  59. true);
  60. registry.SetValue("ProxyEnable", 0);
  61. registry.SetValue("ProxyServer", "");
  62. registry.SetValue("AutoConfigURL", "");
  63. SystemProxy.NotifyIE();
  64. CopyProxySettingFromLan();
  65. }
  66. catch (Exception)
  67. {
  68. MessageBox.Show("can not change registry!");
  69. throw;
  70. }
  71. }
  72. private static void CopyProxySettingFromLan()
  73. {
  74. RegistryKey registry =
  75. Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Connections",
  76. true);
  77. var defaultValue = registry.GetValue("DefaultConnectionSettings");
  78. try
  79. {
  80. var connections = registry.GetValueNames();
  81. foreach (String each in connections)
  82. {
  83. if (!(each.Equals("DefaultConnectionSettings")
  84. || each.Equals("LAN Connection")
  85. || each.Equals("SavedLegacySettings")))
  86. {
  87. //set all the connections's proxy as the lan
  88. registry.SetValue(each, defaultValue);
  89. }
  90. }
  91. SystemProxy.NotifyIE();
  92. } catch (IOException e) {
  93. Logging.LogUsefulException(e);
  94. }
  95. }
  96. private static String GetTimestamp(DateTime value)
  97. {
  98. return value.ToString("yyyyMMddHHmmssffff");
  99. }
  100. }
  101. }