Example #1
0
        /// <summary>
        /// Initialize and start the service
        /// </summary>
        /// <param name="cancelToken">Cancel token</param>
        public async Task RunAsync(CancellationToken cancelToken)
        {
            CancelToken = cancelToken;

            if (!IsRunning)
            {
                try
                {
                    IsRunning = true;

                    // set version
                    AssemblyVersion = IPBanService.IPBanAssembly.GetName().Version.ToString();

                    // create db
                    ipDB = new IPBanDB(DatabasePath ?? "ipban.sqlite");

                    // add some services
                    AddUpdater(new IPBanUnblockIPAddressesUpdater(this, Path.Combine(AppContext.BaseDirectory, "unban.txt")));
                    AddUpdater(new IPBanBlockIPAddressesUpdater(this, Path.Combine(AppContext.BaseDirectory, "ban.txt")));
                    AddUpdater(DnsList);

                    // start delegate if we have one
                    IPBanDelegate?.Start(this);

                    Logger.Warn("IPBan service started and initialized. Operating System: {0}",
                                OSUtility.Instance.OSString());
                    Logger.WriteLogLevels();

                    // setup cycle timer if needed
                    if (!ManualCycle)
                    {
                        // create a new timer that goes off in 1 second, this will change as the config is
                        // loaded and the cycle time becomes whatever is in the config
                        cycleTimer = new Timer(async(_state) =>
                        {
                            try
                            {
                                await CycleTimerElapsed();
                            }
                            catch
                            {
                            }
                        }, null, 1000, Timeout.Infinite);
                    }

                    if (!ManualCycle)
                    {
                        await Task.Delay(Timeout.Infinite, cancelToken);
                    }
                }
                catch (Exception ex)
                {
                    if (!(ex is OperationCanceledException))
                    {
                        Logger.Error($"Error in {nameof(IPBanService)}.{nameof(IPBanService.RunAsync)}", ex);
                    }
                }
            }
        }
Example #2
0
        /// <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();
                }
                Logger.Warn("IPBan {0} service started and initialized. Operating System: {1}", OSUtility.Name, OSUtility.OSString());
                Logger.WriteLogLevels();
            }
            catch (Exception ex)
            {
                Logger.Error("Critical error in IPBanService.Start", ex);
            }
        }