/*
Copyright © 2015-2016 Gluzskiy Alexandr (sss)
This file is part of Unknown Download Manager (UDM).
UDM 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.
UDM 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 UDM. If not, see .
*/
#include
#include
#include "server_session.h"
#include "utilities.h"
#include "socket_wraper.h"
#include "client.h"
#include "modules_handler.h"
#include "event_subscription_event.h"
#include "event_subscription_repeated.h"
extern std::map clients;
extern modules_handler *modules;
server_session::server_session(boost::asio::io_service &s, runtime_config_s &config, std::map &clients_, std::map &downloads_, boost::asio::ssl::context *c) : io_service_(s), context_(c), runtime_config(config), clients(clients_), downloads(downloads_)
{
if(runtime_config.config_file.get("server.enable_encryption", false))
{
socket_ = new socket_wraper(new boost::asio::ssl::stream(io_service_, *context_));
socket_->get_ssl_socket().async_handshake(boost::asio::ssl::stream_base::server, boost::bind(&server_session::handle_handshake, this, boost::asio::placeholders::error));
}
else
socket_ = new socket_wraper(new boost::asio::ip::tcp::socket(io_service_));
}
socket_wraper* server_session::socket()
{
return socket_;
}
void server_session::run()
{
recv_data_ = new char[4];
boost::asio::async_read(*socket_, boost::asio::buffer(recv_data_, 4), boost::bind(&server_session::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
void server_session::handle_handshake(const boost::system::error_code& error)
{
if (!error)
{
recv_data_ = new char[4];
boost::asio::async_read(*socket_, boost::asio::buffer(recv_data_, 4), boost::bind(&server_session::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
else
{
delete this;
}
}
void server_session::handle_read(const boost::system::error_code& error, size_t bytes_transferred)
{
if (!error)
{
std::string s;
// bool parsed = false;
unsigned size = ntohl(*(int32_t*)recv_data_);
delete [] recv_data_;
char *buf = new char[size];
boost::system::error_code ec;
boost::asio::read(*socket_, boost::asio::buffer(buf, size), ec);
if(ec)
{
}
//TODO: check for error
s.append(buf, size);
delete [] buf;
if(size != s.length())
{
delete this;
return;
}
//TODO:
client_msg msg;
if(msg.ParseFromString(s))
{
//parsed = true;
BOOST_LOG_TRIVIAL(trace)<<"received message:\n"< &s, module_info *mi)
{
setting *msi = mi->add_settings();
msi->set_name(s.first);
msi->set_value(s.second.value);
for(auto di : s.second.info.dependencies)
msi->mutable_info()->add_dependencies(di);
for(auto bi : s.second.info.blockers)
msi->mutable_info()->add_blockers(bi);
msi->mutable_info()->set_default_value(s.second.info.default_value);
msi->mutable_info()->set_minimal_value(s.second.info.minimal_value);
msi->mutable_info()->set_maximal_value(s.second.info.maximal_value);
msi->mutable_info()->set_description(s.second.info.description);
}
void add_download_info_ui_element(module_download_ui_element_info_s& element, module_download_ui_element_info *mi)
{
module_download_ui_element_info *e = mi->add_children();
e->set_description(element.description);
e->set_name(element.name);
e->set_id(element.id);
switch(element.type)
{
case MODULE_UI_ELEMENT_TYPE_e::UI_EMPTY_:
e->set_type(MODULE_UI_ELEMENT_TYPE::UI_EMPTY);
break;
case MODULE_UI_ELEMENT_TYPE_e::UI_GROUP_:
e->set_type(MODULE_UI_ELEMENT_TYPE::UI_GROUP);
break;
case MODULE_UI_ELEMENT_TYPE_e::UI_INTEGER_:
e->set_type(MODULE_UI_ELEMENT_TYPE::UI_INTEGER);
break;
case MODULE_UI_ELEMENT_TYPE_e::UI_PROGRESS_BAR_:
e->set_type(MODULE_UI_ELEMENT_TYPE::UI_PROGRESS_BAR);
break;
case MODULE_UI_ELEMENT_TYPE_e::UI_STR_:
e->set_type(MODULE_UI_ELEMENT_TYPE::UI_STR);
break;
default:
e->set_type(MODULE_UI_ELEMENT_TYPE::UI_EMPTY);
break;
}
for(auto ui_element: element.children)
add_download_info_ui_element(ui_element, e);
}
void add_download_info_ui_element(module_download_ui_element_info_s& element, module_info *mi)
{
module_download_ui_element_info *e = mi->add_download_info_ui();
e->set_description(element.description);
e->set_name(element.name);
e->set_id(element.id);
switch(element.type)
{
case MODULE_UI_ELEMENT_TYPE_e::UI_EMPTY_:
e->set_type(MODULE_UI_ELEMENT_TYPE::UI_EMPTY);
break;
case MODULE_UI_ELEMENT_TYPE_e::UI_GROUP_:
e->set_type(MODULE_UI_ELEMENT_TYPE::UI_GROUP);
break;
case MODULE_UI_ELEMENT_TYPE_e::UI_INTEGER_:
e->set_type(MODULE_UI_ELEMENT_TYPE::UI_INTEGER);
break;
case MODULE_UI_ELEMENT_TYPE_e::UI_PROGRESS_BAR_:
e->set_type(MODULE_UI_ELEMENT_TYPE::UI_PROGRESS_BAR);
break;
case MODULE_UI_ELEMENT_TYPE_e::UI_STR_:
e->set_type(MODULE_UI_ELEMENT_TYPE::UI_STR);
break;
default:
e->set_type(MODULE_UI_ELEMENT_TYPE::UI_EMPTY);
break;
}
for(auto ui_element: element.children)
add_download_info_ui_element(ui_element, e);
}
void add_download_create_ui_element(module_download_ui_element_info_s& element, module_info *mi)
{
module_download_ui_element_info *e = mi->add_download_creation_ui();
e->set_description(element.description);
e->set_name(element.name);
e->set_id(element.id);
switch(element.type)
{
case MODULE_UI_ELEMENT_TYPE_e::UI_EMPTY_:
e->set_type(MODULE_UI_ELEMENT_TYPE::UI_EMPTY);
break;
case MODULE_UI_ELEMENT_TYPE_e::UI_GROUP_:
e->set_type(MODULE_UI_ELEMENT_TYPE::UI_GROUP);
break;
case MODULE_UI_ELEMENT_TYPE_e::UI_INTEGER_:
e->set_type(MODULE_UI_ELEMENT_TYPE::UI_INTEGER);
break;
case MODULE_UI_ELEMENT_TYPE_e::UI_PROGRESS_BAR_:
e->set_type(MODULE_UI_ELEMENT_TYPE::UI_PROGRESS_BAR);
break;
case MODULE_UI_ELEMENT_TYPE_e::UI_STR_:
e->set_type(MODULE_UI_ELEMENT_TYPE::UI_STR);
break;
default:
e->set_type(MODULE_UI_ELEMENT_TYPE::UI_EMPTY);
break;
}
for(auto ui_element: element.children)
add_download_info_ui_element(ui_element, e);
}
void add_download_menu_element(module_download_menu_element_info_s &element, module_download_menu_element_info *mi)
{
module_download_menu_element_info *e = mi->add_children();
e->set_description(element.description);
e->set_id(element.id);
e->set_name(element.name);
for(auto menu_element: element.children)
add_download_menu_element(menu_element, e);
}
void add_download_menu_element(module_download_menu_element_info_s &element, module_info *mi)
{
module_download_menu_element_info *e = mi->add_download_menu();
e->set_description(element.description);
e->set_id(element.id);
e->set_name(element.name);
for(auto menu_element: element.children)
add_download_menu_element(menu_element, e);
}
void add_download_content_menu_element(module_download_menu_element_info_s &element, module_info *mi)
{
module_download_menu_element_info *e = mi->add_download_content_menu();
e->set_description(element.description);
e->set_id(element.id);
e->set_name(element.name);
for(auto menu_element: element.children)
add_download_menu_element(menu_element, e);
}
bool server_session::handle_command(client_msg *msg)
{
//TODO: thread safety ?
if(msg->type() != CLIENT_MSG_TYPE::CLIENT_AUTH_REQUEST && clients.find(msg->auth_token()) == clients.end())
{
BOOST_LOG_TRIVIAL(error)<<"unauthorized client trying to execute command";
return false;
}
switch(msg->type())
{
case CLIENT_MSG_TYPE::CLIENT_AUTH_REQUEST:
{
server_msg m;
m.set_type(SERVER_MSG_TYPE::SERVER_AUTH_REPLY);
std::string server_password = runtime_config.config_file.get("server.password", "");
if(server_password.empty())
m.mutable_auth_reply()->set_status(true);
else
{
switch(msg->auth_request().hash_type())
{
case PASSWD_HASH_TYPE::HASH_NONE:
{
if(msg->auth_request().password() != server_password)
{
m.mutable_auth_reply()->set_status(false);
m.mutable_auth_reply()->set_error_description("wrong password");
}
else
m.mutable_auth_reply()->set_status(true);
}
break;
case PASSWD_HASH_TYPE::HASH_MD5:
{
//TODO:
}
break;
case PASSWD_HASH_TYPE::HASH_SHA2:
{
//TODO:
}
break;
case PASSWD_HASH_TYPE::HASH_SHA512:
{
unsigned char hash[64];
SHA512((unsigned char*)server_password.c_str(), server_password.length(), hash);
if(msg->auth_request().password() != std::string((const char*)hash))
{
m.mutable_auth_reply()->set_status(false);
m.mutable_auth_reply()->set_error_description("wrong password");
}
else
m.mutable_auth_reply()->set_status(true);
}
break;
default:
break;
}
}
if(m.auth_reply().status() == true)
{
client_auth_token = generate_auth_token();
m.mutable_auth_reply()->set_auth_token(client_auth_token);
auto i = clients.find(client_auth_token);
if(i == clients.end())
{
auto a = new client(client_auth_token);
clients[client_auth_token] = a;
client_ = a;
}
else
client_ = i->second;
//set auth token
}
send_message(&m);
}
break;
case CLIENT_MSG_TYPE::CLIENT_MODULES_REQUEST:
{
server_msg m;
m.set_type(SERVER_MSG_TYPE::SERVER_MODULES_REPLY);
for(auto i : modules->get_downloader_modules())
{
module_info *mi = m.add_server_modules_reply();
mi->set_type(SERVER_MODULE_TYPE::SERVER_MODULE_DOWNLOADER);
mi->set_name(i->get_module_info().name);
mi->set_description(i->get_module_info().description);
mi->set_version(i->get_module_info().version);
for(auto ms : i->get_runtime_module_settings())
add_runtime_module_setting(ms, mi);
const downloader_module_info &dmi = static_cast(i->get_module_info());
for(auto ui_element : dmi.download_info_ui)
add_download_info_ui_element(ui_element, mi);
for(auto ui_element : dmi.download_creation_ui)
add_download_create_ui_element(ui_element, mi);
for(auto menu_entry : dmi.download_root_menu)
add_download_menu_element(menu_entry, mi);
for(auto menu_entry : dmi.download_content_menu)
add_download_content_menu_element(menu_entry, mi);
for(auto si : dmi.get_downloads_parameters_info)
{
string_pair *p = mi->add_get_downloads_parameters_info();
p->set_name(si.first);
p->set_value(si.second);
}
for(auto si : dmi.download_state_names)
{
int_string_pair *p = mi->add_download_state_name();
p->set_id(si.first);
p->set_value(si.second);
}
}
for(auto i : modules->get_metadata_modules())
{
module_info *mi = m.add_server_modules_reply();
mi->set_type(SERVER_MODULE_TYPE::SERVER_MODULE_METADATA_STORAGE);
mi->set_name(i->get_module_info().name);
mi->set_description(i->get_module_info().description);
mi->set_version(i->get_module_info().version);
for(auto ms : i->get_runtime_module_settings())
add_runtime_module_setting(ms, mi);
}
send_message(&m);
}
break;
case CLIENT_MSG_TYPE::CLIENT_CORE_INFO_REQUEST:
{
server_msg m;
m.set_type(SERVER_MSG_TYPE::SERVER_CORE_INFO_REPLY);
m.mutable_server_core_info_reply()->set_version(1);
try{
for(auto it : runtime_config.config_file.get_child("server")) //load server node
{
std::string val = it.second.get_value("empty_value"); //TODO: something better here. we need to avoid subtrees and empty vars
if(/*val == "" ||*/ val == "empty_value")
continue;
setting *i = m.mutable_server_core_info_reply()->add_settings();
i->set_name(it.first);
i->set_value(val);
//TODO: is it possible to set something better than just list of core setting ?
}
}
catch(...)
{
//TODO:
}
send_message(&m);
}
break;
case CLIENT_MSG_TYPE::CLIENT_DOWNLOADS_LIST_REQUEST:
send_download_list();
break;
case CLIENT_MSG_TYPE::CLIENT_DOWNLOAD_INFO_REQUEST:
{
for(auto i : modules->get_downloader_modules())
{
if(i->get_module_info().name == downloads[msg->download_info_request().download_id()].module_name)
{
auto dm = static_cast(i);
server_msg m;
m.set_type(SERVER_MSG_TYPE::SERVER_DOWNLOAD_INFO_REPLY);
download *d = m.mutable_download()->mutable_download();
d->set_id(msg->download_info_request().download_id());
auto dl = dm->get_download(downloads[msg->download_info_request().download_id()].module_id);
d->set_name(dl.name);
d->set_size(dl.size);
d->set_state(dl.state);
d->set_module_name(msg->download_add_request().module_name());
d->set_downloaded(dl.downloaded);
send_message(&m);
}
}
}
break;
case CLIENT_MSG_TYPE::CLIENT_DOWNLOAD_ADD:
{
for(auto i : modules->get_downloader_modules())
{
if(i->get_module_info().name == msg->download_add_request().module_name())
{
std::map params;
for(auto p : msg->download_add_request().params())
params[p.id()] = p.value();
auto dm = static_cast(i);
int download_id = dm->add_download(params);
int core_id = downloads.size();
downloads[core_id].module_id = download_id;
downloads[core_id].module_name = msg->download_add_request().module_name();
server_msg m;
m.set_type(SERVER_MSG_TYPE::SERVER_DOWNLOAD_INFO_REPLY);
download *d = m.mutable_download()->mutable_download();
d->set_id(core_id);
auto dl = dm->get_download(download_id);
d->set_name(dl.name);
d->set_size(dl.size);
d->set_state(dl.state);
d->set_module_name(msg->download_add_request().module_name());
d->set_downloaded(dl.downloaded);
fire_event(SUBSCRIPTION_TYPE::ST_DOWNLOAD_ADDED, m);
break;
}
}
}
break;
case CLIENT_MSG_TYPE::CLIENT_DOWNLOAD_START:
{
server_msg s_msg;
s_msg.set_type(SERVER_MSG_TYPE::SERVER_DOWNLOAD_STATE_CHANGE);
for(auto i : msg->download_start_request())
{
for(auto m : modules->get_downloader_modules())
{
if(m->get_module_info().name == downloads[i.download_id()].module_name)
{
auto dm = static_cast(m);
if(!dm->start_download(i.download_id()))
{
//TODO: handle error
}
else
{
auto dsc = s_msg.add_download_state_changes();
dsc->set_download_id(i.download_id());
dsc->set_state(SUBSCRIPTION_DOWNLOAD_STATE::SDS_STARTED);
}
break;
}
}
}
fire_event(SUBSCRIPTION_TYPE::ST_DOWNLOAD_STATE_CHANGE, s_msg);
}
break;
case CLIENT_MSG_TYPE::CLIENT_DOWNLOAD_STOP:
{
server_msg s_msg;
s_msg.set_type(SERVER_MSG_TYPE::SERVER_DOWNLOAD_STATE_CHANGE);
for(auto i : msg->download_stop_request())
{
for(auto m : modules->get_downloader_modules())
{
if(m->get_module_info().name == downloads[i.download_id()].module_name)
{
auto dm = static_cast(m);
if(!dm->stop_download(i.download_id()))
{
//TODO: handle error
}
else
{
auto dsc = s_msg.add_download_state_changes();
dsc->set_download_id(i.download_id());
dsc->set_state(SUBSCRIPTION_DOWNLOAD_STATE::SDS_STOPPED);
}
break;
}
}
}
fire_event(SUBSCRIPTION_TYPE::ST_DOWNLOAD_STATE_CHANGE, s_msg);
}
break;
case CLIENT_MSG_TYPE::CLIENT_DOWNLOAD_DELETE:
{
server_msg s_msg;
s_msg.set_type(SERVER_MSG_TYPE::SERVER_DOWNLOAD_STATE_CHANGE);
for(auto i : msg->download_delete_request())
{
for(auto m : modules->get_downloader_modules())
{
if(m->get_module_info().name == downloads[i.download_id()].module_name)
{
auto dm = static_cast(m);
if(!dm->delete_download(i.download_id(), i.with_data()))
{
//TODO: handle error
}
else
{
downloads.erase(i.download_id());
auto dsc = s_msg.add_download_state_changes();
dsc->set_download_id(i.download_id());
dsc->set_state(SUBSCRIPTION_DOWNLOAD_STATE::SDS_DELETED);
}
break;
}
}
}
fire_event(SUBSCRIPTION_TYPE::ST_DOWNLOAD_STATE_CHANGE, s_msg);
}
break;
case CLIENT_MSG_TYPE::CLIENT_SUBSCRIPTION_REQUEST:
{
for(auto i : msg->subscription_request())
{
switch(i.mode())
{
case SUBSCRIPTION_MODE::SM_EVENT:
{
event_subscription_event *e = new event_subscription_event(&i);
client_->add_event_subscription(e);
}
break;
case SUBSCRIPTION_MODE::SM_REPEATED:
{
event_subscription_repeated *e = new event_subscription_repeated(&i);
client_->add_event_subscription(e);
}
break;
default:
break;
}
}
}
break;
default:
return false;
break;
}
return true;
}
void server_session::send_download_list()
{
//TODO: thread safety
//draft implementation, need a lot of optimizations
server_msg m;
m.set_type(SERVER_MSG_TYPE::SERVER_DOWNLOADS_LIST_REPLY);
for(auto i : downloads)
{
server_download_reply *r = m.add_downloads();
download *d = r->mutable_download();
d->set_id(i.first); //set core_id for later access
d->set_module_name(i.second.module_name);
module_base *module = nullptr;
for(auto dm : modules->get_downloader_modules())
{
if(dm->get_module_info().name == i.second.module_name)
{
module = dm;
break;
}
}
if(!module)
{
//downloader module not found, this should be a fatal error
continue;
}
module_downloader *dm = static_cast(module);
download_s ds = dm->get_download(i.second.module_id);
d->set_downloaded(ds.downloaded);
d->set_size(ds.size);
d->set_name(ds.name);
d->set_state(ds.state);
}
send_message(&m);
}
void server_session::send_message(server_msg *msg)
{
//TODO: thread safety
int size = 0;
std::shared_ptr ptr = pack_msg(msg, &size);
boost::asio::async_write(*socket_, boost::asio::buffer(*ptr, size), boost::bind(&server_session::handle_write, this, boost::asio::placeholders::error));
}
void server_session::handle_write(const boost::system::error_code& error)
{
if(!error)
{
//TODO:
}
else
{
//TODO: handle error
}
// recv_data_ = new char[4];
// boost::asio::async_read(socket_, boost::asio::buffer(recv_data_, 4), boost::bind(&server_session::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
void server_session::fire_event(SUBSCRIPTION_TYPE type, server_msg &msg)
{
//TODO: thread safety
for(auto i : clients)
{
for(auto s : i.second->get_subscriptions())
{
if(s->get_subscription_request().type() != type)
continue;
if(s->get_subtype() != EVENT_SUBTYPE::EVENT_SUBTYPE_EVENT)
continue;
switch(type)
{
case SUBSCRIPTION_TYPE::ST_DOWNLOAD_ADDED:
{
//this message already filled with all required info, just send it
send_message(&msg);
}
break;
case SUBSCRIPTION_TYPE::ST_DOWNLOAD_STATE_CHANGE:
{
server_msg filtered_message; //use new message to send only events from subscription and filter out all other download state change events
filtered_message.set_type(msg.type());
for(auto sc : msg.download_state_changes())
{
auto dsc = std::find(s->get_subscription_request().download_state_change().states().begin(), s->get_subscription_request().download_state_change().states().end(), sc.state());
if(dsc != s->get_subscription_request().download_state_change().states().end())
{
auto sc2 = filtered_message.add_download_state_changes();
*sc2 = sc;
}
}
if(filtered_message.download_state_changes_size())
send_message(&filtered_message);
}
default:
break;
}
}
}
}
server_session::~server_session()
{
//dtor
}