/// <summary> /// Run the service /// </summary> /// <param name="requireAdministrator">True to require administrator, false otherwise</param> /// <returns>Exit code</returns> public async Task RunAsync(bool requireAdministrator = true) { if (requireAdministrator) { IPBanExtensionMethods.RequireAdministrator(); } if (args.Length != 0 && (args[0].Equals("info", StringComparison.OrdinalIgnoreCase) || args[0].Equals("-info", StringComparison.OrdinalIgnoreCase))) { IPBanLog.Warn("System info: {0}", IPBanOS.OSString()); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { await RunWindowsService(args); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { await RunLinuxService(args); } else { throw new PlatformNotSupportedException(); } }
/// <summary> /// Initialize and start the service /// </summary> public async Task StartAsync() { if (IsRunning) { return; } try { IsRunning = true; ipDB = new IPBanDB(DatabasePath ?? "ipban.sqlite"); AddWindowsEventViewer(); AddUpdater(new IPBanUnblockIPAddressesUpdater(this, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "unban.txt"))); AddUpdater(new IPBanBlockIPAddressesUpdater(this, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ban.txt"))); AssemblyVersion = IPBanService.IPBanAssembly.GetName().Version.ToString(); await ReadAppSettings(); UpdateBannedIPAddressesOnStart(); IPBanDelegate?.Start(this); if (!ManualCycle) { if (RunFirstCycleRightAway) { await RunCycle(); // run one cycle right away } cycleTimer = new System.Timers.Timer(Config.CycleTime.TotalMilliseconds); cycleTimer.Elapsed += async(sender, e) => await CycleTimerElapsed(sender, e); cycleTimer.Start(); } IPBanLog.Warn("IPBan {0} service started and initialized. Operating System: {1}", IPBanOS.Name, IPBanOS.OSString()); IPBanLog.WriteLogLevels(); } catch (Exception ex) { IPBanLog.Error("Critical error in IPBanService.Start", ex); } }