From 051742eef1ec19edf329b66ac8ae42f982d973f3 Mon Sep 17 00:00:00 2001 From: fancy Date: Mon, 18 May 2020 04:25:57 +0800 Subject: [PATCH] rename .hpp to .h --- src/CMakeLists.txt | 20 ++++---- src/cgproxy.cpp | 18 ++++---- src/cgproxyd.cpp | 20 +++----- src/cgroup_attach.cpp | 13 +++++- src/{cgroup_attach.hpp => cgroup_attach.h} | 13 ------ src/common.cpp | 3 +- src/{common.hpp => common.h} | 20 ++++---- src/config.cpp | 29 +++++++----- src/{config.hpp => config.h} | 21 +++------ src/socket_client.cpp | 14 +++++- src/socket_client.h | 14 ++++++ src/socket_client.hpp | 30 ------------ src/socket_server.cpp | 13 +++++- src/{socket_server.hpp => socket_server.h} | 11 ----- test/CMakeLists.txt | 1 + test/socket_client_test.cpp | 53 +++++++++++----------- test/test.cpp | 12 ----- tools/CMakeLists.txt | 2 +- tools/cgattach.cpp | 5 +- 19 files changed, 146 insertions(+), 166 deletions(-) rename src/{cgroup_attach.hpp => cgroup_attach.h} (68%) rename src/{common.hpp => common.h} (97%) rename src/{config.hpp => config.h} (71%) create mode 100644 src/socket_client.h delete mode 100644 src/socket_client.hpp rename src/{socket_server.hpp => socket_server.h} (77%) delete mode 100644 test/test.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0336899..80eb72a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,22 +3,22 @@ find_package(nlohmann_json REQUIRED) include_directories(${PROJECT_SOURCE_DIR}) include_directories(${CMAKE_CURRENT_SOURCE_DIR}) -add_library(cgproxy-lib - SHARED - common.cpp - config.cpp - cgroup_attach.cpp - socket_server.cpp socket_client.cpp - ) +add_library(cgproxy-lib SHARED config.cpp socket_client.cpp common.cpp) set_target_properties(cgproxy-lib PROPERTIES LINKER_LANGUAGE CXX) set_target_properties(cgproxy-lib PROPERTIES OUTPUT_NAME cgproxy) target_link_libraries(cgproxy-lib nlohmann_json::nlohmann_json) +add_library(cgproxyd-lib SHARED config.cpp cgroup_attach.cpp socket_server.cpp common.cpp) +set_target_properties(cgproxyd-lib PROPERTIES LINKER_LANGUAGE CXX) +set_target_properties(cgproxyd-lib PROPERTIES OUTPUT_NAME cgproxyd) +target_link_libraries(cgproxyd-lib nlohmann_json::nlohmann_json) + add_executable(cgproxyd cgproxyd.cpp) -add_executable(cgproxy cgproxy.cpp) -target_link_libraries(cgproxyd cgproxy-lib Threads::Threads nlohmann_json::nlohmann_json) -target_link_libraries(cgproxy cgproxy-lib nlohmann_json::nlohmann_json) +add_executable(cgproxy cgproxy.cpp) +target_link_libraries(cgproxyd cgproxyd-lib Threads::Threads) +target_link_libraries(cgproxy cgproxy-lib) install(TARGETS cgproxyd DESTINATION /usr/bin PERMISSIONS ${basic_permission}) install(TARGETS cgproxy DESTINATION /usr/bin PERMISSIONS ${basic_permission}) install(TARGETS cgproxy-lib DESTINATION /usr/lib PERMISSIONS ${basic_permission}) +install(TARGETS cgproxyd-lib DESTINATION /usr/lib PERMISSIONS ${basic_permission}) diff --git a/src/cgproxy.cpp b/src/cgproxy.cpp index 9fa5aca..f8af4ee 100644 --- a/src/cgproxy.cpp +++ b/src/cgproxy.cpp @@ -1,11 +1,14 @@ -#include "common.hpp" -#include "socket_client.hpp" +#include "common.h" +#include "config.h" +#include "socket_client.h" #include +#include using json = nlohmann::json; using namespace CGPROXY; +using namespace CGPROXY::CONFIG; bool print_help = false, proxy = true; -void print_usage() { +inline void print_usage() { fprintf(stdout, "Usage: cgproxy [--help] [--debug] [--noproxy] \n"); fprintf(stdout, "Alias: cgnoproxy = cgproxy --noproxy\n"); } @@ -20,13 +23,11 @@ void processArgs(const int argc, char *argv[], int &shift) { } } -bool attach2cgroup(pid_t pid, bool proxy) { +void send_pid(const pid_t pid, bool proxy, int &status) { json j; j["type"] = proxy ? MSG_TYPE_PROXY_PID : MSG_TYPE_NOPROXY_PID; j["data"] = pid; - int status; SOCKET::send(j.dump(), status); - return status == 0; } int main(int argc, char *argv[]) { @@ -38,8 +39,9 @@ int main(int argc, char *argv[]) { exit(0); } - pid_t pid = getpid(); - if (!attach2cgroup(pid, proxy)) { + int status = -1; + send_pid(getpid(), proxy, status); + if (status != 0) { error("attach process failed"); exit(EXIT_FAILURE); } diff --git a/src/cgproxyd.cpp b/src/cgproxyd.cpp index b8cd624..ffdfefd 100644 --- a/src/cgproxyd.cpp +++ b/src/cgproxyd.cpp @@ -1,18 +1,12 @@ -#include "cgroup_attach.hpp" -#include "common.hpp" -#include "config.hpp" -#include "socket_server.hpp" +#include "cgroup_attach.h" +#include "common.h" +#include "config.h" +#include "socket_server.h" #include -#include #include -#include #include -#include -#include -#include -#include -#include #include +#include using namespace std; using json = nlohmann::json; @@ -81,8 +75,8 @@ class cgproxyd { try { type = j.at("type").get(); switch (type) { - case MSG_TYPE_JSON: - status = config.loadFromJson(j.at("data")); + case MSG_TYPE_CONFIG_JSON: + status = config.loadFromJsonStr(j.at("data").dump()); if (status == SUCCESS) status = applyConfig(&config); return status; break; diff --git a/src/cgroup_attach.cpp b/src/cgroup_attach.cpp index 0a88f60..d2b1df0 100644 --- a/src/cgroup_attach.cpp +++ b/src/cgroup_attach.cpp @@ -1,4 +1,15 @@ -#include "cgroup_attach.hpp" +#include "cgroup_attach.h" +#include "common.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include namespace CGPROXY::CGROUP { diff --git a/src/cgroup_attach.hpp b/src/cgroup_attach.h similarity index 68% rename from src/cgroup_attach.hpp rename to src/cgroup_attach.h index ed09769..ae0ab7d 100644 --- a/src/cgroup_attach.hpp +++ b/src/cgroup_attach.h @@ -1,29 +1,16 @@ #ifndef CGPROUP_ATTACH_H #define CGPROUP_ATTACH_H -#include "common.hpp" -#include -#include -#include -#include -#include #include #include -#include -#include -#include using namespace std; namespace CGPROXY::CGROUP { bool exist(string path); - bool validate(string pid, string cgroup); - string get_cgroup2_mount_point(int &status); - int attach(const string pid, const string cgroup_target); - int attach(const int pid, const string cgroup_target); } // namespace CGPROXY::CGROUP diff --git a/src/common.cpp b/src/common.cpp index 4f3214b..47f3cf2 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -1,4 +1,5 @@ -#include "common.hpp" +#include "common.h" +#include bool enable_debug = false; diff --git a/src/common.hpp b/src/common.h similarity index 97% rename from src/common.hpp rename to src/common.h index 55d27f0..0c20b88 100644 --- a/src/common.hpp +++ b/src/common.h @@ -1,6 +1,12 @@ #ifndef COMMON_H #define COMMON_H 1 +#include +#include +#include +#include +using namespace std; + #define TPROXY_IPTABLS_START "sh /usr/share/cgproxy/scripts/cgroup-tproxy.sh" #define TPROXY_IPTABLS_CLEAN "sh /usr/share/cgproxy/scripts/cgroup-tproxy.sh stop" @@ -12,7 +18,7 @@ #define CGROUP_PROXY_PRESVERED "/proxy.slice" #define CGROUP_NOPROXY_PRESVERED "/noproxy.slice" -#define MSG_TYPE_JSON 1 +#define MSG_TYPE_CONFIG_JSON 1 #define MSG_TYPE_CONFIG_PATH 2 #define MSG_TYPE_PROXY_PID 3 #define MSG_TYPE_NOPROXY_PID 4 @@ -28,12 +34,6 @@ #define CGROUP_ERROR 6 #define FILE_ERROR 7 -#include -#include -#include -#include -using namespace std; - extern bool enable_debug; #define error(...) \ @@ -41,11 +41,13 @@ extern bool enable_debug; fprintf(stderr, __VA_ARGS__); \ fprintf(stderr, "\n"); \ } + #define debug(...) \ if (enable_debug) { \ fprintf(stdout, __VA_ARGS__); \ fprintf(stdout, "\n"); \ } + #define return_error return -1; #define return_success return 0; @@ -58,15 +60,11 @@ template string to_str(T... args) { } string join2str(const vector t, const char delm = ' '); - string join2str(const int argc, char **argv, const char delm = ' '); bool validCgroup(const string cgroup); - bool validCgroup(const vector cgroup); - bool validPid(const string pid); - bool validPort(const int port); #endif \ No newline at end of file diff --git a/src/config.cpp b/src/config.cpp index 685ee34..00de499 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -1,4 +1,9 @@ -#include "config.hpp" +#include "config.h" +#include +#include +#include +#include +using json = nlohmann::json; #define add2json(v) j[#v] = v; #define tryassign(v) \ @@ -29,13 +34,13 @@ void Config::toEnv() { int Config::saveToFile(const string f) { ofstream o(f); if (!o.is_open()) return FILE_ERROR; - json j = toJson(); - o << setw(4) << j << endl; + string js = toJsonStr(); + o << setw(4) << js << endl; o.close(); return 0; } -json Config::toJson() { +string Config::toJsonStr() { json j; add2json(cgroup_proxy); add2json(cgroup_noproxy); @@ -46,34 +51,35 @@ json Config::toJson() { add2json(enable_udp); add2json(enable_ipv4); add2json(enable_ipv6); - return j; + return j.dump(); } int Config::loadFromFile(const string f) { debug("loading config: %s", f.c_str()); ifstream ifs(f); if (ifs.is_open()) { - json j; + string js; try { - ifs >> j; + ifs >> js; } catch (exception &e) { error("parse error: %s", f.c_str()); ifs.close(); return PARSE_ERROR; } ifs.close(); - return loadFromJson(j); + return loadFromJsonStr(js); } else { error("open failed: %s", f.c_str()); return FILE_ERROR; } } -int Config::loadFromJson(const json &j) { - if (!validateJson(j)) { +int Config::loadFromJsonStr(const string js) { + if (!validateJsonStr(js)) { error("json validate fail"); return PARAM_ERROR; } + json j = json::parse(js); tryassign(cgroup_proxy); tryassign(cgroup_noproxy); tryassign(enable_gateway); @@ -91,7 +97,8 @@ void Config::mergeReserved() { merge(cgroup_noproxy); } -bool Config::validateJson(const json &j) { +bool Config::validateJsonStr(const string js) { + json j = json::parse(js); bool status = true; const set boolset = {"enable_gateway", "enable_dns", "enable_tcp", "enable_udp", "enable_ipv4", "enable_ipv6"}; diff --git a/src/config.hpp b/src/config.h similarity index 71% rename from src/config.hpp rename to src/config.h index 47a5489..4273335 100644 --- a/src/config.hpp +++ b/src/config.h @@ -1,18 +1,10 @@ #ifndef CONFIG_H #define CONFIG_H -#include "common.hpp" -#include -#include -#include -#include -#include -#include -#include +#include "common.h" #include #include #include using namespace std; -using json = nlohmann::json; namespace CGPROXY::CONFIG { @@ -21,7 +13,6 @@ public: const string cgroup_proxy_preserved = CGROUP_PROXY_PRESVERED; const string cgroup_noproxy_preserved = CGROUP_NOPROXY_PRESVERED; -private: vector cgroup_proxy; vector cgroup_noproxy; bool enable_gateway = false; @@ -32,14 +23,16 @@ private: bool enable_ipv4 = true; bool enable_ipv6 = true; -public: void toEnv(); int saveToFile(const string f); - json toJson(); + string toJsonStr(); int loadFromFile(const string f); - int loadFromJson(const json &j); + int loadFromJsonStr(const string js); + +private: void mergeReserved(); - bool validateJson(const json &j); + bool validateJsonStr(const string js); + }; } // namespace CGPROXY::CONFIG diff --git a/src/socket_client.cpp b/src/socket_client.cpp index f32723e..26c25d3 100644 --- a/src/socket_client.cpp +++ b/src/socket_client.cpp @@ -1,4 +1,16 @@ -#include "socket_client.hpp" +#include "socket_client.h" +#include "common.h" +#include +#include +#include + +#define return_if_error(flag, msg) \ + if (flag == -1) { \ + perror(msg); \ + status = CONN_ERROR; \ + close(sfd); \ + return; \ + } namespace CGPROXY::SOCKET { diff --git a/src/socket_client.h b/src/socket_client.h new file mode 100644 index 0000000..839db5f --- /dev/null +++ b/src/socket_client.h @@ -0,0 +1,14 @@ +#ifndef SOCKET_CLIENT_H +#define SOCKET_CLIENT_H + +#include +#include +using namespace std; + +namespace CGPROXY::SOCKET { + +void send(const char *msg, int &status); +void send(const string msg, int &status); + +} // namespace CGPROXY::SOCKET +#endif \ No newline at end of file diff --git a/src/socket_client.hpp b/src/socket_client.hpp deleted file mode 100644 index b25c84e..0000000 --- a/src/socket_client.hpp +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef SOCKET_CLIENT_H -#define SOCKET_CLIENT_H - -#include "common.hpp" -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace std; - -namespace CGPROXY::SOCKET { - -#define return_if_error(flag, msg) \ - if (flag == -1) { \ - perror(msg); \ - status = CONN_ERROR; \ - close(sfd); \ - return; \ - } - -void send(const char *msg, int &status); -void send(const string msg, int &status); - -} // namespace CGPROXY::SOCKET -#endif \ No newline at end of file diff --git a/src/socket_server.cpp b/src/socket_server.cpp index ee6fe4c..ae15ab3 100644 --- a/src/socket_server.cpp +++ b/src/socket_server.cpp @@ -1,6 +1,14 @@ -#include "socket_server.hpp" +#include "socket_server.h" +#include "common.h" +#include +#include +#include +#include + +namespace fs = std::filesystem; namespace CGPROXY::SOCKET { + void SocketServer::socketListening(function callback) { debug("starting socket listening"); sfd = socket(AF_UNIX, SOCK_STREAM, 0); @@ -40,15 +48,18 @@ void SocketServer::socketListening(function callback) { continue_if_error(flag, "write back"); } } + void *SocketServer::startThread(void *arg) { thread_arg *p = (thread_arg *)arg; SocketServer server; server.socketListening(p->handle_msg); return (void *)0; } + SocketServer::~SocketServer() { close(sfd); close(cfd); unlink(SOCKET_PATH); } + } // namespace CGPROXY::SOCKET \ No newline at end of file diff --git a/src/socket_server.hpp b/src/socket_server.h similarity index 77% rename from src/socket_server.hpp rename to src/socket_server.h index 392825c..c70bbb1 100644 --- a/src/socket_server.hpp +++ b/src/socket_server.h @@ -1,21 +1,10 @@ #ifndef SOCKET_SERVER_H #define SOCKET_SERVER_H -#include "common.hpp" -#include #include -#include -#include -#include #include -#include -#include -#include -#include #include -#include using namespace std; -namespace fs = std::filesystem; namespace CGPROXY::SOCKET { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7bd3acc..ac81e3f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -3,4 +3,5 @@ include_directories(${PROJECT_SOURCE_DIR}/src) find_package(nlohmann_json REQUIRED) add_executable(client_test socket_client_test.cpp) +target_link_libraries(client_test cgproxy-lib) target_link_libraries(client_test nlohmann_json::nlohmann_json) \ No newline at end of file diff --git a/test/socket_client_test.cpp b/test/socket_client_test.cpp index be28277..8be3754 100644 --- a/test/socket_client_test.cpp +++ b/test/socket_client_test.cpp @@ -1,48 +1,49 @@ -#include "socket_client.hpp" +#include "common.h" +#include "config.h" +#include "socket_client.h" #include using namespace std; using json = nlohmann::json; using namespace CGPROXY; +using namespace CGPROXY::CONFIG; -void test_json() { +void send_config(Config &config, int &status) { json j; - j["type"] = MSG_TYPE_JSON; - j["data"]["cgroup_proxy"] = "/"; - j["data"]["enable_dns"] = false; - int status; + j["type"] = MSG_TYPE_CONFIG_JSON; + j["data"] = config.toJsonStr(); SOCKET::send(j.dump(), status); } -void test_json_array() { - json j; - j["type"] = MSG_TYPE_JSON; - j["data"]["cgroup_proxy"] = "/proxy.slice"; - j["data"]["cgroup_noproxy"] = {"/noproxy.slice", "/system.slice/v2ray.service"}; - int status; - SOCKET::send(j.dump(), status); -} - -void test_file() { +void send_config_path(const string s, int &status) { json j; j["type"] = MSG_TYPE_CONFIG_PATH; - j["data"] = "/etc/cgproxy.conf"; - int status; + j["data"] = s; SOCKET::send(j.dump(), status); } -void test_pid() { +void send_pid(const pid_t pid, bool proxy, int &status) { json j; - j["type"] = MSG_TYPE_PROXY_PID; - j["data"] = "9999"; - int status; + j["type"] = proxy ? MSG_TYPE_PROXY_PID : MSG_TYPE_NOPROXY_PID; + j["data"] = pid; SOCKET::send(j.dump(), status); } + +void test_config(){ + Config config; + config.cgroup_proxy={"/"}; + int status; + send_config(config, status); +} + +void test_config_path(){ + string path="/etc/cgproxy/config.json"; + int status; + send_config_path(path, status); +} + int main() { - test_json_array(); - test_file(); - test_json(); - test_pid(); + test_config(); return 0; } \ No newline at end of file diff --git a/test/test.cpp b/test/test.cpp deleted file mode 100644 index 6901bfb..0000000 --- a/test/test.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -int main() { return 0; } diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 8e369b9..6358358 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -1,4 +1,4 @@ include_directories(${PROJECT_SOURCE_DIR}) include_directories(${PROJECT_SOURCE_DIR}/src) -add_executable(cgattach cgattach.cpp) +add_executable(cgattach cgattach.cpp ../src/cgroup_attach.cpp ../src/common.cpp) install(TARGETS cgattach DESTINATION /usr/bin PERMISSIONS ${basic_permission}) \ No newline at end of file diff --git a/tools/cgattach.cpp b/tools/cgattach.cpp index b88130f..8f87637 100644 --- a/tools/cgattach.cpp +++ b/tools/cgattach.cpp @@ -1,6 +1,7 @@ -#include "cgroup_attach.hpp" -#include "common.hpp" +#include "cgroup_attach.h" +#include "common.h" #include +#include using namespace std; void print_usage() { fprintf(stdout, "usage: cgattach \n"); }