Changeset - 35c13f038f69
[Not reviewed]
0 4 0
Jan Kaluza - 13 years ago 2012-08-29 15:33:43
hanzz.k@gmail.com
Hidden password, allow seeting port
4 files changed with 6 insertions and 5 deletions:
0 comments (0 inline, 0 general)
spectrum_manager/src/main.cpp
Show inline comments
 
@@ -118,49 +118,49 @@ int main(int argc, char **argv)
 
	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[0] == "start") {
 
		start_instances(&config);
 
	}
 
	else if (command[0] == "stop") {
 
		stop_instances(&config);
 
	}
 
	else if (command[0] == "status") {
 
		return show_status(&config);
 
	}
 
	else if (command[0] == "list") {
 
		std::vector<std::string> list = show_list(&config);
 
	}
 
	else if (command[0] == "server") {
 
		Server server(&config);
 
		server.start(8080);
 
		server.start();
 
		while (1) { sleep(10); }
 
	}
 
	else {
 
		if (command.size() < 2) {
 
			std::cout << desc << "\n";
 
			return 11;
 
		}
 
		Swift::SimpleEventLoop eventLoop;
 
		Swift::BoostNetworkFactories networkFactories(&eventLoop);
 
 
		std::string jid = command[0];
 
		command.erase(command.begin());
 
		std::string cmd = boost::algorithm::join(command, " ");
 
 
		if (cmd == "start") {
 
			start_instances(&config, jid);
 
			return 0;
 
		}
 
		else if (cmd == "stop") {
 
			stop_instances(&config, jid);
 
			return 0;
 
		}
 
 
		ask_local_server(&config, networkFactories, jid, cmd);
spectrum_manager/src/managerconfig.cpp
Show inline comments
 
@@ -10,42 +10,43 @@
 
 *
 
 * 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
 
 */
 

	
 
#include "managerconfig.h"
 
#include <fstream>
 

	
 
using namespace boost::program_options;
 

	
 
bool ManagerConfig::load(const std::string &configfile, boost::program_options::options_description &opts) {
 
	std::ifstream ifs(configfile.c_str());
 
	if (!ifs.is_open())
 
		return false;
 

	
 
	opts.add_options()
 
		("service.admin_username", value<std::string>()->default_value(""), "Administrator username.")
 
		("service.admin_password", value<std::string>()->default_value(""), "Administrator password.")
 
		("service.port", value<int>()->default_value(8081), "Web interface port.")
 
		("service.config_directory", value<std::string>()->default_value("/etc/spectrum2/transports/"), "Directory with spectrum2 configuration files. One .cfg file per one instance")
 
		("servers.server", value<std::vector<std::string> >()->multitoken(), "Server.")
 
	;
 

	
 
	store(parse_config_file(ifs, opts), m_variables);
 
	notify(m_variables);
 

	
 
	m_file = configfile;
 

	
 
	onManagerConfigReloaded();
 

	
 
	return true;
 
}
 

	
 
bool ManagerConfig::load(const std::string &configfile) {
 
	options_description opts("Transport options");
 
	return load(configfile, opts);
 
}
spectrum_manager/src/server.cpp
Show inline comments
 
@@ -137,51 +137,51 @@ static void my_strlcpy(char *dst, const char *src, size_t len) {
 
// Note that it is easy to steal session cookies by sniffing traffic.
 
// This is why all communication must be SSL-ed.
 
static void generate_session_id(char *buf, const char *random,
 
                                const char *user) {
 
  mg_md5(buf, random, user, NULL);
 
}
 

	
 
Server::Server(ManagerConfig *config) {
 
	srand((unsigned) time(0));
 
	m_config = config;
 
	m_user = CONFIG_STRING(m_config, "service.admin_username");
 
	m_password = CONFIG_STRING(m_config, "service.admin_password");
 
}
 

	
 
Server::~Server() {
 
	mg_stop(ctx);
 
}
 

	
 

	
 
static void *_event_handler(enum mg_event event, struct mg_connection *conn) {
 
	const struct mg_request_info *request_info = mg_get_request_info(conn);
 
	return static_cast<Server *>(request_info->user_data)->event_handler(event, conn);
 
}
 

	
 
bool Server::start(int port) {
 
bool Server::start() {
 
	const char *options[] = {
 
		"listening_ports", boost::lexical_cast<std::string>(port).c_str(),
 
		"listening_ports", boost::lexical_cast<std::string>(CONFIG_INT(m_config, "service.port")).c_str(),
 
		"num_threads", "1",
 
		NULL
 
	};
 

	
 
	// Setup and start Mongoose
 
	if ((ctx = mg_start(&_event_handler, this, options)) == NULL) {
 
		return false;
 
	}
 

	
 
	return true;
 
}
 

	
 
bool Server::check_password(const char *user, const char *password) {
 
	return (m_user == user && m_password == password);
 
}
 

	
 
// Allocate new session object
 
Server::session *Server::new_session(const char *user) {
 
	Server::session *session = new Server::session;
 

	
 
	my_strlcpy(session->user, user, sizeof(session->user));
 
	snprintf(session->random, sizeof(session->random), "%d", rand());
 
	generate_session_id(session->session_id, session->random, session->user);
 
	session->expire = time(0) + SESSION_TTL;
 
@@ -275,49 +275,49 @@ void Server::print_html(struct mg_connection *conn, const struct mg_request_info
 
	mg_printf(conn,
 
			"HTTP/1.1 200 OK\r\n"
 
			"Content-Type: text/html\r\n"
 
			"Content-Length: %d\r\n"        // Always set Content-Length
 
			"\r\n"
 
			"%s",
 
			(int) html.size(), html.c_str());
 
}
 

	
 
void Server::serve_login(struct mg_connection *conn, const struct mg_request_info *request_info) {
 
	std::string html= "\
 
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\
 
  \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> \
 
<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" dir=\"ltr\"> \
 
  <head>\
 
    <title>Spectrum 2 web interface</title>\
 
    <meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/>\
 
  </head>\
 
  <body>\
 
    <center>\
 
      <h2>Spectrum 2 web interface login</h2>\
 
      <br/>\
 
      <form action=\"/authorize\">\
 
        Username: <input type=\"text\" name=\"user\"></input><br/>\
 
        Password: <input type=\"text\" name=\"password\"></input><br/>\
 
        Password: <input type=\"password\" name=\"password\"></input><br/>\
 
        <input type=\"submit\" value=\"Login\"></input>\
 
      </form>\
 
    </center>\
 
  </body>\
 
</html>";
 

	
 
	print_html(conn, request_info, html);
 
}
 

	
 
void Server::serve_onlineusers(struct mg_connection *conn, const struct mg_request_info *request_info) {
 
	std::string html = get_header();
 
	char jid[255];
 
	get_qsvar(request_info, "jid", jid, sizeof(jid));
 

	
 
	html += std::string("<h2>") + jid + " online users</h2><table><tr><th>JID<th>Command</th></tr>";
 

	
 
	Swift::SimpleEventLoop eventLoop;
 
	Swift::BoostNetworkFactories networkFactories(&eventLoop);
 

	
 
	ask_local_server(m_config, networkFactories, jid, "online_users");
 
	eventLoop.runUntilEvents();
 
	while(get_response().empty()) {
 
		eventLoop.runUntilEvents();
 
	}
spectrum_manager/src/server.h
Show inline comments
 
@@ -25,49 +25,49 @@
 
#include <boost/format.hpp>
 
#include <boost/algorithm/string.hpp>
 
#include <boost/assign.hpp>
 
#include <boost/bind.hpp>
 
#include <boost/signal.hpp>
 

	
 
#include "mongoose.h"
 
#include "managerconfig.h"
 

	
 
class Server {
 
	public:
 
		struct session {
 
			char session_id[33];      // Session ID, must be unique
 
			char random[20];          // Random data used for extra user validation
 
			char user[255];  // Authenticated user
 
			time_t expire;            // Expiration timestamp, UTC
 
		};
 

	
 
		/// Constructor.
 
		Server(ManagerConfig *config);
 

	
 
		/// Destructor
 
		virtual ~Server();
 

	
 
		bool start(int port);
 
		bool start();
 

	
 
		void *event_handler(enum mg_event event, struct mg_connection *conn);
 

	
 
	private:
 
		void serve_login(struct mg_connection *conn, const struct mg_request_info *request_info);
 
		void serve_root(struct mg_connection *conn, const struct mg_request_info *request_info);
 
		void serve_start(struct mg_connection *conn, const struct mg_request_info *request_info);
 
		void serve_stop(struct mg_connection *conn, const struct mg_request_info *request_info);
 
		void serve_onlineusers(struct mg_connection *conn, const struct mg_request_info *request_info);
 
		void serve_cmd(struct mg_connection *conn, const struct mg_request_info *request_info);
 
		void print_html(struct mg_connection *conn, const struct mg_request_info *request_info, const std::string &html);
 

	
 
	private:
 
		bool check_password(const char *user, const char *password);
 
		session *new_session(const char *user);
 
		session *get_session(const struct mg_connection *conn);
 

	
 
		void authorize(struct mg_connection *conn, const struct mg_request_info *request_info);
 

	
 
		bool is_authorized(const struct mg_connection *conn, const struct mg_request_info *request_info);
 

	
 
		void redirect_to(struct mg_connection *conn, const struct mg_request_info *request_info, const char *where);
 

	
 
	private:
0 comments (0 inline, 0 general)