Skip to content

Commit

Permalink
Merge pull request #8 from taketo1113/to_json
Browse files Browse the repository at this point in the history
Support to output with json format
  • Loading branch information
taketo1113 authored Aug 3, 2024
2 parents 054803a + cb4de89 commit 7dc7b4e
Show file tree
Hide file tree
Showing 11 changed files with 446 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/ddig/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ def resolve_all
@ddig = Ddig.lookup(@hostname, nameservers: @options[:nameserver], use_ipv4: @use_ipv4, use_ipv6: @use_ipv6)

if @options[:format] == 'json'
# TODO: to_json
puts @ddig.to_json

else
unless @ddig[:do53][:ipv4].nil?
puts "# Do53 (IPv4)"
Expand Down
14 changes: 14 additions & 0 deletions lib/ddig/ddr.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'resolv'
require 'json'

require_relative "ddr/verify_cert"
require_relative "ddr/designated_resolver"
Expand Down Expand Up @@ -129,6 +130,19 @@ def verify_discovery
end
end

def as_json(*)
{
designated_resolvers: @designated_resolvers.map { |designated_resolver| designated_resolver.as_json },
hostname: @hostname,
nameservers: @nameservers,
ip: @ip,
}
end

def to_json(*args)
as_json.to_json
end

def to_cli
@designated_resolvers.each do |designated_resolver|
puts designated_resolver.to_s
Expand Down
23 changes: 23 additions & 0 deletions lib/ddig/ddr/designated_resolver.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'json'

module Ddig
class Ddr
class DesignatedResolver
Expand Down Expand Up @@ -61,6 +63,27 @@ def lookup(hostname)
end
end

def as_json(*)
{
unencrypted_resolver: @unencrypted_resolver,
target: @target,
protocol: @protocol,
port: @port,
dohpath: @dohpath,
address: @address,
ip: @ip,
verify: @verify_cert&.verify,
hostname: @hostname,
a: @a,
aaaa: @aaaa,
errors: @errors,
}
end

def to_json(*args)
as_json.to_json
end

def to_cli
@resolver.to_cli
end
Expand Down
15 changes: 15 additions & 0 deletions lib/ddig/resolver/do53.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'resolv'
require 'json'

module Ddig
module Resolver
Expand Down Expand Up @@ -33,6 +34,20 @@ def lookup
self
end

def as_json(*)
{
a: @a,
aaaa: @aaaa,
hostname: @hostname,
nameservers: @nameservers,
ip: @ip,
}
end

def to_json(*args)
as_json.to_json
end

def to_cli
@a.each do |address|
rr_type = 'A'
Expand Down
17 changes: 17 additions & 0 deletions lib/ddig/resolver/doh_h1.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'net/http'
require 'resolv'
require 'base64'
require 'json'

require_relative 'dns_message'

Expand Down Expand Up @@ -57,6 +58,22 @@ def get_resources(hostname, typeclass)
end
end

def as_json(*)
{
a: @a,
aaaa: @aaaa,
hostname: @hostname,
server: @server,
address: @address,
dohpath: @dohpath,
port: @port,
}
end

def to_json(*args)
as_json.to_json
end

def to_cli
@a.each do |address|
rr_type = 'A'
Expand Down
16 changes: 16 additions & 0 deletions lib/ddig/resolver/dot.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'openssl'
require 'resolv'
require 'json'

require_relative 'dns_message'

Expand Down Expand Up @@ -75,6 +76,21 @@ def get_socket
end
end

def as_json(*)
{
a: @a,
aaaa: @aaaa,
hostname: @hostname,
server: @server,
server_name: @server_name,
port: @port,
}
end

def to_json(*args)
as_json.to_json
end

def to_cli
@a.each do |address|
rr_type = 'A'
Expand Down
67 changes: 67 additions & 0 deletions spec/ddig/ddr/designated_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,71 @@
expect(@designated_resolver.verify_cert).to eq nil
end
end

context "#as_json" do
before(:each) do
@designated_resolver = Ddig::Ddr::DesignatedResolver.new(unencrypted_resolver: '8.8.8.8', target: 'dns.google', protocol: 'dot', port: nil, dohpath: nil, address: '8.8.4.4', ip: :ipv4)
@designated_resolver.lookup('dns.google')
end

it "return values" do
expect(@designated_resolver.as_json[:unencrypted_resolver]).to eq "8.8.8.8"
expect(@designated_resolver.as_json[:target]).to eq "dns.google"
expect(@designated_resolver.as_json[:protocol]).to eq "dot"
expect(@designated_resolver.as_json[:port]).to eq 853
expect(@designated_resolver.as_json[:dohpath]).to eq nil
expect(@designated_resolver.as_json[:address]).to eq "8.8.4.4"
expect(@designated_resolver.as_json[:ip]).to eq :ipv4
expect(@designated_resolver.as_json[:verify]).to eq nil
expect(@designated_resolver.as_json[:hostname]).to eq "dns.google"

# a
expect(@designated_resolver.as_json[:a]).to include "8.8.8.8"
expect(@designated_resolver.as_json[:a]).to include "8.8.4.4"

# aaaa
expect(@designated_resolver.as_json[:aaaa]).to include "2001:4860:4860::8844"
expect(@designated_resolver.as_json[:aaaa]).to include "2001:4860:4860::8888"

expect(@designated_resolver.as_json[:errors]).to eq []
end
end

