Class: Pwnix::Api::Client::ClientManager

Inherits:
Object
  • Object
show all
Includes:
HTTParty, HTTPartyTime
Defined in:
lib/pwnix-api-client/node/client/client_manager.rb

Instance Method Summary collapse

Methods included from HTTPartyTime

#clear_errors, #errors, #has_errors?, included, #last_error, #safe_api_call

Constructor Details

#initializeClientManager

attr_accessor :clients



10
11
12
# File 'lib/pwnix-api-client/node/client/client_manager.rb', line 10

def initialize 
  @clients = []
end

Instance Method Details

#client_exists?(id) ⇒ Boolean

Public: Check if the client exists in the API

API might be down, so lets make sure this returns a true or false othewise it could be an exception string

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/pwnix-api-client/node/client/client_manager.rb', line 23

def client_exists?(id)
  return true if safe_api_call("/node/clients/#{id}/exist") == true 
false
end

#create(type) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pwnix-api-client/node/client/client_manager.rb', line 28

def create(type)
  # Go ahead and try to create the client
  id = safe_api_call("/node/clients/create", :type => type )
  
  # Check to make sure it was successfully created before adding it to our
  # local list of clients
  if client_exists?(id)
    @clients << Pwnix::Api::Client::ConsoleClient.new(id)
    return true
  end

false
end

#destroy_client(id) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/pwnix-api-client/node/client/client_manager.rb', line 46

def destroy_client(id)
  if client_exists?(id)
    destroyable_client = @clients.select{|c| c.id == id }.first
    destroyable_client.disconnect
    destroyable_client.destroy # destroy it remotely
    @clients.delete(destroyable_client)
    return true
  end
false
end

#get_client(id) ⇒ Object



42
43
44
# File 'lib/pwnix-api-client/node/client/client_manager.rb', line 42

def get_client(id)
    @clients.select{|c| c.id == id }.first
end

#listObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pwnix-api-client/node/client/client_manager.rb', line 57

def list
  @clients = []

  # Since the API & UI are disconnected, we need to check to see if there are
  # any other remote clients at this time - we might be out of sync.
  remote_clients = safe_api_call("/node/clients/list")
  if remote_clients
    remote_clients.each do |remote_client_id|
    # Now go through our clients and see if we have an id that matches
    # TODO - these ids should probably be hard to spoof - needs more 
    # thought than i have time for right now.
    unless (@clients.select{|c| c.id == remote_client_id }).count > 0
      @clients << Pwnix::Api::Client::ConsoleClient.new(remote_client_id) 
    end
    end
  end
@clients
end

#statusObject



14
15
16
# File 'lib/pwnix-api-client/node/client/client_manager.rb', line 14

def status
  safe_api_call("/node/clients/status")
end