|
@@ -11,7 +11,8 @@ using ip::tcp;
|
|
* con_handler *
|
|
* con_handler *
|
|
***************/
|
|
***************/
|
|
|
|
|
|
-con_handler::con_handler(basic_socket_acceptor<ip::tcp>::executor_type &io_service) : sock(io_service), buf(max_length), jsonCommander(fileManager) {
|
|
|
|
|
|
+con_handler::con_handler(basic_socket_acceptor<ip::tcp>::executor_type &io_service, boost::asio::ssl::context &context)
|
|
|
|
+ : sock(io_service), sslsock(sock, context), usessl(Config::getValue("SSLenabled") == "true"), buf(max_length), jsonCommander(fileManager) {
|
|
// disable indentation for json
|
|
// disable indentation for json
|
|
this->jsonStringBuilder.settings_["indentation"] = "";
|
|
this->jsonStringBuilder.settings_["indentation"] = "";
|
|
|
|
|
|
@@ -22,11 +23,32 @@ con_handler::con_handler(basic_socket_acceptor<ip::tcp>::executor_type &io_servi
|
|
|
|
|
|
con_handler::~con_handler() {}
|
|
con_handler::~con_handler() {}
|
|
|
|
|
|
-con_handler::pointer con_handler::create(basic_socket_acceptor<ip::tcp>::executor_type &io_service) { return pointer(new con_handler(io_service)); }
|
|
|
|
|
|
+con_handler::pointer con_handler::create(basic_socket_acceptor<ip::tcp>::executor_type &io_service, boost::asio::ssl::context &context) {
|
|
|
|
+ return pointer(new con_handler(io_service, context));
|
|
|
|
+}
|
|
|
|
|
|
tcp::socket &con_handler::socket() { return sock; }
|
|
tcp::socket &con_handler::socket() { return sock; }
|
|
|
|
|
|
-void con_handler::start() { read(&con_handler::handle_read_version); }
|
|
|
|
|
|
+bool con_handler::handshake() {
|
|
|
|
+ boost::system::error_code err;
|
|
|
|
+ sslsock.handshake(boost::asio::ssl::stream_base::server, err);
|
|
|
|
+ if (err) {
|
|
|
|
+ std::cerr << "SSL handshake failed: " << err.message() << std::endl;
|
|
|
|
+ close_sock();
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void con_handler::close_sock() { sock.close(); }
|
|
|
|
+
|
|
|
|
+void con_handler::start() {
|
|
|
|
+ if (usessl) {
|
|
|
|
+ if (handshake())
|
|
|
|
+ read(&con_handler::handle_read_version);
|
|
|
|
+ } else
|
|
|
|
+ read(&con_handler::handle_read_version);
|
|
|
|
+}
|
|
|
|
|
|
void con_handler::handle_read_version(const boost::system::error_code &err, size_t bytes_transferred) {
|
|
void con_handler::handle_read_version(const boost::system::error_code &err, size_t bytes_transferred) {
|
|
if (!err) {
|
|
if (!err) {
|
|
@@ -43,12 +65,12 @@ void con_handler::handle_read_version(const boost::system::error_code &err, size
|
|
case JsonCommander::Action::closeAndSend:
|
|
case JsonCommander::Action::closeAndSend:
|
|
sendJson(response.json);
|
|
sendJson(response.json);
|
|
default:
|
|
default:
|
|
- sock.close();
|
|
|
|
|
|
+ close_sock();
|
|
}
|
|
}
|
|
|
|
|
|
} else {
|
|
} else {
|
|
- std::cerr << "error: " << err.message() << std::endl;
|
|
|
|
- sock.close();
|
|
|
|
|
|
+ std::cerr << __PRETTY_FUNCTION__ << " error: " << err.message() << std::endl;
|
|
|
|
+ close_sock();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -67,12 +89,12 @@ void con_handler::handle_read_login(const boost::system::error_code &err, size_t
|
|
case JsonCommander::Action::closeAndSend:
|
|
case JsonCommander::Action::closeAndSend:
|
|
sendJson(response.json);
|
|
sendJson(response.json);
|
|
default:
|
|
default:
|
|
- sock.close();
|
|
|
|
|
|
+ close_sock();
|
|
}
|
|
}
|
|
|
|
|
|
} else {
|
|
} else {
|
|
- std::cerr << "error: " << err.message() << std::endl;
|
|
|
|
- sock.close();
|
|
|
|
|
|
+ std::cerr << __PRETTY_FUNCTION__ << " error: " << err.message() << std::endl;
|
|
|
|
+ close_sock();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -91,12 +113,12 @@ void con_handler::handle_read_command(const boost::system::error_code &err, size
|
|
case JsonCommander::Action::closeAndSend:
|
|
case JsonCommander::Action::closeAndSend:
|
|
sendJson(response.json);
|
|
sendJson(response.json);
|
|
default:
|
|
default:
|
|
- sock.close();
|
|
|
|
|
|
+ close_sock();
|
|
}
|
|
}
|
|
|
|
|
|
} else {
|
|
} else {
|
|
- std::cerr << "error: " << err.message() << std::endl;
|
|
|
|
- sock.close();
|
|
|
|
|
|
+ std::cerr << __PRETTY_FUNCTION__ << " error: " << err.message() << std::endl;
|
|
|
|
+ close_sock();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -104,26 +126,28 @@ void con_handler::handle_write(const boost::system::error_code &err, size_t byte
|
|
if (!err) {
|
|
if (!err) {
|
|
std::cout << "Hello World!" << std::endl;
|
|
std::cout << "Hello World!" << std::endl;
|
|
} else {
|
|
} else {
|
|
- std::cerr << "error: " << err.message() << std::endl;
|
|
|
|
- sock.close();
|
|
|
|
|
|
+ std::cerr << __PRETTY_FUNCTION__ << " error: " << err.message() << std::endl;
|
|
|
|
+ close_sock();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void con_handler::read(void (con_handler::*handler)(const boost::system::error_code &err, size_t bytes_transferred)) {
|
|
void con_handler::read(void (con_handler::*handler)(const boost::system::error_code &err, size_t bytes_transferred)) {
|
|
- /*sock.async_read_some(buffer(data, max_length),
|
|
|
|
- boost::bind(handler, shared_from_this(),
|
|
|
|
- placeholders::error,
|
|
|
|
- placeholders::bytes_transferred));*/
|
|
|
|
-
|
|
|
|
- async_read_until(sock, buf, '\n', bind(handler, shared_from_this(), placeholders::error, placeholders::bytes_transferred));
|
|
|
|
|
|
+ if (usessl)
|
|
|
|
+ async_read_until(sslsock, buf, '\n', bind(handler, shared_from_this(), placeholders::error, placeholders::bytes_transferred));
|
|
|
|
+ else
|
|
|
|
+ async_read_until(sock, buf, '\n', bind(handler, shared_from_this(), placeholders::error, placeholders::bytes_transferred));
|
|
}
|
|
}
|
|
|
|
|
|
void con_handler::sendJson(const Json::Value &json) {
|
|
void con_handler::sendJson(const Json::Value &json) {
|
|
std::string jsonString = Json::writeString(jsonStringBuilder, json);
|
|
std::string jsonString = Json::writeString(jsonStringBuilder, json);
|
|
jsonString.append("\n");
|
|
jsonString.append("\n");
|
|
|
|
|
|
- sock.async_write_some(buffer(jsonString, max_length),
|
|
|
|
- boost::bind(&con_handler::handle_write, shared_from_this(), placeholders::error, placeholders::bytes_transferred));
|
|
|
|
|
|
+ if (usessl)
|
|
|
|
+ sslsock.async_write_some(buffer(jsonString, max_length),
|
|
|
|
+ boost::bind(&con_handler::handle_write, shared_from_this(), placeholders::error, placeholders::bytes_transferred));
|
|
|
|
+ else
|
|
|
|
+ sock.async_write_some(buffer(jsonString, max_length),
|
|
|
|
+ boost::bind(&con_handler::handle_write, shared_from_this(), placeholders::error, placeholders::bytes_transferred));
|
|
}
|
|
}
|
|
|
|
|
|
Json::Value con_handler::parseMessage() {
|
|
Json::Value con_handler::parseMessage() {
|
|
@@ -138,7 +162,7 @@ Json::Value con_handler::parseMessage() {
|
|
// parse data
|
|
// parse data
|
|
if (!this->jsonReader->parse(data, data + lineEnd, &root, &err)) {
|
|
if (!this->jsonReader->parse(data, data + lineEnd, &root, &err)) {
|
|
std::cerr << "Json error: " << err << std::endl << "data: " << data;
|
|
std::cerr << "Json error: " << err << std::endl << "data: " << data;
|
|
- sock.close();
|
|
|
|
|
|
+ close_sock();
|
|
}
|
|
}
|
|
|
|
|
|
buf.consume(lineEnd + 1);
|
|
buf.consume(lineEnd + 1);
|
|
@@ -152,11 +176,20 @@ Json::Value con_handler::parseMessage() {
|
|
|
|
|
|
void Server::start_accept() {
|
|
void Server::start_accept() {
|
|
auto executor = acceptor_.get_executor();
|
|
auto executor = acceptor_.get_executor();
|
|
- con_handler::pointer connection = con_handler::create(executor);
|
|
|
|
|
|
+ con_handler::pointer connection = con_handler::create(executor, context_);
|
|
acceptor_.async_accept(connection->socket(), boost::bind(&Server::handle_accept, this, connection, placeholders::error));
|
|
acceptor_.async_accept(connection->socket(), boost::bind(&Server::handle_accept, this, connection, placeholders::error));
|
|
}
|
|
}
|
|
|
|
|
|
-Server::Server(io_service &io_service) : acceptor_(io_service, tcp::endpoint(tcp::v4(), std::stoi(Config::getValue("port")))) { start_accept(); }
|
|
|
|
|
|
+Server::Server(io_service &io_service)
|
|
|
|
+ : acceptor_(io_service, tcp::endpoint(tcp::v4(), std::stoi(Config::getValue("port")))), context_(boost::asio::ssl::context::sslv23) {
|
|
|
|
+ if (Config::getValue("SSLenabled") == "true") {
|
|
|
|
+ context_.set_options(boost::asio::ssl::context::default_workarounds | boost::asio::ssl::context::no_sslv2 | boost::asio::ssl::context::single_dh_use);
|
|
|
|
+ context_.use_certificate_chain_file(Config::getValue("SSLcertificate"));
|
|
|
|
+ context_.use_private_key_file(Config::getValue("SSLprivatekey"), boost::asio::ssl::context::pem);
|
|
|
|
+ context_.use_tmp_dh_file(Config::getValue("SSLdhparams"));
|
|
|
|
+ }
|
|
|
|
+ start_accept();
|
|
|
|
+}
|
|
|
|
|
|
Server::~Server() {}
|
|
Server::~Server() {}
|
|
|
|
|