context "#to_json" do
before(:each) do
@designated_resolver = Ddig::Ddr::DesignatedResolver.new(unencrypted_resolver: '8.8.8.8', target: 'dns.google', protocol: 'dot', port: nil, dohpath: nil, address: '8.8.4.4', ip: :ipv4)
@designated_resolver.lookup('dns.google')
end

it "return values" do
# unencrypted_resolver
expect(@designated_resolver.to_json).to include '8.8.8.8'

# target
expect(@designated_resolver.to_json).to include "dns.google"

# protocol
expect(@designated_resolver.to_json).to include "dot"

# port
expect(@designated_resolver.to_json).to include '853'

# address
expect(@designated_resolver.to_json).to include "8.8.4.4"

# ip
expect(@designated_resolver.to_json).to include 'ipv4'

# hostname
expect(@designated_resolver.to_json).to include "dns.google"

# a
expect(@designated_resolver.to_json).to include "8.8.8.8"
expect(@designated_resolver.to_json).to include "8.8.4.4"

# aaaa
expect(@designated_resolver.to_json).to include "2001:4860:4860::8844"
expect(@designated_resolver.to_json).to include "2001:4860:4860::8888"
end
end
end
50 changes: 47 additions & 3 deletions spec/ddig/ddr_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# frozen_string_literal: true

RSpec.describe Ddig::Ddr do
before(:each) do
end

context "#svcb_records" do
it "return svcb_records with namesevers" do
@ddr = Ddig::Ddr.new(nameservers: ['8.8.8.8'])
Expand Down Expand Up @@ -62,4 +59,51 @@
}.to raise_error(Ddig::Error)
end
end

context "#as_json" do
before(:each) do
@ddr = Ddig::Ddr.new(nameservers: ['8.8.8.8'])
end

it "return values of designated_resolvers" do
@designated_resolvers = @ddr.as_json[:designated_resolvers]
@designated_resolver = @designated_resolvers.first

expect(@designated_resolvers).not_to eq nil
expect(@designated_resolver).not_to eq nil
expect(@designated_resolver[:unencrypted_resolver]).to eq "8.8.8.8"
expect(@designated_resolver[:target]).to eq "dns.google"
expect(@designated_resolver[:protocol]).to eq "dot"
expect(@designated_resolver[:port]).to eq 853
expect(@designated_resolver[:dohpath]).to eq nil
expect(@designated_resolver[:address]).not_to eq nil # "8.8.4.4" or "8.8.8.8"
expect(@designated_resolver[:ip]).to eq :ipv4
expect(@designated_resolver[:verify]).to eq true
expect(@designated_resolver[:hostname]).to eq nil
expect(@designated_resolver[:a]).to eq nil
expect(@designated_resolver[:aaaa]).to eq nil
expect(@designated_resolver[:errors]).to eq nil
end

it "return values" do
expect(@ddr.as_json[:hostname]).to eq nil
expect(@ddr.as_json[:nameservers]).to eq ["8.8.8.8"]
expect(@ddr.as_json[:ip]).to eq nil
end
end

context "#to_json" do
before(:each) do
@ddr = Ddig::Ddr.new(nameservers: ['8.8.8.8'])
end

it "return values" do
expect(@ddr.to_json).to include "8.8.8.8"
expect(@ddr.to_json).to include "dns.google"
expect(@ddr.to_json).to include "dot"
expect(@ddr.to_json).to include '853'
expect(@ddr.to_json).to include 'ipv4'
expect(@ddr.to_json).to include 'true'
end
end
end
67 changes: 67 additions & 0 deletions spec/ddig/resolver/do53_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,71 @@
expect(@do53.ip).to eq :ipv6
end
end

context "#as_json" do
before(:each) do
@do53 = Ddig::Resolver::Do53.new(hostname: 'dns.google')
@do53.lookup
end

it "a/aaaa return values" do
# a
expect(@do53.as_json[:a]).to include "8.8.8.8"
expect(@do53.as_json[:a]).to include "8.8.4.4"

# aaaa
expect(@do53.as_json[:aaaa]).to include "2001:4860:4860::8844"
expect(@do53.as_json[:aaaa]).to include "2001:4860:4860::8888"
end

it "hostname set value" do
expect(@do53.as_json[:hostname]).to eq 'dns.google'
end

it "nameservers set values" do
expect(@do53.as_json[:nameservers]).not_to eq nil
end

it "ip is nil" do
expect(@do53.as_json[:ip]).to eq nil
end
end

context "#to_json" do
before(:each) do
@do53 = Ddig::Resolver::Do53.new(hostname: 'dns.google')
@do53.lookup
end

it "a/aaaa return values" do
# a
expect(@do53.to_json).to include "8.8.8.8"
expect(@do53.to_json).to include "8.8.4.4"

# aaaa
expect(@do53.to_json).to include "2001:4860:4860::8844"
expect(@do53.to_json).to include "2001:4860:4860::8888"
end

it "hostname set value" do
expect(@do53.to_json).to include 'dns.google'
end
end

context "#to_cli" do
before(:each) do
@do53 = Ddig::Resolver::Do53.new(hostname: 'dns.google')
@do53.lookup
end

it "a/aaaa return values" do
# a
expect { @do53.to_cli }.to output(/8.8.8.8/).to_stdout
expect { @do53.to_cli }.to output(/8.8.4.4/).to_stdout

# aaaa
expect { @do53.to_cli }.to output(/2001:4860:4860::8888/).to_stdout
expect { @do53.to_cli }.to output(/2001:4860:4860::8844/).to_stdout
end
end
end
Loading

0 comments on commit 7dc7b4e

Please sign in to comment.