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 2.5 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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. namespace Shadowsocks.Controller
  8. {
  9. public class SystemProxy
  10. {
  11. [DllImport("wininet.dll")]
  12. public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
  13. public const int INTERNET_OPTION_SETTINGS_CHANGED = 39;
  14. public const int INTERNET_OPTION_REFRESH = 37;
  15. static bool _settingsReturn, _refreshReturn;
  16. public static void NotifyIE()
  17. {
  18. // These lines implement the Interface in the beginning of program
  19. // They cause the OS to refresh the settings, causing IP to realy update
  20. _settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
  21. _refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
  22. }
  23. public static void Enable()
  24. {
  25. try
  26. {
  27. RegistryKey registry =
  28. Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
  29. true);
  30. registry.SetValue("ProxyEnable", 0);
  31. registry.SetValue("ProxyServer", "");
  32. registry.SetValue("AutoConfigURL", "http://127.0.0.1:8090/pac?t=" + GetTimestamp(DateTime.Now));
  33. SystemProxy.NotifyIE();
  34. }
  35. catch (Exception)
  36. {
  37. MessageBox.Show("can not change registry!");
  38. throw;
  39. }
  40. }
  41. public static void Disable()
  42. {
  43. try
  44. {
  45. RegistryKey registry =
  46. Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
  47. true);
  48. registry.SetValue("ProxyEnable", 0);
  49. registry.SetValue("ProxyServer", "");
  50. registry.SetValue("AutoConfigURL", "");
  51. SystemProxy.NotifyIE();
  52. }
  53. catch (Exception)
  54. {
  55. MessageBox.Show("can not change registry!");
  56. throw;
  57. }
  58. }
  59. private static String GetTimestamp(DateTime value)
  60. {
  61. return value.ToString("yyyyMMddHHmmssffff");
  62. }
  63. }
  64. }