From 7aadd0326bfe994d741b35a5979dd88d6b5e6c57 Mon Sep 17 00:00:00 2001 From: Wouter van Kleunen Date: Sun, 20 Dec 2020 12:05:55 +0100 Subject: [PATCH] Allow configuration of broker log level through command line --- .github/workflows/gha.yml | 45 +++++-- CMakeLists.txt | 15 ++- example/CMakeLists.txt | 2 + example/broker.cpp | 165 ++++++++++++++++++++++++-- test/system/combi_test.hpp | 5 +- test/system/st_underlying_timeout.cpp | 9 +- test/system/test_ctx_init.hpp | 26 ++-- test/system/test_server_no_tls.hpp | 4 +- test/system/test_server_no_tls_ws.hpp | 7 +- test/system/test_server_tls.hpp | 15 ++- test/system/test_server_tls_ws.hpp | 14 ++- 11 files changed, 253 insertions(+), 54 deletions(-) diff --git a/.github/workflows/gha.yml b/.github/workflows/gha.yml index 5da92e2da..3cbc71390 100644 --- a/.github/workflows/gha.yml +++ b/.github/workflows/gha.yml @@ -5,7 +5,7 @@ on: types: [opened, synchronize] push: branches: - - '*' + - master tags: - '*' @@ -101,21 +101,52 @@ jobs: uses: actions/checkout@v2 - name: Configure env: - S_CFLAGS: - S_CXXFLAGS: -DBOOST_ASIO_NO_DEPRECATED -DBOOST_MULTI_INDEX_DISABLE_SERIALIZATION -DBOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING - S_LDFLAGS: + S_CFLAGS: + S_CXXFLAGS: -DBOOST_ASIO_NO_DEPRECATED -DBOOST_MULTI_INDEX_DISABLE_SERIALIZATION + S_LDFLAGS: -static -static-libgcc -static-libstdc++ run: | export CFLAGS=${S_CFLAGS} && export CXXFLAGS=${S_CXXFLAGS} && export LDFLAGS=${S_LDFLAGS} - FLAGS="-DCMAKE_CXX_COMPILER=clang++ -DMQTT_USE_STATIC_BOOST=ON -DMQTT_TEST_1=OFF -DMQTT_TEST_2=ONFF -DMQTT_TEST_3=OFF -DMQTT_TEST_4=OFF -DMQTT_TEST_5=OFF -DMQTT_TEST_6=OFF -DMQTT_TEST_7=OFF -DMQTT_BUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=Release -DMQTT_USE_WS=ON -DMQTT_USE_STR_CHECK=OFF -DMQTT_USE_LOG=ON -DMQTT_STD_ANY=OFF -DMQTT_STD_OPTIONAL=OFF -DMQTT_STD_VARIANT=OFF -DMQTT_STD_STRING_VIEW=OFF -DMQTT_STD_SHARED_PTR_ARRAY=OFF" + FLAGS="-DCMAKE_CXX_COMPILER=clang++ -DMQTT_USE_STATIC_BOOST=ON -DMQTT_USE_STATIC_OPENSSL=ON -DMQTT_TEST_1=OFF -DMQTT_TEST_2=OFF -DMQTT_TEST_3=OFF -DMQTT_TEST_4=OFF -DMQTT_TEST_5=OFF -DMQTT_TEST_6=OFF -DMQTT_TEST_7=OFF -DMQTT_BUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=Release -DMQTT_USE_WS=ON -DMQTT_USE_TLS=ON -DMQTT_USE_STR_CHECK=OFF -DMQTT_USE_LOG=ON -DMQTT_STD_ANY=OFF -DMQTT_STD_OPTIONAL=OFF -DMQTT_STD_VARIANT=OFF -DMQTT_STD_STRING_VIEW=OFF -DMQTT_STD_SHARED_PTR_ARRAY=OFF" BOOST_ROOT=$BOOST_ROOT_1_72_0 cmake -S ${{ github.workspace }} -B ${{ runner.temp }} ${FLAGS} -DCMAKE_C_FLAGS="${CFLAGS}" -DCMAKE_CXX_FLAGS="${CXXFLAGS}" -DCMAKE_EXE_LINKER_FLAGS="${LDFLAGS}" - name: Check Header Dependencies run: | cmake --build ${{ runner.temp }} --parallel $(nproc) --clean-first --target check_deps - name: Compile run: | - VERBOSE=1 cmake --build ${{ runner.temp }} --clean-first + VERBOSE=1 cmake --build ${{ runner.temp }} --clean-first --target broker + sudo apt-get install upx-ucl + upx-ucl --ultra-brute ${{ runner.temp }}/example/broker - name: 'Upload Broker' uses: actions/upload-artifact@v2 with: - name: broker + name: linux-static-broker path: ${{ runner.temp }}/example/broker + + macos-release: + runs-on: macos-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Install Dependencies + run: | + brew install boost openssl + - name: Configure + env: + OPENSSL_ROOT_DIR: /usr/local/opt/openssl + run: | + cmake -S ${{ github.workspace }} -B ${{ runner.temp }} ${{ matrix.pattern }} -DCMAKE_CXX_COMPILER=clang++ -DMQTT_USE_STATIC_BOOST=ON -DMQTT_USE_STATIC_OPENSSL=ON -DMQTT_TEST_1=OFF -DMQTT_TEST_2=OFF -DMQTT_TEST_3=OFF -DMQTT_TEST_4=OFF -DMQTT_TEST_5=OFF -DMQTT_TEST_6=OFF -DMQTT_TEST_7=OFF -DMQTT_BUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=Release -DMQTT_USE_WS=ON -DMQTT_USE_TLS=ON -DMQTT_USE_STR_CHECK=OFF -DMQTT_USE_LOG=ON -DMQTT_STD_ANY=OFF -DMQTT_STD_OPTIONAL=OFF -DMQTT_STD_VARIANT=OFF -DMQTT_STD_STRING_VIEW=OFF -DMQTT_STD_SHARED_PTR_ARRAY=OFF + - name: Check Header Dependencies + run: | + cmake --build ${{ runner.temp }} --parallel $(sysctl -n hw.ncpu) --clean-first --target check_deps + - name: Compile + env: + CXXFLAGS: -Werror -pedantic -Wall -Wextra -Wno-ignored-qualifiers + LDFLAGS: -Wconversion + run: | + cmake --build ${{ runner.temp }} --parallel $(sysctl -n hw.ncpu) --clean-first --target broker + - name: 'Upload Broker' + uses: actions/upload-artifact@v2 + with: + name: macos-broker + path: ${{ runner.temp }}/example/broker + diff --git a/CMakeLists.txt b/CMakeLists.txt index 72f2c1a3d..cd4ce3c34 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -123,16 +123,25 @@ FIND_PACKAGE (Threads REQUIRED) IF (MQTT_USE_LOG) MESSAGE (STATUS "Logging enabled") - FIND_PACKAGE (Boost 1.67.0 REQUIRED COMPONENTS system date_time log filesystem thread) + FIND_PACKAGE (Boost 1.67.0 REQUIRED COMPONENTS system date_time log filesystem thread program_options) ELSE () MESSAGE (STATUS "Logging disabled") - FIND_PACKAGE (Boost 1.67.0 REQUIRED COMPONENTS system date_time) + FIND_PACKAGE (Boost 1.67.0 REQUIRED COMPONENTS system date_time program_options) ENDIF () IF (MQTT_USE_TLS) FIND_PACKAGE (OpenSSL REQUIRED) IF (MQTT_USE_STATIC_OPENSSL) - FIND_PACKAGE (ZLIB REQUIRED) + IF (UNIX AND NOT APPLE) + # Try to find static zlib library also + set(_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES}) + SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a") + FIND_PACKAGE (ZLIB REQUIRED) + set(CMAKE_FIND_LIBRARY_SUFFIXES ${_CMAKE_FIND_LIBRARY_SUFFIXES}) + unset(_CMAKE_FIND_LIBRARY_SUFFIXES) + ELSE() + FIND_PACKAGE (ZLIB REQUIRED) + ENDIF () ENDIF () ENDIF () diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index e0ddc6509..a7d27e4f4 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -54,6 +54,8 @@ FOREACH (source_file ${exec_PROGRAMS}) TARGET_COMPILE_DEFINITIONS (${source_file_we} PUBLIC $,,BOOST_LOG_DYN_LINK>) TARGET_LINK_LIBRARIES (${source_file_we} Boost::log) ENDIF () + TARGET_COMPILE_DEFINITIONS (${source_file_we} PUBLIC $,,BOOST_PROGRAM_OPTIONS_DYN_LINK>) + TARGET_LINK_LIBRARIES (${source_file_we} Boost::program_options) ENDFOREACH () IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") diff --git a/example/broker.cpp b/example/broker.cpp index aee45fce9..5623bf1d0 100644 --- a/example/broker.cpp +++ b/example/broker.cpp @@ -5,16 +5,165 @@ // http://www.boost.org/LICENSE_1_0.txt) #include - #include "../test/system/test_server_no_tls.hpp" +#include "../test/system/test_server_no_tls_ws.hpp" +#include "../test/system/test_server_tls.hpp" +#include "../test/system/test_server_tls_ws.hpp" #include - #include +#include + +#include + +#if defined(MQTT_USE_TLS) +boost::asio::ssl::context init_ctx(boost::program_options::variables_map const& vm) +{ + boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12); + ctx.set_options( + boost::asio::ssl::context::default_workarounds | + boost::asio::ssl::context::single_dh_use); + ctx.use_certificate_file(vm["tls.certificate"].as(), boost::asio::ssl::context::pem); + ctx.use_private_key_file(vm["tls.private_key"].as(), boost::asio::ssl::context::pem); + return ctx; +} +#endif // defined(MQTT_USE_TLS) + +void run_broker(boost::program_options::variables_map const& vm) +{ + try { + boost::asio::io_context ioc; + MQTT_NS::broker::broker_t b(ioc); + + std::unique_ptr s; + if(vm["tcp.enable"].as()) { + s = std::make_unique(ioc, b, vm["tcp.port"].as()); + } + +#if defined(MQTT_USE_WS) + std::unique_ptr s_ws; + if(vm["ws.enable"].as()) { + s_ws = std::make_unique(ioc, b, vm["ws.port"].as()); + } +#endif // defined(MQTT_USE_WS) + +#if defined(MQTT_USE_TLS) + std::unique_ptr s_tls; + if (vm["tls.enable"].as()) { + s_tls = std::make_unique(ioc, init_ctx(vm), b, vm["tls.port"].as()); + } +#endif // defined(MQTT_USE_TLS) + +#if defined(MQTT_USE_TLS) && defined(MQTT_USE_WS) + std::unique_ptr s_tls_ws; + if (vm["tlsws.enable"].as()) { + s_tls_ws = std::make_unique(ioc, init_ctx(vm), b, vm["tlsws.port"].as()); + } +#endif // defined(MQTT_USE_TLS) && defined(MQTT_USE_WS) + + + ioc.run(); + } catch(std::exception &e) { + MQTT_LOG("mqtt_broker", error) << e.what(); + } +} + +int main(int argc, char **argv) { + try { + boost::program_options::options_description desc; + + boost::program_options::options_description general_desc("General options"); + general_desc.add_options() + ("help", "produce help message") + ("cfg", boost::program_options::value()->default_value(""), "Load configuration file") +#if defined(MQTT_USE_LOG) + ("verbose", boost::program_options::value()->default_value(1), "set verbose level, possible values:\n 0 - Fatal\n 1 - Error\n 2 - Warning\n 3 - Info\n 4 - Debug\n 5 - Trace") +#endif // defined(MQTT_USE_LOG) + ; + + boost::program_options::options_description notls_desc("TCP Server options"); + notls_desc.add_options() + ("tcp.port", boost::program_options::value()->default_value(broker_notls_port), "default port (TCP)") + ("tcp.enable", boost::program_options::value()->default_value(1), "0/1") + ; + + desc.add(general_desc).add(notls_desc); + +#if defined(MQTT_USE_WS) + boost::program_options::options_description ws_desc("TCP Websocket Server options"); + ws_desc.add_options() + ("ws.port", boost::program_options::value()->default_value(broker_notls_ws_port), "default port (TCP)") + ("ws.enable", boost::program_options::value()->default_value(0), "0/1") + ; + + desc.add(ws_desc); +#endif // defined(MQTT_USE_WS) + +#if defined(MQTT_USE_TLS) + boost::program_options::options_description tls_desc("TLS Server options"); + tls_desc.add_options() + ("tls.port", boost::program_options::value()->default_value(broker_tls_port), "default port (TLS)") + ("tls.enable", boost::program_options::value()->default_value(0), "0/1") + ("tls.certificate", boost::program_options::value()->default_value("server.crt.pem"), "Certificate file") + ("tls.private_key", boost::program_options::value()->default_value("server.key.pem"), "Private key file") + ; + desc.add(tls_desc); +#endif // defined(MQTT_USE_TLS) + +#if defined(MQTT_USE_WS) && defined(MQTT_USE_TLS) + boost::program_options::options_description tlsws_desc("TLS Websocket Server options"); + tlsws_desc.add_options() + ("tlsws.port", boost::program_options::value()->default_value(broker_tls_ws_port), "default port (TLS)") + ("tlsws.enable", boost::program_options::value()->default_value(0), "0/1") + ; + desc.add(tlsws_desc); +#endif // defined(MQTT_USE_TLS) && defined(MQTT_USE_WS) + + boost::program_options::variables_map vm; + boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm); + if (!vm["cfg"].as().empty()) { + std::ifstream input(vm["cfg"].as()); + if (input.good()) { + boost::program_options::store(boost::program_options::parse_config_file(input, desc), vm); + } else + { + throw std::runtime_error("Failed to open specified configuration file"); + } + } + + boost::program_options::notify(vm); + + if (vm.count("help")) { + std::cout << desc << std::endl; + return 1; + } + +#if defined(MQTT_USE_LOG) + switch (vm["verbose"].as()) { + case 5: + MQTT_NS::setup_log(mqtt::severity_level::trace); + break; + case 4: + MQTT_NS::setup_log(mqtt::severity_level::debug); + break; + case 3: + MQTT_NS::setup_log(mqtt::severity_level::info); + break; + case 2: + MQTT_NS::setup_log(mqtt::severity_level::warning); + break; + default: + MQTT_NS::setup_log(mqtt::severity_level::error); + break; + case 0: + MQTT_NS::setup_log(mqtt::severity_level::fatal); + break; + } +#else + MQTT_NS::setup_log(); +#endif -int main() { - MQTT_NS::setup_log(); - boost::asio::io_context ioc; - MQTT_NS::broker::broker_t b(ioc); - test_server_no_tls s(ioc, b); - ioc.run(); + run_broker(vm); + } catch(std::exception &e) { + std::cerr << e.what() << std::endl; + } } diff --git a/test/system/combi_test.hpp b/test/system/combi_test.hpp index bd615d6d0..97d247e9a 100644 --- a/test/system/combi_test.hpp +++ b/test/system/combi_test.hpp @@ -16,6 +16,7 @@ #include "test_server_no_tls.hpp" #if defined(MQTT_USE_TLS) #include "test_server_tls.hpp" +#include "test_ctx_init.hpp" #endif // defined(MQTT_USE_TLS) #if defined(MQTT_USE_WS) @@ -91,7 +92,7 @@ inline void do_tls_test( auto f = p.get_future(); std::thread th( [&] { - s.emplace(iocb, b); + s.emplace(iocb, test_ctx_init(), b); p.set_value(); iocb.run(); } @@ -193,7 +194,7 @@ inline void do_tls_ws_test( auto f = p.get_future(); std::thread th( [&] { - s.emplace(iocb, b); + s.emplace(iocb, test_ctx_init(), b); p.set_value(); iocb.run(); } diff --git a/test/system/st_underlying_timeout.cpp b/test/system/st_underlying_timeout.cpp index c5bcd34e2..ff95d2935 100644 --- a/test/system/st_underlying_timeout.cpp +++ b/test/system/st_underlying_timeout.cpp @@ -87,12 +87,11 @@ BOOST_AUTO_TEST_CASE( connect_tls_ws_ashs ) { as::io_context ioc; // server - ctx_init ci; MQTT_NS::server_tls_ws<> server( as::ip::tcp::endpoint( as::ip::tcp::v4(), broker_tls_ws_port), - std::move(ci.ctx), + test_ctx_init(), ioc); server.set_accept_handler( @@ -153,12 +152,11 @@ BOOST_AUTO_TEST_CASE( connect_tls_ws_upg ) { as::io_context ioc; // server - ctx_init ci; MQTT_NS::server_tls_ws<> server( as::ip::tcp::endpoint( as::ip::tcp::v4(), broker_tls_ws_port), - std::move(ci.ctx), + test_ctx_init(), ioc); server.set_accept_handler( @@ -236,12 +234,11 @@ BOOST_AUTO_TEST_CASE( connect_tls_ashs ) { as::io_context ioc; // server - ctx_init ci; MQTT_NS::server_tls<> server( as::ip::tcp::endpoint( as::ip::tcp::v4(), broker_tls_port), - std::move(ci.ctx), + test_ctx_init(), ioc); server.set_accept_handler( diff --git a/test/system/test_ctx_init.hpp b/test/system/test_ctx_init.hpp index be4a3c6c4..32747ae45 100644 --- a/test/system/test_ctx_init.hpp +++ b/test/system/test_ctx_init.hpp @@ -14,19 +14,19 @@ #include "test_settings.hpp" -struct ctx_init { - ctx_init() : ctx(boost::asio::ssl::context::tlsv12) { - ctx.set_options( - boost::asio::ssl::context::default_workarounds | - boost::asio::ssl::context::single_dh_use); - std::string path = boost::unit_test::framework::master_test_suite().argv[0]; - std::size_t pos = path.find_last_of("/\\"); - std::string base = (pos == std::string::npos) ? "" : path.substr(0, pos + 1); - ctx.use_certificate_file(base + "server.crt.pem", boost::asio::ssl::context::pem); - ctx.use_private_key_file(base + "server.key.pem", boost::asio::ssl::context::pem); - } - boost::asio::ssl::context ctx; -}; +static inline boost::asio::ssl::context test_ctx_init() { + boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12); + ctx.set_options( + boost::asio::ssl::context::default_workarounds | + boost::asio::ssl::context::single_dh_use); + std::string path = boost::unit_test::framework::master_test_suite().argv[0]; + std::size_t pos = path.find_last_of("/\\"); + std::string base = (pos == std::string::npos) ? "" : path.substr(0, pos + 1); + ctx.use_certificate_file(base + "server.crt.pem", boost::asio::ssl::context::pem); + ctx.use_private_key_file(base + "server.key.pem", boost::asio::ssl::context::pem); + return ctx; +} + #endif // defined(MQTT_USE_TLS) diff --git a/test/system/test_server_no_tls.hpp b/test/system/test_server_no_tls.hpp index e692dcef9..2b7a1ca08 100644 --- a/test/system/test_server_no_tls.hpp +++ b/test/system/test_server_no_tls.hpp @@ -21,10 +21,10 @@ using con_sp_t = std::shared_ptr; class test_server_no_tls { public: - test_server_no_tls(as::io_context& ioc, MQTT_NS::broker::broker_t& b) + test_server_no_tls(as::io_context& ioc, MQTT_NS::broker::broker_t& b, uint16_t port = broker_notls_port) : server_( as::ip::tcp::endpoint( - as::ip::tcp::v4(), broker_notls_port + as::ip::tcp::v4(), port ), ioc, ioc, diff --git a/test/system/test_server_no_tls_ws.hpp b/test/system/test_server_no_tls_ws.hpp index d377f90ee..d2e01ddaa 100644 --- a/test/system/test_server_no_tls_ws.hpp +++ b/test/system/test_server_no_tls_ws.hpp @@ -7,6 +7,7 @@ #if !defined(MQTT_TEST_SERVER_NO_TLS_WS_HPP) #define MQTT_TEST_SERVER_NO_TLS_WS_HPP +#if defined(MQTT_USE_WS) #include #include @@ -18,10 +19,10 @@ namespace as = boost::asio; class test_server_no_tls_ws { public: - test_server_no_tls_ws(as::io_context& ioc, MQTT_NS::broker::broker_t& b) + test_server_no_tls_ws(as::io_context& ioc, MQTT_NS::broker::broker_t& b, uint16_t port = broker_notls_ws_port) : server_( as::ip::tcp::endpoint( - as::ip::tcp::v4(), broker_notls_ws_port + as::ip::tcp::v4(), port ), ioc, ioc, @@ -56,4 +57,6 @@ class test_server_no_tls_ws { MQTT_NS::broker::broker_t& b_; }; +#endif // defined(MQTT_USE_WS) + #endif // MQTT_TEST_SERVER_NO_TLS_WS_HPP diff --git a/test/system/test_server_tls.hpp b/test/system/test_server_tls.hpp index 88fc06650..dd74d8e5b 100644 --- a/test/system/test_server_tls.hpp +++ b/test/system/test_server_tls.hpp @@ -7,6 +7,7 @@ #if !defined(MQTT_TEST_SERVER_TLS_HPP) #define MQTT_TEST_SERVER_TLS_HPP +#if defined(MQTT_USE_TLS) #include #include @@ -14,22 +15,22 @@ #include #include #include +#include +#include #include -#include "test_settings.hpp" -#include "test_ctx_init.hpp" namespace mi = boost::multi_index; namespace as = boost::asio; -class test_server_tls : ctx_init { +class test_server_tls { public: - test_server_tls(as::io_context& ioc, MQTT_NS::broker::broker_t& b) + test_server_tls(as::io_context& ioc, boost::asio::ssl::context&& ctx, MQTT_NS::broker::broker_t& b, uint16_t port = broker_tls_port) : server_( as::ip::tcp::endpoint( - as::ip::tcp::v4(), broker_tls_port + as::ip::tcp::v4(), port ), - std::move(ctx), + std::forward(ctx), ioc, ioc, [](auto& acceptor) { @@ -63,4 +64,6 @@ class test_server_tls : ctx_init { MQTT_NS::broker::broker_t& b_; }; +#endif + #endif // MQTT_TEST_SERVER_TLS_HPP diff --git a/test/system/test_server_tls_ws.hpp b/test/system/test_server_tls_ws.hpp index 3ece9eceb..5de275e59 100644 --- a/test/system/test_server_tls_ws.hpp +++ b/test/system/test_server_tls_ws.hpp @@ -7,6 +7,8 @@ #if !defined(MQTT_TEST_SERVER_TLS_WS_HPP) #define MQTT_TEST_SERVER_TLS_WS_HPP +#if defined(MQTT_USE_TLS) +#if defined(MQTT_USE_WS) #include #include @@ -17,20 +19,19 @@ #include #include "test_settings.hpp" -#include "test_ctx_init.hpp" namespace mi = boost::multi_index; namespace as = boost::asio; -class test_server_tls_ws : ctx_init { +class test_server_tls_ws { public: - test_server_tls_ws(as::io_context& ioc, MQTT_NS::broker::broker_t& b) + test_server_tls_ws(as::io_context& ioc, boost::asio::ssl::context&& ctx, MQTT_NS::broker::broker_t& b, uint16_t port = broker_tls_ws_port) : server_( as::ip::tcp::endpoint( as::ip::tcp::v4(), - broker_tls_ws_port + port ), - std::move(ctx), + std::forward(ctx), ioc, ioc, [](auto& acceptor) { @@ -64,4 +65,7 @@ class test_server_tls_ws : ctx_init { MQTT_NS::broker::broker_t& b_; }; +#endif // defined(MQTT_USE_WS) +#endif // defined(MQTT_USE_TLS) + #endif // MQTT_TEST_SERVER_TLS_WS_HPP