Fix codes for compiling with mbedTLS

Clean up codes. (+6 squashed commit)

Squashed commit:

[4126d9e] Update build scripts

[2ee5027] Update CMake module

[71da951] Update build scripts

[dea8d3f] Update CMake script

[92113ba] Clean up codes and update build script
This commit is contained in:
Tindy X 2020-02-02 23:23:40 +08:00
parent 00976c9eeb
commit c05b7913af
No known key found for this signature in database
GPG Key ID: C6AD413169968D58
13 changed files with 5255 additions and 85 deletions

View File

@ -1,6 +1,7 @@
project(subconverter LANGUAGES CXX)
cmake_minimum_required(VERSION 3.4)
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}/include/")
IF(NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE Release)
ENDIF()
@ -9,12 +10,13 @@ ADD_DEFINITIONS(-Wall -Wextra -Wno-unused-parameter -Wno-unused-result)
OPTION(USING_STD_REGEX "Use std::regex from C++ library instead of PCRE2." OFF)
OPTION(USING_MALLOC_TRIM "Call malloc_trim after processing request to lower memory usage (Your system must support malloc_trim)." OFF)
OPTION(USING_MBEDTLS "Use mbedTLS instead of OpenSSL for MD5 calculation." OFF)
INCLUDE(CheckCXXSourceCompiles)
CHECK_CXX_SOURCE_COMPILES(
"
#include<string>
int main(){std::to_string(0);return 0;}
void main(){std::to_string(0);}
" HAVE_TO_STRING)
IF(APPLE)
@ -61,9 +63,16 @@ INCLUDE_DIRECTORIES(${CURL_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(subconverter ${CURL_LIBRARIES})
ADD_DEFINITIONS(-DCURL_STATICLIB)
FIND_PACKAGE(OpenSSL 1.1.0 REQUIRED)
INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(subconverter ${OPENSSL_LIBRARIES})
IF(USING_MBEDTLS STREQUAL "ON")
FIND_PACKAGE(MbedTLS REQUIRED)
INCLUDE_DIRECTORIES(${MBEDTLS_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(subconverter ${MBEDCRYPTO_LIBRARY})
ADD_DEFINITIONS(-DUSE_MBEDTLS)
ELSE()
FIND_PACKAGE(OpenSSL 1.1.0 REQUIRED)
INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(subconverter ${OPENSSL_LIBRARIES})
ENDIF()
FIND_PACKAGE(Rapidjson REQUIRED)
INCLUDE_DIRECTORIES(${RAPIDJSON_INCLUDE_DIRS})
@ -77,13 +86,10 @@ TARGET_LINK_LIBRARIES(subconverter ${YAML_CPP_LIBRARY})
IF(USING_STD_REGEX STREQUAL "ON")
ADD_DEFINITIONS(-DUSE_STD_REGEX)
ELSE()
PKG_CHECK_MODULES(PCRE2 libpcre2-8 REQUIRED)
FIND_PACKAGE(PCRE2 REQUIRED)
INCLUDE_DIRECTORIES(${PCRE2_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(subconverter ${PCRE2_LIBRARIES})
TARGET_LINK_LIBRARIES(subconverter ${PCRE2_LIBRARY})
ADD_DEFINITIONS(-DPCRE2_STATIC)
FIND_PACKAGE(JPCRE2 REQUIRED)
INCLUDE_DIRECTORIES(${JPCRE2_INCLUDE_DIRS})
ENDIF()
IF(WIN32)

View File

@ -1,27 +0,0 @@
# - Find JPCRE2
# Find the JPCRE2 headers.
#
# JPCRE2_INCLUDE_DIRS - where to find jpcre2.h.
# JPCRE2_FOUND - True if JPCRE2 found.
# Look for the header file.
FIND_PATH(JPCRE2_INCLUDE_DIR NAMES jpcre2.hpp)
MARK_AS_ADVANCED(JPCRE2_INCLUDE_DIR)
if(JPCRE2_INCLUDE_DIR)
set(JPCRE2_FOUND TRUE)
endif()
IF(JPCRE2_FOUND)
SET(JPCRE2_INCLUDE_DIRS ${JPCRE2_INCLUDE_DIR})
ENDIF(JPCRE2_FOUND)
if(JPCRE2_FOUND)
if(NOT JPCRE2_FIND_QUIETLY)
message(STATUS "Found JPCRE2 header files in ${JPCRE2_INCLUDE_DIRS}")
endif()
elseif(JPCRE2_FIND_REQUIRED)
message(FATAL_ERROR "Could not find JPCRE2")
else()
message(STATUS "Optional package JPCRE2 was not found")
endif()

13
cmake/FindMbedTLS.cmake Normal file
View File

@ -0,0 +1,13 @@
find_path(MBEDTLS_INCLUDE_DIRS mbedtls/ssl.h)
find_library(MBEDTLS_LIBRARY mbedtls)
find_library(MBEDX509_LIBRARY mbedx509)
find_library(MBEDCRYPTO_LIBRARY mbedcrypto)
set(MBEDTLS_LIBRARIES "${MBEDTLS_LIBRARY}" "${MBEDX509_LIBRARY}" "${MBEDCRYPTO_LIBRARY}")
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(MBEDTLS DEFAULT_MSG
MBEDTLS_INCLUDE_DIRS MBEDTLS_LIBRARY MBEDX509_LIBRARY MBEDCRYPTO_LIBRARY)
mark_as_advanced(MBEDTLS_INCLUDE_DIRS MBEDTLS_LIBRARY MBEDX509_LIBRARY MBEDCRYPTO_LIBRARY)

119
cmake/FindPCRE2.cmake Normal file
View File

@ -0,0 +1,119 @@
#
#
# Locate pcre2
#
# This module accepts the following environment variables:
#
# PCRE2_DIR or PCRE2_ROOT - Specify the location of PCRE2
#
# This module defines the following CMake variables:
#
# PCRE2_FOUND - True if libpcre2 is found
# PCRE2_LIBRARY - A variable pointing to the PCRE2 library
# PCRE2_INCLUDE_DIR - Where to find the headers
#=============================================================================
# Inspired by FindGDAL
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See COPYING-CMAKE-SCRIPTS for more information.
#=============================================================================
# This makes the presumption that you are include pcre2.h like
#
#include "pcre2.h"
if (DEFINED PCRE2_ROOT AND NOT PCRE2_ROOT)
set (PCRE2_LIBRARY "" CACHE INTERNAL "")
set (PCRE2_INCLUDE_DIR "" CACHE INTERNAL "")
return ()
endif (DEFINED PCRE2_ROOT AND NOT PCRE2_ROOT)
if (UNIX AND NOT PCRE2_FOUND)
# Use pcre2-config to obtain the library location and name, something like
# -L/sw/lib -lpcre2-8)
find_program (PCRE2_CONFIG pcre2-config
HINTS
${PCRE2_DIR}
${PCRE2_ROOT}
$ENV{PCRE2_DIR}
$ENV{PCRE2_ROOT}
PATH_SUFFIXES bin
PATHS
/sw # Fink
/opt/local # DarwinPorts
/opt/csw # Blastwave
/opt
/usr/local
)
if (PCRE2_CONFIG)
execute_process (COMMAND ${PCRE2_CONFIG} --cflags
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE PCRE2_CONFIG_CFLAGS)
if (PCRE2_CONFIG_CFLAGS)
string (REGEX MATCHALL "-I[^ ]+" _pcre2_dashI ${PCRE2_CONFIG_CFLAGS})
string (REGEX REPLACE "-I" "" _pcre2_includepath "${_pcre2_dashI}")
string (REGEX REPLACE "-I[^ ]+" "" _pcre2_cflags_other ${PCRE2_CONFIG_CFLAGS})
endif (PCRE2_CONFIG_CFLAGS)
execute_process (COMMAND ${PCRE2_CONFIG} --libs8
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE PCRE2_CONFIG_LIBS)
if (PCRE2_CONFIG_LIBS)
string (REGEX MATCHALL "-l[^ ]+" _pcre2_dashl ${PCRE2_CONFIG_LIBS})
string (REGEX REPLACE "-l" "" _pcre2_lib "${_pcre2_dashl}")
string (REGEX MATCHALL "-L[^ ]+" _pcre2_dashL ${PCRE2_CONFIG_LIBS})
string (REGEX REPLACE "-L" "" _pcre2_libpath "${_pcre2_dashL}")
endif (PCRE2_CONFIG_LIBS)
endif (PCRE2_CONFIG)
endif (UNIX AND NOT PCRE2_FOUND)
find_path (PCRE2_INCLUDE_DIR pcre2.h
HINTS
${_pcre2_includepath}
${PCRE2_DIR}
${PCRE2_ROOT}
$ENV{PCRE2_DIR}
$ENV{PCRE2_ROOT}
PATH_SUFFIXES
include/pcre2
include/PCRE2
include
PATHS
~/Library/Frameworks/pcre2.framework/Headers
/Library/Frameworks/pcre2.framework/Headers
/sw # Fink
/opt/local # DarwinPorts
/opt/csw # Blastwave
/opt
/usr/local
)
find_library (PCRE2_LIBRARY
NAMES ${_pcre2_lib} pcre2-8 PCRE2
HINTS
${PCRE2_DIR}
${PCRE2_ROOT}
$ENV{PCRE2_DIR}
$ENV{PCRE2_ROOT}
${_pcre2_libpath}
PATH_SUFFIXES lib
PATHS
~/Library/Frameworks/pcre2.framework
/Library/Frameworks/pcre2.framework
/sw
/opt/local
/opt/csw
/opt
/usr/local
)
include (FindPackageHandleStandardArgs)
find_package_handle_standard_args (PCRE2 DEFAULT_MSG PCRE2_LIBRARY PCRE2_INCLUDE_DIR)
set (PCRE2_LIBRARIES ${PCRE2_LIBRARY})
set (PCRE2_INCLUDE_DIRS ${PCRE2_INCLUDE_DIR})

View File

@ -1,33 +0,0 @@
# - Find PCRECPP
# Find the native PCRECPP headers and libraries.
#
# PCRECPP_INCLUDE_DIRS - where to find pcre.h, etc.
# PCRECPP_LIBRARIES - List of libraries when using pcrecpp
# PCRECPP_FOUND - True if pcrecpp found.
# Look for the header file.
FIND_PATH(PCRECPP_INCLUDE_DIR NAMES pcrecpp.h)
MARK_AS_ADVANCED(PCRECPP_INCLUDE_DIR)
# Look for the library.
FIND_LIBRARY(PCRECPP_LIBRARY NAMES
pcrecpp
pcrecppd
)
MARK_AS_ADVANCED(PCRECPP_LIBRARY)
FIND_LIBRARY(PCRE_LIBRARY NAMES
pcre
pcred
)
MARK_AS_ADVANCED(PCRE_LIBRARY)
# handle the QUIETLY and REQUIRED arguments and set PCRECPP_FOUND to TRUE if
# all listed variables are TRUE
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(PCRECPP DEFAULT_MSG PCRECPP_LIBRARY PCRECPP_INCLUDE_DIR PCRE_LIBRARY)
IF(PCRECPP_FOUND)
SET(PCRECPP_LIBRARIES ${PCRECPP_LIBRARY} ${PCRE_LIBRARY})
SET(PCRECPP_INCLUDE_DIRS ${PCRECPP_INCLUDE_DIR})
ENDIF(PCRECPP_FOUND)

5101
include/jpcre2.hpp Normal file

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,7 @@ mkdir obj
set -xe
apk add gcc g++ build-base linux-headers cmake make autoconf automake libtool
apk add openssl-dev openssl-libs-static curl-dev curl-static nghttp2-static zlib-dev rapidjson-dev libevent-dev libevent-static zlib-static pcre2-dev bzip2-static
apk add openssl-dev openssl-libs-static curl curl-dev curl-static nghttp2-static zlib-dev rapidjson-dev libevent-dev libevent-static zlib-static pcre2-dev bzip2-static
git clone https://github.com/jbeder/yaml-cpp
cd yaml-cpp
@ -11,9 +11,6 @@ cmake -DYAML_CPP_BUILD_TESTS=OFF -DYAML_CPP_BUILD_TOOLS=OFF . > /dev/null
make install -j4 > /dev/null
cd ..
curl -LO https://raw.githubusercontent.com/jpcre2/jpcre2/release/src/jpcre2.hpp
cp jpcre2.hpp /usr/local/include/
cmake .
make -j4
g++ -o base/subconverter CMakeFiles/subconverter.dir/src/*.o -static -lpcre2-8 -levent -lyaml-cpp -lcurl -lnghttp2 -lssl -lcrypto -lz -lbz2 -ldl -lpthread -O3 -s

View File

@ -11,9 +11,6 @@ cd curl
make -j8 > /dev/null
cd ..
curl -LO https://raw.githubusercontent.com/jpcre2/jpcre2/release/src/jpcre2.hpp
cp jpcre2.hpp /usr/local/include/
cp curl/lib/.libs/libcurl.a .
cp /usr/local/lib/libevent.a .
cp /usr/local/opt/zlib/lib/libz.a .

View File

@ -15,6 +15,3 @@ git clone https://github.com/tencent/rapidjson
cd rapidjson
cp -r include/* /data/data/com.termux/files/usr/include/
cd ..
curl -LO https://raw.githubusercontent.com/jpcre2/jpcre2/release/src/jpcre2.hpp
cp jpcre2.hpp /data/data/com.termux/files/usr/include/

View File

@ -298,7 +298,6 @@ void readConf()
ini.GetAll("surge_ruleset", rulesets);
importItems(rulesets);
}
}
else
{

View File

@ -560,7 +560,7 @@ bool regMatch(std::string src, std::string match)
#else
bool regMatch(std::string src, std::string target, bool partial)
bool regMatch(std::string src, std::string target)
{
jp::Regex reg;
reg.setPattern(target).addModifier("gm").compile();
@ -638,8 +638,10 @@ std::string getMD5(std::string data)
mbedtls_md5_context ctx;
mbedtls_md5_init(&ctx);
mbedtls_md5_update(&ctx, data.data(), data.size());
mbedtls_md5_finish(&ctx, (unsigned char *)&digest);
mbedtls_md5_starts_ret(&ctx);
mbedtls_md5_update_ret(&ctx, reinterpret_cast<const unsigned char*>(data.data()), data.size());
mbedtls_md5_finish_ret(&ctx, reinterpret_cast<unsigned char*>(&digest));
mbedtls_md5_free(&ctx);
#else
MD5_CTX ctx;

View File

@ -43,7 +43,7 @@ void sleep(int interval);
bool regValid(std::string &target);
bool regFind(std::string src, std::string target);
std::string regReplace(std::string src, std::string match, std::string rep);
bool regMatch(std::string src, std::string match, bool partial = false);
bool regMatch(std::string src, std::string match);
std::string speedCalc(double speed);
std::string getMD5(std::string data);
bool isIPv4(std::string &address);

View File

@ -5,7 +5,6 @@
#include <time.h>
#include <rapidjson/document.h>
#include <pcrecpp.h>
#include "misc.h"
#include "printout.h"