Example #1
0
 /// <summary>
 /// Check's if the request IP has a valid PTR record.
 /// </summary>
 /// <param name="ip"></param>
 /// <returns></returns>
 private async Task PtrLookupIp(IPAddress ip)
 {
     try
     {
         _requestHostname = await Dns.GetHostEntryAsync(ip);
     }
     catch (SocketException ex)
     {
         _logger.LogEvent(LogLevel.Error, $"PTR Lookup Error: {ex.Message}");
     }
 }
Example #2
0
        /// <summary>
        /// Checks requests IP and/or Hostname against the Rbl's database
        /// </summary>
        /// <param name="context"></param>
        /// <param name="logger"></param>
        /// <returns></returns>
        public async Task <bool> CheckProvider(HttpContext context, TrotterLog logger)
        {
            _logger  = logger;
            _context = context;

            var requestIp   = context.Connection.RemoteIpAddress;
            var formattedIp = FormatIp(requestIp);

            if (ProviderType == RblType.Both || ProviderType == RblType.Hostname)
            {
                _logger.LogEvent(LogLevel.Information, "Initiating PTR Lookup & Hostname check...");
                await PtrLookupIp(requestIp);
                await RblHostLookup(_requestHostname.HostName);

                _logger.LogEvent(LogLevel.Information, "PTR Lookup & Hostname check completed!");
            }

            _logger.LogEvent(LogLevel.Information, "Initiating IP Lookup against RBL");
            await RblHostLookup(formattedIp);

            _logger.LogEvent(LogLevel.Information, $"RBL Lookup has completed for {ProviderName}");
            return(_isListed);
        }
        public async Task Invoke(HttpContext context)
        {
            //If IPAddress is private then ignore.
            if (IsInternal(context.Connection.RemoteIpAddress))
            {
                _logger.LogEvent(LogLevel.Information, $"Not checking the remote IP {context.Connection.RemoteIpAddress} as it's private.");
                await _next.Invoke(context);

                return;
            }

            _logger.LogEvent(LogLevel.Information, $"The remote IP {context.Connection.RemoteIpAddress} is public.");

            var isListed = false;

            //Process the RBL Providers
            foreach (var provider in _rblProviders)
            {
                var providerResult = await InitiateProvider(context, provider);

                if (!providerResult)
                {
                    continue;
                }
                isListed = true;

                if (!_continueChecks)
                {
                    break;
                }
            }

            if (!isListed)
            {
                _logger.LogEvent(LogLevel.Information, $"Remote IP/PTR is NOT listed on any of the Rbl Providers");
                _logger.LogEvent(LogLevel.Information, $"Moving onto the next Middleware Component");
                await _next.Invoke(context);
            }

            //Not Listed == Short-circuits request pipeline (no booley, no invokey)
        }