Changeset - 3932a5c77e9a
[Not reviewed]
0 4 0
HanzZ - 14 years ago 2011-10-27 09:33:28
hanzz.k@gmail.com
Renamed admin_username to admin_jid
4 files changed with 64 insertions and 17 deletions:
0 comments (0 inline, 0 general)
spectrum_manager/src/main.cpp
Show inline comments
 
@@ -17,44 +17,44 @@
 
#include "sys/wait.h"
 
 
 
using namespace Transport;
 
 
using namespace boost::filesystem;
 
 
using namespace boost;
 
 
static int finished;
 
static std::string *m;
 
 
static void handleDisconnected(Swift::Client *client, const boost::optional<Swift::ClientError> &) {
 
	std::cout << "[ DISCONNECTED ] " << client->getJID().getDomain() << "\n";
 
static void handleDisconnected(Swift::Client *client, const boost::optional<Swift::ClientError> &, const std::string &server) {
 
	std::cout << "[ DISCONNECTED ] " << server << "\n";
 
	if (--finished == 0) {
 
		exit(0);
 
	}
 
}
 
 
static void handleConnected(Swift::Client *client) {
 
static void handleConnected(Swift::Client *client, const std::string &server) {
 
	boost::shared_ptr<Swift::Message> message(new Swift::Message());
 
	message->setTo(client->getJID().getDomain());
 
	message->setTo(server);
 
	message->setFrom(client->getJID());
 
	message->setBody(*m);
 
 
	client->sendMessage(message);
 
}
 
 
static void handleMessageReceived(Swift::Client *client, Swift::Message::ref message) {
 
static void handleMessageReceived(Swift::Client *client, Swift::Message::ref message, const std::string &server) {
 
	std::string body = message->getBody();
 
	boost::replace_all(body, "\n", "\n[      OK      ] " + client->getJID().getDomain() + ": ");
 
	std::cout << "[      OK      ] " << client->getJID().getDomain() << ": " << body <<  "\n";
 
	boost::replace_all(body, "\n", "\n[      OK      ] " + server + ": ");
 
	std::cout << "[      OK      ] " << server << ": " << body <<  "\n";
 
	if (--finished == 0) {
 
		exit(0);
 
	}
 
}
 
 
static std::string searchForBinary(const std::string &binary) {
 
	std::vector<std::string> path_list;
 
	char * env_path = getenv("PATH");
 
 
	if (env_path != NULL) {
 
		std::string buffer = "";
 
		for (int s = 0; s < strlen(env_path); s++) {
 
@@ -196,24 +196,65 @@ static void stop_all_instances(ManagerConfig *config) {
 
				else {
 
					std::cout << "Stopping " << itr->path() << ": Not running\n";
 
				}
 
			}
 
		}
 
	}
 
	catch (const filesystem_error& ex) {
 
		std::cerr << "boost filesystem error\n";
 
		exit(5);
 
	}
 
}
 
 
void ask_local_servers(ManagerConfig *config, Swift::BoostNetworkFactories &networkFactories, const std::string &message) {
 
	path p(CONFIG_STRING(config, "service.config_directory"));
 
 
	try {
 
		if (!exists(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(6);
 
		}
 
 
		if (!is_directory(p)) {
 
			std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n";
 
			exit(7);
 
		}
 
 
		directory_iterator end_itr;
 
		for (directory_iterator itr(p); itr != end_itr; ++itr) {
 
			if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") {
 
				Config cfg;
 
				if (cfg.load(itr->path().string()) == false) {
 
					std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n";
 
				}
 
 
				finished++;
 
				Swift::Client *client = new Swift::Client(CONFIG_STRING(&cfg, "service.admin_username"), CONFIG_STRING(&cfg, "service.admin_password"), &networkFactories);
 
				client->setAlwaysTrustCertificates();
 
				client->onConnected.connect(boost::bind(&handleConnected, client, CONFIG_STRING(&cfg, "service.jid")));
 
				client->onDisconnected.connect(bind(&handleDisconnected, client, _1, CONFIG_STRING(&cfg, "service.jid")));
 
				client->onMessageReceived.connect(bind(&handleMessageReceived, client, _1, CONFIG_STRING(&cfg, "service.jid")));
 
				Swift::ClientOptions opt;
 
				opt.allowPLAINWithoutTLS = true;
 
				client->connect(opt);
 
			}
 
		}
 
	}
 
	catch (const filesystem_error& ex) {
 
		std::cerr << "boost filesystem error\n";
 
		exit(5);
 
	}
 
}
 
 
 
int main(int argc, char **argv)
 
{
 
	ManagerConfig config;
 
	std::string config_file;
 
	std::string command;
 
	boost::program_options::variables_map vm;
 
 
	boost::program_options::options_description desc("Usage: spectrum [OPTIONS] <COMMAND>\nAllowed options");
 
	desc.add_options()
 
		("help,h", "Show help output")
 
		("config,c", boost::program_options::value<std::string>(&config_file)->default_value("/etc/spectrum2/spectrum_manager.cfg"), "Spectrum manager config file")
 
		("command", boost::program_options::value<std::string>(&command)->default_value(""), "Command")
 
@@ -234,48 +275,54 @@ int main(int argc, char **argv)
 
	}
 
	catch (std::runtime_error& e)
 
	{
 
		std::cout << desc << "\n";
 
		return 2;
 
	}
 
	catch (...)
 
	{
 
		std::cout << desc << "\n";
 
		return 3;
 
	}
 
 
 
	if (!config.load(config_file)) {
 
		std::cerr << "Can't load configuration file.\n";
 
		return 4;
 
	}
 
 
	if (command.empty()) {
 
		std::cout << desc << "\n";
 
		return 1;
 
	}
 
 
	if (command == "start") {
 
		start_all_instances(&config);
 
	}
 
	else if (command == "stop") {
 
		stop_all_instances(&config);
 
	}
 
	else {
 
		Swift::SimpleEventLoop eventLoop;
 
		Swift::BoostNetworkFactories networkFactories(&eventLoop);
 
 
		std::string message = argv[1];
 
		std::string message = command;
 
		m = &message;
 
 
		ask_local_servers(&config, networkFactories, message);
 
 
		std::vector<std::string> servers = CONFIG_VECTOR(&config, "servers.server");
 
		for (std::vector<std::string>::const_iterator it = servers.begin(); it != servers.end(); it++) {
 
			finished++;
 
			Swift::Client *client = new Swift::Client(CONFIG_STRING(&config, "service.admin_username") + "@" + (*it), CONFIG_STRING(&config, "service.admin_password"), &networkFactories);
 
			Swift::Client *client = new Swift::Client(CONFIG_STRING(&config, "service.admin_username") + "@" + *it, CONFIG_STRING(&config, "service.admin_password"), &networkFactories);
 
			client->setAlwaysTrustCertificates();
 
			client->onConnected.connect(boost::bind(&handleConnected, client));
 
			client->onDisconnected.connect(bind(&handleDisconnected, client, _1));
 
			client->onMessageReceived.connect(bind(&handleMessageReceived, client, _1));
 
			client->onConnected.connect(boost::bind(&handleConnected, client, *it));
 
			client->onDisconnected.connect(bind(&handleDisconnected, client, _1, *it));
 
			client->onMessageReceived.connect(bind(&handleMessageReceived, client, _1, *it));
 
			Swift::ClientOptions opt;
 
			opt.allowPLAINWithoutTLS = true;
 
			client->connect(opt);
 
	// 		std::cout << *it << "\n";
 
		}
 
 
		eventLoop.run();
 
	}
 
}
src/admininterface.cpp
Show inline comments
 
@@ -52,26 +52,26 @@ AdminInterface::AdminInterface(Component *component, UserManager *userManager, N
 
	m_server = server;
 

	
 
	m_component->getStanzaChannel()->onMessageReceived.connect(bind(&AdminInterface::handleMessageReceived, this, _1));
 
}
 

	
 
AdminInterface::~AdminInterface() {
 
}
 

	
 
void AdminInterface::handleMessageReceived(Swift::Message::ref message) {
 
	if (!message->getTo().getNode().empty())
 
		return;
 

	
 
	if (message->getFrom().getNode() != CONFIG_STRING(m_component->getConfig(), "service.admin_username")) {
 
		LOG4CXX_WARN(logger, "Message not from admin user, but from " << message->getFrom().getNode());
 
	if (message->getFrom().toBare().toString() != CONFIG_STRING(m_component->getConfig(), "service.admin_jid")) {
 
		LOG4CXX_WARN(logger, "Message not from admin user, but from " << message->getFrom().toBare().toString());
 
		return;
 
	}
 

	
 
	LOG4CXX_INFO(logger, "Message from admin received");
 
	message->setTo(message->getFrom());
 
	message->setFrom(m_component->getJID());
 

	
 
	if (message->getBody() == "status") {
 
		int users = m_userManager->getUserCount();
 
		int backends = m_server->getBackendCount();
 
		message->setBody("Running (" + boost::lexical_cast<std::string>(users) + " users connected using " + boost::lexical_cast<std::string>(backends) + " backends)");
 
	}
src/config.cpp
Show inline comments
 
@@ -60,25 +60,25 @@ bool Config::load(std::istream &ifs, boost::program_options::options_description
 
		("service.group", value<std::string>()->default_value(""), "The name of group Spectrum runs as.")
 
		("service.backend", value<std::string>()->default_value("libpurple_backend"), "Backend")
 
		("service.protocol", value<std::string>()->default_value(""), "Protocol")
 
		("service.pidfile", value<std::string>()->default_value("/var/run/spectrum2/spectrum2.pid"), "Full path to pid file")
 
		("service.working_dir", value<std::string>()->default_value("/var/lib/spectrum2"), "Working dir")
 
		("service.allowed_servers", value<std::string>()->default_value(""), "Only users from these servers can connect")
 
		("service.server_mode", value<bool>()->default_value(false), "True if Spectrum should behave as server")
 
		("service.users_per_backend", value<int>()->default_value(100), "Number of users per one legacy network backend")
 
		("service.backend_host", value<std::string>()->default_value("localhost"), "Host to bind backend server to")
 
		("service.backend_port", value<std::string>()->default_value("10000"), "Port to bind backend server to")
 
		("service.cert", value<std::string>()->default_value(""), "PKCS#12 Certificate.")
 
		("service.cert_password", value<std::string>()->default_value(""), "PKCS#12 Certificate password.")
 
		("service.admin_username", value<std::string>()->default_value(""), "Administrator username.")
 
		("service.admin_jid", value<std::string>()->default_value(""), "Administrator jid.")
 
		("service.admin_password", value<std::string>()->default_value(""), "Administrator password.")
 
		("service.reuse_old_backends", value<bool>()->default_value(true), "True if Spectrum should use old backends which were full in the past.")
 
		("service.idle_reconnect_time", value<int>()->default_value(0), "Time in seconds after which idle users are reconnected to let their backend die.")
 
		("service.memory_collector_time", value<int>()->default_value(0), "Time in seconds after which backend with most memory is set to die.")
 
		("service.more_resources", value<bool>()->default_value(false), "Allow more resources to be connected in server mode at the same time.")
 
		("service.enable_privacy_lists", value<bool>()->default_value(true), "")
 
		("identity.name", value<std::string>()->default_value("Spectrum 2 Transport"), "Name showed in service discovery.")
 
		("identity.category", value<std::string>()->default_value("gateway"), "Disco#info identity category. 'gateway' by default.")
 
		("identity.type", value<std::string>()->default_value(""), "Type of transport ('icq','msn','gg','irc', ...)")
 
		("registration.enable_public_registration", value<bool>()->default_value(true), "True if users should be able to register.")
 
		("registration.language", value<std::string>()->default_value("en"), "Default language for registration form")
 
		("registration.instructions", value<std::string>()->default_value(""), "Instructions showed to user in registration form")
src/userregistry.cpp
Show inline comments
 
@@ -31,25 +31,25 @@ namespace Transport {
 

	
 
static LoggerPtr logger = Logger::getLogger("UserRegistry");
 

	
 
UserRegistry::UserRegistry(Config *cfg, Swift::NetworkFactories *factories) {
 
	config = cfg;
 
	m_removeTimer = factories->getTimerFactory()->createTimer(1);
 
	m_inRemoveLater = false;
 
}
 

	
 
UserRegistry::~UserRegistry() { m_removeTimer->stop(); }
 

	
 
void UserRegistry::isValidUserPassword(const Swift::JID& user, Swift::ServerFromClientSession *session, const Swift::SafeByteArray& password) {
 
	if (!CONFIG_STRING(config, "service.admin_username").empty() && user.getNode() == CONFIG_STRING(config, "service.admin_username")) {
 
	if (!CONFIG_STRING(config, "service.admin_jid").empty() && user.toBare().toString() == CONFIG_STRING(config, "service.admin_jid")) {
 
		if (Swift::safeByteArrayToString(password) == CONFIG_STRING(config, "service.admin_password")) {
 
			session->handlePasswordValid();
 
		}
 
		else {
 
			session->handlePasswordInvalid();
 
		}
 
		return;
 
	}
 

	
 
	std::string key = user.toBare().toString();
 

	
 
	// Users try to connect twice
0 comments (0 inline, 0 general)