|
#--
|
|
# CyborgHood, a distributed system management software.
|
|
# Copyright (c) 2009-2011 Marc Dequènes (Duck) <Duck@DuckCorp.org>
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
#++
|
|
|
|
|
|
module CyborgHood
|
|
module DSL
|
|
module BotnetTask
|
|
def ask(peer, key, cmd, *args)
|
|
action_name = ['ask', cmd, *args].hash
|
|
|
|
_add_subtask_using_peer(action_name, peer) do |subtask, conv_thread|
|
|
conv_thread.call(cmd, *args) do |reply|
|
|
case reply[:status]
|
|
when :ok
|
|
subtask.results = {key => reply[:result]}
|
|
when :decline
|
|
# TODO: remove this case ???
|
|
when :error
|
|
subtask.errors += reply[:exceptions]
|
|
end
|
|
|
|
subtask.finish
|
|
end
|
|
end
|
|
end
|
|
|
|
def know?(peer, key, cmd)
|
|
action_name = ['know', cmd].hash
|
|
|
|
_add_subtask_using_peer(action_name, peer) do |subtask, conv_thread|
|
|
conv_thread.exists?(cmd) do |reply|
|
|
case reply[:status]
|
|
when :ok
|
|
subtask.results = {key => reply[:result]}
|
|
when :decline
|
|
# TODO: remove this case ???
|
|
when :error
|
|
subtask.errors += reply[:exceptions]
|
|
end
|
|
|
|
subtask.finish
|
|
end
|
|
end
|
|
end
|
|
|
|
def _add_subtask_using_peer(action_name, peer)
|
|
subtask_name = "botnet/peer/#{peer}/#{action_name}/out"
|
|
|
|
_add_subtask(subtask_name) do |subtask|
|
|
logger.debug "Task '#{@name}': Trying to contact peer '#{peer}'"
|
|
|
|
# callback to end peer action and subtask
|
|
# (used when shooting the task)
|
|
defuse_peer_action_cb = Proc.new do
|
|
logger.debug "Task '#{@name}': defusing subtask '#{subtask.name}'"
|
|
|
|
subtask.finish unless subtask.finished?
|
|
|
|
# we already own the mutex here
|
|
registered_resources.delete(subtask_name)
|
|
end
|
|
# register peer action in the task
|
|
tasks_info_mutex.synchronize do
|
|
registered_resources[subtask_name] = defuse_peer_action_cb
|
|
end
|
|
|
|
@bot.contact_peer(peer) do |conv|
|
|
if conv
|
|
logger.debug "Task '#{@name}': subtask '#{subtask.name}': peer '#{peer}' contacted, starting conversation"
|
|
|
|
@peer_contacted << peer
|
|
|
|
# don't use the block call to leave the conversation thread open
|
|
conv_thread = conv.thread(@notification_name)
|
|
|
|
yield(subtask, conv_thread)
|
|
else
|
|
logger.debug "Task '#{@name}': Could not contact peer '#{peer}'"
|
|
subtask.errors << CyberError.new(:unrecoverable, "botnet/client/dsl", "Task '#{@name}': could not contact peer '#{peer}'")
|
|
subtask.finish
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
def _setup
|
|
super
|
|
|
|
@peer_contacted = Set.new
|
|
end
|
|
|
|
def _finished
|
|
super
|
|
|
|
# close opened thread
|
|
cb = Proc.new do |conv|
|
|
conv.close_thread(@notification_name)
|
|
end
|
|
|
|
# loop on all contacted peers to close the thread
|
|
@peer_contacted.each do |peer|
|
|
@bot.contact_peer(peer, true, &cb)
|
|
end
|
|
end
|
|
end
|
|
|
|
Task.class_eval("include BotnetTask")
|
|
end
|
|
end
|