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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
/*
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 "client_session.h"
#include <boost/bind.hpp>
#include <boost/log/trivial.hpp>
#include <openssl/sha.h>
#include <QMetaType>
Q_DECLARE_METATYPE(server_msg);
int shit = qRegisterMetaType <server_msg> ("server_msg");
std::shared_ptr<char*> pack_data(const std::string &buf, int *size_)
{
BOOST_LOG_TRIVIAL(debug)<<__FILE__<<":"<<__LINE__<<"\t"<<__func__;
std::shared_ptr<char*> ptr = std::make_shared<char*>(new char[buf.length() + 4]);
int32_t size = buf.length();
*size_ = size + 4;
(*ptr)[3] = size & 0xff;
(*ptr)[2] = (size>>8) & 0xff;
(*ptr)[1] = (size>>16) & 0xff;
(*ptr)[0] = (size>>24) & 0xff;
char *fck = (*ptr)+4;
memcpy(fck, buf.data(), buf.length());
return ptr;
}
std::shared_ptr<char*> pack_msg(client_msg *msg, int *size_)
{
BOOST_LOG_TRIVIAL(debug)<<__FILE__<<":"<<__LINE__<<"\t"<<__func__;
BOOST_LOG_TRIVIAL(trace)<<"packing message:\n"<<msg->DebugString();
std::string msg_buf;
msg->SerializeToString(&msg_buf);
return pack_data(msg_buf, size_);
}
class socket_wraper
{
public:
socket_wraper(boost::asio::ssl::stream<boost::asio::ip::tcp::socket> *s) : is_ssl(true)
{
socket_ssl_ = s;
}
socket_wraper(tcp::socket *s) : is_ssl(false)
{
socket_ = s;
}
void operator=(boost::asio::ssl::stream<boost::asio::ip::tcp::socket> *s)
{
socket_ssl_ = s;
is_ssl = true;
}
void operator=(tcp::socket *s)
{
socket_ = s;
is_ssl = false;
}
boost::asio::ip::tcp::socket& get_socket()
{
return *socket_;
}
boost::asio::ssl::stream<boost::asio::ip::tcp::socket>& get_ssl_socket()
{
return *socket_ssl_;
}
template<typename MutableBufferSequence> std::size_t read_some(const MutableBufferSequence & buffers)
{
if(is_ssl)
return socket_ssl_->read_some(buffers);
else
return socket_->read_some(buffers);
}
template<typename MutableBufferSequence> std::size_t read_some(const MutableBufferSequence & buffers, boost::system::error_code & ec)
{
if(is_ssl)
return socket_ssl_->read_some(buffers, ec);
else
return socket_->read_some(buffers, ec);
}
template<typename MutableBufferSequence, typename ReadHandler> void async_read_some(const MutableBufferSequence & buffers, ReadHandler handler)
{
if(is_ssl)
socket_ssl_->async_read_some(buffers, handler);
else
socket_->async_read_some(buffers, handler);
}
template<typename ConstBufferSequence, typename WriteHandler> void async_write_some(const ConstBufferSequence & buffers, WriteHandler handler)
{
if(is_ssl)
socket_ssl_->async_write_some(buffers, handler);
else
socket_->async_write_some(buffers, handler);
}
~socket_wraper()
{
if(is_ssl)
delete socket_ssl_;
else
delete socket_;
}
private:
bool is_ssl;
tcp::socket *socket_;
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> *socket_ssl_;
};
client_session::client_session(QObject *parent, boost::asio::io_service *io_service) : QObject(parent), io_service_(*io_service)
{
}
/*tcp::socket& client_session::socket()
{
return *socket_;
} */
void client_session::client_connect(QString host, QString password, int port)
{
//TODO: settings for connection timeout
this->pasword = password;
if(!password.isEmpty())
{
unsigned char hash[64];
SHA512((unsigned char*)password.toUtf8().data(), password.length(), hash);
password_sha512 = (char*)hash;
}
char port_buf[6];
snprintf(port_buf, 5, "%d", port);
boost::asio::ip::tcp::resolver r(io_service_);
boost::asio::ip::tcp::resolver::query query(host.toUtf8().data(), port_buf);
boost::asio::ip::tcp::resolver::iterator it = r.resolve(query);
socket_ = new socket_wraper(new tcp::socket(io_service_));
boost::asio::async_connect(socket_->get_socket(), it, boost::bind(&client_session::handle_connect, this, boost::asio::placeholders::error));
// socket_.async_connect(ep, boost::bind(&client_session::handle_connect, this, boost::asio::placeholders::error));
io_service_.run();
}
void client_session::client_connect_ssl(QString host, QString password, int port, QString ssl_ca, QString ssl_crt, QString ssl_key)
{
ssl_enabled = true;
this->pasword = password;
boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12_client);
ctx.load_verify_file(ssl_ca.toUtf8().data());
ctx.use_certificate_file(ssl_crt.toUtf8().data(), boost::asio::ssl::context::pem);
ctx.use_private_key_file(ssl_key.toUtf8().data(), boost::asio::ssl::context::pem);
auto socket_ssl_ = new boost::asio::ssl::stream<boost::asio::ip::tcp::socket>(io_service_, ctx);
socket_ssl_->set_verify_mode(boost::asio::ssl::verify_peer);
socket_ssl_->set_verify_callback(boost::bind(&client_session::verify_certificate, this, _1, _2 ));
socket_ = new socket_wraper(socket_ssl_);
char port_buf[6];
snprintf(port_buf, 5, "%d", port);
boost::asio::ip::tcp::resolver r(io_service_);
boost::asio::ip::tcp::resolver::query query(host.toUtf8().data(), port_buf);
boost::asio::ip::tcp::resolver::iterator it = r.resolve(query);
boost::asio::async_connect(socket_->get_ssl_socket().lowest_layer(), it, boost::bind(&client_session::handle_connect, this, boost::asio::placeholders::error));
}
bool client_session::verify_certificate(bool preverified, boost::asio::ssl::verify_context& /*&ctx*/)
{
// The verify callback can be used to check whether the certificate that is
// being presented is valid for the peer. For example, RFC 2818 describes
// the steps involved in doing this for HTTPS. Consult the OpenSSL
// documentation for more details. Note that the callback is called once
// for each certificate in the certificate chain, starting from the root
// certificate authority.
// In this example we will simply print the certificate's subject name.
/* char subject_name[256];
X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
X509_NAME_oneline(X509_get_subject_name(cert), subject_name, 256);
std::cout << "Verifying " << subject_name << "\n"; */
return preverified;
}
void client_session::handle_connect(const boost::system::error_code &e)
{
BOOST_LOG_TRIVIAL(debug)<<__FILE__<<":"<<__LINE__<<"\t"<<typeid(this).name()<<"::"<<__func__;
if(e)
{
//TODO: settings for reconnect count and timeout
emit client_connected(false, QString::fromUtf8(e.message().c_str()));
BOOST_LOG_TRIVIAL(error)<<__FILE__<<":"<<__LINE__<<"\t"<<typeid(this).name()<<"::"<<__func__<<"\terror: "<<e.message();
delete this; //suicide
}
else
{
emit client_connected(true, QString::fromUtf8(e.message().c_str()));
//TODO: something better ?
{// for now hust send auth request message
client_msg msg;
msg.set_type(CLIENT_MSG_TYPE::CLIENT_AUTH_REQUEST);
msg.mutable_auth_info()->set_hash_type(PASSWD_HASH_TYPE::HASH_SHA512);
msg.mutable_auth_info()->set_password(password_sha512.data());
send_message(msg);
}
//listen for messages from server
recv_data_ = new char[4];
boost::asio::async_read(*socket_, boost::asio::buffer(recv_data_, 4), boost::bind(&client_session::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
}
void client_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:
server_msg msg;
if(msg.ParseFromString(s))
{
BOOST_LOG_TRIVIAL(trace)<<"received message:\n"<<msg.DebugString();
//parsed = true;
emit server_message_received(msg); //at least one more copy ...., i hope messages will be always small
//another way:
//make signal work with pointer to server_msg
//allocate msg via new server_msg and pass pointer to signal
//check if someone connected to signal and delete message if noone
//this will be limited to only one client connection because client will need to delete message manually
recv_data_ = new char[4];
boost::asio::async_read(*socket_, boost::asio::buffer(recv_data_, 4), boost::bind(&client_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 client_session::send_message(client_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(&client_session::handle_write, this, boost::asio::placeholders::error));
}
void client_session::handle_write(const boost::system::error_code& error)
{
if(error)
{
//TODO: handle error
}
}
client_session::~client_session()
{
//TODO: correct thread termination, reimplement
io_service_.stop();
delete socket_;
boost::asio::io_service *s = &io_service_;
delete s;
//emit terminate_thread();
}
/*boost::asio::io_service &client_session::io_service()
{
return io_service_;
} */
|