Example #1
0
        public void EnableLocalSubnetTrafficViaFirewall()
        {
            string ruleName = RulePrefix + "AllowLocalTraffic";
            string localIP  = DefaultDnsLookup.GetLocalIPAddress().ToString();

            if (localIP != null)
            {
                Match m = Regex.Match(localIP, "\\.[0-9]+$");
                if (m.Success)
                {
                    string remoteIPAddresses = localIP.Substring(0, m.Index) + ".0/24";
                    GetOrCreateRule(ruleName, remoteIPAddresses, NET_FW_ACTION_.NET_FW_ACTION_ALLOW);
                }
            }
        }
        /// <summary>
        /// Test a log file
        /// </summary>
        /// <param name="fileName">Log file</param>
        public static void RunLogFileTest(string fileName,
                                          string regexFailureFile,
                                          string regexFailureTimestampFormat,
                                          string regexSuccessFile,
                                          string regexSuccessTimestampFormat)
        {
            IPBanLogFileScanner scanner = new(new()
            {
                Dns = new DefaultDnsLookup(),
                FailedLoginThreshold = 3,
                FailedLogLevel = LogLevel.Warning,
                LoginHandler = new LogFileWriter(),
                MaxFileSizeBytes = 0,
                PathAndMask = fileName.Trim(),
                PingIntervalMilliseconds = 0,
                RegexFailure = (File.Exists(regexFailureFile) && regexFailureFile.Length > 2 ? IPBanConfig.ParseRegex(File.ReadAllText(regexFailureFile)) : null),
                RegexFailureTimestampFormat = regexFailureTimestampFormat.Trim('.'),
                RegexSuccess = (File.Exists(regexSuccessFile) && regexSuccessFile.Length > 2 ? IPBanConfig.ParseRegex(File.ReadAllText(regexSuccessFile)) : null),
                RegexSuccessTimestampFormat = regexSuccessTimestampFormat.Trim('.'),
                Source = "test",
                SuccessfulLogLevel = LogLevel.Warning
            });

            // start with empty file
            File.Move(fileName, fileName + ".temp");
            File.WriteAllText(fileName, string.Empty);

            // read the empty file
            scanner.ProcessFiles();

            // get rid of the empty file
            File.Delete(fileName);

            // put the full file back
            File.Move(fileName + ".temp", fileName);

            // now the scanner will process the entire file
            scanner.ProcessFiles();
        }