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.

Sysproxy.cs 2.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Shadowsocks.Controller;
  9. using Shadowsocks.Properties;
  10. namespace Shadowsocks.Util.SystemProxy
  11. {
  12. public static class Sysproxy
  13. {
  14. enum RET_ERRORS : int
  15. {
  16. RET_NO_ERROR = 0,
  17. INVALID_FORMAT = 1,
  18. NO_PERMISSION = 2,
  19. SYSCALL_FAILED = 3,
  20. NO_MEMORY = 4,
  21. INVAILD_OPTION_COUNT = 5,
  22. };
  23. static Sysproxy()
  24. {
  25. try
  26. {
  27. FileManager.UncompressFile(Utils.GetTempPath("sysproxy.exe"),
  28. Environment.Is64BitOperatingSystem ? Resources.sysproxy64_exe : Resources.sysproxy_exe);
  29. }
  30. catch (IOException e)
  31. {
  32. Logging.LogUsefulException(e);
  33. }
  34. }
  35. public static void SetIEProxy(bool enable, bool global, string proxyServer, string pacURL)
  36. {
  37. string arguments;
  38. if (enable)
  39. {
  40. if (global)
  41. {
  42. arguments = $"global {proxyServer} <local>;localhost;127.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;172.32.*;192.168.*";
  43. }
  44. else
  45. {
  46. arguments = $"pac {pacURL}";
  47. }
  48. }
  49. else
  50. {
  51. arguments = "off";
  52. }
  53. using (var process = new Process())
  54. {
  55. // Configure the process using the StartInfo properties.
  56. process.StartInfo.FileName = Utils.GetTempPath("sysproxy.exe");
  57. process.StartInfo.Arguments = arguments;
  58. process.StartInfo.WorkingDirectory = Utils.GetTempPath();
  59. process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  60. process.StartInfo.UseShellExecute = false;
  61. process.StartInfo.RedirectStandardError = true;
  62. process.StartInfo.CreateNoWindow = true;
  63. process.Start();
  64. var error = process.StandardError.ReadToEnd();
  65. process.WaitForExit();
  66. var exitCode = process.ExitCode;
  67. if (exitCode != (int) RET_ERRORS.RET_NO_ERROR)
  68. {
  69. throw new ProxyException(error);
  70. }
  71. }
  72. }
  73. }
  74. }