Skip to content

Commit

Permalink
Use OpenSSL to add support for SSL communication with the server. Scr…
Browse files Browse the repository at this point in the history
…ipt add debug mode support. (#273)

* Modification item:
(1)Add OpenSSL and debug options to the ‘build.sh’ script.
(2)Add the ‘ENABLE_OPENSSL’ macro to cmakelist.txt.
(3)Link the ‘libevent_openssl.a’ static library to support OpenSSL.

* Modification item:
(1)Link static libraries on demand.
(2)Add OpenSSL support and use the macro ‘ENABLE_OPENSSL’ to control OpenSSL compilation blocks.
(3)Inline member defaults. The default value of the inline member is intuitive and error free, and the redundant structure list can be removed, which is also recommended.
(4)Add ignore for vscode configuration in .gitignore.

* Add debugging information in debug mode.

* Modification item:
(1)Add C compiler debugging options.
(2)Remove the compiler debugging option from the child test cmake, which will result in inconsistent behavior between the upper and lower cmakes.
(3)Move the SSL initialization context into the constructor so that it is constructed only once and the memory leak in the last commit code is fixed.
(4)SSL handle is managed to bufferevent without memory management. Forced release will result in double-free.

* Modification item:
(1)Extract SSL context initialization into a function.
(2)Customize std::unique_ptr to manage SSL content and strengthen memory leak management.
(3)Add the default certificate configuration path.
(4)Add environment variables to dynamically modify the certificate configuration path.
(5)Enhance friendly output prompt.
(6)The communication function with the server is verified by self signed certificate.
(7)Asan and lsan tests have been passed.

* Modification item:
(1)Add OpenSSL library and compile script.
(2)Remove the system dynamic library connection, and use the project OpenSSL static library link instead.
(3)Fix some errors in CMakeList.txt.

* Modification item:
    (1)Add OpenSSL library and compile script.
    (2)Remove the system dynamic library connection, and use the project OpenSSL static library link instead.
    (3)Fix some errors in CMakeList.txt.

* Modification item:
(1)Remove the OpenSSL option that controls whether or not it is required.
(2)Remove the ENABLE_OPENSSL macro. OpenSSL is supported by default.
(3)Add API to control whether OpenSSL support is enabled.
(4)Add the ReadProperties utility function.
(5)Add certificate related configuration in the properties configuration file.
(6)Add friendly output prompts.

* By mistake, I wrote a letter ‘t’ short.

* Modification item:
(1)Format the code with the 'format.sh' script.
(2)SSL is turned off by default.

* change openssl static library install path.

* using built-in openssl to compile libevent.

* Modification item:
(1)Moving/Returning temporary object prevents copy elision, which may cause RVO and NRVO are invalid technologies.
(2)Libevent only verifies whether the OpenSSL dynamic library has 'SSL_new' function as the basis for the existence of OpenSSL.
(3)Add a dependent static library to MRI.

* remove comments for code format.

* Fix the missing packing problem of static library packing script under Mac OS. For example, there is buffer. o in libcrypto. a, and buffer. o in libevent_core. a, but the symbols of the two are complementary. The method of using 'ar x'  and 'ar cru' will result in the loss of symbol in one of the above two buffer. o.

* chors(style): format code style for eventloop.
  • Loading branch information
WoodsCumming authored Mar 24, 2020
1 parent b29bf5b commit 81f36ec
Show file tree
Hide file tree
Showing 39 changed files with 547 additions and 135 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ build
libs/signature/lib
tmp_*
Testing
.vscode
39 changes: 30 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ set(CMAKE_VERBOSE_MAKEFILE 1)
option(BUILD_ROCKETMQ_STATIC "build rocketmq-client static library" ON)
option(BUILD_ROCKETMQ_SHARED "build rocketmq-client shared library" ON)

option(OPENSSL_USE_STATIC_LIBS "only find openssl static libs" ON) # only find static libs
if (WIN32)
find_package(OpenSSL 1.1.1 REQUIRED COMPONENTS)
if (OPENSSL_FOUND)
include_directories(${OPENSSL_INCLUDE_DIR})
message(STATUS "** OpenSSL Include dir: ${OPENSSL_INCLUDE_DIR}")
message(STATUS "** OpenSSL Libraries: ${OPENSSL_LIBRARIES}")
endif ()
else ()
#find_package(OpenSSL 1.1.1 REQUIRED COMPONENTS)
set(OPENSSL_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/bin/include)
set(OPENSSL_LIBRARIES_DIR ${PROJECT_SOURCE_DIR}/bin/lib)
set(OPENSSL_LIBRARIES ${OPENSSL_LIBRARIES_DIR}/libssl.a;${OPENSSL_LIBRARIES_DIR}/libcrypto.a)
include_directories(${OPENSSL_INCLUDE_DIR})
endif ()
message(STATUS "** OpenSSL_INCLUDE_DIR: ${OPENSSL_INCLUDE_DIR}")
message(STATUS "** OpenSSL_LIBRARIES: ${OPENSSL_LIBRARIES}")

#Find dependency
option(Boost_USE_STATIC_LIBS "only find boost static libs" ON) # only find static libs
set(Boost_USE_MULTITHREADED ON)
Expand All @@ -61,11 +79,11 @@ else ()
#find_package(Boost 1.56 REQUIRED COMPONENTS atomic thread system chrono date_time log log_setup regex serialization filesystem locale iostreams)
set(Boost_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/bin/include)
set(Boost_LIBRARY_DIRS ${PROJECT_SOURCE_DIR}/bin/lib)
set(Boost_LIBRARIES ${Boost_LIBRARY_DIRS}/libboost_atomic.a;${Boost_LIBRARY_DIRS}/libboost_thread.a;${Boost_LIBRARY_DIRS}/libboost_system.a;${Boost_LIBRARY_DIRS}/libboost_chrono.a;
${Boost_LIBRARY_DIRS}/libboost_date_time.a;${Boost_LIBRARY_DIRS}/libboost_log.a;${Boost_LIBRARY_DIRS}/libboost_log_setup.a;
${Boost_LIBRARY_DIRS}/libboost_regex.a;${Boost_LIBRARY_DIRS}/libboost_serialization.a;${Boost_LIBRARY_DIRS}/libboost_filesystem.a;
${Boost_LIBRARY_DIRS}/libboost_locale.a;${Boost_LIBRARY_DIRS}/libboost_iostreams.a)
include_directories(${Boost_INCLUDE_DIRS})
set(Boost_LIBRARIES ${Boost_LIBRARY_DIRS}/libboost_atomic.a;${Boost_LIBRARY_DIRS}/libboost_thread.a;${Boost_LIBRARY_DIRS}/libboost_system.a;
${Boost_LIBRARY_DIRS}/libboost_chrono.a;${Boost_LIBRARY_DIRS}/libboost_date_time.a;${Boost_LIBRARY_DIRS}/libboost_log.a;
${Boost_LIBRARY_DIRS}/libboost_log_setup.a;${Boost_LIBRARY_DIRS}/libboost_regex.a;${Boost_LIBRARY_DIRS}/libboost_serialization.a;
${Boost_LIBRARY_DIRS}/libboost_filesystem.a;${Boost_LIBRARY_DIRS}/libboost_locale.a;${Boost_LIBRARY_DIRS}/libboost_iostreams.a)
include_directories(${Boost_INCLUDE_DIR})
endif ()

message(STATUS "** Boost_INCLUDE_DIR: ${Boost_INCLUDE_DIR}")
Expand All @@ -76,18 +94,19 @@ if (WIN32)
find_package(Libevent 2.0.22 REQUIRED COMPONENTS)
if (LIBEVENT_FOUND)
include_directories(${LIBEVENT_INCLUDE_DIRS})
message(STATUS "** libevent Include dir: ${LIBEVENT_INCLUDE_DIR}")
message(STATUS "** libevent Include dir: ${LIBEVENT_INCLUDE_DIRS}")
message(STATUS "** libevent Libraries: ${LIBEVENT_LIBRARIES}")
endif ()
else ()
#find_package(Libevent 2.0.22 REQUIRED COMPONENTS)
set(LIBEVENT_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/bin/include)
set(LIBEVENT_LIBRARIES_DIR ${PROJECT_SOURCE_DIR}/bin/lib)
set(LIBEVENT_LIBRARIES ${LIBEVENT_LIBRARIES_DIR}/libevent.a;${LIBEVENT_LIBRARIES_DIR}/libevent_core.a;${LIBEVENT_LIBRARIES_DIR}/libevent_extra.a;${LIBEVENT_LIBRARIES_DIR}/libevent_pthreads.a)
set(LIBEVENT_LIBRARIES ${LIBEVENT_LIBRARIES_DIR}/libevent.a;${LIBEVENT_LIBRARIES_DIR}/libevent_core.a;${LIBEVENT_LIBRARIES_DIR}/libevent_extra.a;
${LIBEVENT_LIBRARIES_DIR}/libevent_pthreads.a;${LIBEVENT_LIBRARIES_DIR}/libevent_openssl.a)
include_directories(${LIBEVENT_INCLUDE_DIRS})
endif ()

message(STATUS "** LIBEVENT_INCLUDE_DIR: ${LIBEVENT_INCLUDE_DIR}")
message(STATUS "** LIBEVENT_INCLUDE_DIR: ${LIBEVENT_INCLUDE_DIRS}")
message(STATUS "** LIBEVENT_LIBRARIES: ${LIBEVENT_LIBRARIES}")

option(JSONCPP_USE_STATIC_LIBS "only find jsoncpp static libs" ON) # only find static libs
Expand Down Expand Up @@ -187,8 +206,10 @@ ELSE ()
message(STATUS "** ENABLE_LSAN: ${ENABLE_LSAN} Enable lsan reporting")
endif ()

set(CMAKE_CXX_FLAGS_DEBUG "-O0 -DDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -DDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")
set(CMAKE_C_FLAGS_DEBUG "-g -O0 -DDEBUG")
set(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG")


# Declare deplibs, so we can use list in linker later. There's probably
Expand Down
120 changes: 97 additions & 23 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ declare build_dir="${basepath}/tmp_build_dir"
declare packet_dir="${basepath}/tmp_packet_dir"
declare install_lib_dir="${basepath}/bin"
declare static_package_dir="${basepath}/tmp_static_package_dir"
declare fname_openssl="openssl*.tar.gz"

declare fname_libevent="libevent*.zip"
declare fname_jsoncpp="jsoncpp*.zip"
declare fname_boost="boost*.tar.gz"
declare fname_openssl_down="openssl-1.1.1d.tar.gz"
declare fname_libevent_down="release-2.1.11-stable.zip"
declare fname_jsoncpp_down="0.10.7.zip"
declare fname_boost_down="1.58.0/boost_1_58_0.tar.gz"
Expand All @@ -44,24 +47,30 @@ if test "$(uname)" = "Linux"; then
elif test "$(uname)" = "Darwin" ; then
declare cpu_num=$(sysctl -n machdep.cpu.thread_count)
fi
declare need_build_jsoncpp=1

declare need_build_openssl=1
declare need_build_libevent=1
declare need_build_jsoncpp=1
declare need_build_boost=1
declare enable_asan=0
declare enable_lsan=0
declare verbose=1
declare codecov=0
declare debug=0
declare test=0

pasres_arguments() {
for var in "$@"; do
case "$var" in
noJson)
need_build_jsoncpp=0
noOpenSSL)
need_build_openssl=0
;;
noEvent)
need_build_libevent=0
;;
noJson)
need_build_jsoncpp=0
;;
noBoost)
need_build_boost=0
;;
Expand All @@ -77,6 +86,9 @@ pasres_arguments() {
codecov)
codecov=1
;;
debug)
debug=1
;;
test)
test=1
;;
Expand All @@ -88,16 +100,21 @@ pasres_arguments $@

