Browse Source

use single method to get ExecutablePath

tags/4.2.0.0
Student Main 4 years ago
parent
commit
ced49e2de1
No known key found for this signature in database GPG Key ID: AA78519C208C8742
8 changed files with 18 additions and 29 deletions
  1. +2
    -2
      shadowsocks-csharp/Controller/Service/PACDaemon.cs
  2. +1
    -1
      shadowsocks-csharp/Controller/Service/PrivoxyRunner.cs
  3. +1
    -4
      shadowsocks-csharp/Controller/Service/Sip003Plugin.cs
  4. +5
    -8
      shadowsocks-csharp/Controller/System/AutoStartup.cs
  5. +2
    -8
      shadowsocks-csharp/Controller/System/ProtocolHandler.cs
  6. +0
    -1
      shadowsocks-csharp/Model/StatisticsStrategyConfiguration.cs
  7. +6
    -4
      shadowsocks-csharp/Program.cs
  8. +1
    -1
      shadowsocks-csharp/Util/Util.cs

+ 2
- 2
shadowsocks-csharp/Controller/Service/PACDaemon.cs View File

@@ -72,7 +72,7 @@ namespace Shadowsocks.Controller
private void WatchPacFile() private void WatchPacFile()
{ {
PACFileWatcher?.Dispose(); PACFileWatcher?.Dispose();
PACFileWatcher = new FileSystemWatcher(Directory.GetCurrentDirectory());
PACFileWatcher = new FileSystemWatcher(Program.WorkingDirectory);
PACFileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; PACFileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
PACFileWatcher.Filter = PAC_FILE; PACFileWatcher.Filter = PAC_FILE;
PACFileWatcher.Changed += PACFileWatcher_Changed; PACFileWatcher.Changed += PACFileWatcher_Changed;
@@ -85,7 +85,7 @@ namespace Shadowsocks.Controller
private void WatchUserRuleFile() private void WatchUserRuleFile()
{ {
UserRuleFileWatcher?.Dispose(); UserRuleFileWatcher?.Dispose();
UserRuleFileWatcher = new FileSystemWatcher(Directory.GetCurrentDirectory());
UserRuleFileWatcher = new FileSystemWatcher(Program.WorkingDirectory);
UserRuleFileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; UserRuleFileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
UserRuleFileWatcher.Filter = USER_RULE_FILE; UserRuleFileWatcher.Filter = USER_RULE_FILE;
UserRuleFileWatcher.Changed += UserRuleFileWatcher_Changed; UserRuleFileWatcher.Changed += UserRuleFileWatcher_Changed;


+ 1
- 1
shadowsocks-csharp/Controller/Service/PrivoxyRunner.cs View File

@@ -28,7 +28,7 @@ namespace Shadowsocks.Controller
{ {
try try
{ {
_uid = Application.StartupPath.GetHashCode(); // Currently we use ss's StartupPath to identify different Privoxy instance.
_uid = Program.WorkingDirectory.GetHashCode(); // Currently we use ss's StartupPath to identify different Privoxy instance.
_uniqueConfigFile = $"privoxy_{_uid}.conf"; _uniqueConfigFile = $"privoxy_{_uid}.conf";
_privoxyJob = new Job(); _privoxyJob = new Job();


+ 1
- 4
shadowsocks-csharp/Controller/Service/Sip003Plugin.cs View File

@@ -4,7 +4,6 @@ using System.Diagnostics;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Reflection;
using Shadowsocks.Model; using Shadowsocks.Model;
using Shadowsocks.Util.ProcessManagement; using Shadowsocks.Util.ProcessManagement;
@@ -55,8 +54,6 @@ namespace Shadowsocks.Controller.Service
throw new ArgumentOutOfRangeException("serverPort"); throw new ArgumentOutOfRangeException("serverPort");
} }
var appPath = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath);
_pluginProcess = new Process _pluginProcess = new Process
{ {
StartInfo = new ProcessStartInfo StartInfo = new ProcessStartInfo
@@ -67,7 +64,7 @@ namespace Shadowsocks.Controller.Service
CreateNoWindow = !showPluginOutput, CreateNoWindow = !showPluginOutput,
ErrorDialog = false, ErrorDialog = false,
WindowStyle = ProcessWindowStyle.Hidden, WindowStyle = ProcessWindowStyle.Hidden,
WorkingDirectory = appPath ?? Environment.CurrentDirectory,
WorkingDirectory = Program.WorkingDirectory ?? Environment.CurrentDirectory,
Environment = Environment =
{ {
["SS_REMOTE_HOST"] = serverAddress, ["SS_REMOTE_HOST"] = serverAddress,


+ 5
- 8
shadowsocks-csharp/Controller/System/AutoStartup.cs View File

@@ -1,9 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Win32; using Microsoft.Win32;
using NLog; using NLog;
using Shadowsocks.Util; using Shadowsocks.Util;
@@ -16,9 +14,8 @@ namespace Shadowsocks.Controller
// Don't use Application.ExecutablePath // Don't use Application.ExecutablePath
// see https://stackoverflow.com/questions/12945805/odd-c-sharp-path-issue // see https://stackoverflow.com/questions/12945805/odd-c-sharp-path-issue
private static readonly string ExecutablePath = Assembly.GetEntryAssembly().Location;
private static string Key = "Shadowsocks_" + Application.StartupPath.GetHashCode();
private static string Key = "Shadowsocks_" + Program.ExecutablePath.GetHashCode();
public static bool Set(bool enabled) public static bool Set(bool enabled)
{ {
@@ -33,7 +30,7 @@ namespace Shadowsocks.Controller
} }
if (enabled) if (enabled)
{ {
runKey.SetValue(Key, ExecutablePath);
runKey.SetValue(Key, Program.ExecutablePath);
} }
else else
{ {
@@ -82,10 +79,10 @@ namespace Shadowsocks.Controller
else if (item.Equals("Shadowsocks", StringComparison.OrdinalIgnoreCase)) // Compatibility with older versions else if (item.Equals("Shadowsocks", StringComparison.OrdinalIgnoreCase)) // Compatibility with older versions
{ {
string value = Convert.ToString(runKey.GetValue(item)); string value = Convert.ToString(runKey.GetValue(item));
if (ExecutablePath.Equals(value, StringComparison.OrdinalIgnoreCase))
if (Program.ExecutablePath.Equals(value, StringComparison.OrdinalIgnoreCase))
{ {
runKey.DeleteValue(item); runKey.DeleteValue(item);
runKey.SetValue(Key, ExecutablePath);
runKey.SetValue(Key, Program.ExecutablePath);
return true; return true;
} }
} }


+ 2
- 8
shadowsocks-csharp/Controller/System/ProtocolHandler.cs View File

@@ -4,7 +4,6 @@ using Shadowsocks.Util;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;


@@ -16,11 +15,6 @@ namespace Shadowsocks.Controller


private static Logger logger = LogManager.GetCurrentClassLogger(); private static Logger logger = LogManager.GetCurrentClassLogger();


// Don't use Application.ExecutablePath
// see https://stackoverflow.com/questions/12945805/odd-c-sharp-path-issue
private static readonly string ExecutablePath = Assembly.GetEntryAssembly().Location;

// TODO: Elevate when necessary
public static bool Set(bool enabled) public static bool Set(bool enabled)
{ {
RegistryKey ssURLAssociation = null; RegistryKey ssURLAssociation = null;
@@ -38,7 +32,7 @@ namespace Shadowsocks.Controller
ssURLAssociation.SetValue("", "URL:Shadowsocks"); ssURLAssociation.SetValue("", "URL:Shadowsocks");
ssURLAssociation.SetValue("URL Protocol", ""); ssURLAssociation.SetValue("URL Protocol", "");
var shellOpen = ssURLAssociation.CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command"); var shellOpen = ssURLAssociation.CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command");
shellOpen.SetValue("", $"{ExecutablePath} --open-url %1");
shellOpen.SetValue("", $"{Program.ExecutablePath} --open-url %1");
logger.Info(@"Successfully added ss:// association."); logger.Info(@"Successfully added ss:// association.");
} }
else else
@@ -81,7 +75,7 @@ namespace Shadowsocks.Controller
} }


var shellOpen = ssURLAssociation.OpenSubKey("shell").OpenSubKey("open").OpenSubKey("command"); var shellOpen = ssURLAssociation.OpenSubKey("shell").OpenSubKey("open").OpenSubKey("command");
return (string)shellOpen.GetValue("") == $"{ExecutablePath} --open-url %1";
return (string)shellOpen.GetValue("") == $"{Program.ExecutablePath} --open-url %1";
} }
catch (Exception e) catch (Exception e)
{ {


+ 0
- 1
shadowsocks-csharp/Model/StatisticsStrategyConfiguration.cs View File

@@ -6,7 +6,6 @@ using System.Reflection;


using Newtonsoft.Json; using Newtonsoft.Json;
using NLog; using NLog;
using Shadowsocks.Controller;


namespace Shadowsocks.Model namespace Shadowsocks.Model
{ {


+ 6
- 4
shadowsocks-csharp/Program.cs View File

@@ -24,6 +24,10 @@ namespace Shadowsocks
public static ShadowsocksController MainController { get; private set; } public static ShadowsocksController MainController { get; private set; }
public static MenuViewController MenuController { get; private set; } public static MenuViewController MenuController { get; private set; }
public static string[] Args { get; private set; } public static string[] Args { get; private set; }
// https://github.com/dotnet/runtime/issues/13051#issuecomment-510267727
public static readonly string ExecutablePath = Process.GetCurrentProcess().MainModule?.FileName;
public static readonly string WorkingDirectory = Path.GetDirectoryName(ExecutablePath);
/// <summary> /// <summary>
/// 应用程序的主入口点。 /// 应用程序的主入口点。
/// </summary> /// </summary>
@@ -31,7 +35,7 @@ namespace Shadowsocks
[STAThread] [STAThread]
private static void Main(string[] args) private static void Main(string[] args)
{ {
Directory.SetCurrentDirectory(Application.StartupPath);
Directory.SetCurrentDirectory(WorkingDirectory);
// todo: initialize the NLog configuartion // todo: initialize the NLog configuartion
Model.NLogConfig.TouchAndApplyNLogConfig(); Model.NLogConfig.TouchAndApplyNLogConfig();
@@ -59,7 +63,7 @@ namespace Shadowsocks
} }
return; return;
} }
string pipename = $"Shadowsocks\\{Application.StartupPath.GetHashCode()}";
string pipename = $"Shadowsocks\\{ExecutablePath.GetHashCode()}";
string addedUrl = null; string addedUrl = null;
@@ -134,8 +138,6 @@ namespace Shadowsocks
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
AutoStartup.RegisterForRestart(true); AutoStartup.RegisterForRestart(true);
Directory.SetCurrentDirectory(Application.StartupPath);
#if DEBUG #if DEBUG
// truncate privoxy log file while debugging // truncate privoxy log file while debugging
string privoxyLogFilename = Utils.GetTempPath("privoxy.log"); string privoxyLogFilename = Utils.GetTempPath("privoxy.log");


+ 1
- 1
shadowsocks-csharp/Util/Util.cs View File

@@ -46,7 +46,7 @@ namespace Shadowsocks.Util
} }
else else
{ {
_tempPath = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), @"Shadowsocks\ss_win_temp_" + Application.ExecutablePath.GetHashCode())).FullName;
_tempPath = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), @"Shadowsocks\ss_win_temp_" + Program.ExecutablePath.GetHashCode())).FullName;
} }
} }
catch (Exception e) catch (Exception e)


Loading…
Cancel
Save