|
#--
|
|
# 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/>.
|
|
#++
|
|
|
|
require 'cyborghood/cyborg'
|
|
require 'cyborghood/cyborg/botnet/interface'
|
|
require 'cyborghood/cyborg/botnet/conversation'
|
|
require 'cyborghood/cyborg/botnet/dsl'
|
|
require 'set'
|
|
|
|
module CyborgHood
|
|
module BotNet
|
|
CORE_API_VERSION = "1.0~"
|
|
|
|
def self.included(base)
|
|
case Config.instance.botnet.connection_type
|
|
when 'unix_socket'
|
|
return base.class_eval("include BotNetUNIXSocket")
|
|
else
|
|
raise CyberError.new(:unrecoverable, "config", "Unknown botnet connection type")
|
|
end
|
|
end
|
|
|
|
def setup
|
|
super
|
|
|
|
@comm_list = {}
|
|
@comm_list_attempt = {}
|
|
|
|
# default empty interface
|
|
define_interface("0") {}
|
|
end
|
|
|
|
def define_interface(version, &block)
|
|
@interface_creation = Proc.new do
|
|
interface = DSL::ServerApiNode.new(self, &block)
|
|
# additional behavior to comply with the Cyborg API
|
|
interface.add_behavior do
|
|
node "_cyborg_" do
|
|
on_request do |request|
|
|
request.reply.results = {
|
|
:name => bot.name,
|
|
:product_name => PRODUCT,
|
|
:product_version => VERSION,
|
|
:core_api_version => CORE_API_VERSION,
|
|
:cyborg_api_version => version
|
|
}
|
|
request.send_reply
|
|
end
|
|
end
|
|
end
|
|
interface
|
|
end # @interface_creation
|
|
end
|
|
|
|
def interface
|
|
# recreate dynamic interface each time, to ensure concurrent calls do not clash
|
|
@interface_creation.call
|
|
end
|
|
|
|
def contact_peer(peer, block, dont_open_new_connection = false)
|
|
if @comm_list.has_key? peer
|
|
if @comm_list[peer].usuable?
|
|
logger.debug "Botnet: Reusing connection for peer #{peer}"
|
|
return block.call @comm_list[peer]
|
|
end
|
|
end
|
|
|
|
return if dont_open_new_connection
|
|
|
|
logger.debug "Botnet: Trying to open a new connection to peer #{peer}"
|
|
|
|
if @comm_list_attempt.has_key? peer
|
|
@comm_list_attempt[peer] << block
|
|
else
|
|
@comm_list_attempt[peer] = [block]
|
|
|
|
# demultiplex callbacks
|
|
callback = proc do |conversation|
|
|
block_list = @comm_list_attempt[peer]
|
|
# purge list at once to avoid races
|
|
@comm_list_attempt.delete(peer)
|
|
|
|
block_list.each do |block|
|
|
block.call conversation
|
|
end
|
|
end
|
|
begin
|
|
yield(callback)
|
|
rescue
|
|
logger.debug "Botnet: connection attempt to peer #{peer} failed: " + $!
|
|
# TODO: retry (wait_timer + recursive call + counter)
|
|
block.call false
|
|
end
|
|
end
|
|
end
|
|
|
|
# used to quit properly and later to reuse communication channels
|
|
def register_communication(peer, conversation)
|
|
logger.debug "Botnet: conversation with '#{peer}' ready"
|
|
@comm_list[peer] = conversation
|
|
end
|
|
|
|
def unregister_communication(peer)
|
|
logger.debug "Botnet: conversation with '#{peer}' closed"
|
|
@comm_list.delete(peer)
|
|
@pending_conversation_close.delete(peer)
|
|
end
|
|
|
|
def stop(condition)
|
|
super do
|
|
@comm_list.each_value{|conv| conv.stop(condition) }
|
|
end
|
|
end
|
|
|
|
protected
|
|
|
|
def process_system_notification(msg)
|
|
if msg[:topic] == 'CONVERSATION IDLE'
|
|
peer = @comm_list[msg[:peer]]
|
|
return if peer.nil?
|
|
|
|
peer.bye
|
|
else
|
|
super
|
|
end
|
|
end
|
|
|
|
def ready_to_stop?
|
|
@comm_list.empty? and super
|
|
end
|
|
end
|
|
|
|
autoload :BotNetUNIXSocket, 'cyborghood/cyborg/botnet/backend/unix'
|
|
end
|