5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/cloudfront/rails/importer.rb', line 5
def fetch
uri = URI("https://ip-ranges.amazonaws.com/ip-ranges.json")
response = Net::HTTP.start(uri.host,
uri.port,
use_ssl: uri.scheme == 'https',
open_timeout: ::Rails.application.config.cloudfront.timeout,
read_timeout: ::Rails.application.config.cloudfront.timeout,
ssl_timeout: ::Rails.application.config.cloudfront.timeout
) do |http|
http.request(Net::HTTP::Get.new(uri))
end
case response
when Net::HTTPSuccess
json = ActiveSupport::JSON.decode response.body
trusted_ipv4_proxies = json["prefixes"].select do |details|
details["service"] == 'CLOUDFRONT'
end.map do |details|
IPAddr.new(details["ip_prefix"])
end
trusted_ipv6_proxies = json["ipv6_prefixes"].select do |details|
details["service"] == 'CLOUDFRONT'
end.map do |details|
IPAddr.new(details["ipv6_prefix"])
end
trusted_ipv4_proxies + trusted_ipv6_proxies
else
raise ResponseError.new(response)
end
end
|