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.

PolipoRunner.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
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using Shadowsocks.Model;
  2. using Shadowsocks.Properties;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.IO.Compression;
  8. using System.Text;
  9. namespace Shadowsocks.Controller
  10. {
  11. class PolipoRunner
  12. {
  13. private Process _process;
  14. public void Start(Server config)
  15. {
  16. if (_process == null)
  17. {
  18. Process[] existingPolipo = Process.GetProcessesByName("ss_polipo");
  19. foreach (Process p in existingPolipo)
  20. {
  21. try
  22. {
  23. p.Kill();
  24. p.WaitForExit();
  25. }
  26. catch (Exception e)
  27. {
  28. Console.WriteLine(e.ToString());
  29. }
  30. }
  31. string temppath = Path.GetTempPath();
  32. string polipoConfig = Resources.polipo_config;
  33. polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", config.local_port.ToString());
  34. FileManager.ByteArrayToFile(temppath + "/polipo.conf", System.Text.Encoding.UTF8.GetBytes(polipoConfig));
  35. FileManager.UncompressFile(temppath + "/ss_polipo.exe", Resources.polipo_exe);
  36. _process = new Process();
  37. // Configure the process using the StartInfo properties.
  38. _process.StartInfo.FileName = temppath + "/ss_polipo.exe";
  39. _process.StartInfo.Arguments = "-c \"" + temppath + "/polipo.conf\"";
  40. _process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  41. _process.StartInfo.UseShellExecute = false;
  42. _process.StartInfo.CreateNoWindow = true;
  43. _process.StartInfo.RedirectStandardOutput = true;
  44. _process.StartInfo.RedirectStandardError = true;
  45. //process.StandardOutput
  46. _process.Start();
  47. }
  48. }
  49. public void Stop()
  50. {
  51. if (_process != null)
  52. {
  53. try
  54. {
  55. _process.Kill();
  56. _process.WaitForExit();
  57. }
  58. catch (InvalidOperationException)
  59. {
  60. // do nothing
  61. }
  62. _process = null;
  63. }
  64. }
  65. }
  66. }