Skip to content

Commit

Permalink
Simplify the temp folder mechanism: portable mode is back
Browse files Browse the repository at this point in the history
- Portable mode ON: executable file's folder
- Portable mode OFF: current user's temp folder
  • Loading branch information
celeron533 committed May 31, 2018
1 parent 348e601 commit 79f33bc
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 84 deletions.
9 changes: 2 additions & 7 deletions shadowsocks-csharp/Controller/ShadowsocksController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,11 @@ public EndPoint GetPluginLocalEndPointIfConfigured(Server server)
return plugin.LocalEndPoint;
}

public void SaveServers(List<Server> servers, int localPort)
public void SaveServers(List<Server> 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);
}

Expand Down
2 changes: 2 additions & 0 deletions shadowsocks-csharp/Data/ja.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ Plugin Program=プラグインプログラム
Plugin Options=プラグインのオプション
Plugin Arguments=プラグインの引数
Proxy Port=プロキシポート
Portable Mode=ポータブルモード
Restart required=再起動SSが必要
Remarks=付記
Timeout(Sec)=タイムアウト (秒)
OK=OK
Expand Down
3 changes: 2 additions & 1 deletion shadowsocks-csharp/Data/zh_CN.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ Plugin Program=插件程序
Plugin Options=插件选项
Plugin Arguments=插件参数
Proxy Port=代理端口
Portable Mode=便携模式
Restart required=需要重新启动SS
Remarks=备注
Timeout(Sec)=超时(秒)
OK=确定
Cancel=取消
New server=未配置的服务器
Move &Up=上移(&U)
Move D&own=下移(&O)
Temp Folder=临时文件夹

# Proxy Form

Expand Down
3 changes: 2 additions & 1 deletion shadowsocks-csharp/Data/zh_TW.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ Plugin Program=外掛程式
Plugin Options=外掛程式選項
Plugin Arguments=外掛程式參數
Proxy Port=Proxy 連接埠
Portable Mode=便攜模式
Restart required=需要重新啟動SS
Remarks=註解
Timeout(Sec)=逾時 (秒)
OK=確定
Cancel=取消
New server=新伺服器
Move &Up=上移 (&U)
Move D&own=下移 (&O)
Temp Folder=臨時資料夾

# Proxy Form

Expand Down
9 changes: 1 addition & 8 deletions shadowsocks-csharp/Model/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class Configuration
public bool shareOverLan;
public bool isDefault;
public int localPort;
public bool portableMode = true;
public string pacUrl;
public bool useOnlinePac;
public bool secureLocalPac = true;
Expand All @@ -30,7 +31,6 @@ public class Configuration
public LogViewerConfig logViewer;
public ProxyConfig proxy;
public HotkeyConfig hotkey;
public string tempFolder;

private static string CONFIG_FILE = "gui-config.json";

Expand Down Expand Up @@ -146,13 +146,6 @@ public static void CheckLocalPort(int port)
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())
Expand Down
40 changes: 9 additions & 31 deletions shadowsocks-csharp/Util/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,45 +27,24 @@ public BandwidthScaleInfo(float value, string unitName, long unit)
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)
{
Expand Down
48 changes: 18 additions & 30 deletions shadowsocks-csharp/View/ConfigForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions shadowsocks-csharp/View/ConfigForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ private void UpdateTexts()
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");
Expand Down Expand Up @@ -112,11 +113,11 @@ private bool SaveOldSelectedServer()
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;
}
Expand Down Expand Up @@ -167,7 +168,7 @@ private void LoadCurrentConfiguration()
ServersListBox.SelectedIndex = _lastSelectedIndex;
UpdateMoveUpAndDownButton();
LoadSelectedServer();
TempFolderTextBox.Text = _modifiedConfiguration.tempFolder;
PortableModeCheckBox.Checked = _modifiedConfiguration.portableMode;
}

private void ConfigForm_Load(object sender, EventArgs e)
Expand All @@ -191,7 +192,7 @@ private void ConfigForm_KeyDown(object sender, KeyEventArgs e)
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));
}

Expand Down Expand Up @@ -279,8 +280,7 @@ private void OKButton_Click(object sender, EventArgs e)
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
Expand Down
3 changes: 3 additions & 0 deletions shadowsocks-csharp/View/ConfigForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,7 @@
<metadata name="toolTip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="toolTip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>

0 comments on commit 79f33bc

Please sign in to comment.