|
1 | 1 | /*
|
2 | 2 | MIT License
|
3 | 3 |
|
4 |
| -Copyright (c) 2019 Digital Ruby, LLC - https://www.digitalruby.com |
| 4 | +Copyright (c) 2012-present Digital Ruby, LLC - https://www.digitalruby.com |
5 | 5 |
|
6 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 7 | 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
|
41 | 41 | namespace DigitalRuby.IPBanCore
|
42 | 42 | {
|
43 | 43 | /// <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. |
45 | 46 | /// </summary>
|
46 | 47 | public partial class IPBanService : IIPBanService, IIsWhitelisted
|
47 | 48 | {
|
@@ -80,15 +81,22 @@ where typeOfT.IsAssignableFrom(type)
|
80 | 81 | /// </summary>
|
81 | 82 | public async Task RunCycle()
|
82 | 83 | {
|
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 | + } |
92 | 100 | }
|
93 | 101 |
|
94 | 102 | /// <summary>
|
@@ -288,37 +296,45 @@ public void Dispose()
|
288 | 296 | /// Initialize and start the service
|
289 | 297 | /// </summary>
|
290 | 298 | /// <param name="cancelToken">Cancel token</param>
|
291 |
| - public async Task StartAsync(CancellationToken cancelToken) |
| 299 | + public Task StartAsync(CancellationToken cancelToken) |
292 | 300 | {
|
293 |
| - if (IsRunning) |
| 301 | + if (!IsRunning) |
294 | 302 | {
|
295 |
| - return; |
296 |
| - } |
| 303 | + try |
| 304 | + { |
| 305 | + IsRunning = true; |
297 | 306 |
|
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) |
310 | 333 | {
|
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); |
314 | 335 | }
|
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); |
321 | 336 | }
|
| 337 | + return Task.CompletedTask; |
322 | 338 | }
|
323 | 339 |
|
324 | 340 | /// <summary>
|
@@ -512,6 +528,7 @@ public static T CreateAndStartIPBanTestService<T>(string directory = null, strin
|
512 | 528 | }
|
513 | 529 | service.Version = "1.1.1.1";
|
514 | 530 | service.StartAsync(CancellationToken.None).Sync();
|
| 531 | + service.RunCycle().Sync(); |
515 | 532 | service.DB.Truncate(true);
|
516 | 533 | service.Firewall.Truncate();
|
517 | 534 | return service;
|
|
0 commit comments