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 16 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 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
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using Shadowsocks.Controller.Strategy;
  9. using Shadowsocks.Model;
  10. using Shadowsocks.Properties;
  11. using Shadowsocks.Util;
  12. namespace Shadowsocks.Controller
  13. {
  14. public class ShadowsocksController
  15. {
  16. // controller:
  17. // handle user actions
  18. // manipulates UI
  19. // interacts with low level logic
  20. private Thread _ramThread;
  21. private Listener _listener;
  22. private PACServer _pacServer;
  23. private Configuration _config;
  24. private StrategyManager _strategyManager;
  25. private PolipoRunner polipoRunner;
  26. private GFWListUpdater gfwListUpdater;
  27. public AvailabilityStatistics availabilityStatistics { get; private set; }
  28. public StatisticsStrategyConfiguration StatisticsConfiguration { get; private set; }
  29. public long inboundCounter = 0;
  30. public long outboundCounter = 0;
  31. private bool stopped = false;
  32. private bool _systemProxyIsDirty = false;
  33. public class PathEventArgs : EventArgs
  34. {
  35. public string Path;
  36. }
  37. public event EventHandler ConfigChanged;
  38. public event EventHandler EnableStatusChanged;
  39. public event EventHandler EnableGlobalChanged;
  40. public event EventHandler ShareOverLANStatusChanged;
  41. // when user clicked Edit PAC, and PAC file has already created
  42. public event EventHandler<PathEventArgs> PACFileReadyToOpen;
  43. public event EventHandler<PathEventArgs> UserRuleFileReadyToOpen;
  44. public event EventHandler<GFWListUpdater.ResultEventArgs> UpdatePACFromGFWListCompleted;
  45. public event ErrorEventHandler UpdatePACFromGFWListError;
  46. public event ErrorEventHandler Errored;
  47. public ShadowsocksController()
  48. {
  49. _config = Configuration.Load();
  50. StatisticsConfiguration = StatisticsStrategyConfiguration.Load();
  51. _strategyManager = new StrategyManager(this);
  52. StartReleasingMemory();
  53. }
  54. public void Start()
  55. {
  56. Reload();
  57. }
  58. protected void ReportError(Exception e)
  59. {
  60. if (Errored != null)
  61. {
  62. Errored(this, new ErrorEventArgs(e));
  63. }
  64. }
  65. public Server GetCurrentServer()
  66. {
  67. return _config.GetCurrentServer();
  68. }
  69. // always return copy
  70. public Configuration GetConfigurationCopy()
  71. {
  72. return Configuration.Load();
  73. }
  74. // always return current instance
  75. public Configuration GetCurrentConfiguration()
  76. {
  77. return _config;
  78. }
  79. public IList<IStrategy> GetStrategies()
  80. {
  81. return _strategyManager.GetStrategies();
  82. }
  83. public IStrategy GetCurrentStrategy()
  84. {
  85. foreach (var strategy in _strategyManager.GetStrategies())
  86. {
  87. if (strategy.ID == this._config.strategy)
  88. {
  89. return strategy;
  90. }
  91. }
  92. return null;
  93. }
  94. public Server GetAServer(IStrategyCallerType type, IPEndPoint localIPEndPoint)
  95. {
  96. IStrategy strategy = GetCurrentStrategy();
  97. if (strategy != null)
  98. {
  99. return strategy.GetAServer(type, localIPEndPoint);
  100. }
  101. if (_config.index < 0)
  102. {
  103. _config.index = 0;
  104. }
  105. return GetCurrentServer();
  106. }
  107. public void SaveServers(List<Server> servers, int localPort)
  108. {
  109. _config.configs = servers;
  110. _config.localPort = localPort;
  111. Configuration.Save(_config);
  112. }
  113. public void SaveStrategyConfigurations(StatisticsStrategyConfiguration configuration)
  114. {
  115. StatisticsConfiguration = configuration;
  116. StatisticsStrategyConfiguration.Save(configuration);
  117. }
  118. public bool AddServerBySSURL(string ssURL)
  119. {
  120. try
  121. {
  122. var server = new Server(ssURL);
  123. _config.configs.Add(server);
  124. _config.index = _config.configs.Count - 1;
  125. SaveConfig(_config);
  126. return true;
  127. }
  128. catch (Exception e)
  129. {
  130. Logging.LogUsefulException(e);
  131. return false;
  132. }
  133. }
  134. public void ToggleEnable(bool enabled)
  135. {
  136. _config.enabled = enabled;
  137. UpdateSystemProxy();
  138. SaveConfig(_config);
  139. if (EnableStatusChanged != null)
  140. {
  141. EnableStatusChanged(this, new EventArgs());
  142. }
  143. }
  144. public void ToggleGlobal(bool global)
  145. {
  146. _config.global = global;
  147. UpdateSystemProxy();
  148. SaveConfig(_config);
  149. if (EnableGlobalChanged != null)
  150. {
  151. EnableGlobalChanged(this, new EventArgs());
  152. }
  153. }
  154. public void ToggleShareOverLAN(bool enabled)
  155. {
  156. _config.shareOverLan = enabled;
  157. SaveConfig(_config);
  158. if (ShareOverLANStatusChanged != null)
  159. {
  160. ShareOverLANStatusChanged(this, new EventArgs());
  161. }
  162. }
  163. public void SelectServerIndex(int index)
  164. {
  165. _config.index = index;
  166. _config.strategy = null;
  167. SaveConfig(_config);
  168. }
  169. public void SelectStrategy(string strategyID)
  170. {
  171. _config.index = -1;
  172. _config.strategy = strategyID;
  173. SaveConfig(_config);
  174. }
  175. public void Stop()
  176. {
  177. if (stopped)
  178. {
  179. return;
  180. }
  181. stopped = true;
  182. if (_listener != null)
  183. {
  184. _listener.Stop();
  185. }
  186. if (polipoRunner != null)
  187. {
  188. polipoRunner.Stop();
  189. }
  190. if (_config.enabled)
  191. {
  192. SystemProxy.Update(_config, true);
  193. }
  194. }
  195. public void TouchPACFile()
  196. {
  197. string pacFilename = _pacServer.TouchPACFile();
  198. if (PACFileReadyToOpen != null)
  199. {
  200. PACFileReadyToOpen(this, new PathEventArgs() { Path = pacFilename });
  201. }
  202. }
  203. public void TouchUserRuleFile()
  204. {
  205. string userRuleFilename = _pacServer.TouchUserRuleFile();
  206. if (UserRuleFileReadyToOpen != null)
  207. {
  208. UserRuleFileReadyToOpen(this, new PathEventArgs() { Path = userRuleFilename });
  209. }
  210. }
  211. public string GetQRCodeForCurrentServer()
  212. {
  213. Server server = GetCurrentServer();
  214. return GetQRCode(server);
  215. }
  216. public static string GetQRCode(Server server)
  217. {
  218. string parts = server.method + ":" + server.password + "@" + server.server + ":" + server.server_port;
  219. string base64 = System.Convert.ToBase64String(Encoding.UTF8.GetBytes(parts));
  220. return "ss://" + base64;
  221. }
  222. public void UpdatePACFromGFWList()
  223. {
  224. if (gfwListUpdater != null)
  225. {
  226. gfwListUpdater.UpdatePACFromGFWList(_config);
  227. }
  228. }
  229. public void UpdateStatisticsConfiguration(bool enabled)
  230. {
  231. if (availabilityStatistics == null) return;
  232. availabilityStatistics.UpdateConfiguration(_config, StatisticsConfiguration);
  233. _config.availabilityStatistics = enabled;
  234. SaveConfig(_config);
  235. }
  236. public void SavePACUrl(string pacUrl)
  237. {
  238. _config.pacUrl = pacUrl;
  239. UpdateSystemProxy();
  240. SaveConfig(_config);
  241. if (ConfigChanged != null)
  242. {
  243. ConfigChanged(this, new EventArgs());
  244. }
  245. }
  246. public void UseOnlinePAC(bool useOnlinePac)
  247. {
  248. _config.useOnlinePac = useOnlinePac;
  249. UpdateSystemProxy();
  250. SaveConfig(_config);
  251. if (ConfigChanged != null)
  252. {
  253. ConfigChanged(this, new EventArgs());
  254. }
  255. }
  256. public void ToggleCheckingUpdate(bool enabled)
  257. {
  258. _config.autoCheckUpdate = enabled;
  259. Configuration.Save(_config);
  260. }
  261. public void SaveLogViewerConfig(LogViewerConfig newConfig)
  262. {
  263. _config.logViewer = newConfig;
  264. Configuration.Save(_config);
  265. }
  266. public void UpdateInboundCounter(long n)
  267. {
  268. inboundCounter += n;
  269. }
  270. public void UpdateOutboundCounter(long n)
  271. {
  272. outboundCounter += n;
  273. }
  274. protected void Reload()
  275. {
  276. // some logic in configuration updated the config when saving, we need to read it again
  277. _config = Configuration.Load();
  278. StatisticsConfiguration = StatisticsStrategyConfiguration.Load();
  279. if (polipoRunner == null)
  280. {
  281. polipoRunner = new PolipoRunner();
  282. }
  283. if (_pacServer == null)
  284. {
  285. _pacServer = new PACServer();
  286. _pacServer.PACFileChanged += pacServer_PACFileChanged;
  287. _pacServer.UserRuleFileChanged += pacServer_UserRuleFileChanged;
  288. }
  289. _pacServer.UpdateConfiguration(_config);
  290. if (gfwListUpdater == null)
  291. {
  292. gfwListUpdater = new GFWListUpdater();
  293. gfwListUpdater.UpdateCompleted += pacServer_PACUpdateCompleted;
  294. gfwListUpdater.Error += pacServer_PACUpdateError;
  295. }
  296. if (availabilityStatistics == null)
  297. {
  298. availabilityStatistics = new AvailabilityStatistics(_config, StatisticsConfiguration);
  299. }
  300. availabilityStatistics.UpdateConfiguration(_config, StatisticsConfiguration);
  301. if (_listener != null)
  302. {
  303. _listener.Stop();
  304. }
  305. // don't put polipoRunner.Start() before pacServer.Stop()
  306. // or bind will fail when switching bind address from 0.0.0.0 to 127.0.0.1
  307. // though UseShellExecute is set to true now
  308. // http://stackoverflow.com/questions/10235093/socket-doesnt-close-after-application-exits-if-a-launched-process-is-open
  309. polipoRunner.Stop();
  310. try
  311. {
  312. var strategy = GetCurrentStrategy();
  313. if (strategy != null)
  314. {
  315. strategy.ReloadServers();
  316. }
  317. polipoRunner.Start(_config);
  318. TCPRelay tcpRelay = new TCPRelay(this);
  319. UDPRelay udpRelay = new UDPRelay(this);
  320. List<Listener.Service> services = new List<Listener.Service>();
  321. services.Add(tcpRelay);
  322. services.Add(udpRelay);
  323. services.Add(_pacServer);
  324. services.Add(new PortForwarder(polipoRunner.RunningPort));
  325. _listener = new Listener(services);
  326. _listener.Start(_config);
  327. }
  328. catch (Exception e)
  329. {
  330. // translate Microsoft language into human language
  331. // i.e. An attempt was made to access a socket in a way forbidden by its access permissions => Port already in use
  332. if (e is SocketException)
  333. {
  334. SocketException se = (SocketException)e;
  335. if (se.SocketErrorCode == SocketError.AccessDenied)
  336. {
  337. e = new Exception(I18N.GetString("Port already in use"), e);
  338. }
  339. }
  340. Logging.LogUsefulException(e);
  341. ReportError(e);
  342. }
  343. if (ConfigChanged != null)
  344. {
  345. ConfigChanged(this, new EventArgs());
  346. }
  347. UpdateSystemProxy();
  348. Util.Utils.ReleaseMemory(true);
  349. }
  350. protected void SaveConfig(Configuration newConfig)
  351. {
  352. Configuration.Save(newConfig);
  353. Reload();
  354. }
  355. private void UpdateSystemProxy()
  356. {
  357. if (_config.enabled)
  358. {
  359. SystemProxy.Update(_config, false);
  360. _systemProxyIsDirty = true;
  361. }
  362. else
  363. {
  364. // only switch it off if we have switched it on
  365. if (_systemProxyIsDirty)
  366. {
  367. SystemProxy.Update(_config, false);
  368. _systemProxyIsDirty = false;
  369. }
  370. }
  371. }
  372. private void pacServer_PACFileChanged(object sender, EventArgs e)
  373. {
  374. UpdateSystemProxy();
  375. }
  376. private void pacServer_PACUpdateCompleted(object sender, GFWListUpdater.ResultEventArgs e)
  377. {
  378. if (UpdatePACFromGFWListCompleted != null)
  379. UpdatePACFromGFWListCompleted(this, e);
  380. }
  381. private void pacServer_PACUpdateError(object sender, ErrorEventArgs e)
  382. {
  383. if (UpdatePACFromGFWListError != null)
  384. UpdatePACFromGFWListError(this, e);
  385. }
  386. private void pacServer_UserRuleFileChanged(object sender, EventArgs e)
  387. {
  388. // TODO: this is a dirty hack. (from code GListUpdater.http_DownloadStringCompleted())
  389. if (!File.Exists(Utils.GetTempPath("gfwlist.txt")))
  390. {
  391. UpdatePACFromGFWList();
  392. return;
  393. }
  394. List<string> lines = GFWListUpdater.ParseResult(File.ReadAllText(Utils.GetTempPath("gfwlist.txt")));
  395. if (File.Exists(PACServer.USER_RULE_FILE))
  396. {
  397. string local = File.ReadAllText(PACServer.USER_RULE_FILE, Encoding.UTF8);
  398. string[] rules = local.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
  399. foreach (string rule in rules)
  400. {
  401. if (rule.StartsWith("!") || rule.StartsWith("["))
  402. continue;
  403. lines.Add(rule);
  404. }
  405. }
  406. string abpContent = Utils.UnGzip(Resources.abp_js);
  407. abpContent = abpContent.Replace("__RULES__", SimpleJson.SimpleJson.SerializeObject(lines));
  408. if (File.Exists(PACServer.PAC_FILE))
  409. {
  410. string original = File.ReadAllText(PACServer.PAC_FILE, Encoding.UTF8);
  411. if (original == abpContent)
  412. {
  413. return;
  414. }
  415. }
  416. File.WriteAllText(PACServer.PAC_FILE, abpContent, Encoding.UTF8);
  417. }
  418. private void StartReleasingMemory()
  419. {
  420. _ramThread = new Thread(new ThreadStart(ReleaseMemory));
  421. _ramThread.IsBackground = true;
  422. _ramThread.Start();
  423. }
  424. private void ReleaseMemory()
  425. {
  426. while (true)
  427. {
  428. Util.Utils.ReleaseMemory(false);
  429. Thread.Sleep(30 * 1000);
  430. }
  431. }
  432. }
  433. }