cloudflare 1.1.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,554 @@
1
+ # Copyright, 2012, by Marcin Prokop.
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'net/http'
22
+ require 'json'
23
+
24
+ # For more information please visit:
25
+ # - http://www.cloudflare.com/docs/client-api.html
26
+ # - http://www.cloudflare.com/docs/host-api.html
27
+ #
28
+ module CloudFlare
29
+ class RequestError < StandardError
30
+ def initialize(what, response)
31
+ super(what)
32
+
33
+ @response = response
34
+ end
35
+
36
+ attr :response
37
+ end
38
+
39
+ class Connection
40
+ # URL for Client and Host API
41
+ URL_API = {
42
+ client: 'https://www.cloudflare.com/api_json.html',
43
+ host: 'https://api.cloudflare.com/host-gw.html'
44
+ }
45
+
46
+ TIMEOUT = 5 # Default is 5 seconds
47
+
48
+ # @param api_key [String] user or Host API key.
49
+ # @param email [String] it is for a Client API.
50
+ def initialize(api_key, email = nil)
51
+ @params = Hash.new
52
+
53
+ if email.nil?
54
+ @params[:api_key] = api_key
55
+ else
56
+ @params[:api_key] = api_key
57
+ @params[:email] = email
58
+ end
59
+
60
+ end
61
+
62
+ # CLIENT
63
+
64
+ # This function can be used to get currently settings of values such as the security level.
65
+ #
66
+ # @see http://www.cloudflare.com/docs/client-api.html#s3.1
67
+ #
68
+ # @param zone [String]
69
+ # @param interval [Integer]
70
+
71
+ def stats(zone, interval = 20)
72
+ send_req({a: :stats, z: zone, interval: interval})
73
+ end
74
+
75
+ # This function lists all domains in a CloudFlare account along with other data.
76
+ #
77
+ # @see http://www.cloudflare.com/docs/client-api.html#s3.2
78
+
79
+ def zone_load_multi
80
+ send_req(a: :zone_load_multi)
81
+ end
82
+
83
+ # This function lists all of the DNS records from a particular domain in a CloudFlare account.
84
+ #
85
+ # @see http://www.cloudflare.com/docs/client-api.html#s3.3
86
+ #
87
+ # @param zone [String]
88
+
89
+ def rec_load_all(zone)
90
+ send_req({a: :rec_load_all, z: zone})
91
+ end
92
+
93
+ # This function checks whether one or more websites/domains are active under an account and return the zone ids (zids) for these.
94
+ #
95
+ # @see http://www.cloudflare.com/docs/client-api.html#s3.4
96
+ #
97
+ # @param zones [String or Array]
98
+
99
+ def zone_check(*zones)
100
+ send_req({a: :zone_check, zones: zones.kind_of?(Array) ? zones.join(',') : zones})
101
+ end
102
+
103
+ # This function pulls recent IPs hitting your site.
104
+ #
105
+ # @see http://www.cloudflare.com/docs/client-api.html#s3.5
106
+ #
107
+ # @param zone [String]
108
+ # @param hours [Integer] max 48
109
+ # @param classification [String] (optional) values: r|c|t
110
+ # @param geo [Fixnum] (optional)
111
+
112
+ def zone_ips(zone, classification = nil, hours = 24, geo = 1)
113
+ send_req({a: :zone_ips, z: zone, hours: hours, "class" => classification, geo: geo})
114
+ end
115
+
116
+ # This function checks the threat score for a given IP.
117
+ #
118
+ # @see http://www.cloudflare.com/docs/client-api.html#s3.6
119
+ #
120
+ # @param ip [String]
121
+
122
+ def ip_lkup(ip)
123
+ send_req({a: :ip_lkup, ip: ip})
124
+ end
125
+
126
+ # This function retrieves all current settings for a given domain.
127
+ #
128
+ # @see http://www.cloudflare.com/docs/client-api.html#s3.7
129
+ #
130
+ # @param zone [String]
131
+
132
+ def zone_settings(zone)
133
+ send_req({a: :zone_settings, z: zone})
134
+ end
135
+
136
+ # This function sets the Basic Security Level to HELP I'M UNDER ATTACK / HIGH / MEDIUM / LOW / ESSENTIALLY OFF.
137
+ #
138
+ # @see http://www.cloudflare.com/docs/client-api.html#s4.1
139
+ #
140
+ # @param zone [String]
141
+ # @param value [String] values: low|med|high|help|eoff
142
+
143
+ def sec_lvl(zone, value)
144
+ send_req({a: :sec_lvl, z: zone, v: value})
145
+ end
146
+
147
+ # This function sets the Caching Level to Aggressive or Basic.
148
+ #
149
+ # @see http://www.cloudflare.com/docs/client-api.html#s4.2
150
+ #
151
+ # @param zone [String]
152
+ # @param value [String] values: agg|basic
153
+
154
+ def cache_lvl(zone, value)
155
+ send_req({a: :cache_lvl, z: zone, v: value})
156
+ end
157
+
158
+ # This function allows you to toggle Development Mode on or off for a particular domain.
159
+ #
160
+ # @see http://www.cloudflare.com/docs/client-api.html#s4.3
161
+ #
162
+ # @param zone [String]
163
+ # @param value [Boolean]
164
+
165
+ def devmode(zone, value)
166
+ send_req({a: :devmode, z: zone, v: value ? 1 : 0})
167
+ end
168
+
169
+ # This function will purge CloudFlare of any cached files.
170
+ #
171
+ # @see http://www.cloudflare.com/docs/client-api.html#s4.4
172
+ #
173
+ # @param zone [String]
174
+
175
+ def fpurge_ts(zone)
176
+ send_req({a: :fpurge_ts, z: zone, v: 1})
177
+ end
178
+
179
+ # This function will purge a single file from CloudFlare's cache.
180
+ #
181
+ # @see http://www.cloudflare.com/docs/client-api.html#s4.5
182
+ #
183
+ # @param zone [String]
184
+ # @param url [String]
185
+
186
+ def zone_file_purge(zone, url)
187
+ send_req({a: :zone_file_purge, z: zone, url: url})
188
+ end
189
+
190
+ # This function updates the snapshot of your site for CloudFlare's challenge page.
191
+ #
192
+ # @see http://www.cloudflare.com/docs/client-api.html#s4.6
193
+ #
194
+ # @param zoneid [Integer]
195
+
196
+ def zone_grab(zoneid)
197
+ send_req({a: :zone_grab, zid: zoneid})
198
+ end
199
+
200
+ # This function adds an IP address to your white lists.
201
+ #
202
+ # @see http://www.cloudflare.com/docs/client-api.html#s4.7
203
+ #
204
+ # @param ip [String]
205
+
206
+ def whitelist(ip)
207
+ send_req({a: :wl, key: ip})
208
+ end
209
+
210
+
211
+ # This function adds an IP address to your black lists.
212
+ #
213
+ # @see http://www.cloudflare.com/docs/client-api.html#s4.7
214
+ #
215
+ # @param ip [String]
216
+
217
+ def blacklist(ip)
218
+ send_req({a: :ban, key: ip})
219
+ end
220
+
221
+ # This function removes the IP from whitelist or blacklist.
222
+ #
223
+ # @see http://www.cloudflare.com/docs/client-api.html#s4.7
224
+ #
225
+ # @param ip [String]
226
+
227
+ def remove_ip(ip)
228
+ send_req({a: :nul, key: ip})
229
+ end
230
+
231
+ # This function toggles IPv6 support.
232
+ #
233
+ # @see http://www.cloudflare.com/docs/client-api.html#s4.8
234
+ #
235
+ # @param zone [String]
236
+ # @param value [Boolean]
237
+
238
+ def ipv46(zone, value)
239
+ send_req({a: :ipv46, z: zone, v: value ? 1 : 0})
240
+ end
241
+
242
+ # This function changes Rocket Loader setting.
243
+ #
244
+ # @see http://www.cloudflare.com/docs/client-api.html#s4.9
245
+ #
246
+ # @param zone [String]
247
+ # @param value [Integer or String] values: 0|a|m
248
+
249
+ def async(zone, value)
250
+ send_req({a: :async, z: zone, v: value})
251
+ end
252
+
253
+ # This function changes minification settings.
254
+ #
255
+ # @see http://www.cloudflare.com/docs/client-api.html#s4.10
256
+ #
257
+ # @param zone [String]
258
+ # @param value [Integer] values: 0|2|3|4|5|6|7
259
+
260
+ def minify(zone, value)
261
+ send_req({a: :minify, z: zone, v: value})
262
+ end
263
+
264
+
265
+ # This function changes mirage2 settings.
266
+ #
267
+ # @see http://www.cloudflare.com/docs/client-api.html#s4.11
268
+ #
269
+ # @param zone [String]
270
+ # @param value [Integer] values: 0|1
271
+
272
+ def mirage2(zone, value)
273
+ send_req({a: :mirage2, z: zone, v: value})
274
+ end
275
+
276
+ # This function creates a new DNS record for your site. This can be either a CNAME or A record.
277
+ #
278
+ # @see http://www.cloudflare.com/docs/client-api.html#s5.1
279
+ #
280
+ # @param zone [String]
281
+ # @param type [String] values: A|CNAME|MX|TXT|SPF|AAAA|NS|SRV|LOC
282
+ # @param name [String]
283
+ # @param content [String]
284
+ # @param ttl [Integer] values: 1|120...4294967295
285
+ # @param prio [Integer] (applies to MX/SRV)
286
+ # @param service [String] (applies to SRV)
287
+ # @param srvname [String] (applies to SRV)
288
+ # @param protocol [Integer] (applies to SRV) values: _tcp|_udp|_tls
289
+ # @param weight [Intger] (applies to SRV)
290
+ # @param port [Integer] (applies to SRV)
291
+ # @param target [String] (applies to SRV)
292
+
293
+ def rec_new(zone, type, name, content, ttl, prio = nil, service = nil, srvname = nil, protocol = nil, weight = nil, port = nil, target = nil)
294
+ send_req({
295
+ a: :rec_new,
296
+ z: zone,
297
+ type: type,
298
+ name: name,
299
+ content: content,
300
+ ttl: ttl,
301
+ prio: prio,
302
+ service: service,
303
+ srvname: srvname,
304
+ protocol: protocol,
305
+ weight: weight,
306
+ port: port,
307
+ target: target
308
+ })
309
+ end
310
+
311
+ # This function edits a DNS record for a zone.
312
+ #
313
+ # @see http://www.cloudflare.com/docs/client-api.html#s5.2
314
+ #
315
+ # @param zone [String]
316
+ # @param type [String] values: A|CNAME|MX|TXT|SPF|AAAA|NS|SRV|LOC
317
+ # @param record_id [Integer]
318
+ # @param name [String]
319
+ # @param content [String]
320
+ # @param ttl [Integer] values: 1|120...4294967295
321
+ # @param service_mode [Boolean] (applies to A/AAAA/CNAME)
322
+ # @param prio [Integer] (applies to MX/SRV)
323
+ # @param service [String] (applies to SRV)
324
+ # @param srvname [String] (applies to SRV)
325
+ # @param protocol [Integer] (applies to SRV) values: _tcp/_udp/_tls
326
+ # @param weight [Intger] (applies to SRV)
327
+ # @param port [Integer] (applies to SRV)
328
+ # @param target [String] (applies to SRV)
329
+
330
+ def rec_edit(zone, type, record_id, name, content, ttl, service_mode = nil, prio = nil, service = nil, srvname = nil, protocol = nil, weight = nil, port = nil, target = nil)
331
+ send_req({
332
+ a: :rec_edit,
333
+ z: zone,
334
+ type: type,
335
+ id: record_id,
336
+ name: name,
337
+ content: content,
338
+ ttl: ttl,
339
+ service_mode: service_mode ? 1 : 0,
340
+ prio: prio,
341
+ service: service,
342
+ srvname: srvname,
343
+ protocol: protocol,
344
+ weight: weight,
345
+ port: port,
346
+ target: target
347
+ })
348
+ end
349
+
350
+ # This functon deletes a record for a domain.
351
+ #
352
+ # @see http://www.cloudflare.com/docs/client-api.html#s5.3
353
+ #
354
+ # @param zone [String]
355
+ # @param zoneid [Integer]
356
+
357
+ def rec_delete(zone, zoneid)
358
+ send_req({a: :rec_delete, z: zone, id: zoneid})
359
+ end
360
+
361
+ # HOST
362
+
363
+ # This function creates a CloudFlare account mapped to your user.
364
+ #
365
+ # @see http://www.cloudflare.com/docs/host-api.html#s3.2.1
366
+ #
367
+ # @param email [String]
368
+ # @param pass [String]
369
+ # @param login [String] (optional) cloudflare_username
370
+ # @param id [Integer] (optional) unique_id
371
+ # @param cui [Integer] (optional) clobber_unique_id
372
+
373
+ def create_user(email, pass, login = nil, id = nil, cui = nil)
374
+ send_req({
375
+ act: :user_create,
376
+ cloudflare_email: email,
377
+ cloudflare_pass: pass,
378
+ cloudflare_username: login,
379
+ unique_id: id,
380
+ clobber_unique_id: cui
381
+ })
382
+ end
383
+
384
+ # This function setups a User's zone for CNAME hosting.
385
+ #
386
+ # @see http://www.cloudflare.com/docs/host-api.html#s3.2.2
387
+ #
388
+ # @param user_key [String]
389
+ # @param zone [String]
390
+ # @param resolve_to [String]
391
+ # @param subdomains [String or Array]
392
+
393
+ def add_zone(user_key, zone, resolve_to, subdomains)
394
+ send_req({
395
+ act: :zone_set,
396
+ user_key: user_key,
397
+ zone_name: zone,
398
+ resolve_to: resolve_to,
399
+ subdomains: subdomains.kind_of?(Array) ? zones.join(',') : subdomains
400
+ })
401
+ end
402
+
403
+ # This function lookups a user's CloudFlare account information.
404
+ #
405
+ # @see http://www.cloudflare.com/docs/host-api.html#s3.2.3
406
+ #
407
+ # *Example:*
408
+ #
409
+ # cf = CloudFlare('your_host_key')
410
+ # cf.user_lookup('unique_id', true)
411
+ #
412
+ # If +id+ is set to true, email is a unique_id.
413
+ #
414
+ # @param email [String or Integer]
415
+ # @param id [Boolean]
416
+
417
+ def user_lookup(email, id = false)
418
+ if id
419
+ send_req({act: :user_lookup, unique_id: email})
420
+ else
421
+ send_req({act: :user_lookup, cloudflare_email: email})
422
+ end
423
+ end
424
+
425
+ # This function authorizes access to a user's existing CloudFlare account.
426
+ #
427
+ # @see http://www.cloudflare.com/docs/host-api.html#s3.2.4
428
+ #
429
+ # @param email [String]
430
+ # @param pass [String]
431
+ # @param unique_id [Integer] (optional)
432
+ # @param cui [Integer] (optional) clobber_unique_id
433
+
434
+ def user_auth(email, pass, id = nil, cui = nil)
435
+ send_req({
436
+ act: :user_auth,
437
+ cloudflare_email: email,
438
+ cloudflare_pass: pass,
439
+ unique_id: id,
440
+ clobber_unique_id: cui
441
+ })
442
+ end
443
+
444
+ # This function lookups a specific user's zone.
445
+ #
446
+ # @see http://www.cloudflare.com/docs/host-api.html#s3.2.5
447
+ #
448
+ # @param user_key [String]
449
+ # @param zone [String]
450
+
451
+ def zone_lookup(user_key, zone)
452
+ send_req({act: :zone_lookup, user_key: user_key, zone_name: zone})
453
+ end
454
+
455
+ # This function deletes a specific zone on behalf of a user.
456
+ #
457
+ # @see http://www.cloudflare.com/docs/host-api.html#s3.2.6
458
+ #
459
+ # @param user_key [String]
460
+ # @param zone [String]
461
+
462
+ def del_zone(user_key, zone)
463
+ send_req({act: :zone_delete, user_key: user_key, zone_name: zone})
464
+ end
465
+
466
+ # This function creates a new child host provider.
467
+ #
468
+ # @see http://www.cloudflare.com/docs/host-api.html#s3.2.7
469
+ #
470
+ # @param host_name [String]
471
+ # @param pub_name [String]
472
+ # @param prefix [String]
473
+ # @param website [String]
474
+ # @param email [String]
475
+
476
+ def host_child_new(host_name, pub_name, prefix, website, email)
477
+ send_req({
478
+ act: :host_child_new,
479
+ pub_name: pub_name,
480
+ prefix: prefix,
481
+ website: website,
482
+ email: email
483
+ })
484
+ end
485
+
486
+ # This function regenerates your host key.
487
+ #
488
+ # @see http://www.cloudflare.com/docs/host-api.html#s3.2.8
489
+
490
+ def host_key_regen
491
+ send_req(act: :host_key_regen)
492
+ end
493
+
494
+ # This function stops a child host provider account.
495
+ #
496
+ # @see http://www.cloudflare.com/docs/host-api.html#s3.2.9
497
+ #
498
+ # @param id [Integer] child_id
499
+
500
+ def host_child_stop(id)
501
+ send_req({act: :host_child_stop, child_id: id})
502
+ end
503
+
504
+ # This function lists the domains currently active on CloudFlare for the given host.
505
+ #
506
+ # @see http://www.cloudflare.com/docs/host-api.html#s3.2.10
507
+ #
508
+ # @param limit [Integer] (optional)
509
+ # @param offset [Integer] (optional)
510
+ # @param name [String] (optional) zone_name
511
+ # @param sub_id [Integer] (optional) sub_id
512
+ # @param status [String] (optional) values: V|D|ALL
513
+
514
+ def zone_list(limit = 100, offset = 0, name = nil, sub_id = nil, status = nil)
515
+ send_req({
516
+ act: :zone_list,
517
+ offset: offset,
518
+ zone_name: name,
519
+ sub_id: sub_id,
520
+ zone_status: status
521
+ })
522
+ end
523
+
524
+ private
525
+
526
+ def send_req(params)
527
+ if @params[:email]
528
+ params[:tkn] = @params[:api_key]
529
+ params[:u] = @params[:email]
530
+ uri = URI(URL_API[:client])
531
+ else
532
+ params[:host_key] = @params[:api_key]
533
+ uri = URI(URL_API[:host])
534
+ end
535
+
536
+ req = Net::HTTP::Post.new(uri.path)
537
+ req.set_form_data(params)
538
+
539
+ http = Net::HTTP.new(uri.host, uri.port)
540
+ http.use_ssl = true
541
+ http.read_timeout = TIMEOUT
542
+
543
+ res = http.request(req)
544
+ out = JSON.parse(res.body)
545
+
546
+ # If there is an error, raise an exception:
547
+ if out['result'] == 'error'
548
+ raise RequestError.new(out['msg'], out)
549
+ else
550
+ return out
551
+ end
552
+ end
553
+ end
554
+ end