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.

ConfigForm.cs 4.5 kB

12 years ago
12 years ago
10 years ago
12 years ago
10 years ago
10 years ago
10 years ago
12 years ago
12 years ago
10 years ago
10 years ago
12 years ago
10 years ago
10 years ago
10 years ago
10 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. using System.Diagnostics;
  8. namespace shadowsocks_csharp
  9. {
  10. public partial class ConfigForm : Form
  11. {
  12. Local local;
  13. PACServer pacServer;
  14. Config config;
  15. PolipoRunner polipoRunner;
  16. public ConfigForm()
  17. {
  18. config = Config.Load();
  19. InitializeComponent();
  20. notifyIcon1.ContextMenu = contextMenu1;
  21. enableItem.Checked = config.enabled;
  22. configToTextBox();
  23. }
  24. private void showWindow()
  25. {
  26. this.Opacity = 1;
  27. this.Show();
  28. }
  29. private void configToTextBox()
  30. {
  31. textBox1.Text = config.server;
  32. textBox2.Text = config.server_port.ToString();
  33. textBox3.Text = config.password;
  34. textBox4.Text = config.local_port.ToString();
  35. comboBox1.Text = config.method == null ? "table" : config.method;
  36. }
  37. private void Form1_Load(object sender, EventArgs e)
  38. {
  39. if (!config.isDefault)
  40. {
  41. this.Opacity = 0;
  42. reload(config); BeginInvoke(new MethodInvoker(delegate
  43. {
  44. this.Hide();
  45. }));
  46. }
  47. pacServer = new PACServer();
  48. pacServer.Start();
  49. updateSystemProxy();
  50. }
  51. private void reload(Config config)
  52. {
  53. if (local != null)
  54. {
  55. local.Stop();
  56. if (polipoRunner != null)
  57. {
  58. polipoRunner.Stop();
  59. }
  60. }
  61. if (polipoRunner == null)
  62. {
  63. polipoRunner = new PolipoRunner();
  64. }
  65. polipoRunner.Start(config);
  66. local = new Local(config);
  67. local.Start();
  68. }
  69. private void Config_Click(object sender, EventArgs e)
  70. {
  71. showWindow();
  72. }
  73. private void Quit_Click(object sender, EventArgs e)
  74. {
  75. this.Close();
  76. }
  77. private void OKButton_Click(object sender, EventArgs e)
  78. {
  79. try
  80. {
  81. Config config = new Config
  82. {
  83. server = textBox1.Text,
  84. server_port = int.Parse(textBox2.Text),
  85. password = textBox3.Text,
  86. local_port = int.Parse(textBox4.Text),
  87. method = comboBox1.Text,
  88. isDefault = false
  89. };
  90. Config.Save(config);
  91. this.config = config;
  92. reload(config);
  93. this.Hide();
  94. }
  95. catch (FormatException)
  96. {
  97. MessageBox.Show("there is format problem");
  98. }
  99. catch (Exception ex)
  100. {
  101. MessageBox.Show(ex.ToString());
  102. }
  103. }
  104. private void cancelButton_Click(object sender, EventArgs e)
  105. {
  106. this.Hide();
  107. configToTextBox();
  108. }
  109. private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  110. {
  111. if (local != null) local.Stop();
  112. if (polipoRunner != null) polipoRunner.Stop();
  113. if (config.enabled)
  114. {
  115. SystemProxy.Disable();
  116. }
  117. }
  118. private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
  119. {
  120. Process.Start("https://github.com/clowwindy/shadowsocks-csharp");
  121. }
  122. private void notifyIcon1_DoubleClick(object sender, EventArgs e)
  123. {
  124. showWindow();
  125. }
  126. private void updateSystemProxy()
  127. {
  128. if (config.enabled)
  129. {
  130. SystemProxy.Enable();
  131. }
  132. else
  133. {
  134. SystemProxy.Disable();
  135. }
  136. }
  137. private void EnableItem_Click(object sender, EventArgs e)
  138. {
  139. enableItem.Checked = !enableItem.Checked;
  140. config.enabled = enableItem.Checked;
  141. Config.Save(config);
  142. updateSystemProxy();
  143. }
  144. }
  145. }