Skip to content

Commit 34cb692

Browse files
committed
Improve event viewer query creation performance
1 parent 5787603 commit 34cb692

6 files changed

+21
-13
lines changed

Core/IPBanConfigWindowsEventViewer.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,18 @@ public string Keywords
145145
}
146146
}
147147

148-
public string GetQueryString(int id = 1)
148+
public void AppendQueryString(StringBuilder builder, int id = 1)
149149
{
150150
ulong keywordsDecimal = ulong.Parse(Keywords.Substring(2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
151-
return "<Query Id='" + id.ToString(CultureInfo.InvariantCulture) + "' Path='" + Path + "'><Select Path='" + Path + "'>*[System[(band(Keywords," + keywordsDecimal.ToString() + "))]]</Select></Query>";
151+
builder.Append("<Query Id='");
152+
builder.Append(id.ToStringInvariant());
153+
builder.Append("' Path='");
154+
builder.Append(Path);
155+
builder.Append("'><Select Path='");
156+
builder.Append(Path);
157+
builder.Append("'>*[System[(band(Keywords,");
158+
builder.Append(keywordsDecimal.ToStringInvariant());
159+
builder.Append("))]]</Select></Query>");
152160
}
153161

154162
public void SetExpressionsFromExpressionsText()

Core/IPBanMemoryFirewall.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public bool Contains(UInt128 ipv6UInt128)
256256
private readonly Dictionary<string, MemoryFirewallRule> blockRules = new Dictionary<string, MemoryFirewallRule>();
257257
private readonly MemoryFirewallRule allowRule = new MemoryFirewallRule();
258258

259-
public string RulePrefix { get; set; }
259+
public string RulePrefix { get; set; } = "IPBan_";
260260

261261
private string ScrubRuleNamePrefix(string ruleNamePrefix)
262262
{
@@ -390,8 +390,8 @@ public IEnumerable<string> GetRuleNames(string ruleNamePrefix = null)
390390
{
391391
yield return key;
392392
}
393-
if (prefix.StartsWith(RulePrefix, StringComparison.OrdinalIgnoreCase) ||
394-
prefix.StartsWith(RulePrefix + "Allow", StringComparison.OrdinalIgnoreCase))
393+
if (RulePrefix.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) ||
394+
RulePrefix.StartsWith(prefix + "Allow", StringComparison.OrdinalIgnoreCase))
395395
{
396396
yield return RulePrefix + "Allow";
397397
}

Core/IPBanService.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,6 @@ public static T CreateAndStartIPBanTestService<T>(string directory = null, strin
12971297
string configFilePath = Path.Combine(directory, configFileName);
12981298
string configFileText = File.ReadAllText(configFilePath);
12991299
configFilePath += ".tmp";
1300-
configFileText = configFileText.Replace("<add key=\"UseDefaultBannedIPAddressHandler\" value=\"true\" />", "<add key=\"UseDefaultBannedIPAddressHandler\" value=\"false\" />");
13011300
if (configFileModifier != null)
13021301
{
13031302
configFileText = configFileModifier(configFileText);

IPBanTests/IPBanConfigTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public void TestDefaultConfig()
152152
Assert.AreEqual("IPBan_", cfg.FirewallRulePrefix);
153153
Assert.AreEqual(TimeSpan.FromSeconds(1.0), cfg.MinimumTimeBetweenFailedLoginAttempts);
154154
Assert.IsEmpty(cfg.ProcessToRunOnBan);
155-
Assert.IsFalse(cfg.UseDefaultBannedIPAddressHandler); // the create and start test service forces this false, it is true otherwise in production by default
155+
Assert.IsTrue(cfg.UseDefaultBannedIPAddressHandler);
156156
Assert.IsEmpty(cfg.UserNameWhitelist);
157157
Assert.IsEmpty(cfg.WhiteList);
158158
Assert.IsEmpty(cfg.WhiteListRegex);

IPBanTests/IPBanMemoryFirewallTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void BasicTest()
2727
f.BlockIPAddresses("TestRule", new IPAddressRange[] { range }, new PortRange[0]);
2828
string[] banned = f.EnumerateBannedIPAddresses().ToArray();
2929
IPAddressRange[] banned2 = f.EnumerateIPAddresses("TestRule").ToArray();
30-
30+
Assert.AreEqual(0, f.GetRuleNames("CB").Count());
3131
Assert.IsTrue(f.IsIPAddressAllowed(allowIP));
3232
Assert.IsFalse(f.IsIPAddressBlocked(allowIP));
3333
Assert.IsFalse(f.IsIPAddressBlocked(otherIP));

Windows/IPBanWindowsEventViewer.cs

+6-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2828
using System.Diagnostics.Eventing.Reader;
2929
using System.Globalization;
3030
using System.IO;
31+
using System.Text;
3132
using System.Text.RegularExpressions;
3233
using System.Threading.Tasks;
3334
using System.Xml;
@@ -228,8 +229,8 @@ private string GetEventLogQueryString(List<string> ignored)
228229
return null;
229230
}
230231

232+
StringBuilder queryString = new StringBuilder("<QueryList>");
231233
int id = 0;
232-
string queryString = "<QueryList>";
233234
HashSet<string> logNames = new HashSet<string>(System.Diagnostics.Eventing.Reader.EventLogSession.GlobalSession.GetLogNames());
234235
foreach (EventViewerExpressionGroup group in service.Config.WindowsEventViewerExpressionsToBlock.Groups)
235236
{
@@ -241,12 +242,12 @@ private string GetEventLogQueryString(List<string> ignored)
241242
}
242243
else
243244
{
244-
queryString += group.GetQueryString(++id);
245+
group.AppendQueryString(queryString, ++id);
245246
}
246247
}
247-
queryString += "</QueryList>";
248+
queryString.Append("</QueryList>");
248249

249-
return queryString;
250+
return queryString.Length < 32 ? null : queryString.ToString();
250251
}
251252

252253
private void SetupEventLogWatcher()
@@ -255,7 +256,7 @@ private void SetupEventLogWatcher()
255256
{
256257
List<string> ignored = new List<string>();
257258
string queryString = GetEventLogQueryString(ignored);
258-
if (queryString != previousQueryString)
259+
if (queryString != null && queryString != previousQueryString)
259260
{
260261
IPBanLog.Warn("Event viewer query string: {0}", queryString);
261262
foreach (string path in ignored)

0 commit comments

Comments
 (0)