From 88c2e1f6def389537cf59e3deb55080854e9a4c9 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Mon, 8 Oct 2018 15:23:01 -0700 Subject: [PATCH] make TLS support optional --- CMakeLists.txt | 46 ++++++++++++++++++++++ README.md | 6 ++- examples/chat/CMakeLists.txt | 23 +++++++++++ examples/chat/README.md | 39 ++++++++++++++++++ examples/chat/{build.sh => build_macos.sh} | 2 + examples/chat/cmd_websocket_chat.cpp | 2 +- ixwebsocket/IXWebSocketTransport.cpp | 20 ++++++---- 7 files changed, 129 insertions(+), 9 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 examples/chat/CMakeLists.txt create mode 100644 examples/chat/README.md rename examples/chat/{build.sh => build_macos.sh} (88%) diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..de9e3977 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,46 @@ +# +# cmd_websocket_chat.cpp +# Author: Benjamin Sergeant +# Copyright (c) 2018 Machine Zone, Inc. All rights reserved. +# + +cmake_minimum_required(VERSION 3.4.1) +project(ixwebsocket C CXX) + +set (CMAKE_CXX_STANDARD 11) +set (CXX_STANDARD_REQUIRED ON) +set (CMAKE_CXX_EXTENSIONS OFF) + +set( IXWEBSOCKET_SOURCES + ixwebsocket/IXSocket.cpp + ixwebsocket/IXWebSocket.cpp + ixwebsocket/IXWebSocketTransport.cpp +) + +set( IXWEBSOCKET_HEADERS + ixwebsocket/IXSocket.h + ixwebsocket/IXWebSocket.h + ixwebsocket/IXWebSocketTransport.h +) + +if (USE_TLS) + add_definitions(-DIXWEBSOCKET_USE_TLS) + + if (APPLE) + list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSocketAppleSSL.h) + list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSocketAppleSSL.cpp) + else() + list( APPEND IXWEBSOCKET_HEADERS ixwebsocket/IXSocketOpenSSL.h) + list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/IXSocketOpenSSL.cpp) + endif() +endif() + +add_library( ixwebsocket STATIC + ${IXWEBSOCKET_SOURCES} + ${IXWEBSOCKET_HEADERS} +) + +set( IXWEBSOCKET_INCLUDE_DIRS + . + ../../shared/OpenSSL/include) +target_include_directories( ixwebsocket PUBLIC ${IXWEBSOCKET_INCLUDE_DIRS} ) diff --git a/README.md b/README.md index eed38f8d..40e8c7d7 100644 --- a/README.md +++ b/README.md @@ -39,11 +39,15 @@ webSocket.send("hello world"); webSocket.stop() ``` +## Build + +CMakefiles for the library and the examples are available. This library has few dependencies, so it is possible to just add the source files into your project. + ## Implementation details ### TLS/SSL -Connections can be optionally secured and encrypted with TLS/SSL when using a wss:// endpoint, or using normal un-encrypted socket with ws:// endpoints. AppleSSL is used on iOS and OpenSSL is used on Android. +Connections can be optionally secured and encrypted with TLS/SSL when using a wss:// endpoint, or using normal un-encrypted socket with ws:// endpoints. AppleSSL is used on iOS and macOS, and OpenSSL is used on Android and Linux. ### Polling and background thread work diff --git a/examples/chat/CMakeLists.txt b/examples/chat/CMakeLists.txt new file mode 100644 index 00000000..c878e35b --- /dev/null +++ b/examples/chat/CMakeLists.txt @@ -0,0 +1,23 @@ +# +# cmd_websocket_chat.cpp +# Author: Benjamin Sergeant +# Copyright (c) 2018 Machine Zone, Inc. All rights reserved. +# + +cmake_minimum_required (VERSION 3.4.1) +project (cmd_websocket_chat) + +set (CMAKE_CXX_STANDARD 11) + +option(USE_TLS "Add TLS support" ON) + +add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ixwebsocket) + +add_executable(cmd_websocket_chat cmd_websocket_chat.cpp) + +if (APPLE AND USE_TLS) + target_link_libraries(cmd_websocket_chat "-framework foundation" "-framework security") +endif() + +target_link_libraries(cmd_websocket_chat ixwebsocket) +install(TARGETS cmd_websocket_chat DESTINATION bin) diff --git a/examples/chat/README.md b/examples/chat/README.md new file mode 100644 index 00000000..95da4b0e --- /dev/null +++ b/examples/chat/README.md @@ -0,0 +1,39 @@ +# Building + +1. cmake -G . +2. make + +## Disable TLS + +chat$ cmake -DUSE_TLS=OFF . +-- Configuring done +-- Generating done +-- Build files have been written to: /Users/bsergeant/src/foss/ixwebsocket/examples/chat +chat$ make +Scanning dependencies of target ixwebsocket +[ 16%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXSocket.cpp.o +[ 33%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXWebSocket.cpp.o +[ 50%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXWebSocketTransport.cpp.o +[ 66%] Linking CXX static library libixwebsocket.a +[ 66%] Built target ixwebsocket +[ 83%] Linking CXX executable cmd_websocket_chat +[100%] Built target cmd_websocket_chat + +## Enable TLS (default) + +``` +chat$ cmake -DUSE_TLS=ON . +-- Configuring done +-- Generating done +-- Build files have been written to: /Users/bsergeant/src/foss/ixwebsocket/examples/chat +(venv) chat$ make +Scanning dependencies of target ixwebsocket +[ 14%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXSocket.cpp.o +[ 28%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXWebSocket.cpp.o +[ 42%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXWebSocketTransport.cpp.o +[ 57%] Building CXX object ixwebsocket/CMakeFiles/ixwebsocket.dir/ixwebsocket/IXSocketAppleSSL.cpp.o +[ 71%] Linking CXX static library libixwebsocket.a +[ 71%] Built target ixwebsocket +[ 85%] Linking CXX executable cmd_websocket_chat +[100%] Built target cmd_websocket_chat +``` diff --git a/examples/chat/build.sh b/examples/chat/build_macos.sh similarity index 88% rename from examples/chat/build.sh rename to examples/chat/build_macos.sh index a6d24207..8f484d66 100644 --- a/examples/chat/build.sh +++ b/examples/chat/build_macos.sh @@ -4,6 +4,8 @@ # Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved. # +# 'manual' way of building. You can also use cmake. + clang++ --std=c++11 --stdlib=libc++ \ ../../ixwebsocket/IXSocket.cpp \ ../../ixwebsocket/IXWebSocketTransport.cpp \ diff --git a/examples/chat/cmd_websocket_chat.cpp b/examples/chat/cmd_websocket_chat.cpp index 6af2da23..b52d9a19 100644 --- a/examples/chat/cmd_websocket_chat.cpp +++ b/examples/chat/cmd_websocket_chat.cpp @@ -12,7 +12,7 @@ #include #include #include -#include "../../ixwebsocket/IXWebSocket.h" +#include #include "nlohmann/json.hpp" diff --git a/ixwebsocket/IXWebSocketTransport.cpp b/ixwebsocket/IXWebSocketTransport.cpp index 61da99d4..5095077c 100644 --- a/ixwebsocket/IXWebSocketTransport.cpp +++ b/ixwebsocket/IXWebSocketTransport.cpp @@ -11,10 +11,12 @@ #include "IXWebSocketTransport.h" #include "IXSocket.h" -#ifdef __APPLE__ -# include "IXSocketAppleSSL.h" -#else -# include "IXSocketOpenSSL.h" +#ifdef IXWEBSOCKET_USE_TLS +# ifdef __APPLE__ +# include "IXSocketAppleSSL.h" +# else +# include "IXSocketOpenSSL.h" +# endif #endif #include @@ -140,10 +142,14 @@ namespace ix { if (protocol == "wss") { _socket.reset(); -#ifdef __APPLE__ - _socket = std::make_shared(); +#ifdef IXWEBSOCKET_USE_TLS +# ifdef __APPLE__ + _socket = std::make_shared(); +# else + _socket = std::make_shared(); +# endif #else - _socket = std::make_shared(); + return WebSocketInitResult(false, 0, "TLS is not supported."); #endif } else