From 8017bdecbaf05aa0c2fe10b4c6f0de7161eb17fa Mon Sep 17 00:00:00 2001 From: StudentEx Date: Wed, 19 Sep 2018 12:51:52 +0800 Subject: [PATCH] Throw a more clear exception when parse fail. --- .../Util/SystemProxy/Sysproxy.cs | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/shadowsocks-csharp/Util/SystemProxy/Sysproxy.cs b/shadowsocks-csharp/Util/SystemProxy/Sysproxy.cs index d409b00d..d7bb46d7 100644 --- a/shadowsocks-csharp/Util/SystemProxy/Sysproxy.cs +++ b/shadowsocks-csharp/Util/SystemProxy/Sysproxy.cs @@ -149,8 +149,10 @@ namespace Shadowsocks.Util.SystemProxy throw new ProxyException(stderr); } - if (arguments == "query") { - if (stdout.IsNullOrWhiteSpace() || stdout.IsNullOrEmpty()) { + if (arguments == "query") + { + if (stdout.IsNullOrWhiteSpace() || stdout.IsNullOrEmpty()) + { // we cannot get user settings throw new ProxyException("failed to query wininet settings"); } @@ -183,9 +185,13 @@ namespace Shadowsocks.Util.SystemProxy { string configContent = File.ReadAllText(Utils.GetTempPath(_userWininetConfigFile)); _userSettings = JsonConvert.DeserializeObject(configContent); - } catch(Exception) { + } + catch (Exception) + { // Suppress all exceptions. finally block will initialize new user config settings. - } finally { + } + finally + { if (_userSettings == null) _userSettings = new SysproxyConfig(); } } @@ -196,13 +202,18 @@ namespace Shadowsocks.Util.SystemProxy // sometimes sysproxy output in utf16le instead of ascii // manually translate it - if (userSettingsArr.Length == 1) + if (userSettingsArr.Length != 4) { byte[] strByte = Encoding.ASCII.GetBytes(str); str = Encoding.Unicode.GetString(strByte); userSettingsArr = str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); + // still fail, throw exception with string hexdump + if (userSettingsArr.Length != 4) + { + throw new ProxyException("Unexpected sysproxy output:" + BitConverter.ToString(strByte)); + } } - + _userSettings.Flags = userSettingsArr[0]; // handle output from WinINET