From 79f33bc326f92921133b433a1fcd597fe64e7f75 Mon Sep 17 00:00:00 2001 From: celeron533 Date: Thu, 31 May 2018 22:23:32 +0800 Subject: [PATCH] Simplify the temp folder mechanism: portable mode is back - Portable mode ON: executable file's folder - Portable mode OFF: current user's temp folder --- .../Controller/ShadowsocksController.cs | 9 +--- shadowsocks-csharp/Data/ja.txt | 2 + shadowsocks-csharp/Data/zh_CN.txt | 3 +- shadowsocks-csharp/Data/zh_TW.txt | 3 +- shadowsocks-csharp/Model/Configuration.cs | 9 +--- shadowsocks-csharp/Util/Util.cs | 40 ++++------------ .../View/ConfigForm.Designer.cs | 48 +++++++------------ shadowsocks-csharp/View/ConfigForm.cs | 12 ++--- shadowsocks-csharp/View/ConfigForm.resx | 3 ++ 9 files changed, 45 insertions(+), 84 deletions(-) diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index ca5ffdb1..565db6bf 100644 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -175,16 +175,11 @@ namespace Shadowsocks.Controller return plugin.LocalEndPoint; } - public void SaveServers(List servers, int localPort) + public void SaveServers(List servers, int localPort, bool portableMode) { _config.configs = servers; _config.localPort = localPort; - Configuration.Save(_config); - } - - public void SaveTempFolder(string tempFolder) - { - _config.tempFolder = tempFolder; + _config.portableMode = portableMode; Configuration.Save(_config); } diff --git a/shadowsocks-csharp/Data/ja.txt b/shadowsocks-csharp/Data/ja.txt index a2426735..31da1905 100644 --- a/shadowsocks-csharp/Data/ja.txt +++ b/shadowsocks-csharp/Data/ja.txt @@ -55,6 +55,8 @@ Plugin Program=プラグインプログラム Plugin Options=プラグインのオプション Plugin Arguments=プラグインの引数 Proxy Port=プロキシポート +Portable Mode=ポータブルモード +Restart required=再起動SSが必要 Remarks=付記 Timeout(Sec)=タイムアウト (秒) OK=OK diff --git a/shadowsocks-csharp/Data/zh_CN.txt b/shadowsocks-csharp/Data/zh_CN.txt index b8bf5018..072ea66e 100644 --- a/shadowsocks-csharp/Data/zh_CN.txt +++ b/shadowsocks-csharp/Data/zh_CN.txt @@ -55,6 +55,8 @@ Plugin Program=插件程序 Plugin Options=插件选项 Plugin Arguments=插件参数 Proxy Port=代理端口 +Portable Mode=便携模式 +Restart required=需要重新启动SS Remarks=备注 Timeout(Sec)=超时(秒) OK=确定 @@ -62,7 +64,6 @@ Cancel=取消 New server=未配置的服务器 Move &Up=上移(&U) Move D&own=下移(&O) -Temp Folder=临时文件夹 # Proxy Form diff --git a/shadowsocks-csharp/Data/zh_TW.txt b/shadowsocks-csharp/Data/zh_TW.txt index b8164ada..d8a51a3f 100644 --- a/shadowsocks-csharp/Data/zh_TW.txt +++ b/shadowsocks-csharp/Data/zh_TW.txt @@ -55,6 +55,8 @@ Plugin Program=外掛程式 Plugin Options=外掛程式選項 Plugin Arguments=外掛程式參數 Proxy Port=Proxy 連接埠 +Portable Mode=便攜模式 +Restart required=需要重新啟動SS Remarks=註解 Timeout(Sec)=逾時 (秒) OK=確定 @@ -62,7 +64,6 @@ Cancel=取消 New server=新伺服器 Move &Up=上移 (&U) Move D&own=下移 (&O) -Temp Folder=臨時資料夾 # Proxy Form diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs index 68762b88..c7d2fcdd 100644 --- a/shadowsocks-csharp/Model/Configuration.cs +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -20,6 +20,7 @@ namespace Shadowsocks.Model public bool shareOverLan; public bool isDefault; public int localPort; + public bool portableMode = true; public string pacUrl; public bool useOnlinePac; public bool secureLocalPac = true; @@ -30,7 +31,6 @@ namespace Shadowsocks.Model public LogViewerConfig logViewer; public ProxyConfig proxy; public HotkeyConfig hotkey; - public string tempFolder; private static string CONFIG_FILE = "gui-config.json"; @@ -146,13 +146,6 @@ namespace Shadowsocks.Model throw new ArgumentException(I18N.GetString("Port can't be 8123")); } - public static void CheckTempFolder(string tempPath) - { - if (string.IsNullOrWhiteSpace(tempPath)) - return; - Path.GetFullPath(tempPath); - } - private static void CheckPassword(string password) { if (password.IsNullOrEmpty()) diff --git a/shadowsocks-csharp/Util/Util.cs b/shadowsocks-csharp/Util/Util.cs index d28fb3a2..8130b961 100755 --- a/shadowsocks-csharp/Util/Util.cs +++ b/shadowsocks-csharp/Util/Util.cs @@ -2,7 +2,6 @@ using System.Diagnostics; using System.IO; using System.IO.Compression; -using System.Linq; using System.Runtime.InteropServices; using System.Windows.Forms; using Microsoft.Win32; @@ -28,45 +27,24 @@ namespace Shadowsocks.Util public static class Utils { private static string _tempPath = null; - private const string TEMP_LOG = "temp.log"; - private static readonly string[] COMMON_ENV = - { - "%Tmp%", - "%Temp%", - "%AppData%", - "%LocalAppData%", - "%Home%", - "%UserProfile%", - "%Public%", - "%CommonProgramFiles%", - "%CommonProgramFiles(x86)%", - "%CommonProgramW6432%", - "%ProgramFiles%", - "%ProgramFiles(x86)%", - "%ProgramW6432%", - "%ProgramData%", - }; // return path to store temporary files public static string GetTempPath() { if (_tempPath == null) { + bool isPortableMode = Configuration.Load().portableMode; try { - var tempFolder = Configuration.Load().tempFolder; - if (string.IsNullOrWhiteSpace(tempFolder)) + if (isPortableMode) + { + _tempPath = Directory.CreateDirectory(Path.Combine(Application.StartupPath, "ss_win_temp")).FullName; // don't use "/", it will fail when we call explorer /select xxx/ss_win_temp\xxx.log - tempFolder = "ss_win_temp"; - else if (COMMON_ENV.Contains(tempFolder, StringComparer.OrdinalIgnoreCase)) - // add subfolder for these common folders - tempFolder += (@"\Shadowsocks\ss_win_temp_" + Application.ExecutablePath.GetHashCode()); - - tempFolder = Environment.ExpandEnvironmentVariables(tempFolder); - // If `tempFolder` is an absolute path, `Application.StartupPath` will be ignored. - var tempDirectory = Directory.CreateDirectory(Path.Combine(Application.StartupPath, tempFolder)); - _tempPath = tempDirectory.FullName; - File.AppendAllText(Path.Combine(_tempPath, TEMP_LOG), $"[{DateTimeOffset.Now.ToString("u")}] Temp folder used by \"{Application.ExecutablePath}\"{Environment.NewLine}"); + } + else + { + _tempPath = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), @"Shadowsocks\ss_win_temp_" + Application.ExecutablePath.GetHashCode())).FullName; + } } catch (Exception e) { diff --git a/shadowsocks-csharp/View/ConfigForm.Designer.cs b/shadowsocks-csharp/View/ConfigForm.Designer.cs index 4ec5dfcc..77cf857f 100755 --- a/shadowsocks-csharp/View/ConfigForm.Designer.cs +++ b/shadowsocks-csharp/View/ConfigForm.Designer.cs @@ -63,8 +63,7 @@ this.tableLayoutPanel5 = new System.Windows.Forms.TableLayoutPanel(); this.ProxyPortTextBox = new System.Windows.Forms.TextBox(); this.ProxyPortLabel = new System.Windows.Forms.Label(); - this.TempFolderLabel = new System.Windows.Forms.Label(); - this.TempFolderTextBox = new System.Windows.Forms.TextBox(); + this.PortableModeCheckBox = new System.Windows.Forms.CheckBox(); this.tableLayoutPanel3 = new System.Windows.Forms.TableLayoutPanel(); this.tableLayoutPanel4 = new System.Windows.Forms.TableLayoutPanel(); this.DuplicateButton = new System.Windows.Forms.Button(); @@ -506,26 +505,21 @@ this.tableLayoutPanel5.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel5.Controls.Add(this.ProxyPortTextBox, 1, 0); this.tableLayoutPanel5.Controls.Add(this.ProxyPortLabel, 0, 0); - this.tableLayoutPanel5.Controls.Add(this.TempFolderLabel, 0, 1); - this.tableLayoutPanel5.Controls.Add(this.TempFolderTextBox, 1, 1); + this.tableLayoutPanel5.Controls.Add(this.PortableModeCheckBox, 0, 1); this.tableLayoutPanel5.Location = new System.Drawing.Point(166, 308); this.tableLayoutPanel5.Margin = new System.Windows.Forms.Padding(0); this.tableLayoutPanel5.Name = "tableLayoutPanel5"; this.tableLayoutPanel5.Padding = new System.Windows.Forms.Padding(3); this.tableLayoutPanel5.RowCount = 2; this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle()); - this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F)); - this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 58F)); - this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 58F)); - this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 58F)); - this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 58F)); - this.tableLayoutPanel5.Size = new System.Drawing.Size(202, 64); + this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle()); + this.tableLayoutPanel5.Size = new System.Drawing.Size(196, 64); this.tableLayoutPanel5.TabIndex = 9; // // ProxyPortTextBox // this.ProxyPortTextBox.Anchor = System.Windows.Forms.AnchorStyles.Left; - this.ProxyPortTextBox.Location = new System.Drawing.Point(83, 6); + this.ProxyPortTextBox.Location = new System.Drawing.Point(77, 6); this.ProxyPortTextBox.MaxLength = 10; this.ProxyPortTextBox.Name = "ProxyPortTextBox"; this.ProxyPortTextBox.Size = new System.Drawing.Size(113, 21); @@ -536,29 +530,24 @@ // this.ProxyPortLabel.Anchor = System.Windows.Forms.AnchorStyles.Right; this.ProxyPortLabel.AutoSize = true; - this.ProxyPortLabel.Location = new System.Drawing.Point(12, 10); + this.ProxyPortLabel.Location = new System.Drawing.Point(6, 10); this.ProxyPortLabel.Name = "ProxyPortLabel"; this.ProxyPortLabel.Size = new System.Drawing.Size(65, 12); this.ProxyPortLabel.TabIndex = 10; this.ProxyPortLabel.Text = "Proxy Port"; // - // TempFolderLabel - // - this.TempFolderLabel.Anchor = System.Windows.Forms.AnchorStyles.Right; - this.TempFolderLabel.AutoSize = true; - this.TempFolderLabel.Location = new System.Drawing.Point(6, 39); - this.TempFolderLabel.Name = "TempFolderLabel"; - this.TempFolderLabel.Size = new System.Drawing.Size(71, 12); - this.TempFolderLabel.TabIndex = 9; - this.TempFolderLabel.Text = "Temp Folder"; - // - // TempFolderTextBox + // PortableModeCheckBox // - this.TempFolderTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); - this.TempFolderTextBox.Location = new System.Drawing.Point(83, 35); - this.TempFolderTextBox.Name = "TempFolderTextBox"; - this.TempFolderTextBox.Size = new System.Drawing.Size(113, 21); - this.TempFolderTextBox.TabIndex = 11; + this.PortableModeCheckBox.Anchor = System.Windows.Forms.AnchorStyles.Left; + this.PortableModeCheckBox.AutoSize = true; + this.tableLayoutPanel5.SetColumnSpan(this.PortableModeCheckBox, 2); + this.PortableModeCheckBox.Location = new System.Drawing.Point(6, 37); + this.PortableModeCheckBox.Name = "PortableModeCheckBox"; + this.PortableModeCheckBox.Size = new System.Drawing.Size(102, 16); + this.PortableModeCheckBox.TabIndex = 11; + this.PortableModeCheckBox.Text = "Portable Mode"; + this.toolTip1.SetToolTip(this.PortableModeCheckBox, "restart required"); + this.PortableModeCheckBox.UseVisualStyleBackColor = true; // // tableLayoutPanel3 // @@ -686,11 +675,10 @@ private System.Windows.Forms.TextBox PluginOptionsTextBox; private System.Windows.Forms.CheckBox ShowPasswdCheckBox; private System.Windows.Forms.TextBox PasswordTextBox; - private System.Windows.Forms.Label TempFolderLabel; private System.Windows.Forms.TextBox PluginArgumentsTextBox; private System.Windows.Forms.Label PluginArgumentsLabel; private System.Windows.Forms.ToolTip toolTip1; - private System.Windows.Forms.TextBox TempFolderTextBox; + private System.Windows.Forms.CheckBox PortableModeCheckBox; } } diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 338eb402..08895455 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -54,7 +54,8 @@ namespace Shadowsocks.View PluginOptionsLabel.Text = I18N.GetString("Plugin Options"); PluginArgumentsLabel.Text = I18N.GetString("Plugin Arguments"); ProxyPortLabel.Text = I18N.GetString("Proxy Port"); - TempFolderLabel.Text = I18N.GetString("Temp Folder"); + PortableModeCheckBox.Text = I18N.GetString("Portable Mode"); + toolTip1.SetToolTip(this.PortableModeCheckBox, I18N.GetString("Restart required")); RemarksLabel.Text = I18N.GetString("Remarks"); TimeoutLabel.Text = I18N.GetString("Timeout(Sec)"); ServerGroupBox.Text = I18N.GetString("Server"); @@ -112,11 +113,11 @@ namespace Shadowsocks.View return false; } int localPort = int.Parse(ProxyPortTextBox.Text); - Configuration.CheckTempFolder(TempFolderTextBox.Text); Configuration.CheckServer(server); Configuration.CheckLocalPort(localPort); _modifiedConfiguration.configs[_lastSelectedIndex] = server; _modifiedConfiguration.localPort = localPort; + _modifiedConfiguration.portableMode = PortableModeCheckBox.Checked; return true; } @@ -167,7 +168,7 @@ namespace Shadowsocks.View ServersListBox.SelectedIndex = _lastSelectedIndex; UpdateMoveUpAndDownButton(); LoadSelectedServer(); - TempFolderTextBox.Text = _modifiedConfiguration.tempFolder; + PortableModeCheckBox.Checked = _modifiedConfiguration.portableMode; } private void ConfigForm_Load(object sender, EventArgs e) @@ -191,7 +192,7 @@ namespace Shadowsocks.View MessageBox.Show(I18N.GetString("Please add at least one server")); return; } - controller.SaveServers(_modifiedConfiguration.configs, _modifiedConfiguration.localPort); + controller.SaveServers(_modifiedConfiguration.configs, _modifiedConfiguration.localPort, _modifiedConfiguration.portableMode); controller.SelectServerIndex(_modifiedConfiguration.configs.IndexOf(server)); } @@ -279,8 +280,7 @@ namespace Shadowsocks.View MessageBox.Show(I18N.GetString("Please add at least one server")); return; } - controller.SaveServers(_modifiedConfiguration.configs, _modifiedConfiguration.localPort); - controller.SaveTempFolder(TempFolderTextBox.Text); + controller.SaveServers(_modifiedConfiguration.configs, _modifiedConfiguration.localPort, _modifiedConfiguration.portableMode); // SelectedIndex remains valid // We handled this in event handlers, e.g. Add/DeleteButton, SelectedIndexChanged // and move operations diff --git a/shadowsocks-csharp/View/ConfigForm.resx b/shadowsocks-csharp/View/ConfigForm.resx index 9d9bc4df..8674430e 100755 --- a/shadowsocks-csharp/View/ConfigForm.resx +++ b/shadowsocks-csharp/View/ConfigForm.resx @@ -120,4 +120,7 @@ 17, 17 + + 17, 17 + \ No newline at end of file