From b9067189d54ae33811a405acc8ce302e5476e52d Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Mon, 19 Dec 2016 09:47:19 +0800 Subject: [PATCH] ssl suport added --- CMakeLists.txt | 10 +++++---- main.cpp | 50 +++++++++++++++++++++++++++++++++++++++++- src/CmdLineOptions.cpp | 4 ++++ 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 255a441..f9b7040 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,8 +53,8 @@ set_property(TARGET common PROPERTY IMPORTED_LOCATION ${MONERO_LIBS_DIR}/libcomm add_library(blocks STATIC IMPORTED) set_property(TARGET blocks PROPERTY IMPORTED_LOCATION ${MONERO_LIBS_DIR}/libblocks.a) -add_library(crypto STATIC IMPORTED) -set_property(TARGET crypto +add_library(cryptoxmr STATIC IMPORTED) +set_property(TARGET cryptoxmr PROPERTY IMPORTED_LOCATION ${MONERO_LIBS_DIR}/libcrypto.a) add_library(cryptonote_core STATIC IMPORTED) @@ -163,7 +163,7 @@ target_link_libraries(${PROJECT_NAME} cryptonote_core cryptonote_protocol blockchain_db - crypto + cryptoxmr blocks lmdb ringct @@ -173,4 +173,6 @@ target_link_libraries(${PROJECT_NAME} unbound unwind curl - dl) + dl + crypto + ssl) diff --git a/main.cpp b/main.cpp index 9741b09..26bf18a 100644 --- a/main.cpp +++ b/main.cpp @@ -1,3 +1,4 @@ +#define CROW_ENABLE_SSL #include "ext/crow/crow.h" @@ -41,11 +42,47 @@ int main(int ac, const char* av[]) { auto bc_path_opt = opts.get_option("bc-path"); auto custom_db_path_opt = opts.get_option("custom-db-path"); auto deamon_url_opt = opts.get_option("deamon-url"); + auto ssl_crt_file_opt = opts.get_option("ssl-crt-file"); + auto ssl_key_file_opt = opts.get_option("ssl-key-file"); //cast port number in string to uint16 uint16_t app_port = boost::lexical_cast(*port_opt); + + bool use_ssl {false}; + + string ssl_crt_file; + string ssl_key_file; + + // check if ssl enabled and files exist + + if (ssl_crt_file_opt and ssl_key_file_opt) + { + if (!boost::filesystem::exists(boost::filesystem::path(*ssl_crt_file_opt))) + { + cerr << "ssl_crt_file path: " << *ssl_crt_file_opt + << "does not exist!" << endl; + + return EXIT_FAILURE; + } + + if (!boost::filesystem::exists(boost::filesystem::path(*ssl_key_file_opt))) + { + cerr << "ssl_key_file path: " << *ssl_key_file_opt + << "does not exist!" << endl; + + return EXIT_FAILURE; + } + + ssl_crt_file = *ssl_crt_file_opt; + ssl_key_file = *ssl_key_file_opt; + + use_ssl = true; + } + + + // get blockchain path path blockchain_path; @@ -271,7 +308,18 @@ int main(int ac, const char* av[]) { }); // run the crow http server - app.port(app_port).multithreaded().run(); + + if (use_ssl) + { + cout << "Staring in ssl mode" << endl; + app.port(app_port).ssl_file(ssl_crt_file, ssl_key_file).multithreaded().run(); + } + else + { + cout << "Staring in non-ssl mode" << endl; + app.port(app_port).multithreaded().run(); + } + return EXIT_SUCCESS; } diff --git a/src/CmdLineOptions.cpp b/src/CmdLineOptions.cpp index 9252257..5959e77 100644 --- a/src/CmdLineOptions.cpp +++ b/src/CmdLineOptions.cpp @@ -31,6 +31,10 @@ namespace xmreg "default port") ("bc-path,b", value(), "path to lmdb blockchain") + ("ssl-crt-file", value(), + "A path to crt file for ssl (https) functionality") + ("ssl-key-file", value(), + "A path to key file for ssl (https) functionality") ("custom-db-path,c", value(), "path to the custom lmdb database used for searching things") ("deamon-url,d", value()->default_value("http:://127.0.0.1:18081"),