ractor_dns 0.1.0

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: 3ce870d0f62325886ce1055117b49e081cbd7b5f74ba1d4aa9894e5b16c7f8ad
4
+ data.tar.gz: e8d04590744cf6458bd5d395457930e38916590568a2f57e261ad20fe47f829a
5
+ SHA512:
6
+ metadata.gz: 4a5ce6ba4208da2b999f8087f4748eb9090451618f192c726c77f90287c83ac387db2618146e01626040306084f76b1492a2ec5271b79fc46e31f4390e5cf808
7
+ data.tar.gz: a2caa666683138cb78fb239e856e12383a5ee651fb7fe0e5d86ddb7d042b4f9cdee96117974971c1b1c4f12c828d2ba969e5388fe1a124f968bdcb5ec6eeef1a
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 3.0
data/LICENSE.md ADDED
@@ -0,0 +1,157 @@
1
+ # GNU LESSER GENERAL PUBLIC LICENSE
2
+
3
+ Version 3, 29 June 2007
4
+
5
+ Copyright (C) 2007 Free Software Foundation, Inc.
6
+ <https://fsf.org/>
7
+
8
+ Everyone is permitted to copy and distribute verbatim copies of this
9
+ license document, but changing it is not allowed.
10
+
11
+ This version of the GNU Lesser General Public License incorporates the
12
+ terms and conditions of version 3 of the GNU General Public License,
13
+ supplemented by the additional permissions listed below.
14
+
15
+ ## 0. Additional Definitions.
16
+
17
+ As used herein, "this License" refers to version 3 of the GNU Lesser
18
+ General Public License, and the "GNU GPL" refers to version 3 of the
19
+ GNU General Public License.
20
+
21
+ "The Library" refers to a covered work governed by this License, other
22
+ than an Application or a Combined Work as defined below.
23
+
24
+ An "Application" is any work that makes use of an interface provided
25
+ by the Library, but which is not otherwise based on the Library.
26
+ Defining a subclass of a class defined by the Library is deemed a mode
27
+ of using an interface provided by the Library.
28
+
29
+ A "Combined Work" is a work produced by combining or linking an
30
+ Application with the Library. The particular version of the Library
31
+ with which the Combined Work was made is also called the "Linked
32
+ Version".
33
+
34
+ The "Minimal Corresponding Source" for a Combined Work means the
35
+ Corresponding Source for the Combined Work, excluding any source code
36
+ for portions of the Combined Work that, considered in isolation, are
37
+ based on the Application, and not on the Linked Version.
38
+
39
+ The "Corresponding Application Code" for a Combined Work means the
40
+ object code and/or source code for the Application, including any data
41
+ and utility programs needed for reproducing the Combined Work from the
42
+ Application, but excluding the System Libraries of the Combined Work.
43
+
44
+ ## 1. Exception to Section 3 of the GNU GPL.
45
+
46
+ You may convey a covered work under sections 3 and 4 of this License
47
+ without being bound by section 3 of the GNU GPL.
48
+
49
+ ## 2. Conveying Modified Versions.
50
+
51
+ If you modify a copy of the Library, and, in your modifications, a
52
+ facility refers to a function or data to be supplied by an Application
53
+ that uses the facility (other than as an argument passed when the
54
+ facility is invoked), then you may convey a copy of the modified
55
+ version:
56
+
57
+ - a) under this License, provided that you make a good faith effort
58
+ to ensure that, in the event an Application does not supply the
59
+ function or data, the facility still operates, and performs
60
+ whatever part of its purpose remains meaningful, or
61
+ - b) under the GNU GPL, with none of the additional permissions of
62
+ this License applicable to that copy.
63
+
64
+ ## 3. Object Code Incorporating Material from Library Header Files.
65
+
66
+ The object code form of an Application may incorporate material from a
67
+ header file that is part of the Library. You may convey such object
68
+ code under terms of your choice, provided that, if the incorporated
69
+ material is not limited to numerical parameters, data structure
70
+ layouts and accessors, or small macros, inline functions and templates
71
+ (ten or fewer lines in length), you do both of the following:
72
+
73
+ - a) Give prominent notice with each copy of the object code that
74
+ the Library is used in it and that the Library and its use are
75
+ covered by this License.
76
+ - b) Accompany the object code with a copy of the GNU GPL and this
77
+ license document.
78
+
79
+ ## 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that, taken
82
+ together, effectively do not restrict modification of the portions of
83
+ the Library contained in the Combined Work and reverse engineering for
84
+ debugging such modifications, if you also do each of the following:
85
+
86
+ - a) Give prominent notice with each copy of the Combined Work that
87
+ the Library is used in it and that the Library and its use are
88
+ covered by this License.
89
+ - b) Accompany the Combined Work with a copy of the GNU GPL and this
90
+ license document.
91
+ - c) For a Combined Work that displays copyright notices during
92
+ execution, include the copyright notice for the Library among
93
+ these notices, as well as a reference directing the user to the
94
+ copies of the GNU GPL and this license document.
95
+ - d) Do one of the following:
96
+ - 0) Convey the Minimal Corresponding Source under the terms of
97
+ this License, and the Corresponding Application Code in a form
98
+ suitable for, and under terms that permit, the user to
99
+ recombine or relink the Application with a modified version of
100
+ the Linked Version to produce a modified Combined Work, in the
101
+ manner specified by section 6 of the GNU GPL for conveying
102
+ Corresponding Source.
103
+ - 1) Use a suitable shared library mechanism for linking with
104
+ the Library. A suitable mechanism is one that (a) uses at run
105
+ time a copy of the Library already present on the user's
106
+ computer system, and (b) will operate properly with a modified
107
+ version of the Library that is interface-compatible with the
108
+ Linked Version.
109
+ - e) Provide Installation Information, but only if you would
110
+ otherwise be required to provide such information under section 6
111
+ of the GNU GPL, and only to the extent that such information is
112
+ necessary to install and execute a modified version of the
113
+ Combined Work produced by recombining or relinking the Application
114
+ with a modified version of the Linked Version. (If you use option
115
+ 4d0, the Installation Information must accompany the Minimal
116
+ Corresponding Source and Corresponding Application Code. If you
117
+ use option 4d1, you must provide the Installation Information in
118
+ the manner specified by section 6 of the GNU GPL for conveying
119
+ Corresponding Source.)
120
+
121
+ ## 5. Combined Libraries.
122
+
123
+ You may place library facilities that are a work based on the Library
124
+ side by side in a single library together with other library
125
+ facilities that are not Applications and are not covered by this
126
+ License, and convey such a combined library under terms of your
127
+ choice, if you do both of the following:
128
+
129
+ - a) Accompany the combined library with a copy of the same work
130
+ based on the Library, uncombined with any other library
131
+ facilities, conveyed under the terms of this License.
132
+ - b) Give prominent notice with the combined library that part of it
133
+ is a work based on the Library, and explaining where to find the
134
+ accompanying uncombined form of the same work.
135
+
136
+ ## 6. Revised Versions of the GNU Lesser General Public License.
137
+
138
+ The Free Software Foundation may publish revised and/or new versions
139
+ of the GNU Lesser General Public License from time to time. Such new
140
+ versions will be similar in spirit to the present version, but may
141
+ differ in detail to address new problems or concerns.
142
+
143
+ Each version is given a distinguishing version number. If the Library
144
+ as you received it specifies that a certain numbered version of the
145
+ GNU Lesser General Public License "or any later version" applies to
146
+ it, you have the option of following the terms and conditions either
147
+ of that published version or of any later version published by the
148
+ Free Software Foundation. If the Library as you received it does not
149
+ specify a version number of the GNU Lesser General Public License, you
150
+ may choose any version of the GNU Lesser General Public License ever
151
+ published by the Free Software Foundation.
152
+
153
+ If the Library as you received it specifies that a proxy can decide
154
+ whether future versions of the GNU Lesser General Public License shall
155
+ apply, that proxy's public statement of acceptance of any version is
156
+ permanent authorization for you to choose that version for the
157
+ Library.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # RactorDNS
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ractor_dns`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ 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).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ractor_dns.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "standard/rake"
5
+
6
+ task default: :standard
@@ -0,0 +1,11 @@
1
+ module RactorDNS
2
+ class Inflector < Zeitwerk::Inflector
3
+ def camelize(basename, abspath)
4
+ if basename =~ /\Aractor_dns(.*)/
5
+ "RactorDNS" + super($1, abspath)
6
+ else
7
+ super
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,68 @@
1
+ module RactorDNS
2
+ class Server
3
+ attr_reader :authority, :ractor, :zones
4
+
5
+ def initialize(authority:, cpu_count:, zones:)
6
+ @authority = authority
7
+ @cpu_count = cpu_count
8
+ @zones = zones
9
+ end
10
+
11
+ def start
12
+ @ractor = Ractor.new(@authority, @cpu_count, @zones) do |authority, cpu_count, zones|
13
+ pipe = Ractor.new do
14
+ loop do
15
+ Ractor.yield(Ractor.recv)
16
+ end
17
+ end
18
+
19
+ # Create socket outside of Ractor context
20
+ server_socket = UDPSocket.new :INET6
21
+ server_socket.bind("::", 8080)
22
+
23
+ workers = cpu_count.times.map do
24
+ Ractor.new(pipe, server_socket, authority) do |pipe, server_socket, authority|
25
+ loop do
26
+ message, client, zone_h = pipe.take
27
+
28
+ decoded = RRs::Message.decode(message)
29
+
30
+ # Extract client information
31
+ addr_info = Addrinfo.new(client)
32
+
33
+ response = RRs::Message.new(decoded.id)
34
+ response.add_authority(*authority)
35
+ response.instance_exec(decoded) do |decoded|
36
+ @question.concat(decoded.question)
37
+ end
38
+ decoded.question.each do |q|
39
+ (zone_h.search(q[0]) || []).filter { |el| el[:rr].class == q[1]; el[:name] == q[0].to_s }&.each do |ans|
40
+ response.add_answer(ans[:name], ans[:ttl], ans[:rr])
41
+ end
42
+ end
43
+
44
+ server_socket.send(response.encode, 0, addr_info.ip_address, addr_info.ip_port)
45
+ end
46
+ end
47
+ end
48
+
49
+ listener = Ractor.new(pipe, server_socket, zones) do |pipe, server_socket, zones|
50
+ loop do
51
+ msg, client = server_socket.recvfrom(1024)
52
+ pipe.send([msg, client, zones.freeze])
53
+ end
54
+ end
55
+
56
+ loop do
57
+ Signal.trap('INT') { exit! }
58
+ ractor, _result = Ractor.select(listener, *workers, pipe)
59
+ ractor.take
60
+ end
61
+ end
62
+ end
63
+
64
+ def stop
65
+ @ractor.take
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,27 @@
1
+ require "active_support/core_ext/hash/deep_transform_values"
2
+ module RactorDNS
3
+ class Transaction
4
+ def initialize(&block)
5
+ # exploiting a bug in ruby for fun and profit (https://bugs.ruby-lang.org/issues/19374)
6
+ @block = Ractor.make_shareable(block.curry)
7
+ @original_state = nil
8
+ end
9
+
10
+ def apply(obj)
11
+ begin
12
+ @original_state = obj.dup
13
+ @new_obj = obj.deep_transform_values { |value| value = value.dup }
14
+ @block.call(@new_obj)
15
+ obj.replace(@new_obj)
16
+ rescue => e
17
+ puts e
18
+ rollback(obj)
19
+ raise e
20
+ end
21
+ end
22
+
23
+ def rollback(obj)
24
+ obj.replace(@original_state) if @original_state
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,84 @@
1
+ module RactorDNS
2
+ class ZoneCollection
3
+ def initialize(obj = {})
4
+ @queue = Ractor.new(Ractor.make_shareable(obj.freeze)) do |obj|
5
+ zones = obj.dup
6
+ loop do
7
+ msg, data = Ractor.receive
8
+
9
+ if msg == :transact
10
+ transaction = data.dup
11
+ begin
12
+ transaction.apply(zones)
13
+ Ractor.yield zones.to_h
14
+ rescue => e
15
+ puts e
16
+ transaction.rollback(zones)
17
+ raise e
18
+ end
19
+ elsif msg == :get
20
+ Ractor.yield zones[data]
21
+ elsif msg == :search
22
+ Ractor.yield ZoneCollection.search(zones, data)
23
+ elsif msg == :all
24
+ Ractor.yield zones
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ def search(name)
31
+ @queue.send([:search, name])
32
+ @queue.take
33
+ end
34
+
35
+ def self.search(obj, name)
36
+ if name.nil?
37
+ return nil
38
+ end
39
+ if name.is_a? RRs::Name
40
+ name = name.to_a
41
+ end
42
+ obj.dig(name.join(".")).presence || ZoneCollection.search(obj, name.drop(1))
43
+ end
44
+
45
+ def [](k)
46
+ @queue.send([:get, k])
47
+ @queue.take
48
+ end
49
+
50
+ def to_h
51
+ @queue.send([:all, nil])
52
+ @queue.take
53
+ end
54
+
55
+ def transact(transaction)
56
+ @queue.send([:transact, Ractor.make_shareable(transaction)])
57
+ @queue.take
58
+ end
59
+
60
+ def freeze
61
+ FrozenZoneCollection.new(self.to_h)
62
+ end
63
+ end
64
+
65
+ class FrozenZoneCollection
66
+ def initialize(obj)
67
+ @obj = Ractor.make_shareable(obj.freeze)
68
+ end
69
+
70
+ def [](k)
71
+ @obj[k]
72
+ end
73
+
74
+ def search(name)
75
+ if name.nil?
76
+ return nil
77
+ end
78
+ if name.is_a? RRs::Name
79
+ name = name.to_a
80
+ end
81
+ @obj.dig(name.join(".")).presence || search(name.drop(1))
82
+ end
83
+ end
84
+ end
data/lib/ractor_dns.rb ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+ require "zeitwerk"
3
+ require_relative "ractor_dns/inflector"
4
+ require 'socket'
5
+ require "active_support/all"
6
+ require "resolv"
7
+ require "json"
8
+ require "rrs"
9
+
10
+ loader = Zeitwerk::Loader.for_gem
11
+ loader.inflector = RactorDNS::Inflector.new
12
+ loader.setup
13
+
14
+ module RactorDNS
15
+ VERSION = "0.1.0"
16
+ class Error < StandardError; end
17
+ end
18
+
19
+ loader.eager_load
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ractor_dns
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - reesericci
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-08-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '7.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '7.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rrs
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A concurrent DNS server built on Ractors with a simple and expressive
42
+ API to integrate into your projects.
43
+ email:
44
+ - me@reeseric.ci
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".standard.yml"
50
+ - LICENSE.md
51
+ - README.md
52
+ - Rakefile
53
+ - lib/ractor_dns.rb
54
+ - lib/ractor_dns/inflector.rb
55
+ - lib/ractor_dns/server.rb
56
+ - lib/ractor_dns/transaction.rb
57
+ - lib/ractor_dns/zone_collection.rb
58
+ homepage: https://codeberg.org/oblong/ractor_dns
59
+ licenses: []
60
+ metadata:
61
+ homepage_uri: https://codeberg.org/oblong/ractor_dns
62
+ source_code_uri: https://codeberg.org/oblong/ractor_dns
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 3.0.0
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubygems_version: 3.5.17
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: A concurrent DNS server with a simple and expressive API.
82
+ test_files: []