Cloudflare
It is a Ruby wrapper for the Cloudflare V4 API. It provides a light weight wrapper using RestClient::Resource
. The wrapper functionality is limited to zones and DNS records at this time, PRs welcome.
Installation
Add this line to your application's Gemfile:
gem 'cloudflare'
And then execute:
$ bundle
Or install it yourself as:
$ gem install cloudflare
Usage
Prepare a connection to the remote API:
require 'cloudflare'
# Grab some details from somewhere:
email = ENV['CLOUDFLARE_EMAIL']
key = ENV['CLOUDFLARE_KEY']
# Set up the connection:
connection = Cloudflare.connect(key: key, email: email)
Get all available zones:
zones = connection.zones.all
Get a specific zone:
zone = connection.zones.find_by_id("...")
zone = connection.zones.find_by_name("example.com")
Get DNS records for a given zone:
dns_records = zones.first.dns_records.all
Show some details of the DNS record:
dns_record = records.first
puts records.first.record[:name]
puts records
Get firewall rules:
all_rules = zones.first.firewall_rules.all
block_rules = zones.first.firewall_rules.all("block") # or "whitelist" or "challenge"
Get blocked ips:
block_rules = zones.first.firewall_rules.all("block")
blocked_ips = zones.first.firewall_rules.firewalled_ips(block_rules)
Block an ip:
# ip = "nnn.nnn.nnn.nnn"
# note: "some note about the block"
data = {"mode":"block","configuration":{"target":"ip","value":"#{ip}"},"notes":"#{note} #{Time.now.strftime("%m/%d/%y")} "}
response = zones.first.firewall_rules.post(data.to_json, content_type: 'application/json')
Add a DNS record dynamically. Here we add an A record for batman.example.com
:
client = Cloudflare.connect(key: CF_KEY, email: CF_EMAIL)
zone = client.zones.find_by_name("example.com")
zone.dns_records.post({"type":"A","name":"batman","content":"127.0.0.1","proxied":false}.to_json, :content_type => "application/json")
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
See Also
- Cloudflare::DNS::Update - A dynamic DNS updater based on this gem.
- Rubyflare - Another implementation.
License
Released under the MIT license.
Copyright, 2012, 2014, by Marcin Prokop.
Copyright, 2017, by Samuel G. D. Williams.
Copyright, 2017, by David Rosenbloom.
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.