Skip to content

Commit

Permalink
添加linux端打包程序
Browse files Browse the repository at this point in the history
  • Loading branch information
da1234cao committed Mar 25, 2023
1 parent 4f3d8a0 commit ef45876
Show file tree
Hide file tree
Showing 16 changed files with 157 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
**/build
*/bin/**
windows/WFP/bin
windows/WFP/block/x64
windows/WFP/block/.vs
78 changes: 78 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"files.associations": {
"ostream": "cpp",
"any": "cpp",
"array": "cpp",
"atomic": "cpp",
"strstream": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"cfenv": "cpp",
"charconv": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"codecvt": "cpp",
"complex": "cpp",
"condition_variable": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"forward_list": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"regex": "cpp",
"source_location": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"shared_mutex": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cinttypes": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"valarray": "cpp",
"variant": "cpp",
"*.ipp": "cpp"
}
}
7 changes: 5 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.11)

project ("doamin_block")
project ("domain_block")

# 可执行文件输出到bin目录
## 静态库
Expand All @@ -13,6 +13,7 @@ SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
include_directories(${CMAKE_SOURCE_DIR})

# boost
set(Boost_USE_STATIC_LIBS ON)
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
find_package(Boost REQUIRED COMPONENTS thread system filesystem)
else()
Expand Down Expand Up @@ -46,12 +47,12 @@ FetchContent_Declare(
)
FetchContent_MakeAvailable(tomlplusplus)
include_directories(${tomlplusplus_SOURCE_DIR}/include)
# set(CPACK_EXCLUDE_FROM_ALL ${CPACK_EXCLUDE_FROM_ALL} ${tomlplusplus_SOURCE_DIR})
message("fetch toml from gitee instead of github")

# json
FetchContent_Declare(json URL https://github.com/nlohmann/json/releases/download/v3.11.2/json.tar.xz)
FetchContent_MakeAvailable(json)
include_directories(${tomlplusplus_SOURCE_DIR}/include)

if(CMAKE_SYSTEM_NAME MATCHES "Windows")
if(MSVC)
Expand All @@ -68,4 +69,6 @@ if(CMAKE_SYSTEM_NAME MATCHES "Windows")
else()
add_subdirectory(UI/domain_block_client)
add_subdirectory(linux)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/package/linux")
include(package)
endif()
6 changes: 4 additions & 2 deletions UI/domain_block_client/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ config::config() {
// 暂时没去查这种设置为啥不对
// QSettings settings("da1234cao", "domain_block");
QCoreApplication::setOrganizationName("da1234cao");
QCoreApplication::setApplicationName("domain_block");
QCoreApplication::setApplicationName("domain_block_client");

// 获取配置文件路径
QString appName = QCoreApplication::applicationName();
Expand All @@ -24,7 +24,9 @@ config::config() {
// 如果默认路径的配置文件不存在,则拷贝一份
QFileInfo defaultFile(m_default_path);
if (!defaultFile.exists()) {
QString currentPath = QDir::currentPath() + "/" + appName + ".ini";
QString currentPath =
QCoreApplication::applicationDirPath() + "/" + appName + ".ini";
qDebug() << currentPath;
utils::copyFile(currentPath, m_default_path);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 服务器配置
[server]
ip = 192.168.2.149
ip = 127.0.0.1
port = 6666
Binary file added bin/domain_block_client
Binary file not shown.
Binary file added bin/domain_block_service
Binary file not shown.
Binary file added bin/libspdlog.a
Binary file not shown.
3 changes: 2 additions & 1 deletion linux/domain_block.service
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[Unit]
Description=A domain block service
[Service]
ExecStart=/home/dacao/work/self/domain_block/bin/domain_block_service
ExecStart=/opt/domain_block/domain_block_service
ExecStop=/bin/kill -TERM $MAINPID
Type=simple
TimeoutStopSec=1
[Install]
WantedBy=multi-user.target
2 changes: 1 addition & 1 deletion linux/config.toml → linux/domain_block_service.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 日志文件
[log]
path = 'C:\domain_block_services.log'
path = '/var/log/domain_block/domain_block_services.log'
level = 'trace'
max_file_size = '1' # MB单位
max_files = '3'
Expand Down
22 changes: 0 additions & 22 deletions linux/log.txt

This file was deleted.

5 changes: 4 additions & 1 deletion linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
int main(int argc, char *agrv[]) {
boost::filesystem::path cur_path =
boost::dll::program_location().parent_path();
boost::filesystem::path cfg_path = cur_path / "config.toml";
boost::filesystem::path cfg_path = cur_path / "domain_block_service.toml";

// 解析配置文件
config::instance().parse_toml(cfg_path);
Expand All @@ -25,7 +25,10 @@ int main(int argc, char *agrv[]) {
// 创建server对象
server<nf_connection> s(config::instance().get_str("ip"),
std::to_string(config::instance().get_int("port")));
nf_help::instance(); // 显示的在server启动前创建nftalbe的表和链
s.run();

nf_help::instance().flush_all();
LOG_DEBUG("process end.");
return 0;
}
33 changes: 18 additions & 15 deletions linux/nf_help.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,24 @@ class nf_help {
exec_rules(nf_rules);
}

~nf_help() { flush_all(); }
void flush_all() {
std::vector<std::string> nf_exit_cmds;
// 清空链
nf_exit_cmds.emplace_back("nft flush chain inet block OUTPUT");
// 删除链
nf_exit_cmds.emplace_back("nft delete chain inet block OUTPUT");
// 清空表
nf_exit_cmds.emplace_back("nft flush table inet block");
// 删除表
nf_exit_cmds.emplace_back("nft delete table inet block");

exec_rules(nf_exit_cmds);
}

// 这里在使用systemctl管理的时候,析构函数没有执行完便退出,不知道为什么
// 单独运行程序,不使用systemctl则没有这个问题
// 所以,这里把flush_all()放在main()末尾显示执行
// ~nf_help() { flush_all(); }

private:
nf_help() {
Expand All @@ -94,20 +111,6 @@ class nf_help {
exec_rules(nf_init_cmds);
}

void flush_all() {
std::vector<std::string> nf_exit_cmds;
// 清空链
nf_exit_cmds.emplace_back("nft flush chain inet block OUTPUT");
// 删除链
nf_exit_cmds.emplace_back("nft delete chain inet block OUTPUT");
// 清空表
nf_exit_cmds.emplace_back("nft flush table inet block");
// 删除表
nf_exit_cmds.emplace_back("nft delete table inet block");

exec_rules(nf_exit_cmds);
}

void exec_rules(std::vector<std::string> &cmds) {
// 同步执行,不要执行耗时任务
for (auto &cmd : cmds) {
Expand Down
36 changes: 36 additions & 0 deletions package/linux/package.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

cmake_minimum_required(VERSION 3.11)

set(CPACK_PACKAGE_DESCRIPTION "domain block service")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "域名阻断的服务程序")
set(CPACK_PACKAGE_VENDOR "da234cao")
set(CPACK_PACKAGE_CONTACT "da1234cao.github.io")
set(CPACK_PACKAGE_NAME "domain_block_service")
set(CPACK_PACKAGE_VERSION_MAJOR 0)
set(CPACK_PACKAGE_VERSION_MINOR 1)
set(CPACK_PACKAGE_VERSION_PATCH 0)

set(INSTALL_DIR /opt/domain_block)
set(SYSTEMD_DIR /etc/systemd/system/)
set(POST_INSTALL_SCRIPT ${CMAKE_SOURCE_DIR}/linux/package/post_install.sh)

# 服务程序
install(TARGETS domain_block_service RUNTIME DESTINATION ${INSTALL_DIR})
# 服务端配置文件-把配置文件和可执行程序防在一起,不符合fhs
install(FILES ${CMAKE_SOURCE_DIR}/linux/domain_block_service.toml DESTINATION ${INSTALL_DIR})

# 客户端程序
# 客户端配置文件-执行的时候会复制一份到家目录的.config文件
install(TARGETS domain_block_client RUNTIME DESTINATION ${INSTALL_DIR})
install(FILES ${CMAKE_SOURCE_DIR}/UI/domain_block_client/domain_block_client.ini DESTINATION ${INSTALL_DIR})


# systemctl配置文件
install(FILES ${CMAKE_SOURCE_DIR}/linux/domain_block.service DESTINATION ${SYSTEMD_DIR})

set(CPACK_DEBIAN_PACKAGE_POST_INSTALL_SCRIPT_FILE ${POST_INSTALL_SCRIPT})

set(CPACK_GENERATOR "DEB")

include(CPack)

6 changes: 6 additions & 0 deletions package/linux/postinst.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

systemctl daemon-reload
systemctl start domain_block

echo "postinst end.\n"
1 change: 1 addition & 0 deletions server/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ template <typename T = connection> class server {
m_signals.add(SIGTERM);
m_signals.async_wait([this](boost::system::error_code ec, int signo) {
if (signo == SIGINT || signo == SIGTERM) {
LOG_DEBUG("server receive signo: {}", signo);
stop();
}
});
Expand Down

0 comments on commit ef45876

Please sign in to comment.