Class: Rack::CloudflareMiddleware::TrustedIps
- Inherits:
-
Object
- Object
- Rack::CloudflareMiddleware::TrustedIps
- Includes:
- Singleton
- Defined in:
- lib/rack/cloudflare_middleware/trusted_ips.rb
Constant Summary collapse
- TIMEOUT =
7.0- OPEN_TIMEOUT =
4.0- UPDATE_THRESHOLD =
21600
Instance Attribute Summary collapse
-
#mtimes ⇒ Object
readonly
Returns the value of attribute mtimes.
-
#ranges ⇒ Object
readonly
Returns the value of attribute ranges.
Instance Method Summary collapse
- #check_update ⇒ Object
- #include?(ip) ⇒ Boolean
-
#initialize ⇒ TrustedIps
constructor
A new instance of TrustedIps.
- #reset! ⇒ Object
- #update! ⇒ Object
Constructor Details
#initialize ⇒ TrustedIps
Returns a new instance of TrustedIps.
21 22 23 |
# File 'lib/rack/cloudflare_middleware/trusted_ips.rb', line 21 def initialize reset! end |
Instance Attribute Details
#mtimes ⇒ Object (readonly)
Returns the value of attribute mtimes.
17 18 19 |
# File 'lib/rack/cloudflare_middleware/trusted_ips.rb', line 17 def mtimes @mtimes end |
#ranges ⇒ Object (readonly)
Returns the value of attribute ranges.
16 17 18 |
# File 'lib/rack/cloudflare_middleware/trusted_ips.rb', line 16 def ranges @ranges end |
Instance Method Details
#check_update ⇒ Object
47 48 49 50 51 |
# File 'lib/rack/cloudflare_middleware/trusted_ips.rb', line 47 def check_update if [4, 6].any? { (Time.now - @mtimes[_1]) > UPDATE_THRESHOLD } update! end end |
#include?(ip) ⇒ Boolean
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rack/cloudflare_middleware/trusted_ips.rb', line 31 def include?(ip) unless ip.is_a? IPAddr ip = IPAddr.new(ip) end if ip.ipv4? @ranges[4].any? { _1.include? ip } else @ranges[6].any? { _1.include? ip } end end |
#reset! ⇒ Object
25 26 27 28 29 |
# File 'lib/rack/cloudflare_middleware/trusted_ips.rb', line 25 def reset! @ranges = {4 => Set.new, 6 => Set.new} @mtimes = {4 => Time.new(1970, 1, 1), 6 => Time.new(1970, 1, 1)} load_from_files end |
#update! ⇒ Object
42 43 44 45 |
# File 'lib/rack/cloudflare_middleware/trusted_ips.rb', line 42 def update! read_network "https://www.cloudflare.com/ips-v4/", 4 read_network "https://www.cloudflare.com/ips-v6/", 6 end |