Skip to content

Commit

Permalink
feat: add discover command
Browse files Browse the repository at this point in the history
  • Loading branch information
ninoseki committed Sep 7, 2018
1 parent 357a21e commit 445b444
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
37 changes: 31 additions & 6 deletions lib/ryo/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,62 @@ module Ryo
class CLI < Thor
desc "dir URL", "Discover directories and files belong to a given URL"
def dir(url)
hash = discover(url, dir: true)
hash = run_discovery(url, dir: true)
puts hash.to_json
end

desc "subdomain URL", "Discover subdomains of a given URL"
def subdomain(url)
hash = discover(url, subdomain: true)
hash = run_discovery(url, subdomain: true)
puts hash.to_json
end

desc "tech URL", "Discover used technolgies of a given URL"
def tech(url)
hash = discover(url, tech: true)
hash = run_discovery(url, tech: true)
puts hash.to_json
end

desc "whois URL", "Discover whois information of a given URL"
def whois(url)
hash = discover(url, whois: true)
hash = run_discovery(url, whois: true)
puts hash.to_json
end

desc "discover URL", "Run discovery plugin(s) against a given URL"
method_option :dir, type: :boolean, default: false
method_option :subdomain, type: :boolean, default: false
method_option :tech, type: :boolean, default: false
method_option :whois, type: :boolean, default: false
def discovery(url)
hash = run_discovery(url, options)
puts hash.to_json
end

desc "all URL", "Run all discovery plugins against a given URL"
def all(url)
hash = discover(url, all: true)
hash = run_discovery(url, all: true)
puts hash.to_json
end

no_commands do
def discover(url, options)
def validate_url(url)
uri = URI.parse(url)
raise InvalidURLError unless %w(http https).include?(uri.scheme)
end

def validate_options(options)
raise InvalidOptionsError unless options.any? { |_, v| v }
end

def run_discovery(url, options)
validate_url(url)
validate_options(options)
Ryo.discover url, options
rescue InvalidURLError => _
{ error: "Please input a valid URL" }
rescue InvalidOptionsError => _
{ error: "Please input at least one option" }
rescue StandardError => e
{ error: "Warning: #{e}" }
end
Expand Down
6 changes: 5 additions & 1 deletion lib/ryo/error.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# frozen_string_literal: true

module Ryo
class NotFoundError < StandardError; end
end
class InvalidURLError < StandardError; end
class InvalidOptionsError < StandardError; end
end
19 changes: 19 additions & 0 deletions spec/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@
expect(json).to be_a(Hash)
end
end
describe "#discover" do
context "when given an empty options" do
it "should return a JSON with an error message" do
output = capture(:stdout) { subject.start %w(discover http://localhost) }
json = JSON.parse(output)
expect(json["error"]).to eq("Please input at least one option")
end
end
context "when given valid options" do
before {
allow(Ryo::Plugin::Whois).to receive(:discover).and_return({})
}
it "should return a JSON" do
output = capture(:stdout) { subject.start %w(discover http://localhost --whois) }
json = JSON.parse(output)
expect(json).to be_a(Hash)
end
end
end
describe "#all" do
before {
allow(Ryo::Plugin::Dir).to receive(:discover).and_return({})
Expand Down

0 comments on commit 445b444

Please sign in to comment.