PrintParams() {
echo "###########################################################################"
if [ $need_build_libevent -eq 0 ]; then
echo "no need build libevent lib"
if [ $need_build_openssl -eq 0 ]; then
echo "no need build openssl lib"
else
echo "need build libevent lib"
echo "need build openssl lib"
fi
if [ $need_build_jsoncpp -eq 0 ]; then
echo "no need build jsoncpp lib"
else
echo "need build jsoncpp lib"
fi
if [ $need_build_libevent -eq 0 ]; then
echo "no need build libevent lib"
else
echo "need build libevent lib"
fi
if [ $need_build_boost -eq 0 ]; then
echo "no need build boost lib"
else
Expand All @@ -113,18 +130,25 @@ PrintParams() {
else
echo "disable lsan reporting"
fi
if [ $test -eq 1 ]; then
echo "build unit tests"
if [ $verbose -eq 0 ]; then
echo "no need print detail logs"
else
echo "without build unit tests"
echo "need print detail logs"
fi
if [ $codecov -eq 1 ]; then
echo "run unit tests with code coverage"
else
echo "run unit tests without code coverage"
fi
if [ $verbose -eq 0 ]; then
echo "no need print detail logs"
if [ $debug -eq 1 ]; then
echo "enable debug"
else
echo "need print detail logs"
echo "disable debug"
fi
if [ $test -eq 1 ]; then
echo "build unit tests"
else
echo "without build unit tests"
fi

echo "###########################################################################"
Expand All @@ -141,6 +165,10 @@ Prepare() {
fi

cd ${basepath}
if [ -e ${fname_openssl} ]; then
mv -f ${basepath}/${fname_openssl} ${down_dir}
fi

if [ -e ${fname_libevent} ]; then
mv -f ${basepath}/${fname_libevent} ${down_dir}
fi
Expand Down Expand Up @@ -174,6 +202,52 @@ Prepare() {
fi
}

BuildOpenSSL() {
if [ $need_build_openssl -eq 0 ]; then
echo "no need build openssl lib"
return 0
fi

cd ${down_dir}
if [ -e ${fname_openssl} ]; then
echo "${fname_openssl} is exist"
else
wget https://www.openssl.org/source/${fname_openssl_down} -O ${fname_openssl_down}
fi
tar -zxvf ${fname_openssl} &> unzipopenssl.txt
if [ $? -ne 0 ]; then
exit 1
fi

openssl_dir=$(ls | grep ^openssl | grep .*[^gz]$)
cd ${openssl_dir}
if [ $? -ne 0 ]; then
exit 1
fi
echo "build openssl static #####################"
if [ $verbose -eq 0 ]; then
./config shared CFLAGS=-fPIC CPPFLAGS=-fPIC --prefix=${install_lib_dir} --openssldir=${install_lib_dir} &> opensslconfig.txt
else
./config shared CFLAGS=-fPIC CPPFLAGS=-fPIC --prefix=${install_lib_dir} --openssldir=${install_lib_dir}
fi
if [ $? -ne 0 ]; then
exit 1
fi
if [ $verbose -eq 0 ]; then
echo "build openssl without detail log."
make depend &> opensslbuild.txt
make -j $cpu_num &> opensslbuild.txt
else
make depend
make -j $cpu_num
fi
if [ $? -ne 0 ]; then
exit 1
fi
make install
echo "build openssl success."
}

BuildLibevent() {
if [ $need_build_libevent -eq 0 ]; then
echo "no need build libevent lib"
Expand Down Expand Up @@ -202,9 +276,9 @@ BuildLibevent() {
fi
echo "build libevent static #####################"
if [ $verbose -eq 0 ]; then
./configure --disable-openssl --enable-static=yes --enable-shared=no CFLAGS=-fPIC CPPFLAGS=-fPIC --prefix=${install_lib_dir} &> libeventconfig.txt
./configure --enable-static=yes --enable-shared=no CFLAGS="-fPIC -I${install_lib_dir}/include" CPPFLAGS="-fPIC -I${install_lib_dir}/include" LDFLAGS="-L${install_lib_dir}/lib" --prefix=${install_lib_dir} &> libeventconfig.txt
else
./configure --disable-openssl --enable-static=yes --enable-shared=no CFLAGS=-fPIC CPPFLAGS=-fPIC --prefix=${install_lib_dir}
./configure --enable-static=yes --enable-shared=no CFLAGS="-fPIC -I${install_lib_dir}/include" CPPFLAGS="-fPIC -I${install_lib_dir}/include" LDFLAGS="-L${install_lib_dir}/lib" --prefix=${install_lib_dir}
fi
if [ $? -ne 0 ]; then
exit 1
Expand Down Expand Up @@ -330,6 +404,11 @@ BuildRocketMQClient() {
else
ROCKETMQ_CMAKE_FLAG=$ROCKETMQ_CMAKE_FLAG" -DENABLE_LSAN=OFF"
fi
if [ $debug -eq 1 ]; then
ROCKETMQ_CMAKE_FLAG=$ROCKETMQ_CMAKE_FLAG" -DCMAKE_BUILD_TYPE=Debug"
else
ROCKETMQ_CMAKE_FLAG=$ROCKETMQ_CMAKE_FLAG" -DCMAKE_BUILD_TYPE=Release"
fi
cmake .. $ROCKETMQ_CMAKE_FLAG
if [ $verbose -eq 0 ]; then
echo "build rocketmq without detail log."
Expand Down Expand Up @@ -426,22 +505,16 @@ PackageRocketMQStatic() {
cp -f ${install_lib_dir}/librocketmq.a .
echo "Md5 Hash RocketMQ Before:"
md5sum librocketmq.a
local dir=`ls *.a | grep -v gtest | grep -v gmock `
local dir=`ls *.a | grep -E 'gtest|gmock'`
for i in $dir
do
echo $i
ar x $i
rm -rf $i
done
echo "At last, ar libboost_filesystem"
ar x libboost_filesystem.a
ar cru librocketmq.a *.o
ranlib librocketmq.a
libtool -no_warning_for_no_symbols -static -o librocketmq.a *.a
echo "Md5 Hash RocketMQ After:"
md5sum librocketmq.a
echo "Try to copy $(pwd)/librocketmq.a to ${install_lib_dir}/"
cp -f librocketmq.a ${install_lib_dir}/
rm -rf *.o
rm -rf __.*
cd ${basepath}
rm -rf ${static_package_dir}
fi
Expand All @@ -450,6 +523,7 @@ PackageRocketMQStatic() {

PrintParams
Prepare
BuildOpenSSL
BuildLibevent
BuildJsonCPP
BuildBoost
Expand Down
5 changes: 3 additions & 2 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
link_directories(${LIBEVENT_LIBRARY})
link_directories(${JSONCPP_LIBRARY})
link_directories(${OPENSSL_LIBRARIES_DIR})

#if (BUILD_ROCKETMQ_SHARED)
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBOOST_ALL_DYN_LINK -shared ")
Expand All @@ -43,10 +44,10 @@ foreach(file ${files})
if (MSVC)
if (BUILD_ROCKETMQ_SHARED)
target_link_libraries (${basename} rocketmq_shared ${deplibs}
${Boost_LIBRARIES} ${LIBEVENT_LIBRARIES} ${JSONCPP_LIBRARIES})
${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} ${LIBEVENT_LIBRARIES} ${JSONCPP_LIBRARIES})
else()
target_link_libraries (${basename} rocketmq_static ${deplibs}
${Boost_LIBRARIES} ${LIBEVENT_LIBRARIES} ${JSONCPP_LIBRARIES})
${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} ${LIBEVENT_LIBRARIES} ${JSONCPP_LIBRARIES})
endif()
else()
if (BUILD_ROCKETMQ_SHARED)
Expand Down
2 changes: 1 addition & 1 deletion example/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
#ifndef ROCKETMQ_CLIENT4CPP_EXAMPLE_COMMON_H_
#define ROCKETMQ_CLIENT4CPP_EXAMPLE_COMMON_H_

#include <functional>
#include <atomic>
#include <chrono>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
Expand Down
6 changes: 6 additions & 0 deletions include/DefaultMQProducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ class ROCKETMQCLIENT_API DefaultMQProducer {
void setMessageTrace(bool messageTrace);
bool getMessageTrace() const;

void setEnableSsl(bool enableSsl);
bool getEnableSsl() const;

void setSslPropertyFile(const std::string& sslPropertyFile);
const std::string& getSslPropertyFile() const;

private:
DefaultMQProducerImpl* impl;
};
Expand Down
6 changes: 6 additions & 0 deletions include/DefaultMQPullConsumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ class ROCKETMQCLIENT_API DefaultMQPullConsumer {
const std::string& getGroupName() const;
void setGroupName(const std::string& groupname);

void setEnableSsl(bool enableSsl);
bool getEnableSsl() const;

void setSslPropertyFile(const std::string& sslPropertyFile);
const std::string& getSslPropertyFile() const;

/**
* Log configuration interface, default LOG_LEVEL is LOG_LEVEL_INFO, default
* log file num is 3, each log size is 100M
Expand Down
6 changes: 6 additions & 0 deletions include/DefaultMQPushConsumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ class ROCKETMQCLIENT_API DefaultMQPushConsumer {
const std::string& getGroupName() const;
void setGroupName(const std::string& groupname);

void setEnableSsl(bool enableSsl);
bool getEnableSsl() const;

void setSslPropertyFile(const std::string& sslPropertyFile);
const std::string& getSslPropertyFile() const;

/**
* Log configuration interface, default LOG_LEVEL is LOG_LEVEL_INFO, default
* log file num is 3, each log size is 100M
Expand Down
Loading

0 comments on commit 81f36ec

Please sign in to comment.