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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace shadowsocks_csharp
  5. {
  6. public class ShadowsocksController
  7. {
  8. // controller:
  9. // handle user actions
  10. // manipulates UI
  11. // interacts with low level logic
  12. private Local local;
  13. private PACServer pacServer;
  14. private Config config;
  15. private PolipoRunner polipoRunner;
  16. private bool stopped = false;
  17. public event EventHandler ConfigChanged;
  18. public event EventHandler EnableStatusChanged;
  19. public ShadowsocksController()
  20. {
  21. config = Config.Load();
  22. polipoRunner = new PolipoRunner();
  23. polipoRunner.Start(config);
  24. local = new Local(config);
  25. local.Start();
  26. pacServer = new PACServer();
  27. pacServer.Start();
  28. updateSystemProxy();
  29. }
  30. public void SaveConfig(Config newConfig)
  31. {
  32. Config.Save(newConfig);
  33. config = newConfig;
  34. if (ConfigChanged != null)
  35. {
  36. ConfigChanged(this, new EventArgs());
  37. }
  38. }
  39. public Config GetConfig()
  40. {
  41. return config;
  42. }
  43. public void ToggleEnable(bool enabled)
  44. {
  45. config.enabled = enabled;
  46. updateSystemProxy();
  47. SaveConfig(config);
  48. if (EnableStatusChanged != null)
  49. {
  50. EnableStatusChanged(this, new EventArgs());
  51. }
  52. }
  53. public void Stop()
  54. {
  55. if (stopped)
  56. {
  57. return;
  58. }
  59. stopped = true;
  60. local.Stop();
  61. polipoRunner.Stop();
  62. if (config.enabled)
  63. {
  64. SystemProxy.Disable();
  65. }
  66. }
  67. private void updateSystemProxy()
  68. {
  69. if (config.enabled)
  70. {
  71. SystemProxy.Enable();
  72. }
  73. else
  74. {
  75. SystemProxy.Disable();
  76. }
  77. }
  78. }
  79. }