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 5.8 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System.IO;
  2. using Shadowsocks.Model;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Threading;
  7. namespace Shadowsocks.Controller
  8. {
  9. public class ShadowsocksController
  10. {
  11. // controller:
  12. // handle user actions
  13. // manipulates UI
  14. // interacts with low level logic
  15. private Thread _ramThread;
  16. private Local local;
  17. private PACServer pacServer;
  18. private Configuration _config;
  19. private PolipoRunner polipoRunner;
  20. private bool stopped = false;
  21. public class PathEventArgs : EventArgs
  22. {
  23. public string Path;
  24. }
  25. public event EventHandler ConfigChanged;
  26. public event EventHandler EnableStatusChanged;
  27. public event EventHandler ShareOverLANStatusChanged;
  28. // when user clicked Edit PAC, and PAC file has already created
  29. public event EventHandler<PathEventArgs> PACFileReadyToOpen;
  30. public ShadowsocksController()
  31. {
  32. _config = Configuration.Load();
  33. polipoRunner = new PolipoRunner();
  34. polipoRunner.Start(_config);
  35. local = new Local(_config);
  36. try
  37. {
  38. local.Start();
  39. }
  40. catch (Exception e)
  41. {
  42. Console.WriteLine(e);
  43. }
  44. try
  45. {
  46. pacServer = new PACServer();
  47. pacServer.PACFileChanged += pacServer_PACFileChanged;
  48. pacServer.Start(_config);
  49. }
  50. catch (Exception e)
  51. {
  52. Console.WriteLine(e);
  53. }
  54. UpdateSystemProxy();
  55. StartReleasingMemory();
  56. }
  57. public Server GetCurrentServer()
  58. {
  59. return _config.GetCurrentServer();
  60. }
  61. // always return copy
  62. public Configuration GetConfiguration()
  63. {
  64. return Configuration.Load();
  65. }
  66. public void SaveServers(List<Server> servers)
  67. {
  68. _config.configs = servers;
  69. SaveConfig(_config);
  70. }
  71. public void ToggleEnable(bool enabled)
  72. {
  73. _config.enabled = enabled;
  74. UpdateSystemProxy();
  75. SaveConfig(_config);
  76. if (EnableStatusChanged != null)
  77. {
  78. EnableStatusChanged(this, new EventArgs());
  79. }
  80. }
  81. public void ToggleShareOverLAN(bool enabled)
  82. {
  83. _config.shareOverLan = enabled;
  84. SaveConfig(_config);
  85. if (ShareOverLANStatusChanged != null)
  86. {
  87. ShareOverLANStatusChanged(this, new EventArgs());
  88. }
  89. }
  90. public void SelectServerIndex(int index)
  91. {
  92. _config.index = index;
  93. SaveConfig(_config);
  94. }
  95. public void Stop()
  96. {
  97. if (stopped)
  98. {
  99. return;
  100. }
  101. stopped = true;
  102. local.Stop();
  103. polipoRunner.Stop();
  104. if (_config.enabled)
  105. {
  106. SystemProxy.Disable();
  107. }
  108. }
  109. public void TouchPACFile()
  110. {
  111. string pacFilename = pacServer.TouchPACFile();
  112. if (PACFileReadyToOpen != null)
  113. {
  114. PACFileReadyToOpen(this, new PathEventArgs() { Path = pacFilename });
  115. }
  116. }
  117. public string GetQRCodeForCurrentServer()
  118. {
  119. Server server = GetCurrentServer();
  120. string parts = server.method + ":" + server.password + "@" + server.server + ":" + server.server_port;
  121. string base64 = System.Convert.ToBase64String(Encoding.UTF8.GetBytes(parts));
  122. return "ss://" + base64;
  123. }
  124. protected void SaveConfig(Configuration newConfig)
  125. {
  126. Configuration.Save(newConfig);
  127. // some logic in configuration updated the config when saving, we need to read it again
  128. _config = Configuration.Load();
  129. pacServer.Stop();
  130. local.Stop();
  131. // don't put polipoRunner.Start() before pacServer.Stop()
  132. // or bind will fail when switching bind address from 0.0.0.0 to 127.0.0.1
  133. // though UseShellExecute is set to true now
  134. // http://stackoverflow.com/questions/10235093/socket-doesnt-close-after-application-exits-if-a-launched-process-is-open
  135. polipoRunner.Stop();
  136. polipoRunner.Start(_config);
  137. local = new Local(_config);
  138. local.Start();
  139. pacServer.Start(_config);
  140. if (ConfigChanged != null)
  141. {
  142. ConfigChanged(this, new EventArgs());
  143. }
  144. Util.Util.ReleaseMemory();
  145. }
  146. private void UpdateSystemProxy()
  147. {
  148. if (_config.enabled)
  149. {
  150. SystemProxy.Enable();
  151. }
  152. else
  153. {
  154. SystemProxy.Disable();
  155. }
  156. }
  157. private void pacServer_PACFileChanged(object sender, EventArgs e)
  158. {
  159. UpdateSystemProxy();
  160. }
  161. private void StartReleasingMemory()
  162. {
  163. _ramThread = new Thread(new ThreadStart(ReleaseMemory));
  164. _ramThread.IsBackground = true;
  165. _ramThread.Start();
  166. }
  167. private void ReleaseMemory()
  168. {
  169. while (true)
  170. {
  171. Util.Util.ReleaseMemory();
  172. Thread.Sleep(30 * 1000);
  173. }
  174. }
  175. }
  176. }