summaryrefslogtreecommitdiff
path: root/server/src/server_session.cpp
blob: c9b8e91d51e4c7a9f3acedce0aee1b5b21eea52e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
	Copyright © 2015 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 <http://www.gnu.org/licenses/>.

*/


#include <boost/bind.hpp>
#include <boost/log/trivial.hpp>
#include "server_session.h"
#include "../../protocol/udm.pb.h"
#include "utilities.h"
#include "socket_wraper.h"
#include "client.h"
#include "modules_handler.h"

extern std::map<std::string, client> clients;
extern modules_handler *modules;




server_session::server_session(boost::asio::io_service &s, runtime_config_s &config, std::map<std::string, client*> &clients_, boost::asio::ssl::context *c) : io_service_(s), context_(c), runtime_config(config), clients(clients_)
{
	if(runtime_config.config_file.get<bool>("server.enable_encryption", false))
	{
		socket_ = new socket_wraper(new boost::asio::ssl::stream<boost::asio::ip::tcp::socket>(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"<<msg.DebugString();
            handle_command(&msg);
            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));
        }
//        if(!parsed)
		else
        {
        	BOOST_LOG_TRIVIAL(error)<<"failed to parse client message";
            delete this; //close connection
            return;
        }
    }
    else
    {
        delete this;
    }
}

void server_session::handle_command(client_msg *msg)
{
	switch(msg->type())
	{
	case CLIENT_MSG_TYPE::CLIENT_AUTH_REQUEST:
	{
		server_msg m;
		m.set_type(SERVER_MSG_TYPE::SERVER_AUTH_REPLY);
		//TODO: check for already existing auth token
		std::string server_password = runtime_config.config_file.get<std::string>("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 msg;
		msg.set_type(SERVER_MSG_TYPE::SERVER_MODULES_REPLY);
		for(auto i : modules->get_downloader_modules())
		{
			module_info *mi = msg.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())
			{
				setting *msi = mi->add_settings();
                msi->set_name(ms.first);
                msi->set_value(ms.second.value);
                for(auto di : ms.second.info.dependencies)
					msi->mutable_info()->add_dependencies(di);
                for(auto bi : ms.second.info.blockers)
					msi->mutable_info()->add_blockers(bi);
				msi->mutable_info()->set_default_value(ms.second.info.default_value);
				msi->mutable_info()->set_minimal_value(ms.second.info.minimal_value);
				msi->mutable_info()->set_maximal_value(ms.second.info.maximal_value);
				msi->mutable_info()->set_description(ms.second.info.description);
			}
		}
		for(auto i : modules->get_metadata_modules())
		{
			module_info *mi = msg.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())
			{
				setting *msi = mi->add_settings();
                msi->set_name(ms.first);
                msi->set_value(ms.second.value);
                for(auto di : ms.second.info.dependencies)
					msi->mutable_info()->add_dependencies(di);
                for(auto bi : ms.second.info.blockers)
					msi->mutable_info()->add_blockers(bi);
				msi->mutable_info()->set_default_value(ms.second.info.default_value);
				msi->mutable_info()->set_minimal_value(ms.second.info.minimal_value);
				msi->mutable_info()->set_maximal_value(ms.second.info.maximal_value);
				msi->mutable_info()->set_description(ms.second.info.description);
			}
		}
		send_message(&msg);
	}
	break;
	case CLIENT_MSG_TYPE::CLIENT_CORE_INFO_REQUEST:
	{
		server_msg msg;
		msg.set_type(SERVER_MSG_TYPE::SERVER_CORE_INFO_REPLY);
		msg.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<std::string>("empty_value"); //TODO: something better here. we need to avoid subtrees and empty vars
			if(/*val == "" ||*/ val == "empty_value")
				continue;
			setting *i = msg.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(&msg);

	}
	break;
	case CLIENT_MSG_TYPE::CLIENT_DOWNLOADS_LIST_REQUEST:
	{
		//TODO:
	}
	break;
	default:
		break;
	}
}

void server_session::send_message(server_msg *msg)
{
	int size = 0;
	std::shared_ptr<char*> 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));
}


server_session::~server_session()
{
	//dtor
}