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