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.

ShadowsocksController.cs 3.5 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using shadowsocks_csharp.Model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace shadowsocks_csharp.Controller
  6. {
  7. public class ShadowsocksController
  8. {
  9. // controller:
  10. // handle user actions
  11. // manipulates UI
  12. // interacts with low level logic
  13. private Local local;
  14. private PACServer pacServer;
  15. private Config config;
  16. private PolipoRunner polipoRunner;
  17. private bool stopped = false;
  18. public class PathEventArgs : EventArgs
  19. {
  20. public string Path;
  21. }
  22. public class ErrorEventArgs : EventArgs
  23. {
  24. public string Error;
  25. }
  26. public event EventHandler ConfigChanged;
  27. public event EventHandler EnableStatusChanged;
  28. public event EventHandler<ErrorEventArgs> LocalFailToStart;
  29. // when user clicked Edit PAC, and PAC file has already created
  30. public event EventHandler<PathEventArgs> PACFileReadyToOpen;
  31. public ShadowsocksController()
  32. {
  33. config = Config.Load();
  34. polipoRunner = new PolipoRunner();
  35. polipoRunner.Start(config);
  36. local = new Local(config);
  37. try
  38. {
  39. local.Start();
  40. pacServer = new PACServer();
  41. pacServer.PACFileChanged += pacServer_PACFileChanged;
  42. pacServer.Start();
  43. }
  44. catch (Exception e)
  45. {
  46. Console.WriteLine(e.ToString());
  47. }
  48. updateSystemProxy();
  49. }
  50. public void SaveConfig(Config newConfig)
  51. {
  52. Config.Save(newConfig);
  53. config = newConfig;
  54. local.Stop();
  55. polipoRunner.Stop();
  56. polipoRunner.Start(config);
  57. local = new Local(config);
  58. local.Start();
  59. if (ConfigChanged != null)
  60. {
  61. ConfigChanged(this, new EventArgs());
  62. }
  63. }
  64. public Config GetConfig()
  65. {
  66. return config;
  67. }
  68. public void ToggleEnable(bool enabled)
  69. {
  70. config.enabled = enabled;
  71. updateSystemProxy();
  72. SaveConfig(config);
  73. if (EnableStatusChanged != null)
  74. {
  75. EnableStatusChanged(this, new EventArgs());
  76. }
  77. }
  78. public void Stop()
  79. {
  80. if (stopped)
  81. {
  82. return;
  83. }
  84. stopped = true;
  85. local.Stop();
  86. polipoRunner.Stop();
  87. if (config.enabled)
  88. {
  89. SystemProxy.Disable();
  90. }
  91. }
  92. public void TouchPACFile()
  93. {
  94. string pacFilename = pacServer.TouchPACFile();
  95. if (PACFileReadyToOpen != null)
  96. {
  97. PACFileReadyToOpen(this, new PathEventArgs() { Path = pacFilename });
  98. }
  99. }
  100. private void updateSystemProxy()
  101. {
  102. if (config.enabled)
  103. {
  104. SystemProxy.Enable();
  105. }
  106. else
  107. {
  108. SystemProxy.Disable();
  109. }
  110. }
  111. private void pacServer_PACFileChanged(object sender, EventArgs e)
  112. {
  113. updateSystemProxy();
  114. }
  115. }
  116. }