Skip to content

Commit

Permalink
Internal improvements
Browse files Browse the repository at this point in the history
Remove reference to outdated Newtonsoft library
Reduce plugin size
  • Loading branch information
Rookiestyle committed Jun 24, 2022
1 parent 5b738e2 commit 04b69b1
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 44 deletions.
92 changes: 58 additions & 34 deletions src/DAO/TFASites.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using KeePass.Resources;
using KeePassLib.Serialization;
using KeePassLib.Utility;
using Newtonsoft.Json.Linq;
using PluginTools;
using PluginTranslation;
using System;
Expand Down Expand Up @@ -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<string>(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<string> GetJSonList(JToken jt, string sName)
private static List<string> GetJSonList(string sSearch, string v)
{
List<string> lResult = new List<string>();
var jArrayName = jt.Value<JArray>(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<string>();

return s.Split(new string[] { "\",\"", "\"" }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
private static void ReadOTPSites(object s)
{
lock (m_TFAReadLock)
Expand All @@ -201,7 +210,7 @@ private static void ReadOTPSites(object s)
m_LoadState = TFALoadProcess.Loading;
}
m_dTFA.Clear();
JArray ja = null;
List<string> lTFAEntries = new List<string>();
bool bException = false;
IOConnectionInfo ioc = IOConnectionInfo.FromPath(TFA_JSON_FILE);
bool bExists = false;
Expand All @@ -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)
Expand All @@ -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)
{
Expand All @@ -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<string, TFAData> dTFAEntries = new Dictionary<string, TFAData>();
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;
Expand All @@ -279,6 +284,25 @@ private static void ReadOTPSites(object s)
}
}

private static List<string> 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<string> lResult = new List<string>();
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
Expand Down
7 changes: 0 additions & 7 deletions src/KeePassOTP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,9 @@
<PlgxReference>bin\Release\protobuf-net.dll</PlgxReference>
<PlgxReference>bin\Release\zxing.dll</PlgxReference>
<PlgxReference>bin\Release\zxing.presentation.dll</PlgxReference>
<PlgxReference>bin\Release\Newtonsoft.Json.dll</PlgxReference>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json">
<!-- Do NOT use newer versions, will conflict with mono verify-all -->
<!-- reference: https://github.com/Rookiestyle/KeePassOTP/issues/79 -->
<Version>3.5.8</Version>
<ExcludeFromPlgx />
</PackageReference>
<PackageReference Include="PlgxTool">
<Version>1.0.0</Version>
<ExcludeFromPlgx />
Expand Down
4 changes: 2 additions & 2 deletions src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
2 changes: 1 addition & 1 deletion version.info
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
:
KeePassOTP:1.4
KeePassOTP:1.5
KeePassOTP!de:23
KeePassOTP!fr:7
KeePassOTP!nl:3
Expand Down

0 comments on commit 04b69b1

Please sign in to comment.