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.4 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. registry.SetValue("ProxyServer", "");
  55. registry.SetValue("AutoConfigURL", pacUrl);
  56. }
  57. }
  58. else
  59. {
  60. registry.SetValue("ProxyEnable", 0);
  61. registry.SetValue("ProxyServer", "");
  62. registry.SetValue("AutoConfigURL", "");
  63. }
  64. SystemProxy.NotifyIE();
  65. //Must Notify IE first, or the connections do not chanage
  66. CopyProxySettingFromLan();
  67. }
  68. catch (Exception e)
  69. {
  70. Logging.LogUsefulException(e);
  71. // TODO this should be moved into views
  72. MessageBox.Show(I18N.GetString("Failed to update registry"));
  73. }
  74. }
  75. private static void CopyProxySettingFromLan()
  76. {
  77. RegistryKey registry =
  78. Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Connections",
  79. true);
  80. var defaultValue = registry.GetValue("DefaultConnectionSettings");
  81. try
  82. {
  83. var connections = registry.GetValueNames();
  84. foreach (String each in connections)
  85. {
  86. if (!(each.Equals("DefaultConnectionSettings")
  87. || each.Equals("LAN Connection")
  88. || each.Equals("SavedLegacySettings")))
  89. {
  90. //set all the connections's proxy as the lan
  91. registry.SetValue(each, defaultValue);
  92. }
  93. }
  94. SystemProxy.NotifyIE();
  95. } catch (IOException e) {
  96. Logging.LogUsefulException(e);
  97. }
  98. }
  99. private static String GetTimestamp(DateTime value)
  100. {
  101. return value.ToString("yyyyMMddHHmmssffff");
  102. }
  103. }
  104. }