ssl working
This commit is contained in:
+311
-264
@@ -1,6 +1,10 @@
|
|||||||
// websocket
|
// websocket
|
||||||
#include <boost/beast/core.hpp>
|
#include <boost/beast/core.hpp>
|
||||||
|
#include <boost/beast/ssl.hpp>
|
||||||
#include <boost/beast/websocket.hpp>
|
#include <boost/beast/websocket.hpp>
|
||||||
|
#include <boost/beast/websocket/ssl.hpp>
|
||||||
|
#include <boost/asio/ip/tcp.hpp>
|
||||||
|
#include <boost/asio/ssl/stream.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
@@ -40,10 +44,14 @@
|
|||||||
// file read/write
|
// file read/write
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
|
// openssl
|
||||||
|
#define BOOST_NETWORK_ENABLE_HTTPS
|
||||||
|
|
||||||
namespace beast = boost::beast;
|
namespace beast = boost::beast;
|
||||||
namespace http = beast::http;
|
namespace http = beast::http;
|
||||||
namespace websocket = beast::websocket;
|
namespace websocket = beast::websocket;
|
||||||
namespace net = boost::asio;
|
namespace net = boost::asio;
|
||||||
|
namespace ssl = boost::asio::ssl;
|
||||||
using tcp = boost::asio::ip::tcp;
|
using tcp = boost::asio::ip::tcp;
|
||||||
|
|
||||||
|
|
||||||
@@ -135,6 +143,8 @@ void LOG(std::stringstream&& s, WORD color)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void do_session(tcp::socket sock, ssl::context& ctx, sessionManager& sessManager, std::string ip, uint64_t& user_n, std::string appPath, std::string appArgs, uint16_t portStream, uint16_t portHttp);
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
// disable console input
|
// disable console input
|
||||||
@@ -175,30 +185,33 @@ int main(int argc, char* argv[])
|
|||||||
// 5 - start_http_port
|
// 5 - start_http_port
|
||||||
// 6 - start_streamer_port
|
// 6 - start_streamer_port
|
||||||
|
|
||||||
// 7 - path_to_application
|
// 7 - path to crt
|
||||||
// 8 - application_args (without -pixelStreamingPort cos its argv[3])
|
// 8 - path to key
|
||||||
|
// 9 - path to pem
|
||||||
|
|
||||||
|
// 10 - path_to_application
|
||||||
|
// 11 - application_args (without -pixelStreamingPort cos its argv[3])
|
||||||
|
|
||||||
// sessions
|
// sessions
|
||||||
uint16_t sessionLimit = std::atoi(argv[1]);
|
uint16_t sessionLimit = std::atoi(argv[1]);
|
||||||
sessionManager sessManager;
|
sessionManager sessManager;
|
||||||
sessManager.setlimit(sessionLimit);
|
sessManager.setlimit(sessionLimit);
|
||||||
|
|
||||||
|
// user count
|
||||||
|
uint64_t user_n = 0;
|
||||||
|
|
||||||
// merging app arguments
|
// merging app arguments
|
||||||
std::string appPath = argv[7];
|
std::string appPath = argv[10];
|
||||||
std::string appArgs;
|
std::string appArgs;
|
||||||
for (int i = 8; i < argc; ++i)
|
for (int i = 11; i < argc; ++i)
|
||||||
appArgs += argv[i] + std::string(" ");
|
appArgs += argv[i] + std::string(" ");
|
||||||
|
|
||||||
appArgs += "-PixelStreamingPort=";
|
appArgs += "-PixelStreamingPort=";
|
||||||
|
|
||||||
// first app ports
|
// first app ports
|
||||||
uint16_t portHttp = std::atoi(argv[5]);
|
uint16_t portHttp = std::atoi(argv[5]);
|
||||||
uint16_t portStream = std::atoi(argv[6]);
|
uint16_t portStream = std::atoi(argv[6]);
|
||||||
|
|
||||||
// node js
|
|
||||||
/*std::string nodePath = "C:\\Program Files\\nodejs\\node.exe";
|
|
||||||
std::string nodeFilePath = argv[2];*/
|
|
||||||
|
|
||||||
// starting node js servers
|
// starting node js servers
|
||||||
for (uint16_t i = 0; i < sessionLimit; ++i)
|
for (uint16_t i = 0; i < sessionLimit; ++i)
|
||||||
{
|
{
|
||||||
@@ -209,297 +222,331 @@ int main(int argc, char* argv[])
|
|||||||
std::string ip(argv[2]);
|
std::string ip(argv[2]);
|
||||||
LOG((std::stringstream() << "ip: " << ip), 7);
|
LOG((std::stringstream() << "ip: " << ip), 7);
|
||||||
auto const address = net::ip::make_address(ip);
|
auto const address = net::ip::make_address(ip);
|
||||||
auto const port = static_cast<unsigned short>(std::atoi(argv[3]));
|
auto const port = static_cast<uint16_t>(std::atoi(argv[3]));
|
||||||
LOG((std::stringstream() << "port: " << port), 7);
|
LOG((std::stringstream() << "port: " << port), 7);
|
||||||
net::io_context ioc{ 1 };
|
|
||||||
tcp::acceptor acceptor{ ioc, {address, port} };
|
|
||||||
|
|
||||||
// stats count
|
try
|
||||||
uint64_t user_n = 0;
|
{
|
||||||
|
// the io_context is required for all I/O
|
||||||
|
net::io_context ioc{ 1 };
|
||||||
|
|
||||||
|
// the SSL context is required, and holds certificates
|
||||||
|
ssl::context ctx{ ssl::context::tlsv12 };
|
||||||
|
|
||||||
|
//// loading certificate, key, pem
|
||||||
|
ctx.use_certificate_chain_file(argv[7]);
|
||||||
|
ctx.use_private_key_file(argv[8], boost::asio::ssl::context::file_format::pem);
|
||||||
|
ctx.use_tmp_dh_file(argv[9]);
|
||||||
|
|
||||||
|
// the acceptor receives incoming connections
|
||||||
|
tcp::acceptor acceptor{ ioc, {address, port} };
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
// This will receive the new connection
|
||||||
|
tcp::socket socket(ioc);
|
||||||
|
// Block until we get a connection
|
||||||
|
acceptor.accept(socket);
|
||||||
|
|
||||||
|
// Launch the session, transferring ownership of the socket
|
||||||
|
std::thread
|
||||||
|
{
|
||||||
|
&do_session,
|
||||||
|
std::move(socket),
|
||||||
|
std::ref(ctx),
|
||||||
|
std::ref(sessManager),
|
||||||
|
ip,
|
||||||
|
std::ref(user_n),
|
||||||
|
appPath,
|
||||||
|
appArgs,
|
||||||
|
portStream,
|
||||||
|
portHttp
|
||||||
|
}.detach();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (const std::exception& e)
|
||||||
|
{
|
||||||
|
LOG(std::stringstream() << "Error: " << e.what(), 12);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
logFile.close();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void do_session(tcp::socket sock, ssl::context& ctx, sessionManager& sessManager, std::string ip, uint64_t& user_n, std::string appPath, std::string appArgs, uint16_t portStream, uint16_t portHttp)
|
||||||
|
{
|
||||||
|
std::string user_ip = sock.remote_endpoint().address().to_string();
|
||||||
|
uint64_t user_id = std::chrono::system_clock::now().time_since_epoch().count();
|
||||||
|
user_id = hash6(user_ip + std::to_string(user_id));
|
||||||
|
uint64_t thisUserNum = ++user_n;
|
||||||
|
LOG((std::stringstream() << "user(" << thisUserNum << ") connected (ip: " << user_ip << "), (id: " << user_id << ")"), 7);
|
||||||
|
|
||||||
|
// Construct the websocket stream around the socket
|
||||||
|
websocket::stream<beast::ssl_stream<tcp::socket&>> ws(sock, ctx);
|
||||||
|
//websocket::stream<tcp::socket> ws{ std::move(sock) };
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Perform the SSL handshake
|
||||||
|
ws.next_layer().handshake(ssl::stream_base::server);
|
||||||
|
// Set a decorator to change the Server of the handshake
|
||||||
|
ws.set_option(websocket::stream_base::decorator(
|
||||||
|
[](websocket::response_type& res)
|
||||||
|
{
|
||||||
|
res.set(http::field::server,
|
||||||
|
std::string(BOOST_BEAST_VERSION_STRING) +
|
||||||
|
" websocket-server-sync-ssl");
|
||||||
|
}));
|
||||||
|
|
||||||
|
ws.accept();
|
||||||
|
}
|
||||||
|
catch (const std::exception& e)
|
||||||
|
{
|
||||||
|
LOG(std::stringstream() << "Error: " << e.what(), 12);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
catch (beast::system_error const& se)
|
||||||
|
{
|
||||||
|
LOG(std::stringstream() << "Error: " << se.what(), 12);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
nlohmann::json jsonData;
|
||||||
|
std::string message;
|
||||||
|
std::string content;
|
||||||
|
|
||||||
|
//session sessTmp;
|
||||||
|
session* sessCur = nullptr;
|
||||||
|
uint32_t sessId = 0;
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
tcp::socket socket(ioc);
|
try
|
||||||
acceptor.accept(socket);
|
|
||||||
|
|
||||||
std::thread([
|
|
||||||
sock = std::move(socket),
|
|
||||||
&sessManager,
|
|
||||||
&ip,
|
|
||||||
&user_n,
|
|
||||||
appPath,
|
|
||||||
appArgs,
|
|
||||||
portStream,
|
|
||||||
portHttp
|
|
||||||
//nodePath,
|
|
||||||
//nodeFilePath
|
|
||||||
]() mutable
|
|
||||||
{
|
{
|
||||||
std::string user_ip = sock.remote_endpoint().address().to_string();
|
// This buffer will hold the incoming message
|
||||||
uint64_t user_id = std::chrono::system_clock::now().time_since_epoch().count();
|
// buffer types https://www.boost.org/doc/libs/1_75_0/libs/beast/doc/html/beast/using_io/buffer_types.html
|
||||||
user_id = hash6(user_ip + std::to_string(user_id));
|
// check for the best one
|
||||||
|
// beast::multi_buffer buffer;
|
||||||
|
beast::flat_buffer buffer;
|
||||||
|
|
||||||
uint64_t thisUserNum = ++user_n;
|
// Read a message
|
||||||
LOG((std::stringstream() << "user(" << thisUserNum << ") connected (ip: " << user_ip << "), (id: " << user_id << ")"), 7);
|
ws.read(buffer);
|
||||||
|
|
||||||
// init websocket
|
// cout buffer to string
|
||||||
websocket::stream<tcp::socket> ws(std::move(const_cast<tcp::socket&>(sock)));
|
//std::cout << beast::buffers_to_string(buffer.data()) << std::endl;
|
||||||
ws.accept();
|
|
||||||
/*try
|
// validate errors
|
||||||
|
try
|
||||||
{
|
{
|
||||||
ws.accept();
|
jsonData = nlohmann::json::parse(beast::buffers_to_string(buffer.data()));
|
||||||
|
message = jsonData.at("message");
|
||||||
}
|
}
|
||||||
catch (std::exception const& e)
|
catch (nlohmann::json::parse_error& e)
|
||||||
{
|
{
|
||||||
std::cerr << "Error: " << e.what() << std::endl;
|
std::cout << "Parse error:" << e.what() << std::endl;
|
||||||
}*/
|
}
|
||||||
// Set a decorator to change the Server of the
|
catch (nlohmann::json::out_of_range& e)
|
||||||
/*ws.set_option(websocket::stream_base::decorator(
|
|
||||||
[](websocket::response_type& res)
|
|
||||||
{
|
|
||||||
res.set(http::field::server, std::string(BOOST_BEAST_VERSION_STRING) + " websocket-server-sync");
|
|
||||||
}));*/
|
|
||||||
|
|
||||||
nlohmann::json jsonData;
|
|
||||||
std::string message;
|
|
||||||
std::string content;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//session sessTmp;
|
|
||||||
session* sessCur = nullptr;
|
|
||||||
uint32_t sessId = 0;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
{
|
||||||
try
|
std::cout << "Out of range:" << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
catch (nlohmann::json::exception& e)
|
||||||
|
{
|
||||||
|
std::cout << "JSON exception: " << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
catch (std::exception& e)
|
||||||
|
{
|
||||||
|
std::cout << "Unknown exception" << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// validate message
|
||||||
|
if (message == "NEW_USER")
|
||||||
|
{
|
||||||
|
buffer.clear();
|
||||||
|
boost::beast::ostream(buffer) << "connected";
|
||||||
|
ws.write(buffer.data());
|
||||||
|
|
||||||
|
}
|
||||||
|
else if (message == "NEW_SESS")
|
||||||
|
{
|
||||||
|
buffer.clear();
|
||||||
|
|
||||||
|
if (sessManager.isLimitReached())
|
||||||
{
|
{
|
||||||
// This buffer will hold the incoming message
|
boost::beast::ostream(buffer)
|
||||||
// buffer types https://www.boost.org/doc/libs/1_75_0/libs/beast/doc/html/beast/using_io/buffer_types.html
|
<< std::string("{\"message\" : \"SESS_LIMIT\"}").c_str();
|
||||||
// check for the best one
|
ws.write(buffer.data());
|
||||||
// beast::multi_buffer buffer;
|
continue;
|
||||||
beast::flat_buffer buffer;
|
}
|
||||||
|
if (sessManager.existsOwnerId(user_id))
|
||||||
|
{
|
||||||
|
boost::beast::ostream(buffer)
|
||||||
|
<< std::string("{\"message\" : \"PERSON_LIMIT\"}").c_str();
|
||||||
|
ws.write(buffer.data());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Read a message
|
|
||||||
ws.read(buffer);
|
|
||||||
|
|
||||||
// cout buffer to string
|
// get session port
|
||||||
//std::cout << beast::buffers_to_string(buffer.data()) << std::endl;
|
//uint16_t sessMinPort = sessManager.getMinPort(portHttp);
|
||||||
|
uint16_t sessHttpPort = sessManager.getMinPort(portHttp);//(sessMinPort == 0) ? portHttp : sessMinPort + 1;
|
||||||
|
uint16_t sessStreamPort = portStream + (sessHttpPort - portHttp);
|
||||||
|
|
||||||
// validate errors
|
|
||||||
try
|
// creating process
|
||||||
|
PROCESS_INFORMATION appProcInfo;
|
||||||
|
|
||||||
|
if (!CreateProc(appPath, appPath + " " + appArgs + std::to_string(sessStreamPort), &appProcInfo))
|
||||||
|
{
|
||||||
|
LOG((std::stringstream() << "CreateProcess failed (" << GetLastError() << ")."), 12);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::beast::ostream(buffer)
|
||||||
|
<< std::string("{\"message\" : \"CREATING_SESSION\", \"content\" : \"true\"}").c_str();
|
||||||
|
ws.write(buffer.data());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// bind app process close listen
|
||||||
|
std::thread([appProcInfo]()
|
||||||
{
|
{
|
||||||
jsonData = nlohmann::json::parse(beast::buffers_to_string(buffer.data()));
|
WaitForSingleObject(appProcInfo.hProcess, INFINITE);
|
||||||
message = jsonData.at("message");
|
CloseHandle(appProcInfo.hProcess);
|
||||||
}
|
CloseHandle(appProcInfo.hThread);
|
||||||
catch (nlohmann::json::parse_error& e)
|
}).detach();
|
||||||
{
|
|
||||||
std::cout << "Parse error:" << e.what() << std::endl;
|
|
||||||
}
|
|
||||||
catch (nlohmann::json::out_of_range& e)
|
|
||||||
{
|
|
||||||
std::cout << "Out of range:" << e.what() << std::endl;
|
|
||||||
}
|
|
||||||
catch (nlohmann::json::exception& e)
|
|
||||||
{
|
|
||||||
std::cout << "JSON exception: " << e.what() << std::endl;
|
|
||||||
}
|
|
||||||
catch (std::exception& e)
|
|
||||||
{
|
|
||||||
std::cout << "Unknown exception" << e.what() << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
sessId = hash6(user_id);
|
||||||
|
|
||||||
|
// create session
|
||||||
|
sessManager.add(
|
||||||
// validate message
|
session(
|
||||||
if (message == "NEW_USER")
|
|
||||||
{
|
|
||||||
buffer.clear();
|
|
||||||
boost::beast::ostream(buffer) << "connected";
|
|
||||||
ws.write(buffer.data());
|
|
||||||
|
|
||||||
}
|
|
||||||
else if (message == "NEW_SESS")
|
|
||||||
{
|
|
||||||
buffer.clear();
|
|
||||||
|
|
||||||
if (sessManager.isLimitReached())
|
|
||||||
{
|
|
||||||
boost::beast::ostream(buffer)
|
|
||||||
<< std::string("{\"message\" : \"SESS_LIMIT\"}").c_str();
|
|
||||||
ws.write(buffer.data());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (sessManager.existsOwnerId(user_id))
|
|
||||||
{
|
|
||||||
boost::beast::ostream(buffer)
|
|
||||||
<< std::string("{\"message\" : \"PERSON_LIMIT\"}").c_str();
|
|
||||||
ws.write(buffer.data());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// get session port
|
|
||||||
uint16_t sessMinPort = sessManager.getMinPort();
|
|
||||||
if (!sessMinPort)
|
|
||||||
sessMinPort = portHttp;
|
|
||||||
uint16_t sessHttpPort = sessMinPort + 1;
|
|
||||||
uint16_t sessStreamPort = portStream + (sessHttpPort - portHttp);
|
|
||||||
|
|
||||||
|
|
||||||
// creating process
|
|
||||||
PROCESS_INFORMATION appProcInfo;
|
|
||||||
|
|
||||||
if (!CreateProc(appPath, appPath + " " + appArgs + std::to_string(sessStreamPort), &appProcInfo))
|
|
||||||
{
|
|
||||||
LOG((std::stringstream() << "CreateProcess failed (" << GetLastError() << ")."), 12);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
boost::beast::ostream(buffer)
|
|
||||||
<< std::string("{\"message\" : \"CREATING_SESSION\", \"content\" : \"true\"}").c_str();
|
|
||||||
ws.write(buffer.data());
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// bind app process close listen
|
|
||||||
std::thread([appProcInfo]()
|
|
||||||
{
|
|
||||||
WaitForSingleObject(appProcInfo.hProcess, INFINITE);
|
|
||||||
CloseHandle(appProcInfo.hProcess);
|
|
||||||
CloseHandle(appProcInfo.hThread);
|
|
||||||
}).detach();
|
|
||||||
|
|
||||||
sessId = hash6(user_id);
|
|
||||||
|
|
||||||
// create session
|
|
||||||
sessManager.add(
|
|
||||||
session(
|
|
||||||
sessId,
|
sessId,
|
||||||
ip + std::to_string(sessHttpPort),
|
ip + std::to_string(sessHttpPort),
|
||||||
user_ip,
|
user_ip,
|
||||||
sessHttpPort
|
sessHttpPort
|
||||||
));
|
));
|
||||||
|
|
||||||
if (sessManager.getById(sessId, &sessCur))
|
if (sessManager.getById(sessId, &sessCur))
|
||||||
{
|
|
||||||
// add user
|
|
||||||
sessCur->addUser(user(user_id));
|
|
||||||
|
|
||||||
// add process ids
|
|
||||||
sessCur->addProcId(GetProcessId(appProcInfo.hProcess));
|
|
||||||
//sessCur->addProcId(GetProcessId(nodeProcInfo.hProcess));
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG((std::stringstream() << "session ["
|
|
||||||
<< sessId << "] created, (httpPort: " << sessHttpPort << "), (streamPort: " << std::to_string(sessStreamPort) + ")"), 10);
|
|
||||||
|
|
||||||
// send respond
|
|
||||||
buffer.clear();
|
|
||||||
boost::beast::ostream(buffer)
|
|
||||||
<< (std::string("{\"message\" : \"SESS_CREATION\", \"id\" : \"")
|
|
||||||
+ std::to_string(sessId) + "\", \"port\" : \"" + std::to_string(sessHttpPort) + "\"}");
|
|
||||||
ws.write(buffer.data());
|
|
||||||
}
|
|
||||||
else if (message == "SESS_CONNECT")
|
|
||||||
{
|
{
|
||||||
// read session id
|
// add user
|
||||||
try
|
sessCur->addUser(user(user_id));
|
||||||
{
|
|
||||||
jsonData = nlohmann::json::parse(beast::buffers_to_string(buffer.data()));
|
|
||||||
content = jsonData.at("content");
|
|
||||||
}
|
|
||||||
catch (nlohmann::json::parse_error& e)
|
|
||||||
{
|
|
||||||
std::cout << "Parse error:" << e.what() << std::endl;
|
|
||||||
}
|
|
||||||
catch (nlohmann::json::out_of_range& e)
|
|
||||||
{
|
|
||||||
std::cout << "Out of range:" << e.what() << std::endl;
|
|
||||||
}
|
|
||||||
catch (nlohmann::json::exception& e)
|
|
||||||
{
|
|
||||||
std::cout << "JSON exception: " << e.what() << std::endl;
|
|
||||||
}
|
|
||||||
catch (std::exception& e)
|
|
||||||
{
|
|
||||||
std::cout << "Unknown exception" << e.what() << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
sessId = atoi(content.c_str());
|
// add process ids
|
||||||
// connect to session
|
sessCur->addProcId(GetProcessId(appProcInfo.hProcess));
|
||||||
if (sessManager.getById(sessId, &sessCur))
|
//sessCur->addProcId(GetProcessId(nodeProcInfo.hProcess));
|
||||||
{
|
|
||||||
if (sessCur->existsUserId(user_id))
|
|
||||||
{
|
|
||||||
LOG((std::stringstream() << "user exists: " << user_id), 7);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
sessCur->addUser(user(user_id));
|
|
||||||
LOG((std::stringstream() << "user(" << thisUserNum << ") connected to session (id: " << sessCur->getId()
|
|
||||||
<< "), (users: " << sessCur->getUsersCount() << ")"), 14);
|
|
||||||
|
|
||||||
buffer.clear();
|
|
||||||
boost::beast::ostream(buffer)
|
|
||||||
<< (std::string("{\"message\" : \"SESS_CONNECT\", \"content\" : \"") + std::to_string(sessCur->getPort()) + "\"}");
|
|
||||||
ws.write(buffer.data());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
LOG((std::stringstream() << "no session (id: " << content << ")"), 14);
|
|
||||||
buffer.clear();
|
|
||||||
boost::beast::ostream(buffer)
|
|
||||||
<< (std::string("{\"message\" : \"SESS_NOT_EXISTS\"}"));
|
|
||||||
ws.write(buffer.data());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LOG((std::stringstream() << "session ["
|
||||||
|
<< sessId << "] created, (httpPort: " << sessHttpPort << "), (streamPort: " << std::to_string(sessStreamPort) + ")"), 10);
|
||||||
|
|
||||||
|
// send respond
|
||||||
// Echo the message back
|
buffer.clear();
|
||||||
// ws.text(ws.got_text());
|
boost::beast::ostream(buffer)
|
||||||
// boost::beast::ostream(buffer) << "something";
|
<< (std::string("{\"message\" : \"SESS_CREATION\", \"id\" : \"")
|
||||||
// ws.write(buffer.data());
|
+ std::to_string(sessId) + "\", \"port\" : \"" + std::to_string(sessHttpPort) + "\"}");
|
||||||
|
ws.write(buffer.data());
|
||||||
}
|
}
|
||||||
catch (beast::system_error const& se)
|
else if (message == "SESS_CONNECT")
|
||||||
|
{
|
||||||
|
// read session id
|
||||||
|
try
|
||||||
{
|
{
|
||||||
LOG((std::stringstream() << "user(" << thisUserNum << ") disconnected"), 7);
|
jsonData = nlohmann::json::parse(beast::buffers_to_string(buffer.data()));
|
||||||
|
content = jsonData.at("content");
|
||||||
|
}
|
||||||
|
catch (nlohmann::json::parse_error& e)
|
||||||
|
{
|
||||||
|
std::cout << "Parse error:" << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
catch (nlohmann::json::out_of_range& e)
|
||||||
|
{
|
||||||
|
std::cout << "Out of range:" << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
catch (nlohmann::json::exception& e)
|
||||||
|
{
|
||||||
|
std::cout << "JSON exception: " << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
catch (std::exception& e)
|
||||||
|
{
|
||||||
|
std::cout << "Unknown exception" << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
if (sessId)
|
sessId = atoi(content.c_str());
|
||||||
|
// connect to session
|
||||||
|
if (sessManager.getById(sessId, &sessCur))
|
||||||
|
{
|
||||||
|
if (sessCur->existsUserId(user_id))
|
||||||
{
|
{
|
||||||
sessManager.getById(sessId, &sessCur);
|
LOG((std::stringstream() << "user exists: " << user_id), 7);
|
||||||
if (sessCur == nullptr)
|
continue;
|
||||||
{
|
|
||||||
std::cout << "nullptr session" << std::endl;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
sessCur->removeUserById(user_id);
|
|
||||||
|
|
||||||
if (!sessCur->getUsersCount())
|
|
||||||
{
|
|
||||||
LOG((std::stringstream() << "session [" << sessCur->getId() << "] destroyed"), 12);
|
|
||||||
std::vector<DWORD> procIds(sessCur->getProcIds());
|
|
||||||
for (auto& id : procIds)
|
|
||||||
EndProc(id);
|
|
||||||
sessManager.remove(sessId);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
sessCur->addUser(user(user_id));
|
||||||
|
LOG((std::stringstream() << "user(" << thisUserNum << ") connected to session (id: " << sessCur->getId()
|
||||||
|
<< "), (users: " << sessCur->getUsersCount() << ")"), 14);
|
||||||
|
|
||||||
|
buffer.clear();
|
||||||
|
boost::beast::ostream(buffer)
|
||||||
/* if (se.code() != websocket::error::closed)
|
<< (std::string("{\"message\" : \"SESS_CONNECT\", \"content\" : \"") + std::to_string(sessCur->getPort()) + "\"}");
|
||||||
{
|
ws.write(buffer.data());
|
||||||
std::cerr << "Error: " << se.code().message() << std::endl;
|
}
|
||||||
break;
|
else
|
||||||
}*/
|
{
|
||||||
|
LOG((std::stringstream() << "no session (id: " << content << ")"), 14);
|
||||||
|
buffer.clear();
|
||||||
|
boost::beast::ostream(buffer)
|
||||||
|
<< (std::string("{\"message\" : \"SESS_NOT_EXISTS\"}"));
|
||||||
|
ws.write(buffer.data());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).detach();
|
|
||||||
|
|
||||||
|
|
||||||
|
// Echo the message back
|
||||||
|
// ws.text(ws.got_text());
|
||||||
|
// boost::beast::ostream(buffer) << "something";
|
||||||
|
// ws.write(buffer.data());
|
||||||
|
}
|
||||||
|
catch (const std::exception& e)
|
||||||
|
{
|
||||||
|
LOG((std::stringstream() << "Error: " << e.what()), 14);
|
||||||
|
if (sessId)
|
||||||
|
{
|
||||||
|
sessManager.getById(sessId, &sessCur);
|
||||||
|
if (sessCur == nullptr)
|
||||||
|
{
|
||||||
|
std::cout << "nullptr session" << std::endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sessCur->removeUserById(user_id);
|
||||||
|
|
||||||
|
if (!sessCur->getUsersCount())
|
||||||
|
{
|
||||||
|
LOG((std::stringstream() << "session [" << sessCur->getId() << "] destroyed"), 12);
|
||||||
|
std::vector<DWORD> procIds(sessCur->getProcIds());
|
||||||
|
for (auto& id : procIds)
|
||||||
|
EndProc(id);
|
||||||
|
sessManager.remove(sessId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LOG((std::stringstream() << "user(" << thisUserNum << ") disconnected"), 7);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
catch (beast::system_error const& se)
|
||||||
|
{
|
||||||
|
LOG((std::stringstream() << "user(" << thisUserNum << ") fdisconnected"), 7);
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* if (se.code() != websocket::error::closed)
|
||||||
|
{
|
||||||
|
std::cerr << "Error: " << se.code().message() << std::endl;
|
||||||
|
break;
|
||||||
|
}*/
|
||||||
|
}
|
||||||
}
|
}
|
||||||
logFile.close();
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
@@ -148,12 +148,14 @@
|
|||||||
<SDLCheck>true</SDLCheck>
|
<SDLCheck>true</SDLCheck>
|
||||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
<ConformanceMode>true</ConformanceMode>
|
<ConformanceMode>true</ConformanceMode>
|
||||||
<AdditionalIncludeDirectories>C:\Libraries\json-develop\include;C:\Libraries\boost\include\boost_1_79_0;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
<AdditionalIncludeDirectories>C:\Libraries\openssl\include;C:\Libraries\json-develop\include;C:\Libraries\boost\include\boost_1_79_0;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
<SubSystem>Console</SubSystem>
|
<SubSystem>Console</SubSystem>
|
||||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
<AdditionalLibraryDirectories>C:\Libraries\openssl\apps\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||||
|
<AdditionalDependencies>C:\Libraries\openssl\libcrypto.lib;C:\Libraries\openssl\libssl.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||||
@@ -164,7 +166,7 @@
|
|||||||
<SDLCheck>true</SDLCheck>
|
<SDLCheck>true</SDLCheck>
|
||||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
<ConformanceMode>true</ConformanceMode>
|
<ConformanceMode>true</ConformanceMode>
|
||||||
<AdditionalIncludeDirectories>C:\Libraries\json-develop\include;C:\Libraries\boost\include\boost_1_79_0;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
<AdditionalIncludeDirectories>C:\Libraries\json-develop\include;C:\Libraries\openssl\include;C:\Libraries\boost\include\boost_1_79_0;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
@@ -172,6 +174,7 @@
|
|||||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||||
<OptimizeReferences>true</OptimizeReferences>
|
<OptimizeReferences>true</OptimizeReferences>
|
||||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
<AdditionalDependencies>C:\Libraries\openssl\libcrypto.lib;C:\Libraries\openssl\libssl.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='build|x64'">
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='build|x64'">
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
#pragma once
|
#pragma once
|
||||||
class sessionManager
|
class sessionManager
|
||||||
{
|
{
|
||||||
@@ -58,16 +59,31 @@ public:
|
|||||||
return limit == sessions.size();
|
return limit == sessions.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t getMinPort()
|
uint16_t getMinPort(uint16_t desired)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
// working not properly
|
||||||
if (!sessions.size())
|
if (!sessions.size())
|
||||||
return 0;
|
return desired;
|
||||||
std::vector<uint16_t> ports;
|
std::vector<uint16_t> ports;
|
||||||
for (auto& s : sessions)
|
for (auto& s : sessions)
|
||||||
ports.push_back(s.getPort());
|
ports.push_back(s.getPort());
|
||||||
|
|
||||||
std::sort(ports.begin(), ports.end(), [](uint16_t a, uint16_t b) { return a > b; });
|
std::cout << "amount: " << ports.size() << std::endl;
|
||||||
return *(ports.begin());
|
std::cout << "ports: " << std::endl;
|
||||||
|
for (auto& s : ports)
|
||||||
|
std::cout << s << ", ";
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
std::sort(ports.begin(), ports.end(), [](uint16_t a, uint16_t b) { return a < b; });
|
||||||
|
uint16_t port = desired;
|
||||||
|
for (auto p : ports)
|
||||||
|
{
|
||||||
|
if (desired != p)
|
||||||
|
return desired;
|
||||||
|
++desired;
|
||||||
|
}
|
||||||
|
return ports.back() + 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user