meshname 1.0.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/meshname.rb +86 -0
  3. metadata +59 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 63aa6e751f1fca620263c8d8b1042d30ec76ec27f32fd0277bcb511d198460b6
4
+ data.tar.gz: ec3648959620b1256de293e00301dc1c32be965e03d074c979e604f10acb7fd6
5
+ SHA512:
6
+ metadata.gz: 38469da161ca43477ee054bf7aa0a029b18b75392817ae8f8a9431511589e7a146a3bdb15f9323abc01ca3ba0a5d4a8c3579f151cc68a3ebdc3070f9d2bda7b0
7
+ data.tar.gz: eaca21e8c6a02a868fa7d5fa2951272e557a6c4629159e510b33ae482fc3b453c7a1ad4f72d98151140e562c2351a430e65cf6e7a79e64f41d77d79f7aa07da5
data/lib/meshname.rb ADDED
@@ -0,0 +1,86 @@
1
+
2
+ require "resolv"
3
+ require "ipaddr"
4
+ require "base32"
5
+
6
+ module Meshname
7
+
8
+ # Calculates a meshname from an IPv6 address (without .meshname)
9
+ #
10
+ # @param ipv6 [IPAddr]
11
+ # @return [String]
12
+ # @example
13
+ # Meshname.getname IPAddr.new("215:15c:84e0:8dd5:7590:bfcd:61cf:cff7") => "aikqcxee4cg5k5mqx7gwdt6p64"
14
+ # @example Create a .nameip domain
15
+ # "#{Meshname.getname IPAddr.new("215:15c:84e0:8dd5:7590:bfcd:61cf:cff7")}.meship" => "aikqcxee4cg5k5mqx7gwdt6p64.meship"
16
+ def self.getname ipv6
17
+ # convert IPv6 to 16 bytes
18
+ ipv6_binary = ipv6.hton
19
+ # encode the 16 bytes (base32)
20
+ meshname = Base32.encode ipv6_binary
21
+ # delete padding and convert up in down letters
22
+ meshname.delete! "="
23
+ meshname.downcase!
24
+
25
+ return meshname
26
+ end
27
+
28
+ # Calculates an IPv6 address from a Meshname (without .meshname)
29
+ # Links are not triggered. The mesh name is treated like a .meship domain.
30
+ #
31
+ # @param meshname [String]
32
+ # @return [IPAddr]
33
+ # @example
34
+ # Meshname.getip "aikqcxee4cg5k5mqx7gwdt6p64" => #<IPAddr: IPv6:0215:015c:84e0:8dd5:7590:bfcd:61cf:cff7/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
35
+ # Meshname.getip("aikqcxee4cg5k5mqx7gwdt6p64").to_s => "215:15c:84e0:8dd5:7590:bfcd:61cf:cff7"
36
+ def self.getip meshname
37
+ # decode the meshname to 16 bytes
38
+ ipv6_binary = Base32.decode meshname.upcase
39
+ # convert the 16 bytes to a ipv6
40
+ ipv6 = IPAddr.new_ntoh ipv6_binary
41
+ return ipv6
42
+ end
43
+
44
+ # Resolves a .meshname or .meship domain. Links are taken into account.
45
+ #
46
+ # An array of IP addresses (type: IPAddr) is always returned.
47
+ # The reason for the array is that a name server can store several AAAA records
48
+ # for a domain name. In order to keep the result the same,
49
+ # an array is always returned with a .meship domain,
50
+ # but this then only contains one entry.
51
+ #
52
+ # @param domain [String]
53
+ # @return [Array] Array of IPAddr
54
+ # @example Resolv a .meship domain
55
+ # Meshname.resolv("aikqcxee4cg5k5mqx7gwdt6p64.meship") => [#<IPAddr: IPv6:0215:015c:84e0:8dd5:7590:bfcd:61cf:cff7/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>]
56
+ # Meshname.resolv("aikqcxee4cg5k5mqx7gwdt6p64.meship")[0].to_s => "215:15c:84e0:8dd5:7590:bfcd:61cf:cff7"
57
+ # @example Resolv a .meshname domain
58
+ # Meshname.resolv("aikqcxee4cg5k5mqx7gwdt6p64.meshname") => [#<IPAddr: IPv6:0215:015c:84e0:8dd5:7590:bfcd:61cf:cff7/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>]
59
+ # Meshname.resolv("aikqcxee4cg5k5mqx7gwdt6p64.meshname")[0].to_s => "215:15c:84e0:8dd5:7590:bfcd:61cf:cff7"
60
+ def self.resolv domain
61
+ parts = domain.split "."
62
+ case parts[-1]
63
+ when "meshname" # case meshname
64
+
65
+ # for decoding meshname remove .meshname tld
66
+ dns_server_meshname = domain.delete_suffix ".#{parts[-1]}"
67
+
68
+ # meshname to ipv6 address
69
+ dns_server = self.getip dns_server_meshname
70
+
71
+ # create DNSResolver for using custom nameserver
72
+ dns_resolver = Resolv::DNS.new :nameserver => [dns_server.to_s]
73
+ # request nameserver for domain (that's why also subdomains possible)
74
+ ip = dns_resolver.getaddresses domain
75
+ # transform Resolv::IPv6 to IPAddr
76
+ ip.map! { |record|
77
+ IPAddr.new_ntoh record.address
78
+ }
79
+ return ip
80
+
81
+ when "meship" # case meship
82
+ return [self.getip(parts[-2])]
83
+ end
84
+ end
85
+
86
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: meshname
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Marek Kuethe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-01-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: base32
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.3.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.3.4
27
+ description: Gem, which provides conversion and DNS resolution functions for the Meshname
28
+ protocol (see https://github.com/zhoreeq/meshname)
29
+ email: m.k@mk16.de
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/meshname.rb
35
+ homepage: https://github.com/marek22k/meshname
36
+ licenses:
37
+ - GPL-3.0-or-later
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.3.4
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Gem, which provides conversion and DNS resolution functions for the Meshname
58
+ protocol.
59
+ test_files: []