From 344c95e52c23f899f0f862e29917729d1cb0d57f Mon Sep 17 00:00:00 2001 From: ahmadali shafiee Date: Sun, 1 Nov 2020 09:44:54 +0330 Subject: [PATCH 1/5] remove duplicate startup entries fixes #3011 Signed-off-by: ahmadali shafiee --- shadowsocks-csharp/Controller/System/AutoStartup.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/shadowsocks-csharp/Controller/System/AutoStartup.cs b/shadowsocks-csharp/Controller/System/AutoStartup.cs index 97127b66..a25169d1 100644 --- a/shadowsocks-csharp/Controller/System/AutoStartup.cs +++ b/shadowsocks-csharp/Controller/System/AutoStartup.cs @@ -14,7 +14,7 @@ namespace Shadowsocks.Controller // Don't use Application.ExecutablePath // see https://stackoverflow.com/questions/12945805/odd-c-sharp-path-issue - + private static string Key = "Shadowsocks_" + Program.ExecutablePath.GetHashCode(); public static bool Set(bool enabled) @@ -28,6 +28,17 @@ namespace Shadowsocks.Controller logger.Error(@"Cannot find HKCU\Software\Microsoft\Windows\CurrentVersion\Run"); return false; } + // Remove other startup keys with the same executable path. fixes #3011 + foreach (var valueName in runKey.GetValueNames()) + { + if (valueName == Key) + continue; + + if (string.Equals(runKey.GetValue(valueName).ToString(), Program.ExecutablePath, StringComparison.InvariantCultureIgnoreCase)) + { + runKey.DeleteValue(valueName); + } + } if (enabled) { runKey.SetValue(Key, Program.ExecutablePath); From 753d7709d5fbd6767a5d659aae0658ec0ae05076 Mon Sep 17 00:00:00 2001 From: ahmadali shafiee Date: Mon, 2 Nov 2020 11:08:54 +0330 Subject: [PATCH 2/5] move duplicate key checking to `Check` method Signed-off-by: ahmadali shafiee --- .../Controller/System/AutoStartup.cs | 35 +++++++------------ 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/shadowsocks-csharp/Controller/System/AutoStartup.cs b/shadowsocks-csharp/Controller/System/AutoStartup.cs index a25169d1..8e89fc8e 100644 --- a/shadowsocks-csharp/Controller/System/AutoStartup.cs +++ b/shadowsocks-csharp/Controller/System/AutoStartup.cs @@ -28,17 +28,6 @@ namespace Shadowsocks.Controller logger.Error(@"Cannot find HKCU\Software\Microsoft\Windows\CurrentVersion\Run"); return false; } - // Remove other startup keys with the same executable path. fixes #3011 - foreach (var valueName in runKey.GetValueNames()) - { - if (valueName == Key) - continue; - - if (string.Equals(runKey.GetValue(valueName).ToString(), Program.ExecutablePath, StringComparison.InvariantCultureIgnoreCase)) - { - runKey.DeleteValue(valueName); - } - } if (enabled) { runKey.SetValue(Key, Program.ExecutablePath); @@ -82,21 +71,21 @@ namespace Shadowsocks.Controller logger.Error(@"Cannot find HKCU\Software\Microsoft\Windows\CurrentVersion\Run"); return false; } - string[] runList = runKey.GetValueNames(); - foreach (string item in runList) + // Remove other startup keys with the same executable path. fixes #3011 and also assures compatibility with older versions + foreach (var valueName in runKey.GetValueNames()) + { + if (valueName == Key) + continue; + + if (string.Equals(runKey.GetValue(valueName).ToString(), Program.ExecutablePath, StringComparison.InvariantCultureIgnoreCase)) + { + runKey.DeleteValue(valueName); + } + } + foreach (string item in runKey.GetValueNames()) { if (item.Equals(Key, StringComparison.OrdinalIgnoreCase)) return true; - else if (item.Equals("Shadowsocks", StringComparison.OrdinalIgnoreCase)) // Compatibility with older versions - { - string value = Convert.ToString(runKey.GetValue(item)); - if (Program.ExecutablePath.Equals(value, StringComparison.OrdinalIgnoreCase)) - { - runKey.DeleteValue(item); - runKey.SetValue(Key, Program.ExecutablePath); - return true; - } - } } return false; } From 96650a1f6c5e17e1d7914f41f304995ba81c8927 Mon Sep 17 00:00:00 2001 From: ahmadali shafiee Date: Mon, 2 Nov 2020 11:12:13 +0330 Subject: [PATCH 3/5] combine foreaches Signed-off-by: ahmadali shafiee --- .../Controller/System/AutoStartup.cs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/shadowsocks-csharp/Controller/System/AutoStartup.cs b/shadowsocks-csharp/Controller/System/AutoStartup.cs index 8e89fc8e..557b8a3f 100644 --- a/shadowsocks-csharp/Controller/System/AutoStartup.cs +++ b/shadowsocks-csharp/Controller/System/AutoStartup.cs @@ -71,23 +71,22 @@ namespace Shadowsocks.Controller logger.Error(@"Cannot find HKCU\Software\Microsoft\Windows\CurrentVersion\Run"); return false; } - // Remove other startup keys with the same executable path. fixes #3011 and also assures compatibility with older versions + var check = false; foreach (var valueName in runKey.GetValueNames()) { - if (valueName == Key) + if (string.Equals(valueName, Key, StringComparison.InvariantCultureIgnoreCase)) + { + check = true; continue; + } + // Remove other startup keys with the same executable path. fixes #3011 and also assures compatibility with older versions if (string.Equals(runKey.GetValue(valueName).ToString(), Program.ExecutablePath, StringComparison.InvariantCultureIgnoreCase)) { runKey.DeleteValue(valueName); } } - foreach (string item in runKey.GetValueNames()) - { - if (item.Equals(Key, StringComparison.OrdinalIgnoreCase)) - return true; - } - return false; + return check; } catch (Exception e) { From 8b6088d4a19953b1f2fb5de36564acf62e8e7093 Mon Sep 17 00:00:00 2001 From: ahmadali shafiee Date: Mon, 2 Nov 2020 11:38:42 +0330 Subject: [PATCH 4/5] create proper key if different key exists on registry Signed-off-by: ahmadali shafiee --- shadowsocks-csharp/Controller/System/AutoStartup.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shadowsocks-csharp/Controller/System/AutoStartup.cs b/shadowsocks-csharp/Controller/System/AutoStartup.cs index 557b8a3f..853e7a36 100644 --- a/shadowsocks-csharp/Controller/System/AutoStartup.cs +++ b/shadowsocks-csharp/Controller/System/AutoStartup.cs @@ -84,6 +84,8 @@ namespace Shadowsocks.Controller if (string.Equals(runKey.GetValue(valueName).ToString(), Program.ExecutablePath, StringComparison.InvariantCultureIgnoreCase)) { runKey.DeleteValue(valueName); + runKey.SetValue(Key, Program.ExecutablePath); + check = true; } } return check; From 0f08d0882a530c398f1137ca26f972c7d6f99c85 Mon Sep 17 00:00:00 2001 From: ahmadali shafiee Date: Mon, 2 Nov 2020 11:43:04 +0330 Subject: [PATCH 5/5] fix coding style Signed-off-by: ahmadali shafiee --- shadowsocks-csharp/Controller/System/AutoStartup.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shadowsocks-csharp/Controller/System/AutoStartup.cs b/shadowsocks-csharp/Controller/System/AutoStartup.cs index 853e7a36..d7e92580 100644 --- a/shadowsocks-csharp/Controller/System/AutoStartup.cs +++ b/shadowsocks-csharp/Controller/System/AutoStartup.cs @@ -74,14 +74,14 @@ namespace Shadowsocks.Controller var check = false; foreach (var valueName in runKey.GetValueNames()) { - if (string.Equals(valueName, Key, StringComparison.InvariantCultureIgnoreCase)) + if (valueName.Equals(Key, StringComparison.InvariantCultureIgnoreCase)) { check = true; continue; } // Remove other startup keys with the same executable path. fixes #3011 and also assures compatibility with older versions - if (string.Equals(runKey.GetValue(valueName).ToString(), Program.ExecutablePath, StringComparison.InvariantCultureIgnoreCase)) + if (Program.ExecutablePath.Equals(runKey.GetValue(valueName).ToString(), StringComparison.InvariantCultureIgnoreCase)) { runKey.DeleteValue(valueName); runKey.SetValue(Key, Program.ExecutablePath);