diff --git a/.gitignore b/.gitignore index 6acb7f5..cd5fa58 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ build .directory .vscode +.clangd v2ray_config/proxy v2ray_config/06_outbounds_proxy.json aur-* diff --git a/src/cgproxy.cpp b/src/cgproxy.cpp index 072dcd8..45936e0 100644 --- a/src/cgproxy.cpp +++ b/src/cgproxy.cpp @@ -1,14 +1,16 @@ +#include "common.hpp" #include "socket_client.hpp" #include using json = nlohmann::json; using namespace CGPROXY; -void print_usage() { - fprintf(stdout, "Usage: cgproxy [--help] [--debug] [--noproxy] \n"); - fprintf(stdout, "Alias: cgnoproxy = cgproxy --noproxy\n"); +bool print_help = false, proxy = true; +void print_usage() { + fprintf(stdout, "Usage: cgproxy [--help] [--debug] [--noproxy] \n"); + fprintf(stdout, "Alias: cgnoproxy = cgproxy --noproxy\n"); } -void processArgs(const int argc, char *argv[], int &shift, bool &proxy) { +void processArgs(const int argc, char *argv[], int &shift) { for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "--noproxy") == 0) { proxy = false; } if (strcmp(argv[i], "--debug") == 0) { enable_debug = true; } @@ -18,9 +20,9 @@ void processArgs(const int argc, char *argv[], int &shift, bool &proxy) { } } -bool attach2cgroup(pid_t pid,bool proxy) { +bool attach2cgroup(pid_t pid, bool proxy) { json j; - j["type"] = proxy?MSG_TYPE_PROXY_PID:MSG_TYPE_NOPROXY_PID; + j["type"] = proxy ? MSG_TYPE_PROXY_PID : MSG_TYPE_NOPROXY_PID; j["data"] = pid; int status; SOCKET::send(j.dump(), status); @@ -28,16 +30,16 @@ bool attach2cgroup(pid_t pid,bool proxy) { } int main(int argc, char *argv[]) { - if (argc == 1) { - error( - "usage: cgproxy [--debug] \nexample: cgroxy curl -I https://www.google.com"); - exit(EXIT_FAILURE); + int shift = 1; + processArgs(argc, argv, shift); + + if (argc==shift||print_help) { + print_usage(); + exit(0); } - int shift = 1; bool proxy=true; - processArgs(argc, argv, shift,proxy); pid_t pid = getpid(); - if (!attach2cgroup(pid,proxy)) { + if (!attach2cgroup(pid, proxy)) { error("attach process failed"); exit(EXIT_FAILURE); } diff --git a/src/cgproxyd.cpp b/src/cgproxyd.cpp index 53b4274..72f4639 100644 --- a/src/cgproxyd.cpp +++ b/src/cgproxyd.cpp @@ -121,7 +121,7 @@ public: debug("stopping"); system(TPROXY_IPTABLS_CLEAN); } - + ~cgproxyd() { stop(); } }; @@ -129,6 +129,10 @@ cgproxyd *cgproxyd::instance = NULL; } // namespace CGPROXY +bool print_help = false; + +void print_usage() { printf("cgproxyd [--help] [--debug]\n"); } + void processArgs(const int argc, char *argv[]) { for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "--debug") == 0) { enable_debug = true; } @@ -139,6 +143,10 @@ void processArgs(const int argc, char *argv[]) { int main(int argc, char *argv[]) { processArgs(argc, argv); + if (print_help) { + print_usage(); + exit(0); + } CGPROXY::cgproxyd d; return d.start(); } \ No newline at end of file diff --git a/src/common.hpp b/src/common.hpp index b471f6c..2758b03 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -34,7 +34,6 @@ using namespace std; static bool enable_debug = false; -static bool print_help = false; #define error(...) \ { \ diff --git a/tools/cgattach.cpp b/tools/cgattach.cpp index 7a9f970..b88130f 100644 --- a/tools/cgattach.cpp +++ b/tools/cgattach.cpp @@ -21,9 +21,9 @@ int main(int argc, char *argv[]) { string pid = string(argv[1]); string cgroup_target = string(argv[2]); - if (validPid(pid)&&validCgroup(cgroup_target)){ + if (validPid(pid) && validCgroup(cgroup_target)) { CGPROXY::CGROUP::attach(pid, cgroup_target); - }else{ + } else { error("param not valid"); exit(EXIT_FAILURE); }