Class: SharedTools::Tools::DnsTool

Inherits:
RubyLLM::Tool
  • Object
show all
Defined in:
lib/shared_tools/tools/dns_tool.rb

Overview

DNS lookup tool supporting A, AAAA, MX, TXT, NS, CNAME, reverse, all-records, external IP detection, and WHOIS database queries.

Examples:

tool = SharedTools::Tools::DnsTool.new
tool.execute(action: 'a',           host: 'ruby-lang.org')
tool.execute(action: 'mx',          host: 'gmail.com')
tool.execute(action: 'reverse',     host: '8.8.8.8')
tool.execute(action: 'external_ip')
tool.execute(action: 'whois',       host: 'github.com')
tool.execute(action: 'whois',       host: '8.8.8.8')

Constant Summary collapse

WHOIS_PORT =
43
WHOIS_TIMEOUT =
10
IANA_WHOIS =
'whois.iana.org'
ARIN_WHOIS =
'whois.arin.net'
IP_SERVICES =
%w[
  https://api.ipify.org
  https://ifconfig.me/ip
  https://icanhazip.com
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil) ⇒ DnsTool

Returns a new instance of DnsTool.

Parameters:

  • logger (Logger) (defaults to: nil)

    optional logger



128
129
130
# File 'lib/shared_tools/tools/dns_tool.rb', line 128

def initialize(logger: nil)
  @logger = logger || RubyLLM.logger
end

Class Method Details

.nameObject



23
# File 'lib/shared_tools/tools/dns_tool.rb', line 23

def self.name = 'dns_tool'

Instance Method Details

#execute(action:, host: nil) ⇒ Hash

Returns results.

Parameters:

  • action (String)

    lookup type

  • host (String) (defaults to: nil)

    hostname or IP (not required for external_ip)

Returns:

  • (Hash)

    results



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/shared_tools/tools/dns_tool.rb', line 135

def execute(action:, host: nil)
  @logger.info("DnsTool#execute action=#{action} host=#{host}")

  case action.to_s.downcase
  when 'a'           then lookup_a(host)
  when 'aaaa'        then lookup_aaaa(host)
  when 'mx'          then lookup_mx(host)
  when 'txt'         then lookup_txt(host)
  when 'ns'          then lookup_ns(host)
  when 'cname'       then lookup_cname(host)
  when 'reverse'     then lookup_reverse(host)
  when 'all'         then lookup_all(host)
  when 'external_ip' then lookup_external_ip
  when 'ip_location' then lookup_ip_location(host)
  when 'whois'       then lookup_whois(host)
  else
    { success: false, error: "Unknown action '#{action}'. Use: a, aaaa, mx, txt, ns, cname, reverse, all, external_ip, ip_location, whois" }
  end
rescue => e
  @logger.error("DnsTool error for #{host}: #{e.message}")
  { success: false, host: host, error: e.message }
end