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.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using Shadowsocks.Net.SystemProxy;
  2. using Shadowsocks.PAC;
  3. using Shadowsocks.WPF.Models;
  4. using Shadowsocks.WPF.Services.SystemProxy;
  5. using Splat;
  6. using System.Windows;
  7. namespace Shadowsocks.WPF.Utils
  8. {
  9. public static class SystemProxy
  10. {
  11. public static void Update(bool forceDisable, bool enabled, bool global)
  12. {
  13. var settings = Locator.Current.GetService<Settings>();
  14. var appSettings = settings.App;
  15. var netSettings = settings.Net;
  16. var pacSettings = settings.PAC;
  17. if (forceDisable || !WinINet.operational)
  18. {
  19. enabled = false;
  20. }
  21. try
  22. {
  23. if (enabled)
  24. {
  25. if (global)
  26. {
  27. WinINet.ProxyGlobal($"localhost:{netSettings.HttpListeningPort}", "<local>");
  28. }
  29. else
  30. {
  31. var pacUrl = string.IsNullOrEmpty(pacSettings.CustomPACUrl)
  32. ? Locator.Current.GetService<PACServer>().PacUrl : pacSettings.CustomPACUrl;
  33. WinINet.ProxyPAC(pacUrl);
  34. }
  35. }
  36. else
  37. {
  38. WinINet.Restore();
  39. }
  40. }
  41. catch (ProxyException ex)
  42. {
  43. LogHost.Default.Error(ex, "An error occurred while updating system proxy.");
  44. /*if (ex.Type != ProxyExceptionType.Unspecific && !noRetry)
  45. {
  46. var ret = MessageBox.Show(I18N.GetString("Error occured when process proxy setting, do you want reset current setting and retry?"), I18N.GetString("Shadowsocks"), MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
  47. if (ret == DialogResult.Yes)
  48. {
  49. WinINet.Reset();
  50. Update(config, forceDisable, pacSrv, true);
  51. }
  52. }
  53. else
  54. {
  55. MessageBox.Show(I18N.GetString("Unrecoverable proxy setting error occured, see log for detail"), I18N.GetString("Shadowsocks"), MessageBoxButtons.OK, MessageBoxIcon.Error);
  56. }*/
  57. }
  58. }
  59. }
  60. }