Skip to content

Commit afea522

Browse files
authored
Release 3.1.0 (#340)
* Don't blow away config file directly Only change config file if temp file save was successful AND it can be moved over top of existing config file * New string * Update nugets * New string * Updates * Strip hyphens from firewalld version * Support Kingfisher ssh * Reduce regex * Interface method to delete guid * Allow specify guid * Expose version * Ensure proper encoding for app settings * Don't throw if no appsettings element * Pretty print * More resilient config handling * Update nuget * Fix wrong format index * Typo * Typo * Only dequeue if lock acquired * Assign back * New string * Add linux-arm and linux-arm64 builds * Update nugets * More log info * New string * New string * Update nugets * Don't allow changing prefixes once set * Refactoring Also added new TempFile class * Refactor * Force debug log level when debugging * Refactorings/bug fixes * Bug fixes from command line refactor * Missed an arg: set * Linux tests almost fixed * Fix * Fix the last iptables test * Got linux iptables passing * NFTables firewall done. Needs testing. * Get nftables working * Casing * Optimization For smaller requests, we can go directly over stdin/stdout, eliminating file IO * Sets must use file In case of large datasets, use file for sets instead of memory stream * Use CanSeek * Flush later * Ensure nftables installed * Default to dnf package manager fot redhat/centos * Fix null ref * Nftables use native library * Small optimization * Profiler * Use sudo * Typos
1 parent 7d716f7 commit afea522

File tree

103 files changed

+3323
-409
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+3323
-409
lines changed

IPBan/CreatePackage.ps1

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,33 @@ function CodeSign($folder, $checkFail)
3939
# IPBan Linux x64
4040
& dotnet restore -r linux-x64; CheckFail
4141
& dotnet clean -c Release; CheckFail
42-
& dotnet publish IPBan.csproj --self-contained -f $netFrameworkVersion -o package/linux-x64 -c Release -r linux-x64 /p:DebuggerSupport=false /p:CopyOutputSymbolsToPublishDirectory=false /p:Version=$versionDots /p:AssemblyVersion=$versionDots /p:FileVersion=$versionDots /p:PublishTrimmed=$publishTrimmed /p:PublishSingleFile=true /p:IncludeNativeLibrariesForSelfExtract=true; CheckFail
42+
& dotnet publish IPBan.csproj --self-contained -f $netFrameworkVersion -o package/linux-x64 -c Release -r linux-x64 /p:DebuggerSupport=false /p:CopyOutputSymbolsToPublishDirectory=false /p:Version=$versionDots /p:AssemblyVersion=$versionDots /p:FileVersion=$versionDots /p:PublishTrimmed=$publishTrimmed /p:PublishSingleFile=true /p:IncludeNativeLibrariesForSelfExtract=true /p:PublishReadyToRun=false; CheckFail
43+
44+
# IPBan Arm Linux-arm
45+
& dotnet restore -r linux-arm; CheckFail
46+
& dotnet clean -c Release; CheckFail
47+
& dotnet publish IPBan.csproj --self-contained -f $netFrameworkVersion -o package/linux-arm -c Release -r linux-arm /p:Version=$versionDots /p:AssemblyVersion=$versionDots /p:FileVersion=$versionDots /p:PublishTrimmed=$publishTrimmed /p:PublishSingleFile=true /p:IncludeNativeLibrariesForSelfExtract=true /p:PublishReadyToRun=false; CheckFail
48+
49+
# IPBan Arm Linux-arm64
50+
& dotnet restore -r linux-arm64; CheckFail
51+
& dotnet clean -c Release; CheckFail
52+
& dotnet publish IPBan.csproj --self-contained -f $netFrameworkVersion -o package/linux-arm64 -c Release -r linux-arm64 /p:Version=$versionDots /p:AssemblyVersion=$versionDots /p:FileVersion=$versionDots /p:PublishTrimmed=$publishTrimmed /p:PublishSingleFile=true /p:IncludeNativeLibrariesForSelfExtract=true /p:PublishReadyToRun=false; CheckFail
4353

4454
# IPBan Windows x64
4555
& dotnet restore -r win-x64; CheckFail
4656
& dotnet clean -c Release; CheckFail
47-
& dotnet publish IPBan.csproj --self-contained -f $netFrameworkVersion -o package/win-x64 -c Release -r win-x64 /p:DebuggerSupport=false /p:CopyOutputSymbolsToPublishDirectory=false /p:Version=$versionDots /p:AssemblyVersion=$versionDots /p:FileVersion=$versionDots /p:PublishTrimmed=$publishTrimmed /p:PublishSingleFile=true /p:IncludeNativeLibrariesForSelfExtract=true; CheckFail
57+
& dotnet publish IPBan.csproj --self-contained -f $netFrameworkVersion -o package/win-x64 -c Release -r win-x64 /p:DebuggerSupport=false /p:CopyOutputSymbolsToPublishDirectory=false /p:Version=$versionDots /p:AssemblyVersion=$versionDots /p:FileVersion=$versionDots /p:PublishTrimmed=$publishTrimmed /p:PublishSingleFile=true /p:IncludeNativeLibrariesForSelfExtract=true /p:PublishReadyToRun=false; CheckFail
4858
CodeSign package/win-x64 CheckFail
4959

5060
# IPBan Windows x86
5161
& dotnet restore -r win-x86; CheckFail
5262
& dotnet clean -c Release; CheckFail
53-
& dotnet publish IPBan.csproj --self-contained -f $netFrameworkVersion -o package/win-x86 -c Release -r win-x86 /p:Version=$versionDots /p:AssemblyVersion=$versionDots /p:FileVersion=$versionDots /p:PublishTrimmed=$publishTrimmed /p:PublishSingleFile=true /p:IncludeNativeLibrariesForSelfExtract=true; CheckFail
63+
& dotnet publish IPBan.csproj --self-contained -f $netFrameworkVersion -o package/win-x86 -c Release -r win-x86 /p:Version=$versionDots /p:AssemblyVersion=$versionDots /p:FileVersion=$versionDots /p:PublishTrimmed=$publishTrimmed /p:PublishSingleFile=true /p:IncludeNativeLibrariesForSelfExtract=true /p:PublishReadyToRun=false; CheckFail
5464
CodeSign package/win-x86 CheckFail
5565

5666
Compress-Archive -Path ./package/linux-x64/* -DestinationPath ./package/IPBan-Linux-x64_$version.zip; CheckFail
67+
Compress-Archive -Path ./package/linux-arm/* -DestinationPath ./package/IPBan-Linux-Arm_$version.zip; CheckFail
68+
Compress-Archive -Path ./package/linux-arm64/* -DestinationPath ./package/IPBan-Linux-Arm64_$version.zip; CheckFail
5769
Compress-Archive -Path ./package/win-x64/* -DestinationPath ./package/IPBan-Windows-x64_$version.zip; CheckFail
5870
Compress-Archive -Path ./package/win-x86/* -DestinationPath ./package/IPBan-Windows-x86_$version.zip; CheckFail
5971

IPBan/IPBanMain.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
MIT License
33
4-
Copyright (c) 2019 Digital Ruby, LLC - https://www.digitalruby.com
4+
Copyright (c) 2019 Digital Ruby, LLC - https://ipban.com
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal

IPBan/nlog.config

Lines changed: 0 additions & 11 deletions
This file was deleted.

IPBanCore/Core/IPBan/IPBanBaseFirewall.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
MIT License
33
4-
Copyright (c) 2012-present Digital Ruby, LLC - https://www.digitalruby.com
4+
Copyright (c) 2012-present Digital Ruby, LLC - https://ipban.com
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal
@@ -46,17 +46,17 @@ public abstract class IPBanBaseFirewall : IIPBanFirewall
4646
/// <summary>
4747
/// Rule prefix - defaults to 'IPBan_'
4848
/// </summary>
49-
public string RulePrefix { get; set; } = "IPBan_";
49+
public string RulePrefix { get; private set; } = "IPBan_";
5050

5151
/// <summary>
5252
/// Allow rule prefix
5353
/// </summary>
54-
public string AllowRulePrefix { get; set; }
54+
public string AllowRulePrefix { get; private set; }
5555

5656
/// <summary>
5757
/// Block rule prefix
5858
/// </summary>
59-
public string BlockRulePrefix { get; set; }
59+
public string BlockRulePrefix { get; private set; }
6060

6161
/// <summary>
6262
/// Packet event handler
@@ -136,6 +136,14 @@ public virtual Task Update(CancellationToken cancelToken = default)
136136
return Task.CompletedTask;
137137
}
138138

139+
/// <summary>
140+
/// Clear prefixes - use with caution!
141+
/// </summary>
142+
public void ClearPrefixes()
143+
{
144+
RulePrefix = AllowRulePrefix = BlockRulePrefix = string.Empty;
145+
}
146+
139147
/// <summary>
140148
/// Compile into an optimized in memory representation
141149
/// </summary>
@@ -161,10 +169,10 @@ public virtual Task Update(CancellationToken cancelToken = default)
161169
public virtual IReadOnlyList<(bool blocked, bool allowed, string ruleName)> Query(IReadOnlyCollection<System.Net.IPEndPoint> ipAddresses)
162170
{
163171
List<(bool, bool, string)> result = [];
164-
var memoryFirewall = Compile();
172+
var mem = Compile();
165173
foreach (var ipAddress in ipAddresses)
166174
{
167-
var blocked = memoryFirewall.IsIPAddressBlocked(ipAddress.Address, out var ruleName, out var allowed, ipAddress.Port);
175+
var blocked = mem.IsIPAddressBlocked(ipAddress.Address, out var ruleName, out var allowed, ipAddress.Port);
168176
result.Add((blocked, allowed, ruleName));
169177
}
170178
return result;

IPBanCore/Core/IPBan/IPBanBlockIPAddressesUpdater.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
MIT License
33
4-
Copyright (c) 2012-present Digital Ruby, LLC - https://www.digitalruby.com
4+
Copyright (c) 2012-present Digital Ruby, LLC - https://ipban.com
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal

IPBanCore/Core/IPBan/IPBanConfig.cs

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
MIT License
33
4-
Copyright (c) 2012-present Digital Ruby, LLC - https://www.digitalruby.com
4+
Copyright (c) 2012-present Digital Ruby, LLC - https://ipban.com
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal
@@ -628,33 +628,39 @@ public IReadOnlyCollection<EventViewerExpressionGroup> WindowsEventViewerGetGrou
628628
}
629629

630630
/// <summary>
631-
/// Change an app settings - no XML encoding is done, so ensure your key and new value are already encoded
631+
/// Get an app setting, decoding the XML value
632632
/// </summary>
633-
/// <param name="config">Entire XML config</param>
633+
/// <param name="doc">XML config</param>
634634
/// <param name="key">App setting key to look for</param>
635635
/// <returns>The config value or null if not found</returns>
636-
public static string GetConfigAppSetting(string config, string key)
636+
public static string GetConfigAppSetting(XmlDocument doc, string key)
637637
{
638-
string find = $@"\<add key=""{key}"" value=""(?<value>[^""]*)"" *\/\>";
639-
Match match = Regex.Match(config, find, RegexOptions.IgnoreCase);
640-
return (match is null || !match.Success ? null : match.Groups["value"].Value);
638+
if (string.IsNullOrWhiteSpace(key))
639+
{
640+
return null;
641+
}
642+
643+
XmlNode existingSetting = doc.SelectSingleNode($"/configuration/appSettings/add[@key='{key}']");
644+
if (existingSetting is null)
645+
{
646+
return null;
647+
}
648+
649+
return existingSetting.Attributes["value"].Value;
641650
}
642651

643652
/// <summary>
644-
/// Change an app settings - no XML encoding is done, so ensure your key and new value are already encoded
653+
/// Change an app setting, XML encoding the value
645654
/// </summary>
646-
/// <param name="config">Entire XML config</param>
655+
/// <param name="doc">XML config</param>
647656
/// <param name="key">App setting key to look for</param>
648657
/// <param name="newValue">Replacement value</param>
649-
/// <returns>Modified config or the config passed in if not found</returns>
650-
public static string ChangeConfigAppSetting(string config, string key, string newValue)
658+
public static void ChangeConfigAppSetting(XmlDocument doc, string key, string newValue)
651659
{
652660
newValue ??= string.Empty;
653661

654-
XmlDocument doc = new();
655-
doc.LoadXml(config);
656-
XmlNode appSettings = doc.SelectSingleNode($"/configuration/appSettings") ?? throw new InvalidOperationException("Unable to find appSettings in config");
657662
XmlNode existingSetting = doc.SelectSingleNode($"/configuration/appSettings/add[@key='{key}']");
663+
658664
if (existingSetting is null)
659665
{
660666
existingSetting = doc.CreateElement("add");
@@ -664,13 +670,41 @@ public static string ChangeConfigAppSetting(string config, string key, string ne
664670
XmlAttribute valueAttr = doc.CreateAttribute("value");
665671
valueAttr.Value = newValue;
666672
existingSetting.Attributes.Append(valueAttr);
673+
674+
XmlNode appSettings = doc.SelectSingleNode($"/configuration/appSettings");
675+
676+
// if not found, create appSettings node
677+
if (appSettings is null)
678+
{
679+
var configuration = doc.SelectSingleNode("/configuration");
680+
if (configuration is null)
681+
{
682+
configuration = doc.PrependChild(doc.CreateElement("configuration"));
683+
}
684+
appSettings = configuration.AppendChild(doc.CreateElement("appSettings"));
685+
686+
}
667687
appSettings.AppendChild(existingSetting);
668688
}
669689
else
670690
{
671691
existingSetting.Attributes["value"].Value = newValue;
672692
}
673-
return doc.OuterXml;
693+
}
694+
695+
/// <summary>
696+
/// Change an app setting, XML encoding the value
697+
/// </summary>
698+
/// <param name="xml">XML config</param>
699+
/// <param name="key">App setting key to look for</param>
700+
/// <param name="newValue">Replacement value</param>
701+
/// <returns>New XML config</returns>
702+
public static string ChangeConfigAppSettingAndGetXml(string xml, string key, string newValue)
703+
{
704+
var doc = new XmlDocument();
705+
doc.LoadXml(xml);
706+
ChangeConfigAppSetting(doc, key, newValue);
707+
return doc.PrettyPrint();
674708
}
675709

676710
/// <summary>

IPBanCore/Core/IPBan/IPBanConfigReaderWriter.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
MIT License
33
4-
Copyright (c) 2012-present Digital Ruby, LLC - https://www.digitalruby.com
4+
Copyright (c) 2012-present Digital Ruby, LLC - https://ipban.com
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal
@@ -83,20 +83,26 @@ await ExtensionMethods.RetryAsync(async () =>
8383
/// <returns>Task</returns>
8484
public async Task WriteConfigAsync(string config)
8585
{
86-
if (!Enabled)
86+
if (!Enabled ||
87+
string.IsNullOrWhiteSpace(config))
8788
{
8889
return;
8990
}
90-
else if (UseFile)
91+
92+
config = config.Trim().Replace("\0", string.Empty).Normalize();
93+
94+
if (UseFile)
9195
{
9296
await ConfigLocker.LockActionAsync(async () =>
9397
{
9498
// don't perform needless file write if config is identical
9599
string existingConfig = await File.ReadAllTextAsync(Path);
96100
if (existingConfig != config)
97101
{
102+
string tempConfig = System.IO.Path.Combine(Path + ".tmp");
103+
await ExtensionMethods.FileWriteAllTextWithRetryAsync(tempConfig, config);
104+
ExtensionMethods.Retry(() => File.Move(tempConfig, Path, true));
98105
lastConfigValue = null;
99-
await ExtensionMethods.FileWriteAllTextWithRetryAsync(Path, config);
100106
}
101107
});
102108
}

IPBanCore/Core/IPBan/IPBanConfigWindowsEventViewer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
MIT License
33
4-
Copyright (c) 2012-present Digital Ruby, LLC - https://www.digitalruby.com
4+
Copyright (c) 2012-present Digital Ruby, LLC - https://ipban.com
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal

IPBanCore/Core/IPBan/IPBanDB.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
MIT License
33
4-
Copyright (c) 2012-present Digital Ruby, LLC - https://www.digitalruby.com
4+
Copyright (c) 2012-present Digital Ruby, LLC - https://ipban.com
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal

IPBanCore/Core/IPBan/IPBanDnsServerList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
MIT License
33
4-
Copyright (c) 2012-present Digital Ruby, LLC - https://www.digitalruby.com
4+
Copyright (c) 2012-present Digital Ruby, LLC - https://ipban.com
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal

0 commit comments

Comments
 (0)