-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathip_ranges.rb
More file actions
59 lines (47 loc) · 1.29 KB
/
ip_ranges.rb
File metadata and controls
59 lines (47 loc) · 1.29 KB
1
2
3
4
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
module ActionPack
module Cloudfront
module IpRanges
class Range
attr_reader :ip_prefix, :region, :service
def initialize(attrs)
@service = attrs['service']
@ip_prefix = attrs['ip_prefix'] || attrs['ipv6_prefix']
@region = attrs['region']
end
def cloudfront?
service =~ /cloudfront/i
end
def ipaddr
IPAddr.new(ip_prefix)
end
end
def trusted_proxies
cloudfront_proxies + ActionDispatch::RemoteIp::TRUSTED_PROXIES
end
def cloudfront_proxies
ip_ranges.select(&:cloudfront?).map(&:ipaddr)
end
def ip_ranges
@ip_ranges ||= begin
data = ip_data
prefixes = data['prefixes']
prefixesv6 = data['ipv6_prefixes']
(prefixes + prefixesv6).map do |attrs|
Range.new(attrs)
end
end
end
def ip_data
Timeout.timeout(5) do
uri = URI('https://ip-ranges.amazonaws.com/ip-ranges.json')
res = Net::HTTP.get(uri)
JSON.parse(res, freeze: true)
end
rescue
backup_json = File.join File.dirname(__FILE__), 'ip-ranges.json'
JSON.load_file(backup_json, freeze: true)
end
extend self
end
end
end