cloudflare 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ Copyright (c) 2012 Marcin "B4k3r" Prokop
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8
+
@@ -0,0 +1,69 @@
1
+ CloudFlare
2
+ ==========
3
+
4
+ It is a Ruby wrapper for the CloudFlare API.
5
+
6
+ Official home page is [here](http://b4k3r.github.com/cloudflare). The complete [RDoc](http://rdoc.info/github/B4k3r/cloudflare/) is online.
7
+
8
+ Visit also a CloudFlare API documentation:
9
+
10
+ - [Client](http://www.cloudflare.com/wiki/Client_Interface_API)
11
+ - [Host](http://www.cloudflare.com/docs/host-api.html)
12
+
13
+ Installation
14
+ ------------
15
+
16
+ ```
17
+ gem install cloudflare
18
+ ```
19
+
20
+ Or, if use Rails, include the gem in your Gemfile:
21
+
22
+ ```
23
+ gem 'cloudflare'
24
+ ```
25
+
26
+ Usage
27
+ -----
28
+
29
+ **Example for Client API:**
30
+
31
+ ```
32
+ require 'cloudflare'
33
+
34
+ cf = CloudFlare.new('user_api_key', 'user_email')
35
+ output = cf.add_rec('domain.com', 'A', '212.11.6.211', 'subdomain.domain.com', true)
36
+
37
+ if output['result'] == 'success'
38
+ puts 'Successfuly added DNS record'
39
+ else
40
+ puts output['msg'] // error message
41
+ end
42
+ ```
43
+
44
+ **Example for Host API:**
45
+
46
+ ```
47
+ require 'cloudflare'
48
+
49
+ cf = CloudFlare.new('host_api_key')
50
+ output = cf.create_user('new_user_email', 'new_password', 'new_username (optional)', 'unique id (optional)')
51
+
52
+ if output['result'] == 'success'
53
+ puts output['msg']
54
+ puts "Your login is #{output['response']['cloudflare_username']}"
55
+ else
56
+ puts output['msg'] // error message
57
+ end
58
+ ```
59
+
60
+ License
61
+ -------
62
+
63
+ Copyright © 2012. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
64
+
65
+
66
+
67
+
68
+
69
+
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
@@ -0,0 +1,326 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ # All public functions return (Hash) result: success or error with message and error code
5
+ #
6
+ # For more information please visit:
7
+ # - http://www.cloudflare.com/wiki/Client_Interface_API
8
+ # - http://www.cloudflare.com/docs/host-api.html
9
+
10
+ class CloudFlare
11
+
12
+ # URL for Client and Host API
13
+
14
+ URL_API = {
15
+ client: 'https://www.cloudflare.com/api_json.html',
16
+ host: 'https://api.cloudflare.com/host-gw.html'
17
+ }
18
+
19
+ TIMEOUT = 5 # Default is 5 seconds
20
+
21
+ # @param api_key User or Host API key.
22
+ # @param email It is for a Client API.
23
+ def initialize(api_key, email = nil)
24
+
25
+ @params = Hash.new
26
+
27
+ if email.nil?
28
+ @params[:api_key] = api_key
29
+ else
30
+ @params[:api_key] = api_key
31
+ @params[:email] = email
32
+ end
33
+
34
+ end
35
+
36
+ # CLIENT
37
+
38
+ # This function can be used to get currently settings of values such as the security level.
39
+ #
40
+ # More: https://www.cloudflare.com/wiki/Client_Interface_API#FUNCTION:_Current_Stats_and_Settings
41
+ #
42
+ # @param zone The zone you'd like to run CNAMES through CloudFlare for, e.g. +example.com+.
43
+ # @param interval The interval parameter defines what period you want to look at. Default is 30 days, but 1 day delayed. Pro only intervals are 100, 110, and 120.
44
+ #
45
+ # @return (Hash) the current stats and settings for a particular website
46
+
47
+ def stats(zone, interval = 20)
48
+ send_req({a: :stats, z: zone, interval: interval})
49
+ end
50
+
51
+ # This function sets the Basic Security Level to HELP I'M UNDER ATTACK / HIGH / MEDIUM / LOW / ESSENTIALLY OFF.
52
+ #
53
+ # @param zone The zone you'd like to run CNAMES through CloudFlare for, e.g. +example.com+.
54
+ # @param value Must be one of low|med|high|help|eoff.
55
+
56
+ def set_security_lvl(zone, value)
57
+ send_req({a: :sec_lvl, z: zone, v: value})
58
+ end
59
+
60
+ # This function sets the Caching Level to Aggressive or Basic.
61
+ #
62
+ # @param zone The zone you'd like to run CNAMES through CloudFlare for, e.g. +example.com+.
63
+ # @param value Must be one of agg|basic.
64
+
65
+ def set_cache_lvl(zone, value)
66
+ send_req({a: :cache_lvl, z: zone, v: value})
67
+ end
68
+
69
+ # This function allows you to toggle Development Mode on or off for a particular domain. When Development Mode is on the cache is bypassed. Development mode remains on for 3 hours or until when it is toggled back off.
70
+ #
71
+ # @note Development mode will expire on "expires_on" (3 hours from when it is toggled on). Development mode can be toggled off immediately by setting +value+ to 0.
72
+ #
73
+ # @param zone The zone you'd like to run CNAMES through CloudFlare for, e.g. +example.com+
74
+ # @param value May be set to true (on) or false (off).
75
+ # @return (Hash) expires_on
76
+
77
+ def devmode(zone, value)
78
+ send_req({a: :devmode, z: zone, v: value == true ? 1 : 0})
79
+ end
80
+
81
+ # This function will purge CloudFlare of any cached files. It may take up to 48 hours for the cache to rebuild and optimum performance to be achieved so this function should be used sparingly.
82
+ #
83
+ # @param zone The zone you'd like to run CNAMES through CloudFlare for, e.g. +example.com+.
84
+ # @return (Hash) fpurge_ts, cooldawn
85
+ # @return *fpurge_ts* - Time at which cache was purged.
86
+ # @return *cooldown* - Number of seconds before the next time this call is allowed again.
87
+
88
+ def purge_cache(zone)
89
+ send_req({a: :fpurge_ts, z: zone, v: 1})
90
+ end
91
+
92
+ # This function checks whether one or more websites/domains are active under an account and return the zone ids (zids) for these.
93
+ #
94
+ # @param zones Zones, eg. +example.com+, +exampletwo.com+
95
+ # @return (Hash) Map of passed in zones. If a zone if hosted on CloudFlare and the email + tkn combination is correct for the given zone, the value for the zone will be its zone id (use this for other API calls). Otherwise 0.
96
+
97
+ def zone_check(*zones)
98
+ send_req({a: :zone_check, zones: zones.kind_of?(Array) ? zones.join(',') : zones})
99
+ end
100
+
101
+ # This function pulls recent IPs hitting your site.
102
+ #
103
+ # @param zoneid Id of the zone you would like to check.
104
+ # @param hours Number of hours to go back. Default is 24, max is 48.
105
+ # @param class Restrict the result set to a given class. Currently r|s|t, for regular, crawler, threat resp.
106
+ # @param geo Add to add longitude and latitude information to the response. 0,0 means no data.
107
+ # @return (Hash) A list of IP addresses which hit your site classified by type.
108
+
109
+ def zone_ips(zoneid, classification, hours = 24, geo = '0,0')
110
+ send_req({a: :zone_ips, zid: zoneid, hours: hours, class: classification, geo: geo})
111
+ end
112
+
113
+ # This functions updates the snapshot of your site for CloudFlare's challenge page.
114
+ #
115
+ # @note Yhis call is rate limited to once per zone per day. Also the new image may take up to 1 hour to appear.
116
+ #
117
+ # @param zoneid Id of the zone you would like to check.
118
+
119
+ def update_image(zoneid)
120
+ send_req({a: :zone_grab, zid: zoneid})
121
+ end
122
+
123
+ # This function adds an IP address to your white lists.
124
+ #
125
+ # @param address The address you wish to set a rule for.
126
+
127
+ def whitelist(address)
128
+ send_req({a: :wl, key: address})
129
+ end
130
+
131
+ # This function adds an IP address to your black lists.
132
+ #
133
+ # @param address The address you wish to set a rule for.
134
+
135
+ def blacklist(address)
136
+ send_req({a: :ban, key: address})
137
+ end
138
+
139
+ # This function creates a new DNS record for your site. This can be either a CNAME or A record.
140
+ #
141
+ # @param zone The zone you'd like to run CNAMES through CloudFlare for, e.g. +example.com+.
142
+ # @param type Type of record - CNAME or A.
143
+ # @param content The value of the cname or IP address (the destination).
144
+ # @param name The name of the record you wish to create.
145
+ # @param mode False or true. false means CloudFlare is off (grey cloud) for the new zone, while true means a happy orange cloud.
146
+
147
+ def add_rec(zone, type, content, name, mode)
148
+ send_req({a: :rec_set, zone: zone, type: type, content: content, name: name, mode: mode == true ? 1 : 0})
149
+ end
150
+
151
+ # This function deletes a DNS record.
152
+ #
153
+ # @note All records of the given name will be deleted. For this reason, you must pass in the full DNS name of the record you wish to remove. For +example+, +sub.foo.com+, as opposed to just sub.
154
+ #
155
+ # @param zone
156
+ # @param name The name of the record you wish to remove.
157
+
158
+ def del_rec(zone, name)
159
+ send_req({a: :rec_del, zone: zone, name: name})
160
+ end
161
+
162
+ # This function purges the preloader's cache.
163
+ #
164
+ # @note Can take up to an hour for this to take effect.
165
+ #
166
+ # @param ip The value of the IP address.
167
+
168
+ def pre_purge(ip)
169
+ send_req({a: :pre_purge, zone_name: ip})
170
+ end
171
+
172
+ # This function updates a DNS record for your site. This needs to be an A record.
173
+ #
174
+ # @param ip The value of the IP address (the destination).
175
+ # @param hosts The name of the record you wish to adjust.
176
+
177
+ def update_rec(ip, hosts)
178
+ send_req({a: :DIUP, ip: ip, hosts: hosts})
179
+ end
180
+
181
+ # This function checks the threat score for a given IP.
182
+ #
183
+ # @note scores are logarithmically increasing, like the Richter scale.
184
+ #
185
+ # @param ip IP address to check.
186
+ # @return The current threat score for a given IP.
187
+
188
+ def threat_score(ip)
189
+ send_req({a: :ip_lkup, ip: ip})
190
+ end
191
+
192
+ # This function toggles ipv6 support for a site.
193
+ #
194
+ # @param zone
195
+ # @param value False disables, true enables support.
196
+
197
+ def toggle_ipv6(zone, value)
198
+ send_req({a: :ipv46, z: zone, v: value == true ? 1 : 0})
199
+ end
200
+
201
+ # HOST
202
+
203
+ # This function creates a CloudFlare account mapped to your user.
204
+ #
205
+ # @param email The user's e-mail address for the new CloudFlare account.
206
+ # @param pass The user's password for the new CloudFlare account. CloudFlare will never store this password in clear text.
207
+ # @param login (optional) The user's username for the new CloudFlare account. CloudFlare will auto-generate one if it is not specified.
208
+ # @param id Set a unique string identifying the User. This identifier will serve as an alias to the user's CloudFlare account. Typically you would set this value to the unique ID in your system (e.g., the internal customer number or username stored in your own system). This parameter can be used to retrieve a user_key when it is required. The unique_id must be an ASCII string with a maximum length of 100 characters.
209
+ # @return (String) cloudflare_email
210
+ # @return (String) user_key
211
+ # @return (String) unique_id
212
+ # @return (String) cloudflare_username
213
+
214
+ def create_user(email, pass, login = nil, id = nil)
215
+ send_req({act: :user_create, cloudflare_email: email, cloudflare_pass: pass, cloudflare_username: login, unique_id: id})
216
+ end
217
+
218
+ # This function setups a User's zone for CNAME hosting.
219
+ #
220
+ # @note This function replaces any previous setup for the particular zone_name. If are adding an additional subdomain to an account that already has some subdomains setup, you should specify all the subdomains not only the new subdomains.
221
+ #
222
+ # @param user_key The unique 32 hex character auth string, identifying the user's CloudFlare Account. Generated from a +create_user+ or +user_auth+.
223
+ # @param zone The zone you'd like to run CNAMES through CloudFlare for, e.g. +example.com+.
224
+ # @param resolve_to The CNAME that CloudFlare should ultimately resolve web connections to after they have been filtered, e.g. +resolve-to-cloudflare.example.com+. This record should ultimately resolve to the one or more IP addresses of the hosts for the particular website for all the specified subdomains.
225
+ # @param subdomains A comma-separated string of subdomain(s) that CloudFlare should host, e.g. +www,blog,forums+ or +www.example.com,blog.example.com,forums.example.com+.
226
+ # @return (String) zone_name
227
+ # @return (String) resolving_to
228
+ # @return (Hash) hosted_cnames
229
+ # @return (Hash) forward_tos
230
+
231
+ def add_zone(user_key, zone, resolve_to, subdomains)
232
+ send_req({act: :zone_set, user_key: user_key, zone_name: zone, resolve_to: resolve_to, subdomains: subdomains.kind_of?(Array) ? zones.join(',') : subdomains})
233
+ end
234
+
235
+ # This function lookups a user's CloudFlare account information.
236
+ #
237
+ # @note If you use +unique_id+, +id+ must be +true+.
238
+ #
239
+ # *Example:*
240
+ #
241
+ # cf = CloudFlare('your_host_key')
242
+ # cf.user_lookup('unique_id', true)
243
+ #
244
+ # @param email Lookup a user's account information or status by either +email+ or +unique_id+.
245
+ # @return (String) user_key
246
+ # @return (Boolean) user_exists
247
+ # @return (Boolean) user_authed
248
+ # @return (String) cloudflare_email
249
+ # @return (String) unique_id
250
+ # @return (Array) hosted_zones
251
+
252
+ def user_lookup(email, id = false)
253
+ if id
254
+ send_req({act: :user_lookup, unique_id: email})
255
+ else
256
+ send_req({act: :user_lookup, cloudflare_email: email})
257
+ end
258
+ end
259
+
260
+ # This function authorizes access to a user's existing CloudFlare account.
261
+ #
262
+ # @param email the user's e-mail address for the new CloudFlare account.
263
+ # @param pass the user's password for the new CloudFlare account. CloudFlare will never store this password in clear text.
264
+ # @param unique_id (optional) set a unique string identifying the user. This identifier will serve as an alias to the user's CloudFlare account. Typically you would set this value to the unique ID in your system. This parameter can be used as an alias for other actions (e.g., it can substitute for the +email+ and +pass+ if you choose not to store those fields in your system).
265
+ # @return (Hash) User's e-mail, key and unique id.
266
+
267
+ def user_auth(email, pass, id = nil)
268
+ send_req({act: :user_auth, cloudflare_email: email, cloudflare_pass: pass, unique_id: id})
269
+ end
270
+
271
+ # This function lookups a specific user's zone.
272
+ #
273
+ # @param user_key API user key
274
+ # @param zone the zone you'd like to lookup, e.g. "example.com"
275
+ # @return (String) zone_name
276
+ # @return (Boolean) zones_exists
277
+ # @return (Boolean) zone_hosted
278
+ # @return (Hash) hosted_cnames
279
+ # @return (Hash) hosted_cnames
280
+ # @return (Hash) forward_tos
281
+
282
+ def zone_lookup(user_key, zone)
283
+ send_req({act: :zone_lookup, user_key: user_key, zone_name: zone})
284
+ end
285
+
286
+ # This function deletes a specific zone on behalf of a user.
287
+ #
288
+ # @param user_key API user key
289
+ # @param zone The zone you'd like to lookup, e.g. +example.com+
290
+ # @return (String) zone_name,
291
+ # @return (Boolean) zone_deleted
292
+
293
+ def del_zone(user_key, zone)
294
+ send_req({act: :zone_delee, user_key: user_key, zone_name: zone})
295
+ end
296
+
297
+ private
298
+
299
+ def send_req(params)
300
+
301
+ if @params[:email]
302
+ params[:tkn] = @params[:api_key]
303
+ params[:u] = @params[:email]
304
+ uri = URI(URL_API[:client])
305
+ else
306
+ params[:host_key] = @params[:api_key]
307
+ uri = URI(URL_API[:host])
308
+ end
309
+
310
+ req = Net::HTTP::Post.new(uri.path)
311
+ req.set_form_data(params)
312
+
313
+ http = Net::HTTP.new(uri.host, uri.port)
314
+ http.use_ssl = true
315
+ http.read_timeout = TIMEOUT
316
+
317
+ begin
318
+ res = http.request(req)
319
+ JSON.parse(res.body)
320
+ rescue => e
321
+ puts "#{e.class} #{e.message}"
322
+ end
323
+
324
+ end
325
+
326
+ end
@@ -0,0 +1,16 @@
1
+ require 'test/unit'
2
+ require 'cloudflare'
3
+
4
+ class HostTest < Test::Unit::TestCase
5
+
6
+ def test_client_connection
7
+ cf = CloudFlare.new('example_api', 'example@example.com')
8
+ assert_equal('E_UNAUTH', cf.toggle_ipv6('example.com', true)['err_code'])
9
+ end
10
+
11
+ def test_host_connection
12
+ cf = CloudFlare.new('example_api')
13
+ assert_equal(100, cf.user_auth('example.com', 'password')['err_code'])
14
+ end
15
+
16
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cloudflare
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Marcin 'B4k3r' Prokop
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: &8532060 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *8532060
25
+ description: A Ruby wrapper for the CloudFlare API.
26
+ email: marcin@prokop.co
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files:
30
+ - README.md
31
+ - LICENSE
32
+ files:
33
+ - Rakefile
34
+ - lib/cloudflare.rb
35
+ - test/test_cloudflare.rb
36
+ - README.md
37
+ - LICENSE
38
+ homepage: http://b4k3r.github.com/cloudflare/
39
+ licenses: []
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --main
43
+ - README.md
44
+ - --charset=UTF-8
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: 1.9.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.11
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: A Ruby wrapper for the CloudFlare API.
65
+ test_files:
66
+ - test/test_cloudflare.rb
67
+ has_rdoc: