Changeset - 42b653a23ffc
[Not reviewed]
0 3 0
Jan Kaluza - 9 years ago 2016-01-26 19:24:14
jkaluza@redhat.com
Spectrum2_manager: add method to detect frontend type
3 files changed with 42 insertions and 0 deletions:
0 comments (0 inline, 0 general)
spectrum_manager/src/methods.cpp
Show inline comments
 
@@ -456,96 +456,135 @@ static void handleDataRead(boost::shared_ptr<Swift::Connection> m_conn, boost::s
 
		_data.erase(_data.begin(), _data.begin() + 4 + expected_size);
 

	
 
		if (wrapper.type() == pbnetwork::WrapperMessage_Type_TYPE_QUERY) {
 
			pbnetwork::BackendConfig payload;
 
			if (payload.ParseFromString(wrapper.payload()) == false) {
 
				std::cout << "PARSING ERROR\n";
 
				// TODO: ERROR
 
				continue;
 
			}
 
			m_conn->onDataRead.disconnect(boost::bind(&handleDataRead, m_conn, _1));
 
			m_conn->disconnect();
 
			response = payload.config();
 
			if (response.empty()) {
 
				response = "Empty response";
 
			}
 
			std::cout << payload.config() << "\n";
 
// 			exit(0);
 
		}
 
	}
 
}
 

	
 
static void handleConnected(boost::shared_ptr<Swift::Connection> m_conn, const std::string &msg, bool error) {
 
		m_conn->onConnectFinished.disconnect(boost::bind(&handleConnected, m_conn, msg, _1));
 
	if (error) {
 
		std::cerr << "Can't connect the server\n";
 
		response = "Can't connect the server\n";
 
		m_conn->onDataRead.disconnect(boost::bind(&handleDataRead, m_conn, _1));
 

	
 
// 		exit(50);
 
	}
 
	else {
 
		pbnetwork::BackendConfig m;
 
		m.set_config(msg);
 

	
 
		std::string message;
 
		m.SerializeToString(&message);
 

	
 
		WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_QUERY);
 

	
 
		uint32_t size = htonl(message.size());
 
		char *header = (char *) &size;
 

	
 
		
 
		// send header together with wrapper message
 
		m_conn->write(Swift::createSafeByteArray(std::string(header, 4) + message));
 
	}
 
}
 

	
 
bool is_slack(ManagerConfig *config, const std::string &jid) {
 
	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";
 
					continue;
 
				}
 

	
 
				if (CONFIG_STRING(&cfg, "service.jid") != jid) {
 
					continue;
 
				}
 

	
 
				return CONFIG_STRING(&cfg, "service.frontend") == "slack";
 
			}
 
		}
 

	
 
	}
 
	catch (const filesystem_error& ex) {
 
		return false;
 
	}
 

	
 
	return false;
 
}
 

	
 
void ask_local_server(ManagerConfig *config, Swift::BoostNetworkFactories &networkFactories, const std::string &jid, const std::string &message) {
 
	response = "";
 
	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);
 
		}
 

	
 
		bool found = false;
 
		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";
 
					continue;
 
				}
 

	
 
				if (CONFIG_STRING(&cfg, "service.jid") != jid) {
 
					continue;
 
				}
 

	
 
				found = true;
 

	
 
				boost::shared_ptr<Swift::Connection> m_conn;
 
				m_conn = networkFactories.getConnectionFactory()->createConnection();
 
				m_conn->onDataRead.connect(boost::bind(&handleDataRead, m_conn, _1));
 
				m_conn->onConnectFinished.connect(boost::bind(&handleConnected, m_conn, message, _1));
 
				m_conn->connect(Swift::HostAddressPort(Swift::HostAddress(CONFIG_STRING(&cfg, "service.backend_host")), getPort(CONFIG_STRING(&cfg, "service.portfile"))));
 

	
 
// 				finished++;
 
// 				Swift::Client *client = new Swift::Client(CONFIG_VECTOR(&cfg, "service.admin_jid")[0], 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);
 
			}
 
		}
spectrum_manager/src/methods.h
Show inline comments
 
@@ -6,52 +6,53 @@
 
 * 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 2 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, write to the Free Software
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 
 */
 

	
 
#pragma once
 

	
 
#include <boost/program_options.hpp>
 
#include <boost/foreach.hpp>
 
#include <boost/format.hpp>
 
#include <boost/algorithm/string.hpp>
 
#include <boost/assign.hpp>
 
#include <boost/bind.hpp>
 
#include <boost/signal.hpp>
 

	
 
#include "managerconfig.h"
 
#include "transport/Config.h"
 
#include "transport/protocol.pb.h"
 
#include "Swiften/Swiften.h"
 
#include "Swiften/EventLoop/SimpleEventLoop.h"
 

	
 

	
 

	
 
std::string searchForBinary(const std::string &binary);
 

	
 
// Executes new backend
 
unsigned long exec_(std::string path, std::string config, std::string jid = "");
 

	
 
int getPort(const std::string &portfile);
 

	
 
int isRunning(const std::string &pidfile);
 
int start_instances(ManagerConfig *config, const std::string &_jid = "");
 
int restart_instances(ManagerConfig *config, const std::string &_jid = "");
 
void stop_instances(ManagerConfig *config, const std::string &_jid = "");
 

	
 
int show_status(ManagerConfig *config);
 

	
 
void ask_local_server(ManagerConfig *config, Swift::BoostNetworkFactories &networkFactories, const std::string &jid, const std::string &message);
 
bool is_slack(ManagerConfig *config, const std::string &jid);
 

	
 
