Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use OpenSSL to add support for SSL communication with the server. Script add debug mode support. #273

Merged
merged 17 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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