Class: Arachni::UI::Web::DispatcherManager

Inherits:
Object
  • Object
show all
Defined in:
lib/arachni/ui/web/dispatcher_manager.rb

Overview

Provides methods for dispatcher management.

@author: Tasos “Zapotek” Laskos

<[email protected]>
<[email protected]>

@version: 0.1.1

Defined Under Namespace

Classes: Dispatcher

Instance Method Summary collapse

Constructor Details

#initialize(opts, settings) ⇒ DispatcherManager

Returns a new instance of DispatcherManager.



38
39
40
41
42
43
44
45
46
# File 'lib/arachni/ui/web/dispatcher_manager.rb', line 38

def initialize( opts, settings )
    @opts     = opts
    @settings = settings

    DataMapper::setup( :default, "sqlite3://#{@settings.db}/default.db" )
    DataMapper.finalize

    Dispatcher.auto_upgrade!
end

Instance Method Details

#alive?(url, &block) ⇒ Boolean

Checks wether the dispatcher is alive.

Parameters:

  • url (String)

    URL of the dispatcher

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
# File 'lib/arachni/ui/web/dispatcher_manager.rb', line 83

def alive?( url, &block )
    raise( "This method requires a block!" ) if !block_given?

    connect( url ).alive? {
        |ret|
        block.call( ret.rpc_connection_error? ? false : true )
    }
end

#all(*args) ⇒ Array

Returns all dispatchers stored in the DB.

Returns:



224
225
226
# File 'lib/arachni/ui/web/dispatcher_manager.rb', line 224

def all( *args )
    Dispatcher.all( *args )
end

#all_with_liveness(&block) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/arachni/ui/web/dispatcher_manager.rb', line 228

def all_with_liveness( &block )
    raise( "This method requires a block!" ) if !block_given?

    EM::Iterator.new( all ).map( proc{
        |dispatcher, iter|

        alive?( dispatcher.url ) {
            |liveness|

            m_dispatcher = {}
            dispatcher.attributes.each_pair {
                |k, v|
                m_dispatcher[k.to_s] = v
            }
            m_dispatcher['alive'] = liveness
            iter.return( m_dispatcher )
        }
    }, proc{
        |dispatchers|
        block.call( dispatchers )
    })
end

#connect(url) ⇒ Arachni::RPC::Client::Dispatcher

Provides an easy way to connect to a dispatcher.

Parameters:

Returns:



74
75
76
# File 'lib/arachni/ui/web/dispatcher_manager.rb', line 74

def connect( url )
    Arachni::RPC::Client::Dispatcher.new( @opts, url )
end

#delete(id) ⇒ Object

Removed a dispatcher from the DB.

Parameters:

  • id (Integer)


267
268
269
# File 'lib/arachni/ui/web/dispatcher_manager.rb', line 267

def delete( id )
    Dispatcher.get( id ).destroy
end

#delete_allObject

Removed all dispatchers from the DB.



254
255
256
257
258
259
260
# File 'lib/arachni/ui/web/dispatcher_manager.rb', line 254

def delete_all
    all.each {
        |report|
        delete( report.id )
    }
    all.destroy
end

#first_alive(&block) ⇒ Object



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
# File 'lib/arachni/ui/web/dispatcher_manager.rb', line 92

def first_alive( &block )
    raise( "This method requires a block!" ) if !block_given?

    if !all.empty?
        EM.synchrony do
            dispatchers = EM::Synchrony::Iterator.new( all ).map {
                |dispatcher, iter|
                alive?( dispatcher.url ){
                    |bool|
                    if bool
                        iter.return( dispatcher )
                    else
                        iter.return( nil )
                    end
                }
            }.compact

            if dispatchers.empty?
                block.call( false )
            else
                block.call( dispatchers.pop )
            end
        end
    else
        block.call( false )
    end
end

#jobs(&block) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/arachni/ui/web/dispatcher_manager.rb', line 120

def jobs( &block )
    ::EM::Iterator.new( all, 20 ).map( proc {
        |dispatcher, iter|

        connect( dispatcher.url ).stats {
            |stats|
            iter.return( stats['running_jobs'] ) if !stats.rpc_connection_error?
        }

    }, proc {
        |running|
        block.call( running.flatten )
    })
end

#new(url, neighbours = true) ⇒ Object

Puts a new dispatcher (and it’s neighbours) in the DB.

Parameters:

  • url (String)

    URL of the dispatcher

  • neighbours (Bool) (defaults to: true)

    add its neighbouring dispatchers too?



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/arachni/ui/web/dispatcher_manager.rb', line 54

def new( url, neighbours = true )
    Dispatcher.first_or_create( :url => url )

    return if !neighbours
    connect( url ).node.neighbours {
        |neighbours|
        neighbours.each {
            |node|
            Dispatcher.first_or_create( :url => node )
        }
    }
end

#stats(&block) ⇒ Hash

Provides statistics about running jobs etc using the dispatcher

Returns:

  • (Hash)


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/arachni/ui/web/dispatcher_manager.rb', line 140

def stats( &block )
    raise( "This method requires a block!" ) if !block_given?

    EM.synchrony do

        stats = EM::Synchrony::Iterator.new( all ).map {
            |dispatcher, iter|

            if !dispatcher.rpc_connection_error?
                connect( dispatcher.url ).stats {
                    |stats|
                    if !stats.rpc_exception?

                        # automatically grab and save neighbours
                        stats['neighbours'].each { |n| new( n ) }

                        iter.return( { dispatcher.url => stats } )
                    else
                        iter.return( nil )
                    end
                }
            end
        }.compact

        sorted_stats = {}
        stats.sort{ |a, b| a.keys[0] <=> b.keys[0] }.each {
            |stat|
            sorted_stats.merge!( stat )
        }

        sorted_stats.each_pair {
            |k, stats|

            sorted_stats[k]['running_jobs'] =
                EM::Synchrony::Iterator.new( stats['running_jobs'] ).map {
                |instance, iter|

                if instance['helpers']['rank'] != 'slave'
                    @settings.instances.connect( instance['url'] ).framework.progress_data(
                        :slaves   => false,
                        :messages => false,
                        :issues   => false
                    ) {
                        |prog_data|
                        if prog_data.rpc_exception?
                            iter.return( nil )
                        else
                            instance.merge!( prog_data['stats'] )
                            instance['status']  = prog_data['status'].capitalize!
                            iter.return( instance )
                        end
                    }
                else
                    @settings.instances.connect( instance['helpers']['master'] ).framework.progress_data(
                        :messages => false,
                        :issues   => false
                    ) {
                        |prog_data|
                        if prog_data.rpc_exception?
                            iter.return( nil )
                        else
                            prog_data['instances'].each {
                                |insdat|
                                 if insdat['url'] == instance['url']
                                     instance.merge!( insdat )
                                     instance['status'].capitalize!
                                     iter.return( instance )
                                end
                            }
                        end
                    }
                end
            }.compact
        }

        block.call( sorted_stats )
    end
end