Class: Above::MiddleWare::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/above/middleware/block.rb

Overview

Middleware to block requests based on IP addresses, also supports IPv6 & /CIDR notation

Instance Method Summary collapse

Constructor Details

#initialize(app:) ⇒ Block

Returns a new instance of Block.



9
10
11
# File 'lib/above/middleware/block.rb', line 9

def initialize(app:)
  @app = app # who you gonna call
end

Instance Method Details

#call(env:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/above/middleware/block.rb', line 13

def call(env:)
  # TODO - maybe return - 41? 44? - chosen via config
  requester = env[:socket].io.remote_address.ip_address
  blocklist = env[:config]["Above::MiddleWare::Block"]["blocklist"]

  blocklist&.each do |bad_address_range|
    bad_addr = IPAddr.new(bad_address_range)
    if bad_addr.include?(requester)
      # return here, otherwise go on to reply in the usual way
      return [0, "", []]
    end
  end

  @app&.call(env:)
rescue IPAddr::InvalidAddressError
  puts "Error - Invalid address in blocklist"
  [42, Status::CODE[42], []]
end