How to set a real IP address when using CloudFlare, Heroku and RoR?

I just recently started using CloudFlare and still have the lingering problem of getting CloudFlare proxy IP addresses instead of my visitor's address. CloudFlare has many solutions for this, but I have not seen any Rails.

I am using Rails 3.2.17.

It seems like if I initialize ActionDispatch :: RemoteIp with the custom_proxies argument set to the correct regular expression containing all CloudFlare IP Regions (along with all the standard local and private ranges), this may solve my problem.

Questions:

1) Does it fit right?

CloudFlare has a range of IP ranges that all need to be converted to regular expressions. These ranges may change in the future, although CloudFlare says they are not frequent, and I probably don’t know, so it seems fragile.

2) How to initialize ActionDispatch :: RemoteIP with the argument custom_proxies?

+4
source share
3 answers

You can use the Rack middleware from the remote_ip_proxy_scrubber bug to make sure your Rails application ignores IP addresses from trusted proxies such as CloudFlare.

First add the gem to your gemfile and then bundle install

gem 'remote_ip_proxy_scrubber'

IP- CloudFlare: https://www.cloudflare.com/ips-v4

IP- CloudFlare, config/application.rb conifg/environment/*. rb

# Make sure CloudFlare IP addresses are
# removed from the X-Forwarded-For header
# before our app sees them
config.middleware.insert_before(Rails::Rack::Logger,
   RemoteIpProxyScrubber.filter_middleware, 
   %w{
     199.27.128.0/21
     173.245.48.0/20
     103.21.244.0/22
     103.22.200.0/22
     103.31.4.0/22
     141.101.64.0/18
     108.162.192.0/18
     190.93.240.0/20
     188.114.96.0/20
     197.234.240.0/22
     198.41.128.0/17
     162.158.0.0/15
     104.16.0.0/12
     172.64.0.0/13
  })

# Make sure the customer real IP address (remote_ip)
# is used in our Rails logs.
config.middleware.insert_before(Rails::Rack::Logger, RemoteIpProxyScrubber.patched_logger)
config.middleware.delete(Rails::Rack::Logger)

IP- CloudFlare .

  • CloudFlare, CloudFlare IP- .
  • IFTTT, , CloudFlare IP-.
+5

Cloudflare HTTP- X-Forwarded-For, .

, , request.remote_ip request.ip Rails?

0

" , CloudFlare , ",

, ( ips , ).

" Cloudflare HTTP- X-Forwarded-For, , ."

:)

0

Source: https://habr.com/ru/post/1538917/


All Articles