Class: OpenASN::Middleware

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

Overview

Rack middleware: classifies the request IP once and exposes the result at env (nil when the IP is missing/unparseable — never raises into the request cycle).

use OpenASN::Middleware
# then anywhere downstream:
request.env["openasn.result"]&.infrastructure?

THE CLASSIC INTEGRATION BUG (read this before filing "everything is :private/:hosting"): if your app sits behind a proxy/load balancer and trusted proxies aren't configured, the IP you classify is your own infrastructure's. Inside Rails we use ActionDispatch's remote_ip (which honors config.action_dispatch.trusted_proxies); bare Rack falls back to REMOTE_ADDR. Behind Cloudflare or a CDN, make sure the real client IP reaches Rails (e.g. cloudflare-rails gem or equivalent trusted_proxies setup) BEFORE trusting these verdicts.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



21
22
23
# File 'lib/openasn/middleware.rb', line 21

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



25
26
27
28
# File 'lib/openasn/middleware.rb', line 25

def call(env)
  env["openasn.result"] = classify(env)
  @app.call(env)
end