Skip to content

Commit

Permalink
Don't log force reload of same config
Browse files Browse the repository at this point in the history
  • Loading branch information
jjxtra committed Nov 9, 2020
1 parent ca8a594 commit b0af7ec
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
14 changes: 7 additions & 7 deletions IPBanCore/Core/IPBan/IPBanConfigReaderWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ await Locker.LockActionAsync(async () =>
/// <summary>
/// Check for config change
/// </summary>
/// <returns>Task of config string, will be a null string if no change</returns>
public async Task<string> CheckForConfigChange()
/// <returns>Task of config string and force bool, will be a null string if no change</returns>
public async Task<(string, bool)> CheckForConfigChange()
{
(string, bool) result = new(null, false);
if (UseFile)
{
string result = null;
await Locker.LockActionAsync(async () =>
{
DateTime lastWriteTime = File.GetLastWriteTimeUtc(Path);
Expand All @@ -117,22 +117,22 @@ await Locker.LockActionAsync(async () =>

// if enough time has elapsed, force a reload anyway, in case of dns entries and the
// like in the config that need to be re-resolved
IPBanService.UtcNow - lastConfigIntervalTime > forceLoadInterval)
(result.Item2 = IPBanService.UtcNow - lastConfigIntervalTime > forceLoadInterval))
{
lastConfigWriteTime = lastWriteTime;
lastConfigValue = currentConfig;
lastConfigIntervalTime = IPBanService.UtcNow;
result = currentConfig;
result.Item1 = currentConfig;
}
});
return result;
}
else if (GlobalConfigString != localConfigString)
{
localConfigString = GlobalConfigString;
return localConfigString;
return new(localConfigString, false);
}
return null;
return result;
}

/// <summary>
Expand Down
13 changes: 9 additions & 4 deletions IPBanCore/Core/IPBan/IPBanService_Private.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,22 @@ internal async Task UpdateConfiguration()
try
{
ConfigFilePath = (!File.Exists(ConfigFilePath) ? Path.Combine(AppContext.BaseDirectory, IPBanConfig.DefaultFileName) : ConfigFilePath);
string newXml = await ConfigReaderWriter.CheckForConfigChange();
if (!string.IsNullOrWhiteSpace(newXml))
var configChange = await ConfigReaderWriter.CheckForConfigChange();
if (!string.IsNullOrWhiteSpace(configChange.Item1))
{
IPBanConfig oldConfig = Config;
IPBanConfig newConfig = IPBanConfig.LoadFromXml(newXml, DnsLookup, DnsList, RequestMaker);
IPBanConfig newConfig = IPBanConfig.LoadFromXml(configChange.Item1, DnsLookup, DnsList, RequestMaker);
ConfigChanged?.Invoke(newConfig);
whitelistChanged = (Config is null || Config.Whitelist != newConfig.Whitelist || Config.WhitelistRegex != newConfig.WhitelistRegex);
Config = newConfig;
LoadFirewall(oldConfig);
ParseAndAddUriFirewallRules(newConfig);
Logger.Info("Config file changed");

// if the config change was not a force refresh with no actual config values changed, log it
if (!configChange.Item2)
{
Logger.Info("Config file changed");
}
}
}
catch (Exception ex)
Expand Down

0 comments on commit b0af7ec

Please sign in to comment.