Class: Arachni::Processes::Instances

Inherits:
Object
  • Object
show all
Includes:
Utilities, Singleton
Defined in:
lib/arachni/processes/instances.rb

Overview

Helper for managing RPC::Server::Instance processes.

Author:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#available_port, #cookie_encode, #cookies_from_document, #cookies_from_file, #cookies_from_response, #exception_jail, #exclude_path?, #extract_domain, #follow_protocol?, #form_decode, #form_encode, #form_parse_request_body, #forms_from_document, #forms_from_response, #generate_token, #get_path, #html_decode, #html_encode, #include_path?, #links_from_document, #links_from_response, #normalize_url, #page_from_response, #page_from_url, #parse_query, #parse_set_cookie, #parse_url_vars, #path_in_domain?, #path_too_deep?, #port_available?, #rand_port, #redundant_path?, #remove_constants, #seed, #skip_page?, #skip_path?, #skip_resource?, #to_absolute, #uri_decode, #uri_encode, #uri_parse, #uri_parser, #url_sanitize

Constructor Details

#initializeInstances

Returns a new instance of Instances.



32
33
34
35
# File 'lib/arachni/processes/instances.rb', line 32

def initialize
    @list = {}
    @instance_connections = {}
end

Instance Attribute Details

#listArray<String> (readonly)

Returns URLs and tokens of all running Instances.

Returns:

  • (Array<String>)

    URLs and tokens of all running Instances.



30
31
32
# File 'lib/arachni/processes/instances.rb', line 30

def list
  @list
end

Class Method Details

.method_missing(sym, *args, &block) ⇒ Object



182
183
184
185
186
187
188
# File 'lib/arachni/processes/instances.rb', line 182

def self.method_missing( sym, *args, &block )
    if instance.respond_to?( sym )
        instance.send( sym, *args, &block )
    elsif
    super( sym, *args, &block )
    end
end

.respond_to?(m) ⇒ Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/arachni/processes/instances.rb', line 190

def self.respond_to?( m )
    super( m ) || instance.respond_to?( m )
end

Instance Method Details

#connect(url, token = nil) ⇒ RPC::Client::Instance

Connects to a Instance by URL.

Parameters:

  • url (String)

    URL of the Dispatcher.

  • token (String) (defaults to: nil)

    Authentication token – only need be provided once.

Returns:



46
47
48
49
50
51
# File 'lib/arachni/processes/instances.rb', line 46

def connect( url, token = nil )
    token ||= @list[url]
    @list[url] ||= token

    @instance_connections[url] ||= RPC::Client::Instance.new( Options.instance, url, token )
end

#dispatcher_spawnRPC::Client::Instance

Starts RPC::Server::Dispatcher and returns an Instance.



160
161
162
163
# File 'lib/arachni/processes/instances.rb', line 160

def dispatcher_spawn
    info = Dispatchers.light_spawn.dispatch
    connect( info['url'], info['token'] )
end

#each(&block) ⇒ Object

Parameters:

  • block (Block)

    Block to pass an RPC client for each Instance.



54
55
56
57
58
# File 'lib/arachni/processes/instances.rb', line 54

def each( &block )
    @list.keys.each do |url|
        block.call connect( url )
    end
end

#grid_spawn(options = {}) ⇒ RPC::Client::Instance

Starts RPC::Server::Dispatcher grid and returns a high-performance Instance.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :grid_size (Integer) — default: 2

    Amount of Dispatchers to spawn.

Returns:



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/arachni/processes/instances.rb', line 136

def grid_spawn( options = {} )
    options[:grid_size] ||= 3

    last_member = nil
    options[:grid_size].times do |i|
        last_member = Dispatchers.light_spawn(
            neighbour: last_member ? last_member.url : last_member,
            pipe_id:   available_port.to_s + available_port.to_s
        )
    end

    info = last_member.dispatch

    instance = connect( info['url'], info['token'] )
    instance.framework.set_as_master
    instance.opts.grid_mode = :aggregate
    instance
end

#killallObject

Kills all #list.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/arachni/processes/instances.rb', line 166

def killall
    pids = []
    each do |instance|
        begin
            pids |= instance.service.consumed_pids
        rescue => e
            #ap e
            #ap e.backtrace
        end
    end

    @list.clear
    @instance_connections.clear
    Manager.kill_many pids
end

#spawn(options = {}, &block) ⇒ RPC::Client::Instance

Spawns an RPC::Server::Instance process.

Parameters:

  • options (Hash) (defaults to: {})

    To be passed to Options#set. Allows ‘address` instead of `rpc_address` and `port` instead of `rpc_port`.

  • block (Block)

    Passed Options to configure the Dispatcher options.

Returns:



81
82
83
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/arachni/processes/instances.rb', line 81

def spawn( options = {}, &block )
    options[:token] ||= generate_token

    options = Options.to_hash.symbolize_keys( false ).merge( options )

    options[:rpc_port]    = options.delete( :port ) if options.include?( :port )
    options[:rpc_address] = options.delete( :address ) if options.include?( :address )
    options[:rpc_socket]  = options.delete( :socket ) if options.include?( :socket )

    options[:rpc_port]    ||= available_port

    url = nil
    if options[:rpc_socket]
        url = options[:rpc_socket]
        options.delete :rpc_address
        options.delete :rpc_port
    else
        url = "#{options[:rpc_address]}:#{options[:rpc_port]}"
    end

    Manager.quiet_fork do
        Options.set( options )
        block.call( Options.instance ) if block_given?

        require "#{Arachni::Options.dir['lib']}/rpc/server/instance"

        RPC::Server::Instance.new( Options.instance, options[:token] )
    end

    begin
        Timeout.timeout( 10 ) do
            while sleep( 0.1 )
                begin
                    connect( url, options[:token] ).service.alive?
                    break
                rescue Exception
                end
            end
        end
    rescue Timeout::Error
        abort "Instance '#{url}' never started!"
    end

    @list[url] = options[:token]
    connect( url )
end

#token_for(client_or_url) ⇒ String

Returns Cached authentication token for the given Instance.

Parameters:

Returns:

  • (String)

    Cached authentication token for the given Instance.



65
66
67
# File 'lib/arachni/processes/instances.rb', line 65

def token_for( client_or_url )
    @list[client_or_url.is_a?( String ) ? client_or_url : client_or_url.url ]
end