@@ -0,0 +1,92 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Windows.Forms; | |||
using Shadowsocks.Controller.Hotkeys; | |||
using Shadowsocks.Model; | |||
namespace Shadowsocks.Controller | |||
{ | |||
static class HotkeyReg | |||
{ | |||
public static void RegAllHotkeys() | |||
{ | |||
var hotkeyConfig = Configuration.Load().hotkey; | |||
if (hotkeyConfig == null || !hotkeyConfig.RegHotkeysAtStartup) | |||
return; | |||
// if any of the hotkey reg fail, undo everything | |||
if (RegHotkeyFromString(hotkeyConfig.SwitchSystemProxy, "SwitchSystemProxyCallback") | |||
&& RegHotkeyFromString(hotkeyConfig.SwitchSystemProxyMode, "SwitchSystemProxyModeCallback") | |||
&& RegHotkeyFromString(hotkeyConfig.SwitchAllowLan, "SwitchAllowLanCallback") | |||
&& RegHotkeyFromString(hotkeyConfig.ShowLogs, "ShowLogsCallback") | |||
&& RegHotkeyFromString(hotkeyConfig.ServerMoveUp, "ServerMoveUpCallback") | |||
&& RegHotkeyFromString(hotkeyConfig.ServerMoveDown, "ServerMoveDownCallback") | |||
) | |||
{ | |||
// success | |||
} | |||
else | |||
{ | |||
RegHotkeyFromString("", "SwitchSystemProxyCallback"); | |||
RegHotkeyFromString("", "SwitchSystemProxyModeCallback"); | |||
RegHotkeyFromString("", "SwitchAllowLanCallback"); | |||
RegHotkeyFromString("", "ShowLogsCallback"); | |||
RegHotkeyFromString("", "ServerMoveUpCallback"); | |||
RegHotkeyFromString("", "ServerMoveDownCallback"); | |||
MessageBox.Show(I18N.GetString("Register hotkey failed"), I18N.GetString("Shadowsocks")); | |||
} | |||
} | |||
public static bool RegHotkeyFromString(string hotkeyStr, string callbackName, Action<RegResult> onComplete = null) | |||
{ | |||
var _callback = HotkeyCallbacks.GetCallback(callbackName); | |||
if (_callback == null) | |||
{ | |||
throw new Exception($"{callbackName} not found"); | |||
} | |||
var callback = _callback as HotKeys.HotKeyCallBackHandler; | |||
if (hotkeyStr.IsNullOrEmpty()) | |||
{ | |||
HotKeys.UnregExistingHotkey(callback); | |||
onComplete?.Invoke(RegResult.UnregSuccess); | |||
return true; | |||
} | |||
else | |||
{ | |||
var hotkey = HotKeys.Str2HotKey(hotkeyStr); | |||
if (hotkey == null) | |||
{ | |||
Logging.Error($"Cannot parse hotkey: {hotkeyStr}"); | |||
onComplete?.Invoke(RegResult.ParseError); | |||
return false; | |||
} | |||
else | |||
{ | |||
bool regResult = (HotKeys.RegHotkey(hotkey, callback)); | |||
if (regResult) | |||
{ | |||
onComplete?.Invoke(RegResult.RegSuccess); | |||
} | |||
else | |||
{ | |||
onComplete?.Invoke(RegResult.RegFailure); | |||
} | |||
return regResult; | |||
} | |||
} | |||
} | |||
public enum RegResult | |||
{ | |||
RegSuccess, | |||
RegFailure, | |||
ParseError, | |||
UnregSuccess, | |||
//UnregFailure | |||
} | |||
} | |||
} |
@@ -92,7 +92,7 @@ namespace Shadowsocks.Controller | |||
public void Start() | |||
{ | |||
Reload(); | |||
StartupHotkeyReg.RegHotkey(); | |||
HotkeyReg.RegAllHotkeys(); | |||
} | |||
protected void ReportError(Exception e) | |||
@@ -1,68 +0,0 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Windows.Forms; | |||
using GlobalHotKey; | |||
using Shadowsocks.Controller.Hotkeys; | |||
using Shadowsocks.Model; | |||
using System.Reflection; | |||
namespace Shadowsocks.Controller | |||
{ | |||
static class StartupHotkeyReg | |||
{ | |||
public static void RegHotkey() | |||
{ | |||
var _hotKeyConf = Configuration.Load().hotkey; | |||
if (_hotKeyConf == null || !_hotKeyConf.RegHotkeysAtStartup) | |||
return; | |||
var _hotKeyDic = new Dictionary<HotKey, HotKeys.HotKeyCallBackHandler>(); | |||
try | |||
{ | |||
MethodInfo[] fis = typeof(HotkeyCallbacks).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); | |||
Type ht = _hotKeyConf.GetType(); | |||
for (int i = 0; i < fis.Length; i++) | |||
{ | |||
if (fis[i].Name.EndsWith("Callback")) | |||
{ | |||
var callbackName = fis[i].Name; | |||
var fieldName = callbackName.Replace("Callback", ""); | |||
var hk = HotKeys.Str2HotKey(ht.GetField(fieldName).GetValue(_hotKeyConf) as string); | |||
var cb = HotkeyCallbacks.GetCallback(callbackName) as HotKeys.HotKeyCallBackHandler; | |||
if (hk != null && cb != null) | |||
{ | |||
_hotKeyDic.Add(hk, cb); | |||
} | |||
} | |||
} | |||
int regCount = 0; | |||
foreach (var v in _hotKeyDic) | |||
{ | |||
if (!HotKeys.RegHotkey(v.Key, v.Value)) | |||
{ | |||
foreach (var k in _hotKeyDic) | |||
{ | |||
if (regCount > 0) | |||
{ | |||
HotKeys.UnregExistingHotkey(k.Value); | |||
regCount--; | |||
} | |||
} | |||
MessageBox.Show(I18N.GetString("Register hotkey failed"), I18N.GetString("Shadowsocks")); | |||
return; | |||
} | |||
regCount++; | |||
} | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.Error(e); | |||
} | |||
} | |||
} | |||
} |
@@ -2,15 +2,13 @@ | |||
using System.Collections.Generic; | |||
using System.Drawing; | |||
using System.Linq; | |||
using System.Reflection; | |||
using System.Text; | |||
using System.Windows.Forms; | |||
using Shadowsocks.Controller; | |||
using Shadowsocks.Controller.Hotkeys; | |||
using Shadowsocks.Model; | |||
using Shadowsocks.Properties; | |||
using Shadowsocks.Util; | |||
using static Shadowsocks.Controller.HotkeyReg; | |||
namespace Shadowsocks.View | |||
{ | |||
@@ -174,52 +172,33 @@ namespace Shadowsocks.View | |||
private bool RegisterAllHotkeys(HotkeyConfig hotkeyConfig) | |||
{ | |||
return | |||
RegHotkeyFromString(hotkeyConfig.SwitchSystemProxy, "SwitchSystemProxyCallback", SwitchSystemProxyLabel) | |||
&& RegHotkeyFromString(hotkeyConfig.SwitchSystemProxyMode, "SwitchSystemProxyModeCallback", SwitchProxyModeLabel) | |||
&& RegHotkeyFromString(hotkeyConfig.SwitchAllowLan, "SwitchAllowLanCallback", SwitchAllowLanLabel) | |||
&& RegHotkeyFromString(hotkeyConfig.ShowLogs, "ShowLogsCallback", ShowLogsLabel) | |||
&& RegHotkeyFromString(hotkeyConfig.ServerMoveUp, "ServerMoveUpCallback", ServerMoveUpLabel) | |||
&& RegHotkeyFromString(hotkeyConfig.ServerMoveDown, "ServerMoveDownCallback", ServerMoveDownLabel); | |||
RegHotkeyFromString(hotkeyConfig.SwitchSystemProxy, "SwitchSystemProxyCallback", result => HandleRegResult(hotkeyConfig.SwitchSystemProxy, SwitchSystemProxyLabel, result)) | |||
&& RegHotkeyFromString(hotkeyConfig.SwitchSystemProxyMode, "SwitchSystemProxyModeCallback", result => HandleRegResult(hotkeyConfig.SwitchSystemProxyMode, SwitchProxyModeLabel, result)) | |||
&& RegHotkeyFromString(hotkeyConfig.SwitchAllowLan, "SwitchAllowLanCallback", result => HandleRegResult(hotkeyConfig.SwitchAllowLan, SwitchAllowLanLabel, result)) | |||
&& RegHotkeyFromString(hotkeyConfig.ShowLogs, "ShowLogsCallback", result => HandleRegResult(hotkeyConfig.ShowLogs, ShowLogsLabel, result)) | |||
&& RegHotkeyFromString(hotkeyConfig.ServerMoveUp, "ServerMoveUpCallback", result => HandleRegResult(hotkeyConfig.ServerMoveUp, ServerMoveUpLabel, result)) | |||
&& RegHotkeyFromString(hotkeyConfig.ServerMoveDown, "ServerMoveDownCallback", result => HandleRegResult(hotkeyConfig.ServerMoveDown, ServerMoveDownLabel, result)); | |||
} | |||
private bool RegHotkeyFromString(string hotkeyStr, string callbackName, Label indicator = null) | |||
private void HandleRegResult(string hotkeyStr, Label label, RegResult result) | |||
{ | |||
var _callback = HotkeyCallbacks.GetCallback(callbackName); | |||
if (_callback == null) | |||
switch (result) | |||
{ | |||
throw new Exception($"{callbackName} not found"); | |||
} | |||
var callback = _callback as HotKeys.HotKeyCallBackHandler; | |||
if (hotkeyStr.IsNullOrEmpty()) | |||
{ | |||
HotKeys.UnregExistingHotkey(callback); | |||
if (indicator != null) | |||
{ | |||
indicator.ResetBackColor(); | |||
} | |||
return true; | |||
} | |||
else | |||
{ | |||
var hotkey = HotKeys.Str2HotKey(hotkeyStr); | |||
if (hotkey == null) | |||
{ | |||
case RegResult.ParseError: | |||
MessageBox.Show(string.Format(I18N.GetString("Cannot parse hotkey: {0}"), hotkeyStr)); | |||
return false; | |||
} | |||
else | |||
{ | |||
bool regResult = (HotKeys.RegHotkey(hotkey, callback)); | |||
if (indicator != null) | |||
{ | |||
indicator.BackColor = regResult ? Color.Green : Color.Yellow; | |||
} | |||
return regResult; | |||
} | |||
break; | |||
case RegResult.UnregSuccess: | |||
label.ResetBackColor(); | |||
break; | |||
case RegResult.RegSuccess: | |||
label.BackColor = Color.Green; | |||
break; | |||
case RegResult.RegFailure: | |||
label.BackColor = Color.Red; | |||
break; | |||
default: | |||
break; | |||
} | |||
} | |||
} | |||
} |
@@ -100,7 +100,7 @@ | |||
</Reference> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<Compile Include="Controller\StartupHotkeyReg.cs" /> | |||
<Compile Include="Controller\HotkeyReg.cs" /> | |||
<Compile Include="Controller\System\Hotkeys\HotkeyCallbacks.cs" /> | |||
<Compile Include="Encryption\AEAD\AEADEncryptor.cs" /> | |||
<Compile Include="Encryption\AEAD\AEADMbedTLSEncryptor.cs" /> | |||