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 4.1 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using Shadowsocks.Model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Shadowsocks.Controller
  6. {
  7. public class ShadowsocksController
  8. {
  9. // controller:
  10. // handle user actions
  11. // manipulates UI
  12. // interacts with low level logic
  13. private Local local;
  14. private PACServer pacServer;
  15. private Configuration _config;
  16. private PolipoRunner polipoRunner;
  17. private bool stopped = false;
  18. public class PathEventArgs : EventArgs
  19. {
  20. public string Path;
  21. }
  22. public event EventHandler ConfigChanged;
  23. public event EventHandler EnableStatusChanged;
  24. // when user clicked Edit PAC, and PAC file has already created
  25. public event EventHandler<PathEventArgs> PACFileReadyToOpen;
  26. public ShadowsocksController()
  27. {
  28. _config = Configuration.Load();
  29. polipoRunner = new PolipoRunner();
  30. polipoRunner.Start(_config.GetCurrentServer());
  31. local = new Local(_config.GetCurrentServer());
  32. try
  33. {
  34. local.Start();
  35. pacServer = new PACServer();
  36. pacServer.PACFileChanged += pacServer_PACFileChanged;
  37. pacServer.Start();
  38. }
  39. catch (Exception e)
  40. {
  41. Console.WriteLine(e.Message);
  42. }
  43. UpdateSystemProxy();
  44. }
  45. public void SaveConfig(Configuration newConfig)
  46. {
  47. Configuration.Save(newConfig);
  48. // some logic in configuration updated the config when saving, we need to read it again
  49. _config = Configuration.Load();
  50. local.Stop();
  51. polipoRunner.Stop();
  52. polipoRunner.Start(_config.GetCurrentServer());
  53. local = new Local(_config.GetCurrentServer());
  54. local.Start();
  55. if (ConfigChanged != null)
  56. {
  57. ConfigChanged(this, new EventArgs());
  58. }
  59. }
  60. public Server GetCurrentServer()
  61. {
  62. return _config.GetCurrentServer();
  63. }
  64. // always return copy
  65. public Configuration GetConfiguration()
  66. {
  67. return Configuration.Load();
  68. }
  69. public void ToggleEnable(bool enabled)
  70. {
  71. _config.enabled = enabled;
  72. UpdateSystemProxy();
  73. SaveConfig(_config);
  74. if (EnableStatusChanged != null)
  75. {
  76. EnableStatusChanged(this, new EventArgs());
  77. }
  78. }
  79. public void Stop()
  80. {
  81. if (stopped)
  82. {
  83. return;
  84. }
  85. stopped = true;
  86. local.Stop();
  87. polipoRunner.Stop();
  88. if (_config.enabled)
  89. {
  90. SystemProxy.Disable();
  91. }
  92. }
  93. public void TouchPACFile()
  94. {
  95. string pacFilename = pacServer.TouchPACFile();
  96. if (PACFileReadyToOpen != null)
  97. {
  98. PACFileReadyToOpen(this, new PathEventArgs() { Path = pacFilename });
  99. }
  100. }
  101. public string GetQRCodeForCurrentServer()
  102. {
  103. Server server = GetCurrentServer();
  104. string parts = server.method + ":" + server.password + "@" + server.server + ":" + server.server_port;
  105. string base64 = System.Convert.ToBase64String(Encoding.UTF8.GetBytes(parts));
  106. return "ss://" + base64;
  107. }
  108. private void UpdateSystemProxy()
  109. {
  110. if (_config.enabled)
  111. {
  112. SystemProxy.Enable();
  113. }
  114. else
  115. {
  116. SystemProxy.Disable();
  117. }
  118. }
  119. private void pacServer_PACFileChanged(object sender, EventArgs e)
  120. {
  121. UpdateSystemProxy();
  122. }
  123. }
  124. }