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
.directory
.vscode
.clangd
v2ray_config/proxy
v2ray_config/06_outbounds_proxy.json
aur-*

View File

@@ -1,14 +1,16 @@
#include "common.hpp"
#include "socket_client.hpp"
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace CGPROXY;
void print_usage() {
fprintf(stdout, "Usage: cgproxy [--help] [--debug] [--noproxy] <CMD>\n");
fprintf(stdout, "Alias: cgnoproxy = cgproxy --noproxy\n");
bool print_help = false, proxy = true;
void print_usage() {
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++) {
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] <CMD>\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);
}

View File

@@ -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();
}

View File

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

View File

@@ -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);
}