mirror of
https://github.com/springzfx/cgproxy.git
synced 2026-01-07 13:07:56 +08:00
merge to one executable
This commit is contained in:
@@ -19,6 +19,7 @@ if (build_test)
|
||||
add_subdirectory(test)
|
||||
endif()
|
||||
|
||||
install(FILES cgproxyd DESTINATION /usr/bin PERMISSIONS ${basic_permission})
|
||||
install(FILES cgnoproxy DESTINATION /usr/bin PERMISSIONS ${basic_permission})
|
||||
install(FILES cgproxy.service DESTINATION /usr/lib/systemd/system/)
|
||||
install(FILES config.json DESTINATION /etc/cgproxy/)
|
||||
|
||||
@@ -3,22 +3,12 @@ find_package(nlohmann_json REQUIRED)
|
||||
include_directories(${PROJECT_SOURCE_DIR})
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
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_executable(main main.cpp
|
||||
common.cpp config.cpp cgroup_attach.cpp
|
||||
socket_client.cpp socket_server.cpp)
|
||||
|
||||
target_link_libraries(main nlohmann_json::nlohmann_json Threads::Threads)
|
||||
set_target_properties(main PROPERTIES LINKER_LANGUAGE CXX)
|
||||
set_target_properties(main PROPERTIES OUTPUT_NAME cgproxy)
|
||||
|
||||
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 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})
|
||||
install(TARGETS main DESTINATION /usr/bin PERMISSIONS ${basic_permission})
|
||||
|
||||
@@ -1,16 +1,25 @@
|
||||
#include "common.h"
|
||||
#include "config.h"
|
||||
#include "socket_client.h"
|
||||
#include <cstdlib>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <unistd.h>
|
||||
using json = nlohmann::json;
|
||||
using namespace CGPROXY;
|
||||
using namespace CGPROXY::CONFIG;
|
||||
|
||||
namespace CGPROXY::CGPROXY {
|
||||
|
||||
bool print_help = false, proxy = true;
|
||||
inline void print_usage() {
|
||||
fprintf(stdout, "Usage: cgproxy [--help] [--debug] [--noproxy] <CMD>\n");
|
||||
fprintf(stdout, "Alias: cgnoproxy = cgproxy --noproxy\n");
|
||||
if (proxy) {
|
||||
cout << "Run program with proxy" << endl;
|
||||
cout << "Usage: cgproxy [--help] [--debug] <CMD>" << endl;
|
||||
} else {
|
||||
cout << "Run program without proxy" << endl;
|
||||
cout << "Usage: cgpnoroxy [--help] [--debug] <CMD>" << endl;
|
||||
cout << "Alias: cgnoproxy = cgproxy --noproxy" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
void processArgs(const int argc, char *argv[], int &shift) {
|
||||
@@ -34,11 +43,16 @@ int main(int argc, char *argv[]) {
|
||||
int shift = 1;
|
||||
processArgs(argc, argv, shift);
|
||||
|
||||
if (argc == shift || print_help) {
|
||||
if (print_help) {
|
||||
print_usage();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (argc == shift) {
|
||||
error("no program specified");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int status = -1;
|
||||
send_pid(getpid(), proxy, status);
|
||||
if (status != 0) {
|
||||
@@ -48,4 +62,5 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
string s = join2str(argc - shift, argv + shift, ' ');
|
||||
return system(s.c_str());
|
||||
}
|
||||
}
|
||||
} // namespace CGPROXY::CGPROXY
|
||||
@@ -1,3 +1,6 @@
|
||||
#ifndef CGPROXYD_HPP
|
||||
#define CGPROXYD_HPP
|
||||
|
||||
#include "cgroup_attach.h"
|
||||
#include "common.h"
|
||||
#include "config.h"
|
||||
@@ -10,11 +13,11 @@
|
||||
|
||||
using namespace std;
|
||||
using json = nlohmann::json;
|
||||
using namespace CGPROXY::SOCKET;
|
||||
using namespace CGPROXY::CONFIG;
|
||||
using namespace CGPROXY::CGROUP;
|
||||
using namespace ::CGPROXY::SOCKET;
|
||||
using namespace ::CGPROXY::CONFIG;
|
||||
using namespace ::CGPROXY::CGROUP;
|
||||
|
||||
namespace CGPROXY {
|
||||
namespace CGPROXY::CGPROXYD {
|
||||
|
||||
class cgproxyd {
|
||||
thread_arg arg_t;
|
||||
@@ -146,11 +149,13 @@ public:
|
||||
|
||||
cgproxyd *cgproxyd::instance = NULL;
|
||||
|
||||
} // namespace CGPROXY
|
||||
|
||||
bool print_help = false;
|
||||
|
||||
void print_usage() { printf("cgproxyd [--help] [--debug]\n"); }
|
||||
void print_usage() {
|
||||
cout << "Start a daemon with unix socket to accept control" << endl;
|
||||
cout << "Usage: cgproxyd [--help] [--debug]" << endl;
|
||||
cout << "Alias: cgproxyd = cgproxy --daemon" << endl;
|
||||
}
|
||||
|
||||
void processArgs(const int argc, char *argv[]) {
|
||||
for (int i = 1; i < argc; i++) {
|
||||
@@ -169,10 +174,11 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
if (getuid() != 0) {
|
||||
error("permission denied, need root");
|
||||
print_usage();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
CGPROXY::cgproxyd d;
|
||||
cgproxyd d;
|
||||
return d.start();
|
||||
}
|
||||
}
|
||||
} // namespace CGPROXY::CGPROXYD
|
||||
#endif
|
||||
@@ -38,12 +38,14 @@ extern bool enable_debug;
|
||||
|
||||
#define error(...) \
|
||||
{ \
|
||||
fprintf(stderr, "error: "); \
|
||||
fprintf(stderr, __VA_ARGS__); \
|
||||
fprintf(stderr, "\n"); \
|
||||
}
|
||||
|
||||
#define debug(...) \
|
||||
if (enable_debug) { \
|
||||
fprintf(stderr, "debug: "); \
|
||||
fprintf(stdout, __VA_ARGS__); \
|
||||
fprintf(stdout, "\n"); \
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "config.h"
|
||||
#include "common.h"
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <nlohmann/json.hpp>
|
||||
@@ -58,14 +59,7 @@ int Config::loadFromFile(const string f) {
|
||||
debug("loading config: %s", f.c_str());
|
||||
ifstream ifs(f);
|
||||
if (ifs.is_open()) {
|
||||
string js;
|
||||
try {
|
||||
ifs >> js;
|
||||
} catch (exception &e) {
|
||||
error("parse error: %s", f.c_str());
|
||||
ifs.close();
|
||||
return PARSE_ERROR;
|
||||
}
|
||||
string js = to_str(ifs.rdbuf());
|
||||
ifs.close();
|
||||
return loadFromJsonStr(js);
|
||||
} else {
|
||||
|
||||
@@ -32,7 +32,6 @@ public:
|
||||
private:
|
||||
void mergeReserved();
|
||||
bool validateJsonStr(const string js);
|
||||
|
||||
};
|
||||
|
||||
} // namespace CGPROXY::CONFIG
|
||||
|
||||
17
src/main.cpp
Normal file
17
src/main.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "cgproxy.hpp"
|
||||
#include "cgproxyd.hpp"
|
||||
|
||||
bool as_cgproxyd = false;
|
||||
void processArgs(const int argc, char *argv[]) {
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (strcmp(argv[i], "--daemon") == 0) { as_cgproxyd = true; }
|
||||
if (argv[i][0] != '-') { break; }
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
processArgs(argc, argv);
|
||||
if (as_cgproxyd) ::CGPROXY::CGPROXYD::main(argc, argv);
|
||||
else
|
||||
::CGPROXY::CGPROXY::main(argc, argv);
|
||||
}
|
||||
@@ -2,6 +2,6 @@ include_directories(${PROJECT_SOURCE_DIR})
|
||||
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)
|
||||
add_executable(client_test socket_client_test.cpp
|
||||
../src/socket_client.cpp ../src/common.cpp ../src/config.cpp)
|
||||
target_link_libraries(client_test nlohmann_json::nlohmann_json)
|
||||
@@ -29,16 +29,15 @@ void send_pid(const pid_t pid, bool proxy, int &status) {
|
||||
SOCKET::send(j.dump(), status);
|
||||
}
|
||||
|
||||
|
||||
void test_config(){
|
||||
void test_config() {
|
||||
Config config;
|
||||
config.cgroup_proxy={"/"};
|
||||
config.cgroup_proxy = {"/"};
|
||||
int status;
|
||||
send_config(config, status);
|
||||
}
|
||||
|
||||
void test_config_path(){
|
||||
string path="/etc/cgproxy/config.json";
|
||||
void test_config_path() {
|
||||
string path = "/etc/cgproxy/config.json";
|
||||
int status;
|
||||
send_config_path(path, status);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user