diff --git a/src/DAO/TFASites.cs b/src/DAO/TFASites.cs index a621f22..bd19ac0 100644 --- a/src/DAO/TFASites.cs +++ b/src/DAO/TFASites.cs @@ -2,7 +2,6 @@ using KeePass.Resources; using KeePassLib.Serialization; using KeePassLib.Utility; -using Newtonsoft.Json.Linq; using PluginTools; using PluginTranslation; using System; @@ -176,21 +175,31 @@ private static void RetryReadOTPSites(object s) m_RetryReadOTPSites.Enabled = false; } - private static string GetJSonString(JToken t, string sName) + private static string GetJSonString(string sSearch, string v) { - string sResult = t.Value(sName); - return string.IsNullOrEmpty(sResult) ? string.Empty : sResult; + //Case 1: Simple string + Regex r = new Regex(@"""" + v + @""":""(.*?)"""); + var aMatches = r.Matches(sSearch); + if (aMatches != null && aMatches.Count > 0) + { + return aMatches[0].Groups[1].Value; + } + + //Case 2: Multiple values + r = new Regex(@""""+ v + @""":\[(.*?)\]"); + aMatches = r.Matches(sSearch); + if (aMatches == null) return string.Empty; + if (aMatches.Count < 1) return string.Empty; + return aMatches[0].Groups[1].Value; } - private static List GetJSonList(JToken jt, string sName) + private static List GetJSonList(string sSearch, string v) { - List lResult = new List(); - var jArrayName = jt.Value(sName); - if (jArrayName == null) return lResult; - foreach (var at in jArrayName.Children().ToList()) lResult.Add(at.ToString()); - return lResult; - } + string s = GetJSonString(sSearch, v); + if (string.IsNullOrEmpty(s)) return new List(); + return s.Split(new string[] { "\",\"", "\"" }, StringSplitOptions.RemoveEmptyEntries).ToList(); + } private static void ReadOTPSites(object s) { lock (m_TFAReadLock) @@ -201,7 +210,7 @@ private static void ReadOTPSites(object s) m_LoadState = TFALoadProcess.Loading; } m_dTFA.Clear(); - JArray ja = null; + List lTFAEntries = new List(); bool bException = false; IOConnectionInfo ioc = IOConnectionInfo.FromPath(TFA_JSON_FILE); bool bExists = false; @@ -214,8 +223,7 @@ private static void ReadOTPSites(object s) if (b != null) { string content = StrUtil.Utf8.GetString(b); - ja = Newtonsoft.Json.JsonConvert.DeserializeObject(content) as JArray; - + lTFAEntries = ParseJSON(content); } } catch (System.Net.WebException exWeb) @@ -231,7 +239,7 @@ private static void ReadOTPSites(object s) PluginDebug.AddError("Error reading OTP sites", 0, "Error: " + ex.Message); bException = true; } - if (ja == null) + if (lTFAEntries == null || lTFAEntries.Count < 1) { if (!bExists || bNoInternet) { @@ -243,30 +251,27 @@ private static void ReadOTPSites(object s) return; } DateTime dtStart = DateTime.Now; - - foreach (JToken jtEntryContainer in ja) - { - var lEntry = jtEntryContainer.Children().ToList(); - if (lEntry.Count != 2) continue; - JToken jtEntry = lEntry[1]; + Dictionary dTFAEntries = new Dictionary(); + foreach (string tfaentry in lTFAEntries) + { TFAData tfa = new TFAData(); - tfa.domain = GetJSonString(jtEntry, "domain"); + tfa.domain = GetJSonString(tfaentry, "domain"); string sDomain = tfa.domain.ToLowerInvariant(); if (!sDomain.StartsWith("http://") && !sDomain.StartsWith("https://")) tfa.domain = "https://" + tfa.domain; - tfa.img = GetJSonString(jtEntry, "img"); - tfa.url = GetJSonString(jtEntry, "url"); + tfa.img = GetJSonString(tfaentry, "img"); + tfa.url = GetJSonString(tfaentry, "url"); if (string.IsNullOrEmpty(tfa.url)) tfa.url = tfa.domain; - tfa.tfa = GetJSonList(jtEntry, "tfa"); - tfa.documentation = GetJSonString(jtEntry, "documentation"); - tfa.recovery = GetJSonString(jtEntry, "recovery"); - tfa.notes = GetJSonString(jtEntry, "notes"); - tfa.contact = GetJSonString(jtEntry, "contact"); - tfa.regions = GetJSonList(jtEntry, "regions"); - tfa.additional_domains = GetJSonList(jtEntry, "additional_domains"); - tfa.custom_software = GetJSonList(jtEntry, "custom_software"); - tfa.custom_hardware = GetJSonList(jtEntry, "custom_hardware"); - tfa.keywords = GetJSonList(jtEntry, "keywords"); + tfa.tfa = GetJSonList(tfaentry, "tfa"); + tfa.documentation = GetJSonString(tfaentry, "documentation"); + tfa.recovery = GetJSonString(tfaentry, "recovery"); + tfa.notes = GetJSonString(tfaentry, "notes"); + tfa.contact = GetJSonString(tfaentry, "contact"); + tfa.regions = GetJSonList(tfaentry, "regions"); + tfa.additional_domains = GetJSonList(tfaentry, "additional_domains"); + tfa.custom_software = GetJSonList(tfaentry, "custom_software"); + tfa.custom_hardware = GetJSonList(tfaentry, "custom_hardware"); + tfa.keywords = GetJSonList(tfaentry, "keywords"); string sRegexPattern = CreatePattern(tfa.domain); tfa.RegexUrl = new Regex(sRegexPattern); m_dTFA[sRegexPattern] = tfa; @@ -279,6 +284,25 @@ private static void ReadOTPSites(object s) } } + private static List ParseJSON(string content) + { + bool bRepeat = true; + MatchCollection aMatches = null; + while (bRepeat) + { + try + { + Regex r = new Regex(@"\[(.*?)\](?=(,\[|\]))", RegexOptions.Singleline); + aMatches = r.Matches(content); + bRepeat = false; + } + catch (Exception ex) { } + } + List lResult = new List(); + foreach (Match m in aMatches) lResult.Add(m.Value); + return lResult; + } + private static string CreatePattern(string url) { //Don't use Uri class for performance reasons diff --git a/src/KeePassOTP.csproj b/src/KeePassOTP.csproj index 3e35f3e..29fba3e 100644 --- a/src/KeePassOTP.csproj +++ b/src/KeePassOTP.csproj @@ -149,16 +149,9 @@ bin\Release\protobuf-net.dll bin\Release\zxing.dll bin\Release\zxing.presentation.dll - bin\Release\Newtonsoft.Json.dll - - - - 3.5.8 - - 1.0.0 diff --git a/src/Properties/AssemblyInfo.cs b/src/Properties/AssemblyInfo.cs index 3674f58..0f3c853 100644 --- a/src/Properties/AssemblyInfo.cs +++ b/src/Properties/AssemblyInfo.cs @@ -30,5 +30,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.4")] -[assembly: AssemblyFileVersion("1.4")] +[assembly: AssemblyVersion("1.5")] +[assembly: AssemblyFileVersion("1.5")] diff --git a/version.info b/version.info index 16f68d8..99fe3f7 100644 --- a/version.info +++ b/version.info @@ -1,5 +1,5 @@ : -KeePassOTP:1.4 +KeePassOTP:1.5 KeePassOTP!de:23 KeePassOTP!fr:7 KeePassOTP!nl:3