fix print usage and clang format

This commit is contained in:
fancy
2020-05-17 23:26:11 +08:00
committed by fancy
parent 3a4e62b3c2
commit db4757316a
5 changed files with 27 additions and 17 deletions

1
.gitignore vendored
View File

@@ -1,6 +1,7 @@
build build
.directory .directory
.vscode .vscode
.clangd
v2ray_config/proxy v2ray_config/proxy
v2ray_config/06_outbounds_proxy.json v2ray_config/06_outbounds_proxy.json
aur-* aur-*

View File

@@ -1,14 +1,16 @@
#include "common.hpp"
#include "socket_client.hpp" #include "socket_client.hpp"
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using json = nlohmann::json;
using namespace CGPROXY; using namespace CGPROXY;
void print_usage() { bool print_help = false, proxy = true;
fprintf(stdout, "Usage: cgproxy [--help] [--debug] [--noproxy] <CMD>\n"); void print_usage() {
fprintf(stdout, "Alias: cgnoproxy = cgproxy --noproxy\n"); fprintf(stdout, "Usage: cgproxy [--help] [--debug] [--noproxy] <CMD>\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++) { for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--noproxy") == 0) { proxy = false; } if (strcmp(argv[i], "--noproxy") == 0) { proxy = false; }
if (strcmp(argv[i], "--debug") == 0) { enable_debug = true; } 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; 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; j["data"] = pid;
int status; int status;
SOCKET::send(j.dump(), status); SOCKET::send(j.dump(), status);
@@ -28,16 +30,16 @@ bool attach2cgroup(pid_t pid,bool proxy) {
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (argc == 1) { int shift = 1;
error( processArgs(argc, argv, shift);
"usage: cgproxy [--debug] <CMD>\nexample: cgroxy curl -I https://www.google.com");
exit(EXIT_FAILURE); if (argc==shift||print_help) {
print_usage();
exit(0);
} }
int shift = 1; bool proxy=true;
processArgs(argc, argv, shift,proxy);
pid_t pid = getpid(); pid_t pid = getpid();
if (!attach2cgroup(pid,proxy)) { if (!attach2cgroup(pid, proxy)) {
error("attach process failed"); error("attach process failed");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }

View File

@@ -121,7 +121,7 @@ public:
debug("stopping"); debug("stopping");
system(TPROXY_IPTABLS_CLEAN); system(TPROXY_IPTABLS_CLEAN);
} }
~cgproxyd() { stop(); } ~cgproxyd() { stop(); }
}; };
@@ -129,6 +129,10 @@ cgproxyd *cgproxyd::instance = NULL;
} // namespace CGPROXY } // namespace CGPROXY
bool print_help = false;
void print_usage() { printf("cgproxyd [--help] [--debug]\n"); }
void processArgs(const int argc, char *argv[]) { void processArgs(const int argc, char *argv[]) {
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--debug") == 0) { enable_debug = true; } 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[]) { int main(int argc, char *argv[]) {
processArgs(argc, argv); processArgs(argc, argv);
if (print_help) {
print_usage();
exit(0);
}
CGPROXY::cgproxyd d; CGPROXY::cgproxyd d;
return d.start(); return d.start();
} }

View File

@@ -34,7 +34,6 @@
using namespace std; using namespace std;
static bool enable_debug = false; static bool enable_debug = false;
static bool print_help = false;
#define error(...) \ #define error(...) \
{ \ { \

View File

@@ -21,9 +21,9 @@ int main(int argc, char *argv[]) {
string pid = string(argv[1]); string pid = string(argv[1]);
string cgroup_target = string(argv[2]); string cgroup_target = string(argv[2]);
if (validPid(pid)&&validCgroup(cgroup_target)){ if (validPid(pid) && validCgroup(cgroup_target)) {
CGPROXY::CGROUP::attach(pid, cgroup_target); CGPROXY::CGROUP::attach(pid, cgroup_target);
}else{ } else {
error("param not valid"); error("param not valid");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }