diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index 643d68ab..b4c80379 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -191,6 +191,20 @@ namespace Shadowsocks.Controller } } + public void DisableProxy() + { + _config.useProxy = false; + SaveConfig(_config); + } + + public void EnableProxy(string proxy, int port) + { + _config.useProxy = true; + _config.proxyServer = proxy; + _config.proxyPort = port; + SaveConfig(_config); + } + public void SelectServerIndex(int index) { _config.index = index; diff --git a/shadowsocks-csharp/Data/cn.txt b/shadowsocks-csharp/Data/cn.txt index a1211fb4..644b2d24 100644 --- a/shadowsocks-csharp/Data/cn.txt +++ b/shadowsocks-csharp/Data/cn.txt @@ -12,6 +12,7 @@ Servers=服务器 Edit Servers...=编辑服务器... Statistics Config...=统计配置... Start on Boot=开机启动 +Proxy...=代理设置... Allow Clients from LAN=允许来自局域网的连接 Local PAC=使用本地 PAC Online PAC=使用在线 PAC @@ -50,6 +51,13 @@ New server=未配置的服务器 Move &Up=上移(&U) Move D&own=下移(&O) +# Proxy Form + +Edit Proxy=代理设置 +Use Proxy=使用代理 +Proxy Addr=代理地址 +Proxy Port=代理端口 + # Log Form &File=文件(&F) diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs index 10a42124..0df1e94e 100755 --- a/shadowsocks-csharp/Model/Configuration.cs +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -25,6 +25,9 @@ namespace Shadowsocks.Model public bool availabilityStatistics; public bool autoCheckUpdate; public LogViewerConfig logViewer; + public bool useProxy; + public string proxyServer; + public int proxyPort; private static string CONFIG_FILE = "gui-config.json"; @@ -128,7 +131,7 @@ namespace Shadowsocks.Model throw new ArgumentException(I18N.GetString("Password can not be blank")); } - private static void CheckServer(string server) + public static void CheckServer(string server) { if (server.IsNullOrEmpty()) throw new ArgumentException(I18N.GetString("Server IP can not be blank")); diff --git a/shadowsocks-csharp/View/MenuViewController.cs b/shadowsocks-csharp/View/MenuViewController.cs index fab3d729..d7bd99ff 100755 --- a/shadowsocks-csharp/View/MenuViewController.cs +++ b/shadowsocks-csharp/View/MenuViewController.cs @@ -45,7 +45,9 @@ namespace Shadowsocks.View private MenuItem editGFWUserRuleItem; private MenuItem editOnlinePACItem; private MenuItem autoCheckUpdatesToggleItem; + private MenuItem proxyItem; private ConfigForm configForm; + private ProxyForm proxyForm; private List logForms = new List(); private bool logFormsVisible = false; private string _urlToOpen; @@ -211,6 +213,7 @@ namespace Shadowsocks.View this.editGFWUserRuleItem = CreateMenuItem("Edit User Rule for GFWList...", new EventHandler(this.EditUserRuleFileForGFWListItem_Click)), this.editOnlinePACItem = CreateMenuItem("Edit Online PAC URL...", new EventHandler(this.UpdateOnlinePACURLItem_Click)), }), + this.proxyItem = CreateMenuItem("Proxy...", new EventHandler(this.proxyItem_Click)), new MenuItem("-"), this.AutoStartupItem = CreateMenuItem("Start on Boot", new EventHandler(this.AutoStartupItem_Click)), this.ShareOverLANItem = CreateMenuItem("Allow Clients from LAN", new EventHandler(this.ShareOverLANItem_Click)), @@ -377,6 +380,20 @@ namespace Shadowsocks.View } } + private void ShowProxyForm() + { + if (proxyForm != null) + { + proxyForm.Activate(); + } + else + { + proxyForm = new ProxyForm(controller); + proxyForm.Show(); + proxyForm.FormClosed += proxyForm_FormClosed; + } + } + private void ShowLogForms() { if (logForms.Count == 0) @@ -415,6 +432,12 @@ namespace Shadowsocks.View } } + void proxyForm_FormClosed(object sender, FormClosedEventArgs e) + { + proxyForm = null; + Utils.ReleaseMemory(true); + } + private void Config_Click(object sender, EventArgs e) { ShowConfigForm(); @@ -721,5 +744,10 @@ namespace Shadowsocks.View { updateChecker.CheckUpdate(controller.GetConfigurationCopy()); } + + private void proxyItem_Click(object sender, EventArgs e) + { + ShowProxyForm(); + } } } diff --git a/shadowsocks-csharp/View/ProxyForm.Designer.cs b/shadowsocks-csharp/View/ProxyForm.Designer.cs new file mode 100644 index 00000000..991da996 --- /dev/null +++ b/shadowsocks-csharp/View/ProxyForm.Designer.cs @@ -0,0 +1,218 @@ +namespace Shadowsocks.View +{ + partial class ProxyForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); + this.tableLayoutPanel3 = new System.Windows.Forms.TableLayoutPanel(); + this.MyCancelButton = new System.Windows.Forms.Button(); + this.OKButton = new System.Windows.Forms.Button(); + this.UseProxyCheckBox = new System.Windows.Forms.CheckBox(); + this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel(); + this.ProxyAddrLabel = new System.Windows.Forms.Label(); + this.ProxyServerTextBox = new System.Windows.Forms.TextBox(); + this.ProxyPortLable = new System.Windows.Forms.Label(); + this.ProxyPortTextBox = new System.Windows.Forms.TextBox(); + this.tableLayoutPanel1.SuspendLayout(); + this.tableLayoutPanel3.SuspendLayout(); + this.tableLayoutPanel2.SuspendLayout(); + this.SuspendLayout(); + // + // tableLayoutPanel1 + // + this.tableLayoutPanel1.AutoSize = true; + this.tableLayoutPanel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; + this.tableLayoutPanel1.ColumnCount = 1; + this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); + this.tableLayoutPanel1.Controls.Add(this.tableLayoutPanel3, 0, 2); + this.tableLayoutPanel1.Controls.Add(this.UseProxyCheckBox, 0, 0); + this.tableLayoutPanel1.Controls.Add(this.tableLayoutPanel2, 0, 1); + this.tableLayoutPanel1.Location = new System.Drawing.Point(15, 15); + this.tableLayoutPanel1.Name = "tableLayoutPanel1"; + this.tableLayoutPanel1.RowCount = 3; + this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); + this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); + this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); + this.tableLayoutPanel1.Size = new System.Drawing.Size(395, 87); + this.tableLayoutPanel1.TabIndex = 0; + // + // tableLayoutPanel3 + // + this.tableLayoutPanel3.AutoSize = true; + this.tableLayoutPanel3.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; + this.tableLayoutPanel3.ColumnCount = 2; + this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); + this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); + this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F)); + this.tableLayoutPanel3.Controls.Add(this.MyCancelButton, 1, 0); + this.tableLayoutPanel3.Controls.Add(this.OKButton, 0, 0); + this.tableLayoutPanel3.Dock = System.Windows.Forms.DockStyle.Right; + this.tableLayoutPanel3.Location = new System.Drawing.Point(236, 58); + this.tableLayoutPanel3.Margin = new System.Windows.Forms.Padding(3, 3, 0, 3); + this.tableLayoutPanel3.Name = "tableLayoutPanel3"; + this.tableLayoutPanel3.RowCount = 1; + this.tableLayoutPanel3.RowStyles.Add(new System.Windows.Forms.RowStyle()); + this.tableLayoutPanel3.Size = new System.Drawing.Size(159, 26); + this.tableLayoutPanel3.TabIndex = 9; + // + // MyCancelButton + // + this.MyCancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel; + this.MyCancelButton.Dock = System.Windows.Forms.DockStyle.Right; + this.MyCancelButton.Location = new System.Drawing.Point(84, 3); + this.MyCancelButton.Margin = new System.Windows.Forms.Padding(3, 3, 0, 0); + this.MyCancelButton.Name = "MyCancelButton"; + this.MyCancelButton.Size = new System.Drawing.Size(75, 23); + this.MyCancelButton.TabIndex = 13; + this.MyCancelButton.Text = "Cancel"; + this.MyCancelButton.UseVisualStyleBackColor = true; + this.MyCancelButton.Click += new System.EventHandler(this.CancelButton_Click); + // + // OKButton + // + this.OKButton.DialogResult = System.Windows.Forms.DialogResult.OK; + this.OKButton.Dock = System.Windows.Forms.DockStyle.Right; + this.OKButton.Location = new System.Drawing.Point(3, 3); + this.OKButton.Margin = new System.Windows.Forms.Padding(3, 3, 3, 0); + this.OKButton.Name = "OKButton"; + this.OKButton.Size = new System.Drawing.Size(75, 23); + this.OKButton.TabIndex = 12; + this.OKButton.Text = "OK"; + this.OKButton.UseVisualStyleBackColor = true; + this.OKButton.Click += new System.EventHandler(this.OKButton_Click); + // + // UseProxyCheckBox + // + this.UseProxyCheckBox.AutoSize = true; + this.UseProxyCheckBox.Location = new System.Drawing.Point(3, 3); + this.UseProxyCheckBox.Name = "UseProxyCheckBox"; + this.UseProxyCheckBox.Size = new System.Drawing.Size(78, 16); + this.UseProxyCheckBox.TabIndex = 0; + this.UseProxyCheckBox.Text = "Use Proxy"; + this.UseProxyCheckBox.UseVisualStyleBackColor = true; + this.UseProxyCheckBox.CheckedChanged += new System.EventHandler(this.UseProxyCheckBox_CheckedChanged); + // + // tableLayoutPanel2 + // + this.tableLayoutPanel2.AutoSize = true; + this.tableLayoutPanel2.ColumnCount = 4; + this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); + this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); + this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); + this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); + this.tableLayoutPanel2.Controls.Add(this.ProxyAddrLabel, 0, 0); + this.tableLayoutPanel2.Controls.Add(this.ProxyServerTextBox, 1, 0); + this.tableLayoutPanel2.Controls.Add(this.ProxyPortLable, 2, 0); + this.tableLayoutPanel2.Controls.Add(this.ProxyPortTextBox, 3, 0); + this.tableLayoutPanel2.Location = new System.Drawing.Point(3, 25); + this.tableLayoutPanel2.Name = "tableLayoutPanel2"; + this.tableLayoutPanel2.RowCount = 1; + this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); + this.tableLayoutPanel2.Size = new System.Drawing.Size(389, 27); + this.tableLayoutPanel2.TabIndex = 1; + // + // ProxyAddrLabel + // + this.ProxyAddrLabel.Anchor = System.Windows.Forms.AnchorStyles.Left; + this.ProxyAddrLabel.AutoSize = true; + this.ProxyAddrLabel.Location = new System.Drawing.Point(3, 7); + this.ProxyAddrLabel.Name = "ProxyAddrLabel"; + this.ProxyAddrLabel.Size = new System.Drawing.Size(65, 12); + this.ProxyAddrLabel.TabIndex = 0; + this.ProxyAddrLabel.Text = "Proxy Addr"; + // + // ProxyServerTextBox + // + this.ProxyServerTextBox.Location = new System.Drawing.Point(74, 3); + this.ProxyServerTextBox.MaxLength = 512; + this.ProxyServerTextBox.Name = "ProxyServerTextBox"; + this.ProxyServerTextBox.Size = new System.Drawing.Size(135, 21); + this.ProxyServerTextBox.TabIndex = 1; + this.ProxyServerTextBox.WordWrap = false; + // + // ProxyPortLable + // + this.ProxyPortLable.Anchor = System.Windows.Forms.AnchorStyles.Left; + this.ProxyPortLable.AutoSize = true; + this.ProxyPortLable.Location = new System.Drawing.Point(215, 7); + this.ProxyPortLable.Name = "ProxyPortLable"; + this.ProxyPortLable.Size = new System.Drawing.Size(65, 12); + this.ProxyPortLable.TabIndex = 2; + this.ProxyPortLable.Text = "Proxy Port"; + // + // ProxyPortTextBox + // + this.ProxyPortTextBox.Location = new System.Drawing.Point(286, 3); + this.ProxyPortTextBox.MaxLength = 10; + this.ProxyPortTextBox.Name = "ProxyPortTextBox"; + this.ProxyPortTextBox.Size = new System.Drawing.Size(100, 21); + this.ProxyPortTextBox.TabIndex = 3; + this.ProxyPortTextBox.WordWrap = false; + // + // ProxyForm + // + this.AcceptButton = this.OKButton; + this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; + this.AutoSize = true; + this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; + this.CancelButton = this.MyCancelButton; + this.ClientSize = new System.Drawing.Size(441, 149); + this.Controls.Add(this.tableLayoutPanel1); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; + this.MaximizeBox = false; + this.MinimizeBox = false; + this.Name = "ProxyForm"; + this.Padding = new System.Windows.Forms.Padding(12, 12, 12, 9); + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; + this.Text = "Edit Proxy"; + this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.ProxyForm_FormClosed); + this.tableLayoutPanel1.ResumeLayout(false); + this.tableLayoutPanel1.PerformLayout(); + this.tableLayoutPanel3.ResumeLayout(false); + this.tableLayoutPanel2.ResumeLayout(false); + this.tableLayoutPanel2.PerformLayout(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; + private System.Windows.Forms.CheckBox UseProxyCheckBox; + private System.Windows.Forms.TableLayoutPanel tableLayoutPanel2; + private System.Windows.Forms.Label ProxyAddrLabel; + private System.Windows.Forms.TextBox ProxyServerTextBox; + private System.Windows.Forms.Label ProxyPortLable; + private System.Windows.Forms.TextBox ProxyPortTextBox; + private System.Windows.Forms.TableLayoutPanel tableLayoutPanel3; + private System.Windows.Forms.Button MyCancelButton; + private System.Windows.Forms.Button OKButton; + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/View/ProxyForm.cs b/shadowsocks-csharp/View/ProxyForm.cs new file mode 100644 index 00000000..4f2a900d --- /dev/null +++ b/shadowsocks-csharp/View/ProxyForm.cs @@ -0,0 +1,116 @@ +using System; +using System.Drawing; +using System.Windows.Forms; +using Shadowsocks.Controller; +using Shadowsocks.Model; +using Shadowsocks.Properties; + +namespace Shadowsocks.View +{ + public partial class ProxyForm : Form + { + private ShadowsocksController controller; + + // this is a copy of configuration that we are working on + private Configuration _modifiedConfiguration; + + public ProxyForm(ShadowsocksController controller) + { + this.Font = System.Drawing.SystemFonts.MessageBoxFont; + InitializeComponent(); + + UpdateTexts(); + this.Icon = Icon.FromHandle(Resources.ssw128.GetHicon()); + + this.controller = controller; + controller.ConfigChanged += controller_ConfigChanged; + + UpdateEnabled(); + LoadCurrentConfiguration(); + } + + private void UpdateTexts() + { + UseProxyCheckBox.Text = I18N.GetString("Use Proxy"); + ProxyAddrLabel.Text = I18N.GetString("Proxy Addr"); + ProxyPortLable.Text = I18N.GetString("Proxy Port"); + OKButton.Text = I18N.GetString("OK"); + MyCancelButton.Text = I18N.GetString("Cancel"); + this.Text = I18N.GetString("Edit Proxy"); + } + + private void controller_ConfigChanged(object sender, EventArgs e) + { + LoadCurrentConfiguration(); + } + + private void LoadCurrentConfiguration() + { + _modifiedConfiguration = controller.GetConfigurationCopy(); + + UseProxyCheckBox.Checked = _modifiedConfiguration.useProxy; + ProxyServerTextBox.Text = _modifiedConfiguration.proxyServer; + ProxyPortTextBox.Text = _modifiedConfiguration.proxyPort.ToString(); + } + + private void OKButton_Click(object sender, EventArgs e) + { + if (UseProxyCheckBox.Checked) + { + try + { + var proxy = ProxyServerTextBox.Text; + var port = int.Parse(ProxyPortTextBox.Text); + Configuration.CheckServer(proxy); + Configuration.CheckPort(port); + + controller.EnableProxy(proxy, port); + } + catch (FormatException) + { + MessageBox.Show(I18N.GetString("Illegal port number format")); + return; + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + return; + } + } + else + { + controller.DisableProxy(); + } + this.Close(); + } + + private void CancelButton_Click(object sender, EventArgs e) + { + this.Close(); + } + + private void ProxyForm_FormClosed(object sender, FormClosedEventArgs e) + { + controller.ConfigChanged -= controller_ConfigChanged; + } + + private void UseProxyCheckBox_CheckedChanged(object sender, EventArgs e) + { + UpdateEnabled(); + } + + private void UpdateEnabled() + { + if (UseProxyCheckBox.Checked) + { + ProxyServerTextBox.Enabled = true; + ProxyPortTextBox.Enabled = true; + } + else + { + ProxyServerTextBox.Enabled = false; + ProxyPortTextBox.Enabled = false; + } + } + } +} diff --git a/shadowsocks-csharp/View/ProxyForm.resx b/shadowsocks-csharp/View/ProxyForm.resx new file mode 100644 index 00000000..1af7de15 --- /dev/null +++ b/shadowsocks-csharp/View/ProxyForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/shadowsocks-csharp/shadowsocks-csharp.csproj b/shadowsocks-csharp/shadowsocks-csharp.csproj index 008134ae..d339b634 100644 --- a/shadowsocks-csharp/shadowsocks-csharp.csproj +++ b/shadowsocks-csharp/shadowsocks-csharp.csproj @@ -232,6 +232,12 @@ LogForm.cs + + Form + + + ProxyForm.cs + Form @@ -262,6 +268,9 @@ LogForm.cs + + ProxyForm.cs + QRCodeForm.cs