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