mirror of
https://github.com/springzfx/cgproxy.git
synced 2026-01-07 13:07:56 +08:00
rename .hpp to .h
This commit is contained in:
@@ -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})
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
#include "common.hpp"
|
||||
#include "socket_client.hpp"
|
||||
#include "common.h"
|
||||
#include "config.h"
|
||||
#include "socket_client.h"
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <unistd.h>
|
||||
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] <CMD>\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);
|
||||
}
|
||||
|
||||
@@ -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 <csignal>
|
||||
#include <errno.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <pthread.h>
|
||||
#include <sstream>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <sys/file.h>
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace std;
|
||||
using json = nlohmann::json;
|
||||
@@ -81,8 +75,8 @@ class cgproxyd {
|
||||
try {
|
||||
type = j.at("type").get<int>();
|
||||
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;
|
||||
|
||||
@@ -1,4 +1,15 @@
|
||||
#include "cgroup_attach.hpp"
|
||||
#include "cgroup_attach.h"
|
||||
#include "common.h"
|
||||
#include <errno.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <regex>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace CGPROXY::CGROUP {
|
||||
|
||||
|
||||
@@ -1,29 +1,16 @@
|
||||
#ifndef CGPROUP_ATTACH_H
|
||||
#define CGPROUP_ATTACH_H
|
||||
|
||||
#include "common.hpp"
|
||||
#include <errno.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <regex>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
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
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "common.hpp"
|
||||
#include "common.h"
|
||||
#include <regex>
|
||||
|
||||
bool enable_debug = false;
|
||||
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
#ifndef COMMON_H
|
||||
#define COMMON_H 1
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
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 <iostream>
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
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 <typename... T> string to_str(T... args) {
|
||||
}
|
||||
|
||||
string join2str(const vector<string> t, const char delm = ' ');
|
||||
|
||||
string join2str(const int argc, char **argv, const char delm = ' ');
|
||||
|
||||
bool validCgroup(const string cgroup);
|
||||
|
||||
bool validCgroup(const vector<string> cgroup);
|
||||
|
||||
bool validPid(const string pid);
|
||||
|
||||
bool validPort(const int port);
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,9 @@
|
||||
#include "config.hpp"
|
||||
#include "config.h"
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <set>
|
||||
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<string> boolset = {"enable_gateway", "enable_dns", "enable_tcp",
|
||||
"enable_udp", "enable_ipv4", "enable_ipv6"};
|
||||
|
||||
@@ -1,18 +1,10 @@
|
||||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
#include "common.hpp"
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <stdio.h>
|
||||
#include "common.h"
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
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<string> cgroup_proxy;
|
||||
vector<string> 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
|
||||
@@ -1,4 +1,16 @@
|
||||
#include "socket_client.hpp"
|
||||
#include "socket_client.h"
|
||||
#include "common.h"
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define return_if_error(flag, msg) \
|
||||
if (flag == -1) { \
|
||||
perror(msg); \
|
||||
status = CONN_ERROR; \
|
||||
close(sfd); \
|
||||
return; \
|
||||
}
|
||||
|
||||
namespace CGPROXY::SOCKET {
|
||||
|
||||
|
||||
14
src/socket_client.h
Normal file
14
src/socket_client.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef SOCKET_CLIENT_H
|
||||
#define SOCKET_CLIENT_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
namespace CGPROXY::SOCKET {
|
||||
|
||||
void send(const char *msg, int &status);
|
||||
void send(const string msg, int &status);
|
||||
|
||||
} // namespace CGPROXY::SOCKET
|
||||
#endif
|
||||
@@ -1,30 +0,0 @@
|
||||
#ifndef SOCKET_CLIENT_H
|
||||
#define SOCKET_CLIENT_H
|
||||
|
||||
#include "common.hpp"
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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
|
||||
@@ -1,6 +1,14 @@
|
||||
#include "socket_server.hpp"
|
||||
#include "socket_server.h"
|
||||
#include "common.h"
|
||||
#include <filesystem>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace CGPROXY::SOCKET {
|
||||
|
||||
void SocketServer::socketListening(function<int(char *)> callback) {
|
||||
debug("starting socket listening");
|
||||
sfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
@@ -40,15 +48,18 @@ void SocketServer::socketListening(function<int(char *)> 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
|
||||
@@ -1,21 +1,10 @@
|
||||
#ifndef SOCKET_SERVER_H
|
||||
#define SOCKET_SERVER_H
|
||||
|
||||
#include "common.hpp"
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
using namespace std;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace CGPROXY::SOCKET {
|
||||
|
||||
@@ -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)
|
||||
@@ -1,48 +1,49 @@
|
||||
#include "socket_client.hpp"
|
||||
#include "common.h"
|
||||
#include "config.h"
|
||||
#include "socket_client.h"
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <pthread.h>
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
int main() { return 0; }
|
||||
@@ -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})
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "cgroup_attach.hpp"
|
||||
#include "common.hpp"
|
||||
#include "cgroup_attach.h"
|
||||
#include "common.h"
|
||||
#include <cstdlib>
|
||||
#include <unistd.h>
|
||||
using namespace std;
|
||||
|
||||
void print_usage() { fprintf(stdout, "usage: cgattach <pid> <cgroup>\n"); }
|
||||
|
||||
Reference in New Issue
Block a user