Skip to content

Commit 8e4a775

Browse files
committed
Wait to initialize most things until first cycle
The start async method must not throw an exception or fail, so move most things out of it to the run cycle call. Run cycle can throw or fail and it will re-try on the next cycle. Also updated copyright on most files.
1 parent 9679a51 commit 8e4a775

Some content is hidden

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

58 files changed

+147
-133
lines changed

IPBanCore/Core/IPBan/IPBanBaseFirewall.cs

+1-1
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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/IPBanBlockIPAddressesUpdater.cs

+1-1
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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

+1-1
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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/IPBanConfigReaderWriter.cs

+1-1
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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/IPBanConfigWindowsEventViewer.cs

+1-1
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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

+1-1
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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/IPBanFirewallUtility.cs

+1-1
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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/IPBanLogFileManager.cs

+1-1
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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/IPBanLogFileScanner.cs

+1-1
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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/IPBanLogFileToParse.cs

+1-1
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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/IPBanMemoryFirewall.cs

+1-1
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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/IPBanNullFirewall.cs

+1-1
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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/IPBanService.cs

+53-36
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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
@@ -41,7 +41,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
4141
namespace DigitalRuby.IPBanCore
4242
{
4343
/// <summary>
44-
/// Base ipban service class
44+
/// Base ipban service class. Configuration, firewall and many other properties will
45+
/// not be initialized until the first RunCycle is called.
4546
/// </summary>
4647
public partial class IPBanService : IIPBanService, IIsWhitelisted
4748
{
@@ -80,15 +81,22 @@ where typeOfT.IsAssignableFrom(type)
8081
/// </summary>
8182
public async Task RunCycle()
8283
{
83-
await SetNetworkInfo();
84-
await ReadAppSettings();
85-
await UpdateDelegate();
86-
await UpdateUpdaters();
87-
await UpdateExpiredIPAddressStates();
88-
await ProcessPendingLogEvents();
89-
await ProcessPendingFailedLogins();
90-
await ProcessPendingSuccessfulLogins();
91-
await UpdateFirewall();
84+
try
85+
{
86+
await UpdateConfiguration();
87+
await SetNetworkInfo();
88+
await UpdateDelegate();
89+
await UpdateUpdaters();
90+
await UpdateExpiredIPAddressStates();
91+
await ProcessPendingLogEvents();
92+
await ProcessPendingFailedLogins();
93+
await ProcessPendingSuccessfulLogins();
94+
await UpdateFirewall();
95+
}
96+
catch (Exception ex)
97+
{
98+
Logger.Error($"Error on {nameof(IPBanService)}.{nameof(RunCycle)}", ex);
99+
}
92100
}
93101

94102
/// <summary>
@@ -288,37 +296,45 @@ public void Dispose()
288296
/// Initialize and start the service
289297
/// </summary>
290298
/// <param name="cancelToken">Cancel token</param>
291-
public async Task StartAsync(CancellationToken cancelToken)
299+
public Task StartAsync(CancellationToken cancelToken)
292300
{
293-
if (IsRunning)
301+
if (!IsRunning)
294302
{
295-
return;
296-
}
303+
try
304+
{
305+
IsRunning = true;
297306

298-
try
299-
{
300-
IsRunning = true;
301-
ipDB = new IPBanDB(DatabasePath ?? "ipban.sqlite");
302-
AddWindowsEventViewer();
303-
AddUpdater(new IPBanUnblockIPAddressesUpdater(this, Path.Combine(AppContext.BaseDirectory, "unban.txt")));
304-
AddUpdater(new IPBanBlockIPAddressesUpdater(this, Path.Combine(AppContext.BaseDirectory, "ban.txt")));
305-
AssemblyVersion = IPBanService.IPBanAssembly.GetName().Version.ToString();
306-
await ReadAppSettings();
307-
UpdateBannedIPAddressesOnStart();
308-
IPBanDelegate?.Start(this);
309-
if (!ManualCycle)
307+
// set version
308+
AssemblyVersion = IPBanService.IPBanAssembly.GetName().Version.ToString();
309+
310+
// create db
311+
ipDB = new IPBanDB(DatabasePath ?? "ipban.sqlite");
312+
313+
// add some services
314+
AddUpdater(new IPBanUnblockIPAddressesUpdater(this, Path.Combine(AppContext.BaseDirectory, "unban.txt")));
315+
AddUpdater(new IPBanBlockIPAddressesUpdater(this, Path.Combine(AppContext.BaseDirectory, "ban.txt")));
316+
317+
// start delegate if we have one
318+
IPBanDelegate?.Start(this);
319+
320+
// setup cycle timer if needed
321+
if (!ManualCycle)
322+
{
323+
cycleTimer = new System.Timers.Timer(Config.CycleTime.TotalMilliseconds);
324+
cycleTimer.Elapsed += async (sender, e) => await CycleTimerElapsed(sender, e);
325+
cycleTimer.Start();
326+
}
327+
328+
Logger.Warn("IPBan service started and initialized. Operating System: {0}",
329+
OSUtility.Instance.OSString());
330+
Logger.WriteLogLevels();
331+
}
332+
catch (Exception ex)
310333
{
311-
cycleTimer = new System.Timers.Timer(Config.CycleTime.TotalMilliseconds);
312-
cycleTimer.Elapsed += async (sender, e) => await CycleTimerElapsed(sender, e);
313-
cycleTimer.Start();
334+
Logger.Error("Critical error in IPBanService.Start", ex);
314335
}
315-
Logger.Warn("IPBan {0} service started and initialized. Operating System: {1}", OSUtility.Instance.Name, OSUtility.Instance.OSString());
316-
Logger.WriteLogLevels();
317-
}
318-
catch (Exception ex)
319-
{
320-
Logger.Error("Critical error in IPBanService.Start", ex);
321336
}
337+
return Task.CompletedTask;
322338
}
323339

324340
/// <summary>
@@ -512,6 +528,7 @@ public static T CreateAndStartIPBanTestService<T>(string directory = null, strin
512528
}
513529
service.Version = "1.1.1.1";
514530
service.StartAsync(CancellationToken.None).Sync();
531+
service.RunCycle().Sync();
515532
service.DB.Truncate(true);
516533
service.Firewall.Truncate();
517534
return service;

IPBanCore/Core/IPBan/IPBanServiceRunner.cs

+1-1
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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/IPBanService_Fields.cs

+5-5
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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
@@ -43,10 +43,6 @@ public partial class IPBanService
4343
',', ';', '|', '_', '-', '\'', '\"', '(', ')', '[', ']', '{', '}', ' ', '\r', '\n'
4444
};
4545

46-
private System.Timers.Timer cycleTimer;
47-
private bool firewallNeedsBlockedIPAddressesUpdate;
48-
private IPBanDB ipDB;
49-
5046
// batch failed logins every cycle
5147
private readonly List<IPAddressLogEvent> pendingFailedLogins = new List<IPAddressLogEvent>();
5248
private readonly List<IPAddressLogEvent> pendingSuccessfulLogins = new List<IPAddressLogEvent>();
@@ -56,7 +52,11 @@ public partial class IPBanService
5652
private readonly Dictionary<string, AsyncQueue<Func<CancellationToken, Task>>> firewallQueue = new Dictionary<string, AsyncQueue<Func<CancellationToken, Task>>>();
5753
private readonly CancellationTokenSource serviceCancelTokenSource = new CancellationTokenSource();
5854

55+
private System.Timers.Timer cycleTimer;
56+
private bool firewallNeedsBlockedIPAddressesUpdate;
57+
private IPBanDB ipDB;
5958
private bool whitelistChanged;
59+
private bool updateBannedIPAddressesOnStartCalled;
6060

6161
/// <summary>
6262
/// Whether start url has been gotten

IPBanCore/Core/IPBan/IPBanService_Private.cs

+18-4
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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
@@ -76,7 +76,7 @@ private async Task FirewallTask(AsyncQueue<Func<CancellationToken, Task>> queue)
7676

7777

7878

79-
internal async Task ReadAppSettings()
79+
internal async Task UpdateConfiguration()
8080
{
8181
try
8282
{
@@ -117,6 +117,12 @@ internal async Task ReadAppSettings()
117117
{
118118
BannedIPAddressHandler = NullBannedIPAddressHandler.Instance;
119119
}
120+
121+
// will only execute once
122+
UpdateBannedIPAddressesOnStart();
123+
124+
// will only execute once
125+
SetupWindowsEventViewer();
120126
}
121127

122128
private object BeginTransaction()
@@ -447,6 +453,12 @@ private void ExecuteExternalProcessForBannedIPAddresses(IReadOnlyCollection<IPAd
447453

448454
private void UpdateBannedIPAddressesOnStart()
449455
{
456+
if (updateBannedIPAddressesOnStartCalled)
457+
{
458+
return;
459+
}
460+
updateBannedIPAddressesOnStartCalled = true;
461+
450462
if (Config.ClearBannedIPAddressesOnRestart)
451463
{
452464
Logger.Warn("Clearing all banned ip addresses on start because ClearBannedIPAddressesOnRestart is set");
@@ -986,9 +998,11 @@ private Task ProcessPendingLogEvents()
986998
return Task.CompletedTask;
987999
}
9881000

989-
private void AddWindowsEventViewer()
1001+
private void SetupWindowsEventViewer()
9901002
{
991-
if (UseWindowsEventViewer && RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
1003+
if (EventViewer is null &&
1004+
UseWindowsEventViewer &&
1005+
RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
9921006
{
9931007
// attach Windows event viewer to the service
9941008
EventViewer = new IPBanWindowsEventViewer(this);

IPBanCore/Core/IPBan/IPBanService_Types.cs

+1-1
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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/IPBanUnblockIPAddressesUpdater.cs

+1-1
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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/IPBanUriFirewallRule.cs

+1-1
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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/Interfaces/IBannedIPAddressHandler.cs

+1-1
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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/Interfaces/IDnsLookup.cs

+1-1
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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/Interfaces/IHttpRequestMaker.cs

+1-1
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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/Interfaces/IIPBanFirewall.cs

+1-1
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) 2012-present Digital Ruby, LLC - https://www.digitalruby.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)