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

Added initial cmake build support #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/src/config.h
/src/goodbyedpi-rc.rc
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "WinDivert"]
path = WinDivert
url = https://github.com/basil00/Divert
branch = master
98 changes: 98 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
CMAKE_MINIMUM_REQUIRED(VERSION 3.11)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
include(Hardening)
include(VersionFromGit)
PROJECT(GoodbyeDPI)

set(CMAKE_USE_RELATIVE_PATHS TRUE)

set(PACKAGE_NAME "GoodbyeDPI" CACHE STRING "Program name")
set(PACKAGE_VERSION ${VERSION})
set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
getVersionFromGit(GoodbyeDPI "v0.1.5")

if(${CMAKE_SYSTEM_NAME} MATCHES "Darvin")
set(MACOS TRUE)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set(LINUX TRUE)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
set(FREEBSD TRUE)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set(WINDOWS TRUE)
endif()

set(Source_dir "${CMAKE_CURRENT_SOURCE_DIR}/src")
set(Utils_dir "${CMAKE_CURRENT_SOURCE_DIR}/src/utils")
set(CMake_Misc_Dir "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

file(GLOB_RECURSE SRCFILES "${Source_dir}/*.c" "${Source_dir}/*.cpp")

find_package(WinDivert REQUIRED)

include_directories(GoodbyeDPI "${WinDivert_INCLUDE_DIR}")

#making resource file
set(rc_script_name "goodbyedpi-rc.rc")
set(rc_script "${Source_dir}/${rc_script_name}")
set(rc_script_proto "${CMake_Misc_Dir}/${rc_script_name}.in")
set(license_file "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
file(READ ${license_file} LICENSE_TEXT)
string(REPLACE "\"" "\"\"" LICENSE_TEXT ${LICENSE_TEXT})
string(REPLACE "\r\n" "\n" LICENSE_TEXT ${LICENSE_TEXT})
string(REPLACE "\n" "\\r\\n" LICENSE_TEXT ${LICENSE_TEXT})
message(STATUS "generating resources: ${rc_script_proto} -> ${rc_script}")
configure_file(${rc_script_proto} ${rc_script})
file(GLOB_RECURSE resource_files "${Source_dir}/*.rc" "${Source_dir}/*.ico")


if(MSVC_IDE)
file(GLOB_RECURSE HDRFILES "${Source_dir}/*.h")
endif()

# config.h
set(CONFIG_FILENAME "config.h")
set(CONFIG_FILE "${Source_dir}/${CONFIG_FILENAME}")
mark_as_advanced(CONFIG_FILE)
set(CONFIG_FILE_PROTO "${CMake_Misc_Dir}/${CONFIG_FILENAME}.in")

set(GoodbyeDPI_SERVICE_NAME "${PACKAGE_NAME}" CACHE STRING "Service name")

set(GoodbyeDPI_HOST_MAXLEN 253 CACHE INTEGER "Max length of a host")
set(GoodbyeDPI_MAX_FILTERS 4 CACHE INTEGER "Max length of a host")
set(GoodbyeDPI_MAX_PACKET_SIZE 9016 CACHE INTEGER "Max length of a host")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not a variables and should not be configurable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's always a good idea to have everything hardcoded configurable.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maximum host size is defined in RFC. Max filters should not be changed since it's internal variable for me. Packet size should not be configured in a normal circumstances either.

set(GoodbyeDPI_DEBUG ${GoodbyeDPI_DEBUG} CACHE BOOL "Enable debug output")

mark_as_advanced(GoodbyeDPI_SERVICE_NAME, GoodbyeDPI_HOST_MAXLEN)

message(STATUS "generating config: ${CONFIG_FILE_PROTO} -> ${CONFIG_FILE}")
configure_file(${CONFIG_FILE_PROTO} ${CONFIG_FILE})

add_executable(GoodbyeDPI "${SRCFILES}" "${HDRFILES}" "${resource_files}")
target_link_libraries(GoodbyeDPI "${WinDivert_LIBRARY_PATH}")

harden(GoodbyeDPI)
if(MINGW)
target_link_libraries(GoodbyeDPI "wsock32" "ws2_32" "ssp")
elseif(MSVC)
else()
message(FATAL_ERROR "The compiler is not supported")
endif()


set(RESOURCE_FILES
resourcefile.txt
appresourcedir/appres.txt
)


if(MSVC_IDE)
source_group("res" FILES ${resource_files})
endif()

if(MSVC)
set_source_files_properties(${SRCFILES} PROPERTIES LANGUAGE CXX)
else(MSVC)
#set_source_files_properties(${SRCFILES} PROPERTIES LANGUAGE CXX)
endif(MSVC)

#target_compile_features(GoodbyeDPI PUBLIC cxx_decltype_auto)
37 changes: 37 additions & 0 deletions cmake/FindWinDivert.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#This is free and unencumbered software released into the public domain.
#Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
#In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#For more information, please refer to <https://unlicense.org/>
include(ExternalProject)

set(WinDivert_INCLUDE_DIR_DOC "The dir where WinDivert headers reside")
set(WinDivert_LIBRARY_PATH_DOC "The path to WinDivert library")

find_path(WinDivert_INCLUDE_DIR windivert.h "${CMAKE_SOURCE_DIR}/WinDivert/include" DOC ${WinDivert_INCLUDE_DIR_DOC})
find_library(WinDivert_LIBRARY_PATH NAMES WinDivert.dll PATHS "${CMAKE_SOURCE_DIR}/WinDivert/${CMAKE_SYSTEM_PROCESSOR}/" DOC ${WinDivert_LIBRARY_PATH_DOC})

if(WinDivert_LIBRARY_PATH MATCHES "WinDivert_LIBRARY_PATH-NOTFOUND")
if(PACKAGE_FIND_VERSION)
else(PACKAGE_FIND_VERSION)
set(PACKAGE_FIND_VERSION "1.4.3")
endif(PACKAGE_FIND_VERSION)

set(WinDivert_Dev_SCM_name "basil00")
set(WinDivert_Dev_SCM_repo_name "Divert")
set(WinDivert_download_URI "https://github.com/${WinDivert_Dev_SCM_name}/${WinDivert_Dev_SCM_repo_name}/releases/download/v${PACKAGE_FIND_VERSION}/WinDivert-${PACKAGE_FIND_VERSION}-A.zip")
ExternalProject_Add(WinDivert
URL ${WinDivert_download_URI}
UPDATE_COMMAND ""
PATCH_COMMAND ""
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
ExternalProject_Get_Property(WinDivert SOURCE_DIR)
set(WinDivert_INCLUDE_DIR "${SOURCE_DIR}/include" CACHE PATH ${WinDivert_INCLUDE_DIR_DOC} FORCE)
set(WinDivert_LIBRARY_PATH "${SOURCE_DIR}/${CMAKE_SYSTEM_PROCESSOR}/WinDivert.dll" CACHE FILEPATH ${WinDivert_INCLUDE_DIR_DOC} FORCE)
message(STATUS "WinDivert not found, scheduled downloading prebuilt version from ${WinDivert_download_URI}. The contents will be unpacked to ${SOURCE_DIR}")
add_custom_command(OUTPUT "${WinDivert_INCLUDE_DIR}" "${WinDivert_LIBRARY_PATH}" DEPENDS WinDivert COMMAND "")
endif()
98 changes: 98 additions & 0 deletions cmake/Hardening.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#This is free and unencumbered software released into the public domain.
#Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
#In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#For more information, please refer to <https://unlicense.org/>

include(CheckCXXCompilerFlag)

function(determineSupportedHardeningFlags property)
set(FLAGS_HARDENING "")
foreach(flag ${ARGN})
unset(var_name)
string(REPLACE "=" "_eq_" var_name ${flag})
string(REPLACE "," "_comma_" var_name ${var_name})
set(var_name "SUPPORTS_HARDENING_${property}_${var_name}")
check_cxx_compiler_flag(${flag} ${var_name})#since linker flags and other flags are in the form of compiler flags
if(${${var_name}})
list(APPEND FLAGS_HARDENING "${flag}")
endif()
endforeach(flag)
list(JOIN FLAGS_HARDENING " " FLAGS_HARDENING)
message(STATUS "FLAGS_HARDENING ${FLAGS_HARDENING}")
set(HARDENING_${property} "${FLAGS_HARDENING}" CACHE STRING "Hardening flags")
endfunction(determineSupportedHardeningFlags)

function(processFlagsList target property)
get_target_property(FLAGS_UNHARDENED ${target} ${property})
if(FLAGS_UNHARDENED MATCHES "FLAGS_UNHARDENED-NOTFOUND")
set(FLAGS_UNHARDENED "")
endif()
message(STATUS "processFlagsList ${target} ${property} ${FLAGS_UNHARDENED}")
message(STATUS "HARDENING_${property} ${HARDENING_${property}}")
if(HARDENING_${property})
else()
determineSupportedHardeningFlags(${property} ${ARGN})
endif()

set(FLAGS_HARDENED ${FLAGS_UNHARDENED})
list(APPEND FLAGS_HARDENED ${HARDENING_${property}})
list(JOIN FLAGS_HARDENED " " FLAGS_HARDENED)
message(STATUS "${target} PROPERTIES ${property} ${FLAGS_HARDENED}")
set_target_properties(${target} PROPERTIES ${property} "${FLAGS_HARDENED}")
endfunction(processFlagsList)

function(harden target)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
set(HARDENING_COMPILER_FLAGS
"-Wall" "-Wextra" "-Wconversion" "-Wformat" "-Wformat-security" "-Werror=format-security"
"-fPIE"
"-fno-strict-aliasing" "-mmitigate-rop" "-fno-common" "-fstack-check"
"-mcet"
"-fsanitize=cfi"
"-fsanitize=cfi-cast-strict"
"-fsanitize=cfi-derived-cast"
"-fsanitize=cfi-unrelated-cast"
"-fsanitize=cfi-nvcall"
"-fsanitize=cfi-vcall"
"-fsanitize=cfi-icall"
"-fsanitize=cfi-mfcall"
#"-fstack-protector"
#"-fstack-protector-strong" #hello world works
"-mindirect-branch"
"-mindirect-branch=thunk-extern"
"-mindirect-branch=thunk-inline"
"-mindirect-return"
"-mindirect-branch-register"
"-mindirect-branch-loop"
"-mno-indirect-branch-register"
)
set(HARDENING_LINKER_FLAGS
#"-pie"
#"-Wl,-z,relro"
#"-Wl,-z,now"
"-Wl,-O1"
"-Wl,--sort-common"
"-Wl,--as-needed"
"-Wl,--dynamicbase"
"-Wl,--nxcompat"
"-Wl,--export-all-symbols"
"-Wl,-flto"
)
set(HARDENING_MACRODEFS
"-D_FORTIFY_SOURCE=2"
)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
list(APPEND HARDENING_LINKER_FLAGS "-Wl,--image-base,0x140000000")
endif()
elseif(MSVC)
set(HARDENING_COMPILER_FLAGS "/sdl" "/GS" "/SafeSEH" "/NXCOMPAT" "/dynamicbase" "/guard:cf" "/HIGHENTROPYVA")
set(HARDENING_LINKER_FLAGS "/guard:cf")
else()
message(ERROR "The compiler is not supported")
endif()

processFlagsList(${target} COMPILE_FLAGS ${HARDENING_COMPILER_FLAGS})
processFlagsList(${target} LINK_FLAGS ${HARDENING_LINKER_FLAGS})
add_definitions(${HARDENING_MACRODEFS})
endfunction(harden)
90 changes: 90 additions & 0 deletions cmake/VersionFromGit.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#This is free and unencumbered software released into the public domain.
#Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
#In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#For more information, please refer to <https://unlicense.org/>

FIND_PACKAGE(Git)
function(getVersionFromGit variablesPrefix defaultVersion)
if(GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --dirty --long --tags
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE VERSION_FROM_GIT
RESULT_VARIABLE GIT_RESULT
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NOT GIT_RESULT EQUAL 0)
message(WARNING "git returned error ${VERSION_FROM_GIT}")
set(VERSION_FROM_GIT "unknown")
endif()
else(GIT_FOUND)
set(VERSION_FROM_GIT ${defaultVersion})
endif(GIT_FOUND)


string(REGEX MATCH "^v?[0-9]+(\\.[0-9]+)+" VERSION ${VERSION_FROM_GIT})
string(REGEX MATCHALL "[0-9]+" PARSED_VER ${VERSION})

list(LENGTH PARSED_VER PARSED_VER_LEN)
if(PARSED_VER_LEN GREATER 0)
list(GET PARSED_VER 0 VERSION_MAJOR)
else()
set(VERSION_MAJOR 0)
endif()
if(PARSED_VER_LEN GREATER 1)
list(GET PARSED_VER 1 VERSION_MINOR)
else()
set(VERSION_MINOR 0)
endif()
if(PARSED_VER_LEN GREATER 2)
list(GET PARSED_VER 2 VERSION_PATCH)
else()
set(VERSION_PATCH 0)
endif()
if(PARSED_VER_LEN GREATER 3)
list(GET PARSED_VER 3 VERSION_TWEAK)
else()
set(VERSION_TWEAK 0)
endif()
set(VERSION_MAJOR ${VERSION_MAJOR} CACHE INTERNAL "Major version number")
set(VERSION_MINOR ${VERSION_MINOR} CACHE INTERNAL "Minor version number")
set(VERSION_PATCH ${VERSION_PATCH} CACHE INTERNAL "Patch version number")
set(VERSION_TWEAK ${VERSION_TWEAK} CACHE INTERNAL "Tweak version number")

set(VERSION_BIN "${VERSION_MAJOR}${VERSION_MINOR}${VERSION_PATCH}" CACHE INTERNAL "Version number as a single number")
message(STATUS "${variablesPrefix} version: ${VERSION}")
message(STATUS "${variablesPrefix} bin version: ${VERSION_BIN}")

if(GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:%ct
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE COMPTIME
RESULT_VARIABLE GIT_RESULT
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NOT GIT_RESULT EQUAL 0)
set(${${variablesPrefix}_COMPTIME} "0000000000" CACHE INTERNAL "Compilation time")
endif()

execute_process(
COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:%D
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE VERSION_EXPORT
RESULT_VARIABLE GIT_RESULT
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NOT GIT_RESULT EQUAL 0)
set(VERSION_EXPORT "HEAD -> master" CACHE INTERNAL)
endif()
else(GIT_FOUND)
set(${${variablesPrefix}_COMPTIME} "0000000000")
set(VERSION_EXPORT "HEAD -> master" CACHE INTERNAL)
endif(GIT_FOUND)
set(${variablesPrefix}_VERSION "${VERSION_EXPORT} ${VERSION_FROM_GIT}" CACHE INTERNAL "Project's version from Git")
message(STATUS "version tag: ${${variablesPrefix}_VERSION}")
endfunction()
11 changes: 11 additions & 0 deletions cmake/config.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
static const char SERVICE_NAME[] = "@GoodbyeDPI_SERVICE_NAME@";
static const char VERSION[] = "@GoodbyeDPI_VERSION@";

enum{
HOST_MAXLEN=@GoodbyeDPI_HOST_MAXLEN@u,
MAX_FILTERS=@GoodbyeDPI_MAX_FILTERS@u,
MAX_PACKET_SIZE=@GoodbyeDPI_MAX_PACKET_SIZE@u
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not a variables


#cmakedefine GoodbyeDPI_DEBUG
33 changes: 33 additions & 0 deletions cmake/goodbyedpi-rc.rc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "winver.h"
VS_VERSION_INFO VERSIONINFO
FILEVERSION @VERSION_MAJOR@,@VERSION_MINOR@,@VERSION_PATCH@,@VERSION_TWEAK@
PRODUCTVERSION @VERSION_MAJOR@,@VERSION_MINOR@,@VERSION_PATCH@,@VERSION_TWEAK@
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
#else
FILEFLAGS 0x0L
#endif
FILEOS 0x40004L
FILETYPE 0x1L
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040004b0"
BEGIN
VALUE "FileDescription", "GoodbyeDPI—Passive Deep Packet Inspection blocker and Active DPI circumvention utility (for Windows)."
VALUE "FileVersion", "@GoodbyeDPI_VERSION@"
VALUE "InternalName", "@PACKAGE_NAME@"
VALUE "LegalCopyright", "@LICENSE_TEXT@"
VALUE "ProductName", "@PACKAGE_NAME@"
VALUE "ProductVersion", "@GoodbyeDPI_VERSION@"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x400, 1200
END
END
1 24 "goodbyedpi.exe.manifest"
id ICON "icon.ico"
Loading