Class: ScanSuppressingLogger::Middleware

Inherits:
Rails::Rack::Logger
  • Object
show all
Defined in:
lib/scan_suppressing_logger/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



11
12
13
14
15
16
17
18
19
20
# File 'lib/scan_suppressing_logger/middleware.rb', line 11

def initialize(app, options = {})
  @app = app
  @options = options
  @wrappers = [ScanSuppressingLogger::RailsWrapper]

  configure_networks!
  find_error_trackers!

  super
end

Instance Method Details

#automated_scan?(request) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/scan_suppressing_logger/middleware.rb', line 38

def automated_scan?(request)
  # Check if the request source IP comes from any of the network ranges
  # in config.
  ip = IPAddr.new request.remote_ip
  @networks.any?{|network| network.include? ip }
end

#call(env) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/scan_suppressing_logger/middleware.rb', line 22

def call(env)
  return super(env) unless @configured

  request = ActionDispatch::Request.new env
  if automated_scan? request
    Rails.logger.tagged('ScanSuppressingLogger') { Rails.logger.info "Suppressed for #{request.remote_ip}" }

    # Combine all the wrappers we need.
    @wrappers.inject(proc { @app.call env }) do |acc, wrapper|
      proc { wrapper.call(&acc) }
    end.call
  else
    super env
  end
end