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.6 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
10 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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(Configuration configuration)
  15. {
  16. Server server = configuration.GetCurrentServer();
  17. if (_process == null)
  18. {
  19. Process[] existingPolipo = Process.GetProcessesByName("ss_polipo");
  20. foreach (Process p in existingPolipo)
  21. {
  22. try
  23. {
  24. p.Kill();
  25. p.WaitForExit();
  26. }
  27. catch (Exception e)
  28. {
  29. Console.WriteLine(e.ToString());
  30. }
  31. }
  32. string temppath = Path.GetTempPath();
  33. string polipoConfig = Resources.polipo_config;
  34. polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", server.local_port.ToString());
  35. polipoConfig = polipoConfig.Replace("__POLIPO_BIND_IP__", configuration.shareOverLan ? "0.0.0.0" : "127.0.0.1");
  36. FileManager.ByteArrayToFile(temppath + "/polipo.conf", System.Text.Encoding.UTF8.GetBytes(polipoConfig));
  37. FileManager.UncompressFile(temppath + "/ss_polipo.exe", Resources.polipo_exe);
  38. _process = new Process();
  39. // Configure the process using the StartInfo properties.
  40. _process.StartInfo.FileName = temppath + "/ss_polipo.exe";
  41. _process.StartInfo.Arguments = "-c \"" + temppath + "/polipo.conf\"";
  42. _process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  43. _process.StartInfo.UseShellExecute = true;
  44. _process.StartInfo.CreateNoWindow = true;
  45. //_process.StartInfo.RedirectStandardOutput = true;
  46. //_process.StartInfo.RedirectStandardError = true;
  47. _process.Start();
  48. }
  49. }
  50. public void Stop()
  51. {
  52. if (_process != null)
  53. {
  54. try
  55. {
  56. _process.Kill();
  57. _process.WaitForExit();
  58. }
  59. catch (InvalidOperationException)
  60. {
  61. // do nothing
  62. }
  63. _process = null;
  64. }
  65. }
  66. }
  67. }