From 49a2a247adf775638b6b2757bebc49de8c1f7158 Mon Sep 17 00:00:00 2001 From: KOLANICH Date: Wed, 16 May 2018 18:52:38 +0300 Subject: [PATCH] Added initial cmake build support Fixed according to the latest WinDivert --- .gitignore | 2 + .gitmodules | 4 + CMakeLists.txt | 187 ++++++++++++++++++++++++++++++++++++++ cmake/config.h.in | 11 +++ cmake/goodbyedpi-rc.rc.in | 33 +++++++ src/Makefile | 37 -------- src/goodbyedpi-rc.rc | 2 - src/goodbyedpi.c | 13 +-- src/goodbyedpi.h | 4 +- src/service.c | 3 +- 10 files changed, 242 insertions(+), 54 deletions(-) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 CMakeLists.txt create mode 100644 cmake/config.h.in create mode 100644 cmake/goodbyedpi-rc.rc.in delete mode 100644 src/Makefile delete mode 100644 src/goodbyedpi-rc.rc diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..d7c55662 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/src/config.h +/src/goodbyedpi-rc.rc diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..637bea5c --- /dev/null +++ b/.gitmodules @@ -0,0 +1,4 @@ +[submodule "WinDivert"] + path = WinDivert + url = https://github.com/basil00/Divert + branch = master diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..393662ef --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,187 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.11) +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}") + +FIND_PACKAGE(Git) +if(GIT_FOUND) + execute_process( + COMMAND ${GIT_EXECUTABLE} describe --tags --dirty --long + 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 "v0.1.5") +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_BIN "${VERSION_MAJOR}${VERSION_MINOR}${VERSION_PATCH}") +message(STATUS "GoodbyeDPI version: ${VERSION}") +message(STATUS "GoodbyeDPI 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(COMPTIME "0000000000") + 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") + endif() +else(GIT_FOUND) + set(COMPTIME "0000000000") + set(VERSION_EXPORT "HEAD -> master") +endif(GIT_FOUND) +set(GoodbyeDPI_VERSION "${VERSION_EXPORT} ${VERSION_FROM_GIT}") +message(STATUS "version tag: ${GoodbyeDPI_VERSION}") + +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(WinDivert_dir "${CMAKE_CURRENT_SOURCE_DIR}/WinDivert") +set(CMake_Misc_Dir "${CMAKE_CURRENT_SOURCE_DIR}/cmake") + +file(GLOB_RECURSE SRCFILES "${Source_dir}/*.c" "${Source_dir}/*.cpp") + +include_directories(GoodbyeDPI "${WinDivert_dir}/include") +if(MSVC) + set(divertCompilerName "WDDK") +elseif(MINGW) + set(divertCompilerName "MINGW") +endif() +set(divertLibraryPath "${WinDivert_dir}/install/${divertCompilerName}/${CMAKE_SYSTEM_PROCESSOR}/") +message(STATUS ${divertLibraryPath}) + +#making resource file +set(rc_script "${Source_dir}/goodbyedpi-rc.rc") +set(rc_script_proto "${CMake_Misc_Dir}/goodbyedpi-rc.rc.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") +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 "${divertLibraryPath}/WinDivert.dll") + +if(MINGW) + target_link_libraries(GoodbyeDPI "wsock32" "ws2_32" "ssp") + set_target_properties(GoodbyeDPI PROPERTIES COMPILE_FLAGS "-Wall -Wextra -Wconversion -pie -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -flto -mmitigate-rop -fstack-protector-strong -fno-common -fstack-check") + #-mcet + #-fsanitize=cfi + set_target_properties(GoodbyeDPI PROPERTIES LINK_FLAGS "-Wl,-O1,--sort-common,--as-needed,--dynamicbase,--nxcompat,--export-all-symbols,-flto") + + if(CMAKE_SIZEOF_VOID_P EQUAL 8) + set_target_properties(GoodbyeDPI PROPERTIES LINK_FLAGS "-Wl,--image-base,0x140000000") + endif() +elseif(MSVC) + set_target_properties(GoodbyeDPI PROPERTIES COMPILE_FLAGS "/sdl /GS /SafeSEH /NXCOMPAT /dynamicbase /guard:cf /HIGHENTROPYVA") + set_target_properties(GoodbyeDPI PROPERTIES LINK_FLAGS "/guard:cf") +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) \ No newline at end of file diff --git a/cmake/config.h.in b/cmake/config.h.in new file mode 100644 index 00000000..419202bc --- /dev/null +++ b/cmake/config.h.in @@ -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 +}; + +#cmakedefine GoodbyeDPI_DEBUG \ No newline at end of file diff --git a/cmake/goodbyedpi-rc.rc.in b/cmake/goodbyedpi-rc.rc.in new file mode 100644 index 00000000..d8904345 --- /dev/null +++ b/cmake/goodbyedpi-rc.rc.in @@ -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" \ No newline at end of file diff --git a/src/Makefile b/src/Makefile deleted file mode 100644 index 3fdc7b75..00000000 --- a/src/Makefile +++ /dev/null @@ -1,37 +0,0 @@ -ifndef MSYSTEM - CPREFIX = x86_64-w64-mingw32- -endif - -WINDIVERTHEADERS = ../../../include -WINDIVERTLIBS = ../../binary - -TARGET = goodbyedpi.exe -LIBS = -L$(WINDIVERTLIBS) -lWinDivert -lws2_32 -CC = $(CPREFIX)gcc -CCWINDRES = $(CPREFIX)windres -CFLAGS = -Wall -Wextra -I$(WINDIVERTHEADERS) -L$(WINDIVERTLIBS) \ - -O2 -pie -fPIE -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -LDFLAGS = -Wl,-O1,--sort-common,--as-needed - -.PHONY: default all clean - -default: manifest $(TARGET) -all: default - -OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c utils/*.c)) goodbyedpi-rc.o -HEADERS = $(wildcard *.h utils/*.h) - -%.o: %.c $(HEADERS) - $(CC) $(CFLAGS) -c $< -o $@ - -manifest: - $(CCWINDRES) goodbyedpi-rc.rc goodbyedpi-rc.o - -.PRECIOUS: $(TARGET) $(OBJECTS) - -$(TARGET): $(OBJECTS) - $(CC) $(OBJECTS) $(LDFLAGS) $(LIBS) -s -o $@ - -clean: - -rm -f *.o utils/*.o - -rm -f $(TARGET) diff --git a/src/goodbyedpi-rc.rc b/src/goodbyedpi-rc.rc deleted file mode 100644 index f8205760..00000000 --- a/src/goodbyedpi-rc.rc +++ /dev/null @@ -1,2 +0,0 @@ -1 24 "goodbyedpi.exe.manifest" -id ICON "icon.ico" diff --git a/src/goodbyedpi.c b/src/goodbyedpi.c index 65a262d6..d2356c37 100644 --- a/src/goodbyedpi.c +++ b/src/goodbyedpi.c @@ -23,9 +23,6 @@ WINSOCK_API_LINKAGE INT WSAAPI inet_pton(INT Family, LPCSTR pStringBuf, PVOID pA #define die() do { sleep(20); exit(EXIT_FAILURE); } while (0) -#define MAX_FILTERS 4 -#define MAX_PACKET_SIZE 9016 - #define DIVERT_NO_LOCALNETSv4_DST "(" \ "(ip.DstAddr < 127.0.0.1 or ip.DstAddr > 127.255.255.255) and " \ "(ip.DstAddr < 10.0.0.0 or ip.DstAddr > 10.255.255.255) and " \ @@ -764,7 +761,7 @@ int main(int argc, char *argv[]) { ); WinDivertHelperCalcChecksums( - packet, packetLen - packet_dataLen + http_fragment_size, 0 + packet, packetLen - packet_dataLen + http_fragment_size, &addr, 0 ); WinDivertSend( w_filter, packet, @@ -968,13 +965,7 @@ int main(int argc, char *argv[]) { if (should_reinject) { //printf("Re-injecting!\n"); - if (should_recalc_checksum) { - WinDivertHelperCalcChecksums(packet, packetLen, 0); - } - else { - WinDivertHelperCalcChecksums(packet, packetLen, - WINDIVERT_HELPER_NO_REPLACE); - } + WinDivertHelperCalcChecksums(packet, packetLen, &addr, 0); WinDivertSend(w_filter, packet, packetLen, &addr, NULL); } } diff --git a/src/goodbyedpi.h b/src/goodbyedpi.h index 626e85e1..f23a6507 100644 --- a/src/goodbyedpi.h +++ b/src/goodbyedpi.h @@ -1,6 +1,6 @@ -#define HOST_MAXLEN 253 +#include "config.h" -#ifndef DEBUG +#ifndef GoodbyeDPI_DEBUG #define debug(...) do {} while (0) #else #define debug(...) printf(__VA_ARGS__) diff --git a/src/service.c b/src/service.c index 0cdb13ca..5e0659c1 100644 --- a/src/service.c +++ b/src/service.c @@ -2,8 +2,7 @@ #include #include "goodbyedpi.h" #include "service.h" - -#define SERVICE_NAME "GoodbyeDPI" +#include "config.h" static SERVICE_STATUS ServiceStatus; static SERVICE_STATUS_HANDLE hStatus;