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

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Web;
  10. using System.Windows.Forms;
  11. using Newtonsoft.Json;
  12. using Shadowsocks.Controller.Strategy;
  13. using Shadowsocks.Model;
  14. using Shadowsocks.Properties;
  15. using Shadowsocks.Util;
  16. using System.Linq;
  17. using Shadowsocks.Controller.Service;
  18. using Shadowsocks.Proxy;
  19. namespace Shadowsocks.Controller
  20. {
  21. public class ShadowsocksController
  22. {
  23. // controller:
  24. // handle user actions
  25. // manipulates UI
  26. // interacts with low level logic
  27. private Thread _ramThread;
  28. private Thread _trafficThread;
  29. private Listener _listener;
  30. private PACServer _pacServer;
  31. private Configuration _config;
  32. private StrategyManager _strategyManager;
  33. private PrivoxyRunner privoxyRunner;
  34. private GFWListUpdater gfwListUpdater;
  35. private readonly ConcurrentDictionary<Server, Sip003Plugin> _pluginsByServer;
  36. public AvailabilityStatistics availabilityStatistics = AvailabilityStatistics.Instance;
  37. public StatisticsStrategyConfiguration StatisticsConfiguration { get; private set; }
  38. private long _inboundCounter = 0;
  39. private long _outboundCounter = 0;
  40. public long InboundCounter => Interlocked.Read(ref _inboundCounter);
  41. public long OutboundCounter => Interlocked.Read(ref _outboundCounter);
  42. public Queue<TrafficPerSecond> trafficPerSecondQueue;
  43. private bool stopped = false;
  44. public class PathEventArgs : EventArgs
  45. {
  46. public string Path;
  47. }
  48. public class TrafficPerSecond
  49. {
  50. public long inboundCounter;
  51. public long outboundCounter;
  52. public long inboundIncreasement;
  53. public long outboundIncreasement;
  54. }
  55. public event EventHandler ConfigChanged;
  56. public event EventHandler EnableStatusChanged;
  57. public event EventHandler EnableGlobalChanged;
  58. public event EventHandler ShareOverLANStatusChanged;
  59. public event EventHandler VerboseLoggingStatusChanged;
  60. public event EventHandler TrafficChanged;
  61. // when user clicked Edit PAC, and PAC file has already created
  62. public event EventHandler<PathEventArgs> PACFileReadyToOpen;
  63. public event EventHandler<PathEventArgs> UserRuleFileReadyToOpen;
  64. public event EventHandler<GFWListUpdater.ResultEventArgs> UpdatePACFromGFWListCompleted;
  65. public event ErrorEventHandler UpdatePACFromGFWListError;
  66. public event ErrorEventHandler Errored;
  67. public ShadowsocksController()
  68. {
  69. _config = Configuration.Load();
  70. StatisticsConfiguration = StatisticsStrategyConfiguration.Load();
  71. _strategyManager = new StrategyManager(this);
  72. _pluginsByServer = new ConcurrentDictionary<Server, Sip003Plugin>();
  73. StartReleasingMemory();
  74. StartTrafficStatistics(61);
  75. }
  76. public void Start(bool regHotkeys = true)
  77. {
  78. Reload();
  79. if (regHotkeys)
  80. {
  81. HotkeyReg.RegAllHotkeys();
  82. }
  83. }
  84. protected void ReportError(Exception e)
  85. {
  86. if (Errored != null)
  87. {
  88. Errored(this, new ErrorEventArgs(e));
  89. }
  90. }
  91. public Server GetCurrentServer()
  92. {
  93. return _config.GetCurrentServer();
  94. }
  95. // always return copy
  96. public Configuration GetConfigurationCopy()
  97. {
  98. return Configuration.Load();
  99. }
  100. // always return current instance
  101. public Configuration GetCurrentConfiguration()
  102. {
  103. return _config;
  104. }
  105. public IList<IStrategy> GetStrategies()
  106. {
  107. return _strategyManager.GetStrategies();
  108. }
  109. public IStrategy GetCurrentStrategy()
  110. {
  111. foreach (var strategy in _strategyManager.GetStrategies())
  112. {
  113. if (strategy.ID == this._config.strategy)
  114. {
  115. return strategy;
  116. }
  117. }
  118. return null;
  119. }
  120. public Server GetAServer(IStrategyCallerType type, IPEndPoint localIPEndPoint, EndPoint destEndPoint)
  121. {
  122. IStrategy strategy = GetCurrentStrategy();
  123. if (strategy != null)
  124. {
  125. return strategy.GetAServer(type, localIPEndPoint, destEndPoint);
  126. }
  127. if (_config.index < 0)
  128. {
  129. _config.index = 0;
  130. }
  131. return GetCurrentServer();
  132. }
  133. public EndPoint GetPluginLocalEndPointIfConfigured(Server server)
  134. {
  135. var plugin = _pluginsByServer.GetOrAdd(server, Sip003Plugin.CreateIfConfigured);
  136. if (plugin == null)
  137. {
  138. return null;
  139. }
  140. try
  141. {
  142. if (plugin.StartIfNeeded())
  143. {
  144. Logging.Info(
  145. $"Started SIP003 plugin for {server.Identifier()} on {plugin.LocalEndPoint} - PID: {plugin.ProcessId}");
  146. }
  147. }
  148. catch (Exception ex)
  149. {
  150. Logging.Error("Failed to start SIP003 plugin: " + ex.Message);
  151. throw;
  152. }
  153. return plugin.LocalEndPoint;
  154. }
  155. public void SaveServers(List<Server> servers, int localPort, bool portableMode)
  156. {
  157. _config.configs = servers;
  158. _config.localPort = localPort;
  159. _config.portableMode = portableMode;
  160. Configuration.Save(_config);
  161. }
  162. public void SaveStrategyConfigurations(StatisticsStrategyConfiguration configuration)
  163. {
  164. StatisticsConfiguration = configuration;
  165. StatisticsStrategyConfiguration.Save(configuration);
  166. }
  167. public bool AddServerBySSURL(string ssURL)
  168. {
  169. try
  170. {
  171. if (ssURL.IsNullOrEmpty() || ssURL.IsWhiteSpace()) return false;
  172. var servers = Server.GetServers(ssURL);
  173. if (servers == null || servers.Count == 0) return false;
  174. foreach (var server in servers)
  175. {
  176. _config.configs.Add(server);
  177. }
  178. _config.index = _config.configs.Count - 1;
  179. SaveConfig(_config);
  180. return true;
  181. }
  182. catch (Exception e)
  183. {
  184. Logging.LogUsefulException(e);
  185. return false;
  186. }
  187. }
  188. public void ToggleEnable(bool enabled)
  189. {
  190. _config.enabled = enabled;
  191. SaveConfig(_config);
  192. if (EnableStatusChanged != null)
  193. {
  194. EnableStatusChanged(this, new EventArgs());
  195. }
  196. }
  197. public void ToggleGlobal(bool global)
  198. {
  199. _config.global = global;
  200. SaveConfig(_config);
  201. if (EnableGlobalChanged != null)
  202. {
  203. EnableGlobalChanged(this, new EventArgs());
  204. }
  205. }
  206. public void ToggleShareOverLAN(bool enabled)
  207. {
  208. _config.shareOverLan = enabled;
  209. SaveConfig(_config);
  210. if (ShareOverLANStatusChanged != null)
  211. {
  212. ShareOverLANStatusChanged(this, new EventArgs());
  213. }
  214. }
  215. public void SaveProxy(ProxyConfig proxyConfig)
  216. {
  217. _config.proxy = proxyConfig;
  218. SaveConfig(_config);
  219. }
  220. public void ToggleVerboseLogging(bool enabled)
  221. {
  222. _config.isVerboseLogging = enabled;
  223. SaveConfig(_config);
  224. if (VerboseLoggingStatusChanged != null)
  225. {
  226. VerboseLoggingStatusChanged(this, new EventArgs());
  227. }
  228. }
  229. public void SelectServerIndex(int index)
  230. {
  231. _config.index = index;
  232. _config.strategy = null;
  233. SaveConfig(_config);
  234. }
  235. public void SelectStrategy(string strategyID)
  236. {
  237. _config.index = -1;
  238. _config.strategy = strategyID;
  239. SaveConfig(_config);
  240. }
  241. public void Stop()
  242. {
  243. if (stopped)
  244. {
  245. return;
  246. }
  247. stopped = true;
  248. if (_listener != null)
  249. {
  250. _listener.Stop();
  251. }
  252. StopPlugins();
  253. if (privoxyRunner != null)
  254. {
  255. privoxyRunner.Stop();
  256. }
  257. if (_config.enabled)
  258. {
  259. SystemProxy.Update(_config, true, null);
  260. }
  261. Encryption.RNG.Close();
  262. }
  263. private void StopPlugins()
  264. {
  265. foreach (var serverAndPlugin in _pluginsByServer)
  266. {
  267. serverAndPlugin.Value?.Dispose();
  268. }
  269. _pluginsByServer.Clear();
  270. }
  271. public void TouchPACFile()
  272. {
  273. string pacFilename = _pacServer.TouchPACFile();
  274. if (PACFileReadyToOpen != null)
  275. {
  276. PACFileReadyToOpen(this, new PathEventArgs() { Path = pacFilename });
  277. }
  278. }
  279. public void TouchUserRuleFile()
  280. {
  281. string userRuleFilename = _pacServer.TouchUserRuleFile();
  282. if (UserRuleFileReadyToOpen != null)
  283. {
  284. UserRuleFileReadyToOpen(this, new PathEventArgs() { Path = userRuleFilename });
  285. }
  286. }
  287. public string GetServerURLForCurrentServer()
  288. {
  289. Server server = GetCurrentServer();
  290. return GetServerURL(server);
  291. }
  292. public static string GetServerURL(Server server)
  293. {
  294. string tag = string.Empty;
  295. string url = string.Empty;
  296. if (string.IsNullOrWhiteSpace(server.plugin))
  297. {
  298. // For backwards compatiblity, if no plugin, use old url format
  299. string parts = $"{server.method}:{server.password}@{server.server}:{server.server_port}";
  300. string base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(parts));
  301. url = base64;
  302. }
  303. else
  304. {
  305. // SIP002
  306. string parts = $"{server.method}:{server.password}";
  307. string base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(parts));
  308. string websafeBase64 = base64.Replace('+', '-').Replace('/', '_').TrimEnd('=');
  309. string pluginPart = server.plugin;
  310. if (!string.IsNullOrWhiteSpace(server.plugin_opts))
  311. {
  312. pluginPart += ";" + server.plugin_opts;
  313. }
  314. url = string.Format(
  315. "{0}@{1}:{2}/?plugin={3}",
  316. websafeBase64,
  317. server.FormatHostName(server.server),
  318. server.server_port,
  319. HttpUtility.UrlEncode(pluginPart, Encoding.UTF8));
  320. }
  321. if (!server.remarks.IsNullOrEmpty())
  322. {
  323. tag = $"#{HttpUtility.UrlEncode(server.remarks, Encoding.UTF8)}";
  324. }
  325. return $"ss://{url}{tag}";
  326. }
  327. public void UpdatePACFromGFWList()
  328. {
  329. if (gfwListUpdater != null)
  330. {
  331. gfwListUpdater.UpdatePACFromGFWList(_config);
  332. }
  333. }
  334. public void UpdateStatisticsConfiguration(bool enabled)
  335. {
  336. if (availabilityStatistics == null) return;
  337. availabilityStatistics.UpdateConfiguration(this);
  338. _config.availabilityStatistics = enabled;
  339. SaveConfig(_config);
  340. }
  341. public void SavePACUrl(string pacUrl)
  342. {
  343. _config.pacUrl = pacUrl;
  344. SaveConfig(_config);
  345. if (ConfigChanged != null)
  346. {
  347. ConfigChanged(this, new EventArgs());
  348. }
  349. }
  350. public void UseOnlinePAC(bool useOnlinePac)
  351. {
  352. _config.useOnlinePac = useOnlinePac;
  353. SaveConfig(_config);
  354. if (ConfigChanged != null)
  355. {
  356. ConfigChanged(this, new EventArgs());
  357. }
  358. }
  359. public void ToggleSecureLocalPac(bool enabled)
  360. {
  361. _config.secureLocalPac = enabled;
  362. SaveConfig(_config);
  363. if (ConfigChanged != null)
  364. {
  365. ConfigChanged(this, new EventArgs());
  366. }
  367. }
  368. public void ToggleCheckingUpdate(bool enabled)
  369. {
  370. _config.autoCheckUpdate = enabled;
  371. Configuration.Save(_config);
  372. if (ConfigChanged != null)
  373. {
  374. ConfigChanged(this, new EventArgs());
  375. }
  376. }
  377. public void ToggleCheckingPreRelease(bool enabled)
  378. {
  379. _config.checkPreRelease = enabled;
  380. Configuration.Save(_config);
  381. if (ConfigChanged != null)
  382. {
  383. ConfigChanged(this, new EventArgs());
  384. }
  385. }
  386. public void SaveLogViewerConfig(LogViewerConfig newConfig)
  387. {
  388. _config.logViewer = newConfig;
  389. newConfig.SaveSize();
  390. Configuration.Save(_config);
  391. if (ConfigChanged != null)
  392. {
  393. ConfigChanged(this, new EventArgs());
  394. }
  395. }
  396. public void SaveHotkeyConfig(HotkeyConfig newConfig)
  397. {
  398. _config.hotkey = newConfig;
  399. SaveConfig(_config);
  400. if (ConfigChanged != null)
  401. {
  402. ConfigChanged(this, new EventArgs());
  403. }
  404. }
  405. public void UpdateLatency(Server server, TimeSpan latency)
  406. {
  407. if (_config.availabilityStatistics)
  408. {
  409. availabilityStatistics.UpdateLatency(server, (int)latency.TotalMilliseconds);
  410. }
  411. }
  412. public void UpdateInboundCounter(Server server, long n)
  413. {
  414. Interlocked.Add(ref _inboundCounter, n);
  415. if (_config.availabilityStatistics)
  416. {
  417. availabilityStatistics.UpdateInboundCounter(server, n);
  418. }
  419. }
  420. public void UpdateOutboundCounter(Server server, long n)
  421. {
  422. Interlocked.Add(ref _outboundCounter, n);
  423. if (_config.availabilityStatistics)
  424. {
  425. availabilityStatistics.UpdateOutboundCounter(server, n);
  426. }
  427. }
  428. protected void Reload()
  429. {
  430. Encryption.RNG.Reload();
  431. // some logic in configuration updated the config when saving, we need to read it again
  432. _config = Configuration.Load();
  433. StatisticsConfiguration = StatisticsStrategyConfiguration.Load();
  434. if (privoxyRunner == null)
  435. {
  436. privoxyRunner = new PrivoxyRunner();
  437. }
  438. if (_pacServer == null)
  439. {
  440. _pacServer = new PACServer();
  441. _pacServer.PACFileChanged += pacServer_PACFileChanged;
  442. _pacServer.UserRuleFileChanged += pacServer_UserRuleFileChanged;
  443. }
  444. _pacServer.UpdateConfiguration(_config);
  445. if (gfwListUpdater == null)
  446. {
  447. gfwListUpdater = new GFWListUpdater();
  448. gfwListUpdater.UpdateCompleted += pacServer_PACUpdateCompleted;
  449. gfwListUpdater.Error += pacServer_PACUpdateError;
  450. }
  451. availabilityStatistics.UpdateConfiguration(this);
  452. if (_listener != null)
  453. {
  454. _listener.Stop();
  455. }
  456. StopPlugins();
  457. // don't put PrivoxyRunner.Start() before pacServer.Stop()
  458. // or bind will fail when switching bind address from 0.0.0.0 to 127.0.0.1
  459. // though UseShellExecute is set to true now
  460. // http://stackoverflow.com/questions/10235093/socket-doesnt-close-after-application-exits-if-a-launched-process-is-open
  461. privoxyRunner.Stop();
  462. try
  463. {
  464. var strategy = GetCurrentStrategy();
  465. if (strategy != null)
  466. {
  467. strategy.ReloadServers();
  468. }
  469. StartPlugin();
  470. privoxyRunner.Start(_config);
  471. TCPRelay tcpRelay = new TCPRelay(this, _config);
  472. UDPRelay udpRelay = new UDPRelay(this);
  473. List<Listener.IService> services = new List<Listener.IService>();
  474. services.Add(tcpRelay);
  475. services.Add(udpRelay);
  476. services.Add(_pacServer);
  477. services.Add(new PortForwarder(privoxyRunner.RunningPort));
  478. _listener = new Listener(services);
  479. _listener.Start(_config);
  480. }
  481. catch (Exception e)
  482. {
  483. // translate Microsoft language into human language
  484. // i.e. An attempt was made to access a socket in a way forbidden by its access permissions => Port already in use
  485. if (e is SocketException)
  486. {
  487. SocketException se = (SocketException)e;
  488. if (se.SocketErrorCode == SocketError.AccessDenied)
  489. {
  490. e = new Exception(I18N.GetString("Port already in use"), e);
  491. }
  492. }
  493. Logging.LogUsefulException(e);
  494. ReportError(e);
  495. }
  496. if (ConfigChanged != null)
  497. {
  498. ConfigChanged(this, new EventArgs());
  499. }
  500. UpdateSystemProxy();
  501. Utils.ReleaseMemory(true);
  502. }
  503. private void StartPlugin()
  504. {
  505. var server = _config.GetCurrentServer();
  506. GetPluginLocalEndPointIfConfigured(server);
  507. }
  508. protected void SaveConfig(Configuration newConfig)
  509. {
  510. Configuration.Save(newConfig);
  511. Reload();
  512. }
  513. private void UpdateSystemProxy()
  514. {
  515. SystemProxy.Update(_config, false, _pacServer);
  516. }
  517. private void pacServer_PACFileChanged(object sender, EventArgs e)
  518. {
  519. UpdateSystemProxy();
  520. }
  521. private void pacServer_PACUpdateCompleted(object sender, GFWListUpdater.ResultEventArgs e)
  522. {
  523. if (UpdatePACFromGFWListCompleted != null)
  524. UpdatePACFromGFWListCompleted(this, e);
  525. }
  526. private void pacServer_PACUpdateError(object sender, ErrorEventArgs e)
  527. {
  528. if (UpdatePACFromGFWListError != null)
  529. UpdatePACFromGFWListError(this, e);
  530. }
  531. private static readonly IEnumerable<char> IgnoredLineBegins = new[] { '!', '[' };
  532. private void pacServer_UserRuleFileChanged(object sender, EventArgs e)
  533. {
  534. if (!File.Exists(Utils.GetTempPath("gfwlist.txt")))
  535. {
  536. UpdatePACFromGFWList();
  537. }
  538. else
  539. {
  540. GFWListUpdater.MergeAndWritePACFile(FileManager.NonExclusiveReadAllText(Utils.GetTempPath("gfwlist.txt")));
  541. }
  542. UpdateSystemProxy();
  543. }
  544. public void CopyPacUrl()
  545. {
  546. Clipboard.SetDataObject(_pacServer.PacUrl);
  547. }
  548. #region Memory Management
  549. private void StartReleasingMemory()
  550. {
  551. _ramThread = new Thread(new ThreadStart(ReleaseMemory));
  552. _ramThread.IsBackground = true;
  553. _ramThread.Start();
  554. }
  555. private void ReleaseMemory()
  556. {
  557. while (true)
  558. {
  559. Utils.ReleaseMemory(false);
  560. Thread.Sleep(30 * 1000);
  561. }
  562. }
  563. #endregion
  564. #region Traffic Statistics
  565. private void StartTrafficStatistics(int queueMaxSize)
  566. {
  567. trafficPerSecondQueue = new Queue<TrafficPerSecond>();
  568. for (int i = 0; i < queueMaxSize; i++)
  569. {
  570. trafficPerSecondQueue.Enqueue(new TrafficPerSecond());
  571. }
  572. _trafficThread = new Thread(new ThreadStart(() => TrafficStatistics(queueMaxSize)));
  573. _trafficThread.IsBackground = true;
  574. _trafficThread.Start();
  575. }
  576. private void TrafficStatistics(int queueMaxSize)
  577. {
  578. TrafficPerSecond previous, current;
  579. while (true)
  580. {
  581. previous = trafficPerSecondQueue.Last();
  582. current = new TrafficPerSecond();
  583. current.inboundCounter = InboundCounter;
  584. current.outboundCounter = OutboundCounter;
  585. current.inboundIncreasement = current.inboundCounter - previous.inboundCounter;
  586. current.outboundIncreasement = current.outboundCounter - previous.outboundCounter;
  587. trafficPerSecondQueue.Enqueue(current);
  588. if (trafficPerSecondQueue.Count > queueMaxSize)
  589. trafficPerSecondQueue.Dequeue();
  590. TrafficChanged?.Invoke(this, new EventArgs());
  591. Thread.Sleep(1000);
  592. }
  593. }
  594. #endregion
  595. }
  596. }