cloudflare-ddns 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 298c188002a7a30d02154717a008377c6aa054b92aaf61fdf10e3a75cbedaad9
4
+ data.tar.gz: 9ccaf594f529a37f24c33fce5324ff1b037e5ff2366370d9ba26b141667b57cd
5
+ SHA512:
6
+ metadata.gz: 9cbe83c4d6ad54f72f8542f3b1f08773b4bdc9d79db91951b4ec02297e85f4c772fe72a29b7c4d4819ebea8ffc5d8e84fc8dbd147d4a53c47320b7093a36104a
7
+ data.tar.gz: c03299b2567ea0c2246aedee7000e0e9d17f276ac9327d6cf74de7ebe619759fc0e2a4ac8180e06f31f010922f0b4351acdda281fd88708a620410a91bbda6b4
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rufo ADDED
@@ -0,0 +1 @@
1
+ quote_style :single
@@ -0,0 +1,3 @@
1
+ {
2
+ "editor.formatOnSaveMode": "file"
3
+ }
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cloudflare-ddns.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,44 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cloudflare-ddns (0.1.0)
5
+ docopt (~> 0.5)
6
+ httparty (~> 0.17)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ diff-lcs (1.4.4)
12
+ docopt (0.6.1)
13
+ httparty (0.18.1)
14
+ mime-types (~> 3.0)
15
+ multi_xml (>= 0.5.2)
16
+ mime-types (3.3.1)
17
+ mime-types-data (~> 3.2015)
18
+ mime-types-data (3.2020.1104)
19
+ multi_xml (0.6.0)
20
+ rake (12.3.3)
21
+ rspec (3.10.0)
22
+ rspec-core (~> 3.10.0)
23
+ rspec-expectations (~> 3.10.0)
24
+ rspec-mocks (~> 3.10.0)
25
+ rspec-core (3.10.1)
26
+ rspec-support (~> 3.10.0)
27
+ rspec-expectations (3.10.1)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.10.0)
30
+ rspec-mocks (3.10.1)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.10.0)
33
+ rspec-support (3.10.1)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ cloudflare-ddns!
40
+ rake (~> 12.0)
41
+ rspec (~> 3.0)
42
+
43
+ BUNDLED WITH
44
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Daniel Perez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,16 @@
1
+ # Cloudflare::Ddns
2
+
3
+ Simple script to update DDNS record on Cloudflare
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ gem install cloudflare-ddns
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```
14
+ export CLOUDFLARE_API_TOKEN=very-secret-token
15
+ cloudflare-ddns --zone example.com --record foo.example.com
16
+ ```
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cloudflare/ddns"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ require_relative 'lib/cloudflare/ddns/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'cloudflare-ddns'
5
+ spec.version = Cloudflare::DDNS::VERSION
6
+ spec.authors = ['Daniel Perez']
7
+ spec.email = ['daniel@perez.sh']
8
+
9
+ spec.summary = %q{Set DNS record using Cloudflare API}
10
+ spec.homepage = 'https://github.com/danhper/cloudflare-ddns'
11
+ spec.license = 'MIT'
12
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
13
+
14
+ spec.metadata['homepage_uri'] = spec.homepage
15
+ spec.metadata['source_code_uri'] = 'https://github.com/danhper/cloudflare-ddns'
16
+
17
+ spec.add_runtime_dependency 'httparty', '~> 0.17'
18
+ spec.add_runtime_dependency 'docopt', '~> 0.5'
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = 'exe'
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ['lib']
28
+ end
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'cloudflare/ddns'
4
+ require 'docopt'
5
+
6
+ # Cloudflare::DDNS.hello
7
+ PROGRAM_NAME = File.basename(__FILE__)
8
+
9
+ doc = <<DOCOPT
10
+ Updates Cloudflare DNS record with current IP
11
+
12
+ Usage:
13
+ #{PROGRAM_NAME} --zone=<zone> --record=<record> [--api-token=<api-token>]
14
+
15
+ Options:
16
+ --zone=<zone> The name of the zone to update
17
+ --record=<record> The name of the record to update
18
+ --api-token=<api-token> The API token (can also be passed using CLOUDFLARE_API_TOKEN env variable)
19
+ DOCOPT
20
+
21
+ begin
22
+ args = Docopt.docopt(doc)
23
+ api_token = args['--api-token'] || ENV['CLOUDFLARE_API_TOKEN']
24
+ raise Docopt::Exit, "CLOUDFLARE_API_TOKEN not found, see `#{PROGRAM_NAME} -h` for help" if api_token.nil?
25
+ record = args['--record']
26
+
27
+ result = Cloudflare::DDNS.update_record(api_token, args['--zone'], record)
28
+ puts "#{record} updated to #{result}" if result
29
+ rescue Docopt::Exit => e
30
+ puts e.message
31
+ exit
32
+ end
@@ -0,0 +1,68 @@
1
+ require 'httparty'
2
+
3
+ module Cloudflare
4
+ class BaseAPI
5
+ include HTTParty
6
+
7
+ class Error < StandardError; end
8
+
9
+ class Parser::Cloudflare < HTTParty::Parser
10
+ protected
11
+
12
+ def json
13
+ parsed = JSON.parse(body, :quirks_mode => true, :allow_nan => true)
14
+ raise Error, "call failed: #{body}" if !parsed['success']
15
+ parsed['result']
16
+ end
17
+ end
18
+
19
+ parser Parser::Cloudflare
20
+ end
21
+
22
+ class API < BaseAPI
23
+ base_uri 'api.cloudflare.com/client/v4'
24
+
25
+ def initialize(api_token)
26
+ @options = {
27
+ headers: {
28
+ 'Authorization' => "Bearer #{api_token}",
29
+ 'Content-Type' => 'application/json',
30
+ 'Accept' => 'application/json',
31
+ },
32
+ }
33
+ end
34
+
35
+ def zone_api(name)
36
+ zone_id = zone_id(name)
37
+ Zone.new(@options, zone_id)
38
+ end
39
+
40
+ def zone_id(name)
41
+ zones = self.class.get('/zones', @options.merge(query: { name: name })).parsed_response
42
+ raise "#{name} not found" if zones.empty?
43
+ zones.first['id']
44
+ end
45
+
46
+ class Zone < BaseAPI
47
+ def initialize(options, zone_id)
48
+ @options = options
49
+ self.class.base_uri "https://api.cloudflare.com/client/v4/zones/#{zone_id}"
50
+ end
51
+
52
+ def records(query = {})
53
+ options = @options.merge(query: query)
54
+ self.class.get('/dns_records', options).parsed_response
55
+ end
56
+
57
+ def record(name)
58
+ records = records(name: name)
59
+ raise "#{name} not found" if records.empty?
60
+ records.first
61
+ end
62
+
63
+ def patch_record(id, updates)
64
+ self.class.patch("/dns_records/#{id}", @options.merge(body: updates.to_json))
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,26 @@
1
+ require 'cloudflare/ddns/version'
2
+ require 'httparty'
3
+ require_relative './api'
4
+
5
+ module Cloudflare
6
+ module DDNS
7
+ IP_RESOLVER = 'https://api.ipify.org'
8
+
9
+ class Error < StandardError; end
10
+
11
+ def self.update_record(api_token, zone_name, record_name)
12
+ api = API.new(api_token)
13
+ zone_api = api.zone_api(zone_name)
14
+ record = zone_api.record(record_name)
15
+ current_ip = fetch_ip
16
+ return if record['content'] == current_ip
17
+
18
+ zone_api.patch_record(record['id'], content: current_ip)
19
+ current_ip
20
+ end
21
+
22
+ def self.fetch_ip
23
+ HTTParty.get(IP_RESOLVER).body
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ module Cloudflare
2
+ module DDNS
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cloudflare-ddns
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Perez
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-01-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.17'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: docopt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.5'
41
+ description:
42
+ email:
43
+ - daniel@perez.sh
44
+ executables:
45
+ - cloudflare-ddns
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".rspec"
51
+ - ".rufo"
52
+ - ".vscode/settings.json"
53
+ - Gemfile
54
+ - Gemfile.lock
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - bin/console
59
+ - bin/setup
60
+ - cloudflare-ddns.gemspec
61
+ - exe/cloudflare-ddns
62
+ - lib/cloudflare/api.rb
63
+ - lib/cloudflare/ddns.rb
64
+ - lib/cloudflare/ddns/version.rb
65
+ homepage: https://github.com/danhper/cloudflare-ddns
66
+ licenses:
67
+ - MIT
68
+ metadata:
69
+ homepage_uri: https://github.com/danhper/cloudflare-ddns
70
+ source_code_uri: https://github.com/danhper/cloudflare-ddns
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 2.3.0
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubygems_version: 3.1.4
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Set DNS record using Cloudflare API
90
+ test_files: []