Class: Rex::ServiceManager

Inherits:
Hash
  • Object
show all
Includes:
Singleton
Defined in:
lib/rex/service_manager.rb

Overview

This class manages service allocation and interaction. This class can be used to start HTTP servers and manage them and all that stuff. Yup.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.start(klass, *args) ⇒ Object

Calls the instance method to start a service.


24
25
26
# File 'lib/rex/service_manager.rb', line 24

def self.start(klass, *args)
  self.instance.start(klass, *args)
end

.stop(klass, *args) ⇒ Object

Calls the instance method to stop a service.


31
32
33
# File 'lib/rex/service_manager.rb', line 31

def self.stop(klass, *args)
  self.instance.stop(klass, *args)
end

.stop_by_alias(als) ⇒ Object

Stop a service using the alias that's associated with it.


38
39
40
# File 'lib/rex/service_manager.rb', line 38

def self.stop_by_alias(als)
  self.instance.stop_by_alias(als)
end

.stop_service(service) ⇒ Object

Stop the supplied service instance.


45
46
47
# File 'lib/rex/service_manager.rb', line 45

def self.stop_service(service)
  self.instance.stop_service(service)
end

Instance Method Details

#each(&block) ⇒ Object

Overrides the builtin 'each' operator to avoid the following exception on Ruby 1.9.2+

"can't add a new key into hash during iteration"

135
136
137
138
139
140
141
# File 'lib/rex/service_manager.rb', line 135

def each(&block)
  list = []
  self.keys.sort.each do |sidx|
    list << [sidx, self[sidx]]
  end
  list.each(&block)
end

#start(klass, *args) ⇒ Object

Starts a service and assigns it a unique name in the service hash.


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
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rex/service_manager.rb', line 52

def start(klass, *args)
  # Get the hardcore alias.
  hals = "#{klass}" + klass.hardcore_alias(*args)

  # Has a service already been constructed for this guy?  If so, increment
  # its reference count like it aint no thang.
  if (inst = self[hals])
    inst.ref
    return inst
  end

  inst = klass.new(*args)
  als  = inst.alias

  # Find an alias that isn't taken.
  if (self[als])
    cnt  = 1
    cnt += 1 while (self[als + " #{cnt}"])
    als  = inst.alias + " #{cnt}"
  end

  # Extend the instance as a service.
  inst.extend(Rex::Service)

  # Re-aliases the instance.
  inst.alias = als

  # Fire up the engines.  If an error occurs an exception will be
  # raised.
  inst.start

  # Alias associate and initialize reference counting
  self[als] = self[hals] = inst.refinit

  # Pass the caller a reference
  inst.ref

  inst
end

#stop(klass, *args) ⇒ Object

Stop a service using a given klass and arguments. These should mirror what was originally passed to start exactly. If the reference count of the service drops to zero the service will be destroyed.


97
98
99
# File 'lib/rex/service_manager.rb', line 97

def stop(klass, *args)
  stop_service(hals[hardcore_alias(klass, *args)])
end

#stop_by_alias(als) ⇒ Object

Stops a service using the provided alias.


104
105
106
# File 'lib/rex/service_manager.rb', line 104

def stop_by_alias(als)
  stop_service(self[als])
end

#stop_service(inst) ⇒ Object

Stops a service instance.


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rex/service_manager.rb', line 111

def stop_service(inst)
  # Stop the service and be done wif it, but only if the number of
  # references has dropped to zero
  if (inst)
    # Since the instance may have multiple aliases, scan through
    # all the pairs for matching stuff.
    self.each_pair { |cals, cinst|
      self.delete(cals) if (inst == cinst)
    }

    # Lose the list-held reference to the instance
    inst.deref

    return true
  end

  # Return false if the service isn't there
  return false
end