Module: Unobtainium::Support::PortScanner Private

Included in:
Drivers::Phantom
Defined in:
lib/unobtainium/support/port_scanner.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

A port scanner for finding a free port for running e.g. a selenium or appium server.

Constant Summary collapse

MAX_RETRIES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Retry a port this many times before failing

5
RETRY_DELAY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Delay each retry by this many seconds before trying again

0.5

Instance Method Summary collapse

Instance Method Details

#port_open?(host, port, domains = [:INET, :INET6]) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true if the port is open on the host, false otherwise.

Parameters:

  • host (String)

    host name or IP address

  • port (Integer)

    port number (1..65535)

  • domains (Array/Symbol) (defaults to: [:INET, :INET6])

    :INET, :INET6, etc. or an Array of these. Any from Socket::Constants::AF_* work. Defaults to [:INET, :INET6].

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/unobtainium/support/port_scanner.rb', line 46

def port_open?(host, port, domains = [:INET, :INET6])
  if port < 1 or port > 65535
    raise ArgumentError, "Port must be in range 1..65535!"
  end

  test_domains = nil
  if domains.is_a? Array
    test_domains = domains.dup
  else
    test_domains = [domains]
  end

  test_domains.each do |domain|
    if not DOMAINS.include?(domain)
      raise ArgumentError, "Domains must be one of #{DOMAINS}, or an Array "\
        "of them, but #{domain} isn't!"
    end
  end

  # Test a socket for each domain
  test_domains.each do |domain|
    addr = get_addr(host, port, domain)
    if addr.nil?
      next
    end

    if test_sockaddr(addr, domain)
      return true
    end
  end

  return false
end

#scan(host, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Scan a mixture of ranges and arrays of ports for a given host. Return those that are open or closed, depending on the options given.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/unobtainium/support/port_scanner.rb', line 84

def scan(host, *args)
  # Argument checks
  if args.empty?
    raise ArgumentError, "Need at least one port to scan!"
  end

  args.each do |item|
    if not item.respond_to?(:each) and not item.respond_to?(:to_i)
      raise ArgumentError, "The argument '#{item}' to #scan is not a "\
        "Range, Array or convertible to Integer, aborting!"
    end
  end

  # If the last argument is a Hash, treat it as options.
  opts = {}
  if args.last.is_a? Hash
    opts = args.pop
  end
  opts = { for: :open, amount: :all }.merge(opts)

  if not [:all, :first].include?(opts[:amount])
    raise ArgumentError, ":amount must be one of :all, :first!"
  end
  if not [:open, :closed, :available].include?(opts[:for])
    raise ArgumentError, ":for must beone of :open, :closed, :available!"
  end

  return run_scan(host, opts, *args)
end