std::vector<std::string> show_list(ManagerConfig *config, bool show = true);
 

	
 
std::string get_response();
spectrum_manager/src/server.cpp
Show inline comments
 
@@ -352,99 +352,101 @@ std::string Server::send_command(const std::string &jid, const std::string &cmd)
 
// 	Swift::BoostNetworkFactories networkFactories(&eventLoop);
 
// 
 
// 	ask_local_server(m_config, networkFactories, jid, cmd);
 
// 	while(get_response().empty()) {
 
// 		eventLoop.runUntilEvents();
 
// 	}
 
// 
 
// 	std::string response = get_response();
 
// 	
 
// 	html += "<pre>" + response + "</pre>";
 
// 
 
// 	html += "<a href=\"/\">Back to main page</a>";
 
// 	html += "</body></html>";
 
// 	print_html(conn, hm, html);
 
// }
 

	
 
void Server::serve_logout(struct mg_connection *conn, struct http_message *hm) {
 
	std::string host;
 
	mg_str *host_hdr = mg_get_http_header(hm, "Host");
 
	if (host_hdr) {
 
		if (!CONFIG_STRING(m_config, "service.cert").empty()) {
 
			host += "https://";
 
		}
 
		else {
 
			host += "http://";
 
		}
 
		host += std::string(host_hdr->p, host_hdr->len);
 
	}
 

	
 
	Server:session *session = get_session(hm);
 
	mg_printf(conn, "HTTP/1.1 302 Found\r\n"
 
		"Set-Cookie: session=%s; max-age=0\r\n"
 
		"Set-Cookie: admin=%s; max-age=0\r\n"
 
		"Location: %s%s\r\n\r\n",
 
		session->session_id, session->admin ? "1" : "0", host.c_str(), CONFIG_STRING(m_config, "service.base_location").c_str());
 

	
 
	sessions.erase(session->session_id);
 
	delete session;
 
}
 

	
 
void Server::serve_oauth2(struct mg_connection *conn, struct http_message *hm) {
 
// 	http://slack.spectrum.im/oauth2/localhostxmpp?code=14830663267.19140123492.e7f78a836d&state=534ab3b6-8bf1-4974-8274-847df8490bc5
 
	std::string uri(hm->uri.p, hm->uri.len);
 
	std::string instance = uri.substr(uri.rfind("/") + 1);
 
	std::string code = get_http_var(hm, "code");
 
	std::string state = get_http_var(hm, "state");
 

	
 
	std::string response = send_command(instance, "set_oauth2_code " + code + " " + state);
 
	std::cerr << "set_oauth2_code response: '" << response << "'\n";
 
	if (response.find("Registered as ") == 0) {
 
		std::vector<std::string> args;
 
		boost::split(args, response, boost::is_any_of(" "));
 
		std::cerr << "set_oauth2_code response size " << args.size() << "\n";
 
		if (args.size() == 3) {
 
			Server:session *session = get_session(hm);
 
			UserInfo info;
 
			m_storage->getUser(session->user, info);
 
			std::string username = "";
 
			int type = (int) TYPE_STRING;
 
			m_storage->getUserSetting(info.id, instance, type, username);
 
			m_storage->updateUserSetting(info.id, instance, args[2]);
 
		}
 
	}
 
	redirect_to(conn, hm, "/instances/");
 
}
 

	
 
void Server::event_handler(struct mg_connection *conn, int ev, void *p) {
 
	struct http_message *hm = (struct http_message *) p;
 

	
 
	if (ev == MG_EV_SSI_CALL) {
 
		mbuf_resize(&conn->send_mbuf, conn->send_mbuf.size * 2);
 
		std::string resp(conn->send_mbuf.buf, conn->send_mbuf.len);
 
		boost::replace_all(resp, "href=\"/", std::string("href=\"") + CONFIG_STRING(m_config, "service.base_location"));
 
		boost::replace_all(resp, "src=\"/", std::string("src=\"") + CONFIG_STRING(m_config, "service.base_location"));
 
		boost::replace_all(resp, "action=\"/", std::string("action=\"") + CONFIG_STRING(m_config, "service.base_location"));
 
		strcpy(conn->send_mbuf.buf, resp.c_str());
 
		mbuf_trim(&conn->send_mbuf);
 
		return;
 
	}
 

	
 
	if (ev != MG_EV_HTTP_REQUEST) {
 
		return;
 
	}
 

	
 
	hm->uri.p += CONFIG_STRING(m_config, "service.base_location").size() - 1;
 
	hm->uri.len -= CONFIG_STRING(m_config, "service.base_location").size() - 1;
 

	
 
	if (!is_authorized(conn, hm)) {
 
		redirect_to(conn, hm, "/login");
 
	} else if (mg_vcmp(&hm->uri, "/authorize") == 0) {
 
		authorize(conn, hm);
 
	} else if (mg_vcmp(&hm->uri, "/logout") == 0) {
 
		serve_logout(conn, hm);
 
// 	} else if (mg_vcmp(&hm->uri, "/users") == 0) {
 
// 		serve_users(conn, hm);
 
// 	} else if (mg_vcmp(&hm->uri, "/users/add") == 0) {
 
// 		serve_users_add(conn, hm);
 
// 	} else if (mg_vcmp(&hm->uri, "/users/remove") == 0) {
 
// 		serve_users_remove(conn, hm);
 
	} else if (has_prefix(&hm->uri, "/oauth2")) {
 
		serve_oauth2(conn, hm);
0 comments (0 inline, 0 general)