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 7.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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. using System.Net.Sockets;
  8. namespace Shadowsocks.Controller
  9. {
  10. public class ShadowsocksController
  11. {
  12. // controller:
  13. // handle user actions
  14. // manipulates UI
  15. // interacts with low level logic
  16. private Thread _ramThread;
  17. private Local local;
  18. private PACServer pacServer;
  19. private Configuration _config;
  20. private PolipoRunner polipoRunner;
  21. private bool stopped = false;
  22. private bool _systemProxyIsDirty = false;
  23. public class PathEventArgs : EventArgs
  24. {
  25. public string Path;
  26. }
  27. public event EventHandler ConfigChanged;
  28. public event EventHandler EnableStatusChanged;
  29. public event EventHandler EnableGlobalChanged;
  30. public event EventHandler ShareOverLANStatusChanged;
  31. // when user clicked Edit PAC, and PAC file has already created
  32. public event EventHandler<PathEventArgs> PACFileReadyToOpen;
  33. public event ErrorEventHandler Errored;
  34. public ShadowsocksController()
  35. {
  36. _config = Configuration.Load();
  37. }
  38. public void Start()
  39. {
  40. Reload();
  41. }
  42. protected void ReportError(Exception e)
  43. {
  44. if (Errored != null)
  45. {
  46. // translate Microsoft language into human language
  47. // i.e. 以一种访问权限不允许的方式做了一个访问套接字的尝试 => Port is already used
  48. if (e is SocketException)
  49. {
  50. SocketException se = (SocketException)e;
  51. if (se.SocketErrorCode == SocketError.AccessDenied)
  52. {
  53. e = new Exception(I18N.GetString("Port is already used"), e);
  54. }
  55. }
  56. Errored(this, new ErrorEventArgs(e));
  57. }
  58. }
  59. public Server GetCurrentServer()
  60. {
  61. return _config.GetCurrentServer();
  62. }
  63. // always return copy
  64. public Configuration GetConfiguration()
  65. {
  66. return Configuration.Load();
  67. }
  68. public void SaveServers(List<Server> servers)
  69. {
  70. _config.configs = servers;
  71. SaveConfig(_config);
  72. }
  73. public void ToggleEnable(bool enabled)
  74. {
  75. _config.enabled = enabled;
  76. UpdateSystemProxy();
  77. SaveConfig(_config);
  78. if (EnableStatusChanged != null)
  79. {
  80. EnableStatusChanged(this, new EventArgs());
  81. }
  82. }
  83. public void ToggleGlobal(bool global)
  84. {
  85. _config.global = global;
  86. UpdateSystemProxy();
  87. SaveConfig(_config);
  88. if (EnableGlobalChanged != null)
  89. {
  90. EnableGlobalChanged(this, new EventArgs());
  91. }
  92. }
  93. public void ToggleShareOverLAN(bool enabled)
  94. {
  95. _config.shareOverLan = enabled;
  96. SaveConfig(_config);
  97. if (ShareOverLANStatusChanged != null)
  98. {
  99. ShareOverLANStatusChanged(this, new EventArgs());
  100. }
  101. }
  102. public void SelectServerIndex(int index)
  103. {
  104. _config.index = index;
  105. SaveConfig(_config);
  106. }
  107. public void Stop()
  108. {
  109. if (stopped)
  110. {
  111. return;
  112. }
  113. stopped = true;
  114. if (local != null)
  115. {
  116. local.Stop();
  117. }
  118. if (polipoRunner != null)
  119. {
  120. polipoRunner.Stop();
  121. }
  122. if (_config.enabled)
  123. {
  124. SystemProxy.Disable();
  125. }
  126. }
  127. public void TouchPACFile()
  128. {
  129. string pacFilename = pacServer.TouchPACFile();
  130. if (PACFileReadyToOpen != null)
  131. {
  132. PACFileReadyToOpen(this, new PathEventArgs() { Path = pacFilename });
  133. }
  134. }
  135. public string GetQRCodeForCurrentServer()
  136. {
  137. Server server = GetCurrentServer();
  138. string parts = server.method + ":" + server.password + "@" + server.server + ":" + server.server_port;
  139. string base64 = System.Convert.ToBase64String(Encoding.UTF8.GetBytes(parts));
  140. return "ss://" + base64;
  141. }
  142. protected void Reload()
  143. {
  144. // some logic in configuration updated the config when saving, we need to read it again
  145. _config = Configuration.Load();
  146. if (polipoRunner == null)
  147. {
  148. polipoRunner = new PolipoRunner();
  149. }
  150. if (pacServer == null)
  151. {
  152. pacServer = new PACServer();
  153. pacServer.PACFileChanged += pacServer_PACFileChanged;
  154. }
  155. pacServer.Stop();
  156. if (local != null)
  157. {
  158. local.Stop();
  159. }
  160. // don't put polipoRunner.Start() before pacServer.Stop()
  161. // or bind will fail when switching bind address from 0.0.0.0 to 127.0.0.1
  162. // though UseShellExecute is set to true now
  163. // http://stackoverflow.com/questions/10235093/socket-doesnt-close-after-application-exits-if-a-launched-process-is-open
  164. polipoRunner.Stop();
  165. try
  166. {
  167. polipoRunner.Start(_config);
  168. local = new Local(_config);
  169. local.Start();
  170. pacServer.Start(_config);
  171. }
  172. catch (Exception e)
  173. {
  174. Logging.LogUsefulException(e);
  175. ReportError(e);
  176. }
  177. if (ConfigChanged != null)
  178. {
  179. ConfigChanged(this, new EventArgs());
  180. }
  181. UpdateSystemProxy();
  182. Util.Util.ReleaseMemory();
  183. }
  184. protected void SaveConfig(Configuration newConfig)
  185. {
  186. Configuration.Save(newConfig);
  187. Reload();
  188. }
  189. private void UpdateSystemProxy()
  190. {
  191. if (_config.enabled)
  192. {
  193. SystemProxy.Enable(_config.global);
  194. _systemProxyIsDirty = true;
  195. }
  196. else
  197. {
  198. // only switch it off if we have switched it on
  199. if (_systemProxyIsDirty)
  200. {
  201. SystemProxy.Disable();
  202. _systemProxyIsDirty = false;
  203. }
  204. }
  205. }
  206. private void pacServer_PACFileChanged(object sender, EventArgs e)
  207. {
  208. UpdateSystemProxy();
  209. }
  210. private void StartReleasingMemory()
  211. {
  212. _ramThread = new Thread(new ThreadStart(ReleaseMemory));
  213. _ramThread.IsBackground = true;
  214. _ramThread.Start();
  215. }
  216. private void ReleaseMemory()
  217. {
  218. while (true)
  219. {
  220. Util.Util.ReleaseMemory();
  221. Thread.Sleep(30 * 1000);
  222. }
  223. }
  224. }
  225. }