Class: Cloudeffrontery

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCloudeffrontery

Returns a new instance of Cloudeffrontery.



15
16
17
18
19
20
21
22
# File 'lib/cloudeffrontery.rb', line 15

def initialize
  @awsurl = "https://ip-ranges.amazonaws.com/ip-ranges.json"
  @azureurl = "https://www.cloudflare.com/ips-v4"
  @service = 'CLOUDFRONT'
  @region = '.*'
  @cloud = 'aws'
  @debug = false
end

Instance Attribute Details

#awsurlObject

Returns the value of attribute awsurl.



11
12
13
# File 'lib/cloudeffrontery.rb', line 11

def awsurl
  @awsurl
end

#azureurlObject

Returns the value of attribute azureurl.



12
13
14
# File 'lib/cloudeffrontery.rb', line 12

def azureurl
  @azureurl
end

#cloudObject

Returns the value of attribute cloud.



13
14
15
# File 'lib/cloudeffrontery.rb', line 13

def cloud
  @cloud
end

#debug(message) ⇒ Object

Returns the value of attribute debug.



8
9
10
# File 'lib/cloudeffrontery.rb', line 8

def debug
  @debug
end

#regionObject

Returns the value of attribute region.



9
10
11
# File 'lib/cloudeffrontery.rb', line 9

def region
  @region
end

#serviceObject

Returns the value of attribute service.



10
11
12
# File 'lib/cloudeffrontery.rb', line 10

def service
  @service
end

Instance Method Details

#apacheObject



105
106
107
108
109
110
111
112
113
114
# File 'lib/cloudeffrontery.rb', line 105

def apache
  return_config = []
  ranges = retrieve

  return_config.push "# Cloudfront public IPs for #{@region} and #{@service}"
  ranges.each do |r|
    return_config.push "#{r.strip}"
  end
  return_config.join("\n")
end

#filter(ranges) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/cloudeffrontery.rb', line 116

def filter(ranges)

  unless ranges.is_a?(Hash)
    return nil
  end    

  returnranges = ranges['prefixes'].select { |r| r['region'].match(/#{@region}/) && r['service'] == @service }.map { |r| r['ip_prefix'] }.select { |r| IPAddr.new(r) rescue nil }
  returnranges

end

#nginxObject



95
96
97
98
99
100
101
102
103
# File 'lib/cloudeffrontery.rb', line 95

def nginx
  return_config = []
  ranges = retrieve
  debug("We got #{ranges.size} ranges")
  ranges.each do |r|
    return_config.push "set_real_ip_from #{r.strip};"
  end
  return_config.join("\n")
end


24
25
26
# File 'lib/cloudeffrontery.rb', line 24

def print
  $stdout.puts retrieve.inspect
end

#retrieveObject



44
45
46
47
# File 'lib/cloudeffrontery.rb', line 44

def retrieve 
  method="retrieve#{@cloud}".to_sym
  self.send(method)
end

#retrieveawsObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cloudeffrontery.rb', line 73

def retrieveaws
  theurl = url
  debug("Getting some Amazon ranges using #{theurl}")
  begin
    uri = URI.parse(theurl)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = uri.scheme == 'https' ? true : false
    request = Net::HTTP::Get.new(uri.request_uri)
    thejson = http.request(request)
  rescue => e
    debug("Failed to be getting some Amazon ranges - #{e.inspect}")
    return nil
  end

  if thejson.kind_of? Net::HTTPSuccess
    return filter(JSON::parse(thejson.body))
  else
    return nil
  end

end

#retrieveazureObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cloudeffrontery.rb', line 49

def retrieveazure
  theurl = url
  begin
    uri = URI.parse(theurl)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = uri.scheme == 'https' ? true : false
    request = Net::HTTP::Get.new(uri.request_uri)
    thejson = http.request(request)
  rescue
    return nil
  end

  if thejson.kind_of? Net::HTTPSuccess
    return thejson.body.split("\n").select { |r| IPAddr.new(r) rescue nil }
  else
    return nil
  end

end

#urlObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/cloudeffrontery.rb', line 28

def url
  if @url
    debug("Returning custom URL #{@url}")
    returnurl = @url
  else
    debug("Returning URL for #{@cloud}")
    returnurl = @cloud == 'aws' ? @awsurl : @azureurl
  end
  return returnurl
end

#url=(value) ⇒ Object



39
40
41
42
# File 'lib/cloudeffrontery.rb', line 39

def url=(value)
  debug("Setting URL to #{value}")
  @url = value
end