dns_record_updater 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 76c69d9369e9b0b64c2fef645d07f43f611dc540c06a1c73a4994d90a56ee0c2
4
+ data.tar.gz: 2614538f88b8b3feb1277caf8182d4a6996eea9933e155cd19d7a15d925735ee
5
+ SHA512:
6
+ metadata.gz: 8adbd4a4ed69157ca49b4e13d7cce24087d1cf9b263ec7d461ae13d302c7a73e6d563d3bd6334d9f4a3e675cc12d1901b86931ac9e164ae521acd39fe60b9525
7
+ data.tar.gz: 6a48dd95076b36735c81d6e500567dbba3632c1d25ce7649958d7bb4d19638813fb81efb75b1a201f88fc4d7936486c8af79ff6698b0f590a523200580dda177
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-11-20
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Taeyang Lee
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.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # DnsRecordUpdater
2
+
3
+ Library for updating DNS record on Cloudflare with support for notifications.
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ gem install dns_record_updater
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Here's a simple example of how to use the `DNSRecordUpdater` gem:
14
+
15
+ ```ruby
16
+ notification_service = DNSRecordUpdater::NotificationService.new([
17
+ slackuri='' # URI for Slack WebHook "https://hooks.slack.com/services/xxxxx"
18
+ discorduri='' # URI for Discord WebHook "https://discordapp.com/api/webhooks/xxxxx"
19
+ ])
20
+ cloudflare_provider = DNSRecordUpdater::CloudflareProvider.new({
21
+ auth_email: '', # The email used to login 'https://dash.cloudflare.com'
22
+ auth_method: '', # Set to "global" for Global API Key or "token" for Scoped API Token
23
+ auth_key: '', # Your API Token or Global API Key
24
+ zone_identifier: '', # Can be found in the "Overview" tab of your domain
25
+ record_name: '' # record you want to be synced
26
+ }, notification_service)
27
+ dns_updater = DNSRecordUpdater::DNSUpdater.new(cloudflare_provider)
28
+ ```
29
+
30
+ ## Development
31
+
32
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
33
+
34
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
35
+
36
+ ## Contributing
37
+
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dns_record_updater. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/dns_record_updater/blob/main/CODE_OF_CONDUCT.md).
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
43
+
44
+ ## Code of Conduct
45
+
46
+ Everyone interacting in the DnsRecordUpdater project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/dns_record_updater/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DNSRecordUpdater
4
+ class CloudflareProvider < DNSProvider
5
+ def initialize(config)
6
+ @auth_email = config[:auth_email]
7
+ @auth_method = config[:auth_method]
8
+ @auth_key = config[:auth_key]
9
+ @zone_identifier = config[:zone_identifier]
10
+ @record_name = config[:record_name]
11
+ @ttl = config[:ttl] || 3600
12
+ @proxy = config[:proxy] || false
13
+ end
14
+
15
+ def fetch_dns_record
16
+ auth_header = @auth_method == "global" ? "X-Auth-Key" : "Authorization: Bearer"
17
+ uri = URI("https://api.cloudflare.com/client/v4/zones/#{@zone_identifier}/dns_records?type=A&name=#{@record_name}")
18
+ request = Net::HTTP::Get.new(uri)
19
+ request["X-Auth-Email"] = @auth_email
20
+ request[auth_header] = @auth_key
21
+
22
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(request) }
23
+ result = JSON.parse(response.body)
24
+
25
+ if result["success"]
26
+ record = result["result"].first
27
+ { record_id: record["id"], current_ip: record["content"] }
28
+ else
29
+ raise "Failed to fetch DNS record: #{result['errors']}"
30
+ end
31
+ end
32
+
33
+ def update_dns_record(ip, record_id)
34
+ auth_header = @auth_method == "global" ? "X-Auth-Key" : "Authorization: Bearer"
35
+ uri = URI("https://api.cloudflare.com/client/v4/zones/#{@zone_identifier}/dns_records/#{record_id}")
36
+ request = Net::HTTP::Patch.new(uri)
37
+ request["X-Auth-Email"] = @auth_email
38
+ request[auth_header] = @auth_key
39
+ request["Content-Type"] = "application/json"
40
+ request.body = { type: "A", name: @record_name, content: ip, ttl: @ttl, proxied: @proxy }.to_json
41
+
42
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(request) }
43
+ result = JSON.parse(response.body)
44
+
45
+ if result["success"]
46
+ puts "Updated DNS record: #{@record_name} -> #{ip}"
47
+ true
48
+ else
49
+ puts "Failed to update DNS record: #{result['errors']}"
50
+ false
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,13 @@
1
+ module DNSRecordUpdater
2
+ class DNSProvider
3
+ attr_reader :record_name
4
+
5
+ def fetch_dns_record
6
+ raise NotImplementedError, "Must be implemented by a provider"
7
+ end
8
+
9
+ def update_dns_record(ip, record_id)
10
+ raise NotImplementedError, "Must be implemented by a provider"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,52 @@
1
+ require 'net/http'
2
+
3
+ module DNSRecordUpdater
4
+ class DNSUpdater
5
+ def initialize(provider, notification_service: nil)
6
+ @provider = provider
7
+ @notification_service = notification_service
8
+ end
9
+
10
+ def update(send_notification: false)
11
+ ip = fetch_public_ip
12
+ puts "Detected public IP: #{ip}"
13
+
14
+ dns_record = @provider.fetch_dns_record
15
+ record_id = dns_record[:record_id]
16
+ current_ip = dns_record[:current_ip]
17
+
18
+ if ip == current_ip
19
+ puts "IP has not changed: #{ip}"
20
+ return true
21
+ end
22
+
23
+ success = @provider.update_dns_record(ip, record_id)
24
+ if send_notification && success
25
+ @notification_service&.notify("DNS record updated: #{@provider.record_name} -> #{ip}")
26
+ end
27
+ success
28
+ rescue StandardError => e
29
+ puts "Error during update: #{e.message}"
30
+ false
31
+ end
32
+
33
+ private
34
+
35
+ def fetch_public_ip
36
+ ipv4_regex = /\A((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\z/
37
+ sources = [
38
+ "https://cloudflare.com/cdn-cgi/trace",
39
+ "https://api.ipify.org",
40
+ "https://ipv4.icanhazip.com"
41
+ ]
42
+
43
+ sources.each do |url|
44
+ response = Net::HTTP.get(URI(url))
45
+ ip = response[/\d+\.\d+\.\d+\.\d+/]
46
+ return ip if ip&.match?(ipv4_regex)
47
+ end
48
+
49
+ raise "Unable to fetch a valid public IP address."
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,16 @@
1
+ require 'net/http'
2
+
3
+ module DNSRecordUpdater
4
+ class NotificationService
5
+ def initialize(endpoints)
6
+ @endpoints = endpoints
7
+ end
8
+
9
+ def notify(message)
10
+ @endpoints.each do |uri|
11
+ payload = { content: message }.to_json
12
+ Net::HTTP.post(URI(uri), payload, { "Content-Type" => "application/json" })
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DNSRecordUpdater
4
+ VERSION = "0.1.1"
5
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "dns_record_updater/version"
4
+ require_relative "dns_record_updater/dns_updater"
5
+ require_relative "dns_record_updater/dns_provider"
6
+ require_relative "dns_record_updater/cloudflare_provider"
7
+ require_relative "dns_record_updater/notification_service"
8
+
9
+ module DNSRecordUpdater
10
+ class Error < StandardError; end
11
+ # Your code goes here...
12
+ end
@@ -0,0 +1,4 @@
1
+ module DnsRecordUpdater
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dns_record_updater
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Taeyang Lee
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-11-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: net-http
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: A composable gem to update DNS records with support for Cloudflare and
84
+ notifications.
85
+ email:
86
+ - taeyang91@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".rspec"
92
+ - CHANGELOG.md
93
+ - CODE_OF_CONDUCT.md
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - lib/dns_record_updater.rb
98
+ - lib/dns_record_updater/cloudflare_provider.rb
99
+ - lib/dns_record_updater/dns_provider.rb
100
+ - lib/dns_record_updater/dns_updater.rb
101
+ - lib/dns_record_updater/notification_service.rb
102
+ - lib/dns_record_updater/version.rb
103
+ - sig/dns_record_updater.rbs
104
+ homepage: https://github.com/taeyang91/dns_record_updater
105
+ licenses:
106
+ - MIT
107
+ metadata:
108
+ homepage_uri: https://github.com/taeyang91/dns_record_updater
109
+ source_code_uri: https://github.com/taeyang91/dns_record_updater
110
+ changelog_uri: https://github.com/taeyang91/dns_record_updater/CHANGELOG.md
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: 3.0.0
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubygems_version: 3.3.7
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: A gem for updating DNS records on multiple platforms.
130
+ test_files: []