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, **kwargs) ⇒ Object

Calls the instance method to start a service.



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

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

.stop(klass, *args, **kwargs) ⇒ Object

Calls the instance method to stop a service.



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

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

.stop_by_alias(als) ⇒ Object

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



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

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

.stop_service(service) ⇒ Object

Stop the supplied service instance.



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

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"


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

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

#hardcore_alias(klass, *args) ⇒ Object (protected)

Returns the alias for a given service instance.



147
148
149
# File 'lib/rex/service_manager.rb', line 147

def hardcore_alias(klass, *args)
  "__#{klass.name}#{args}"
end

#start(klass, *args, **kwargs) ⇒ Object

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



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

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

  # 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, **kwargs)
  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, **kwargs) ⇒ 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.



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

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

#stop_by_alias(als) ⇒ Object

Stops a service using the provided alias.



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

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

#stop_service(inst) ⇒ Object

Stops a service instance.



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

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