解析 Ruby 中的 CAA DNS 记录

Resolving a CAA DNS record in Ruby

我正在尝试使用 Ruby 库查找域的 CAA 记录。我期待它看起来像这样:

Resolv::DNS.open do |dns|
  ress = dns.getresources "ruby-lang.org", Resolv::DNS::Resource::CAA
  p ress.map(&:inspect)
end

但 CAA 记录类型未定义 Resolv::DNS::Resource。有谁知道如何查询CAA记录吗?

There exists a gem 具有提供此功能的 MIT 许可证:

class Resolv::DNS::Resource::IN::CAA < Resolv::DNS::Resource
  TypeValue = 257
  ClassValue = IN::ClassValue
  ClassHash[[TypeValue, ClassValue]] = self

  def initialize(flags, tag, value)
    # https://tools.ietf.org/html/rfc8659#section-4.1
    #    +0-1-2-3-4-5-6-7-|0-1-2-3-4-5-6-7-|
    #    | Flags          | Tag Length = n |
    #    +----------------|----------------+...+---------------+
    #    | Tag char 0     | Tag char 1     |...| Tag char n-1  |
    #    +----------------|----------------+...+---------------+
    #    +----------------|----------------+.....+----------------+
    #    | Value byte 0   | Value byte 1   |.....| Value byte m-1 |
    #    +----------------|----------------+.....+----------------+
    @flags = flags
    @tag = tag
    @value = value
  end

  ##
  # Critical Flag

  attr_reader :flags

  ##
  # Property identifier

  attr_reader :tag

  ##
  # A sequence of octets representing the Property Value

  attr_reader :value

  def encode_rdata(msg)
    msg.put_bytes(@flags)
    msg.put_string(@tag)
    msg.put_bytes(@value)
  end

  def self.decode_rdata(msg)
    flags = msg.get_bytes(1)
    tag = msg.get_string
    value = msg.get_bytes
    new(flags, tag, value)
  end
end

结果:

 Resolv::DNS.open do |dns|
 dns.getresources("google.com", Resolv::DNS::Resource::IN::CAA)
end
# => [#<Resolv::DNS::Resource::IN::CAA:0x00005580b4806738 
#      @flags="\x00", @tag="issue", @ttl=86400, @value="pki.goog">]