Skip to content

Commit

Permalink
Merge pull request #14 from ninoseki/add-max_concurrency-option
Browse files Browse the repository at this point in the history
feat: add max_concurrency option
  • Loading branch information
ninoseki committed Sep 26, 2020
2 parents 296e69e + 6758ec3 commit c52a6ca
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/rangescan/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class CLI < Thor
method_option :timeout, type: :numeric, desc: "Timeout in seconds"
method_option :user_agent, type: :string, desc: "User Agent"
method_option :verify_ssl, type: :boolean, desc: "Whether to verify SSL or not"
method_option :max_concurrency, type: :numeric, desc: "Concurrency limit for HTTP requests to scan"
def scan(ip_with_subnet_mask, regexp)
symbolized_options = symbolize_hash_keys(options)
range = Range.new(ip_with_subnet_mask)
Expand Down
7 changes: 4 additions & 3 deletions lib/rangescan/scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module RangeScan
class Scanner
attr_reader :context
attr_reader :host
attr_reader :max_concurrency
attr_reader :port
attr_reader :processor_count
attr_reader :scheme
Expand All @@ -20,7 +21,7 @@ class Scanner
attr_reader :user_agent
attr_reader :verify_ssl

def initialize(host: nil, port: nil, scheme: "http", verify_ssl: true, timeout: 5, user_agent: nil)
def initialize(host: nil, port: nil, scheme: "http", verify_ssl: true, timeout: 5, user_agent: nil, max_concurrency: nil)
@host = host
@port = port || (scheme == "http" ? 80 : 443)
@timeout = timeout
Expand All @@ -32,7 +33,7 @@ def initialize(host: nil, port: nil, scheme: "http", verify_ssl: true, timeout:
@ssl_context = OpenSSL::SSL::SSLContext.new
@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE unless verify_ssl

@processor_count ||= Etc.nprocessors
@max_concurrency = max_concurrency || Etc.nprocessors * 2
end

def url_for(ipv4)
Expand All @@ -45,7 +46,7 @@ def scan(ipv4s)
results = []
Async do
barrier = Async::Barrier.new
semaphore = Async::Semaphore.new(processor_count, parent: barrier)
semaphore = Async::Semaphore.new(max_concurrency, parent: barrier)

ipv4s.each do |ipv4|
semaphore.async do
Expand Down
13 changes: 13 additions & 0 deletions spec/range_scan/scanner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "glint"
require "webrick"
require "etc"

HOST = "127.0.0.1"

Expand Down Expand Up @@ -76,6 +77,18 @@ def server
end
end

describe "#max_concurrency" do
it do
scanner = described_class.new(scheme: "http")
expect(scanner.max_concurrency).to eq(Etc.nprocessors * 2)
end

it do
scanner = described_class.new(scheme: "https", max_concurrency: 3)
expect(scanner.max_concurrency).to eq(3)
end
end

describe "#url_for" do
it do
scanner = described_class.new
Expand Down

0 comments on commit c52a6ca

Please sign in to comment.