mirror of
https://github.com/springzfx/cgproxy.git
synced 2026-04-23 10:11:04 +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(${PROJECT_SOURCE_DIR})
|
||||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||||
|
|
||||||
add_library(cgproxy-lib
|
add_library(cgproxy-lib SHARED config.cpp socket_client.cpp common.cpp)
|
||||||
SHARED
|
|
||||||
common.cpp
|
|
||||||
config.cpp
|
|
||||||
cgroup_attach.cpp
|
|
||||||
socket_server.cpp socket_client.cpp
|
|
||||||
)
|
|
||||||
set_target_properties(cgproxy-lib PROPERTIES LINKER_LANGUAGE CXX)
|
set_target_properties(cgproxy-lib PROPERTIES LINKER_LANGUAGE CXX)
|
||||||
set_target_properties(cgproxy-lib PROPERTIES OUTPUT_NAME cgproxy)
|
set_target_properties(cgproxy-lib PROPERTIES OUTPUT_NAME cgproxy)
|
||||||
target_link_libraries(cgproxy-lib nlohmann_json::nlohmann_json)
|
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(cgproxyd cgproxyd.cpp)
|
||||||
add_executable(cgproxy cgproxy.cpp)
|
add_executable(cgproxy cgproxy.cpp)
|
||||||
target_link_libraries(cgproxyd cgproxy-lib Threads::Threads nlohmann_json::nlohmann_json)
|
target_link_libraries(cgproxyd cgproxyd-lib Threads::Threads)
|
||||||
target_link_libraries(cgproxy cgproxy-lib nlohmann_json::nlohmann_json)
|
target_link_libraries(cgproxy cgproxy-lib)
|
||||||
|
|
||||||
install(TARGETS cgproxyd DESTINATION /usr/bin PERMISSIONS ${basic_permission})
|
install(TARGETS cgproxyd DESTINATION /usr/bin PERMISSIONS ${basic_permission})
|
||||||
install(TARGETS cgproxy 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 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 "common.h"
|
||||||
#include "socket_client.hpp"
|
#include "config.h"
|
||||||
|
#include "socket_client.h"
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
#include <unistd.h>
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
using namespace CGPROXY;
|
using namespace CGPROXY;
|
||||||
|
using namespace CGPROXY::CONFIG;
|
||||||
|
|
||||||
bool print_help = false, proxy = true;
|
bool print_help = false, proxy = true;
|
||||||
void print_usage() {
|
inline void print_usage() {
|
||||||
fprintf(stdout, "Usage: cgproxy [--help] [--debug] [--noproxy] <CMD>\n");
|
fprintf(stdout, "Usage: cgproxy [--help] [--debug] [--noproxy] <CMD>\n");
|
||||||
fprintf(stdout, "Alias: cgnoproxy = cgproxy --noproxy\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;
|
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;
|
|
||||||
SOCKET::send(j.dump(), status);
|
SOCKET::send(j.dump(), status);
|
||||||
return status == 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
@@ -38,8 +39,9 @@ int main(int argc, char *argv[]) {
|
|||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
pid_t pid = getpid();
|
int status = -1;
|
||||||
if (!attach2cgroup(pid, proxy)) {
|
send_pid(getpid(), proxy, status);
|
||||||
|
if (status != 0) {
|
||||||
error("attach process failed");
|
error("attach process failed");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,12 @@
|
|||||||
#include "cgroup_attach.hpp"
|
#include "cgroup_attach.h"
|
||||||
#include "common.hpp"
|
#include "common.h"
|
||||||
#include "config.hpp"
|
#include "config.h"
|
||||||
#include "socket_server.hpp"
|
#include "socket_server.h"
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
#include <errno.h>
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <pthread.h>
|
|
||||||
#include <sstream>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string>
|
|
||||||
#include <sys/file.h>
|
#include <sys/file.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
@@ -81,8 +75,8 @@ class cgproxyd {
|
|||||||
try {
|
try {
|
||||||
type = j.at("type").get<int>();
|
type = j.at("type").get<int>();
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case MSG_TYPE_JSON:
|
case MSG_TYPE_CONFIG_JSON:
|
||||||
status = config.loadFromJson(j.at("data"));
|
status = config.loadFromJsonStr(j.at("data").dump());
|
||||||
if (status == SUCCESS) status = applyConfig(&config);
|
if (status == SUCCESS) status = applyConfig(&config);
|
||||||
return status;
|
return status;
|
||||||
break;
|
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 {
|
namespace CGPROXY::CGROUP {
|
||||||
|
|
||||||
|
|||||||
@@ -1,29 +1,16 @@
|
|||||||
#ifndef CGPROUP_ATTACH_H
|
#ifndef CGPROUP_ATTACH_H
|
||||||
#define 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 <stdlib.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
namespace CGPROXY::CGROUP {
|
namespace CGPROXY::CGROUP {
|
||||||
|
|
||||||
bool exist(string path);
|
bool exist(string path);
|
||||||
|
|
||||||
bool validate(string pid, string cgroup);
|
bool validate(string pid, string cgroup);
|
||||||
|
|
||||||
string get_cgroup2_mount_point(int &status);
|
string get_cgroup2_mount_point(int &status);
|
||||||
|
|
||||||
int attach(const string pid, const string cgroup_target);
|
int attach(const string pid, const string cgroup_target);
|
||||||
|
|
||||||
int attach(const int pid, const string cgroup_target);
|
int attach(const int pid, const string cgroup_target);
|
||||||
|
|
||||||
} // namespace CGPROXY::CGROUP
|
} // namespace CGPROXY::CGROUP
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "common.hpp"
|
#include "common.h"
|
||||||
|
#include <regex>
|
||||||
|
|
||||||
bool enable_debug = false;
|
bool enable_debug = false;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
#ifndef COMMON_H
|
#ifndef COMMON_H
|
||||||
#define COMMON_H 1
|
#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_START "sh /usr/share/cgproxy/scripts/cgroup-tproxy.sh"
|
||||||
#define TPROXY_IPTABLS_CLEAN "sh /usr/share/cgproxy/scripts/cgroup-tproxy.sh stop"
|
#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_PROXY_PRESVERED "/proxy.slice"
|
||||||
#define CGROUP_NOPROXY_PRESVERED "/noproxy.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_CONFIG_PATH 2
|
||||||
#define MSG_TYPE_PROXY_PID 3
|
#define MSG_TYPE_PROXY_PID 3
|
||||||
#define MSG_TYPE_NOPROXY_PID 4
|
#define MSG_TYPE_NOPROXY_PID 4
|
||||||
@@ -28,12 +34,6 @@
|
|||||||
#define CGROUP_ERROR 6
|
#define CGROUP_ERROR 6
|
||||||
#define FILE_ERROR 7
|
#define FILE_ERROR 7
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <regex>
|
|
||||||
#include <sstream>
|
|
||||||
#include <string>
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
extern bool enable_debug;
|
extern bool enable_debug;
|
||||||
|
|
||||||
#define error(...) \
|
#define error(...) \
|
||||||
@@ -41,11 +41,13 @@ extern bool enable_debug;
|
|||||||
fprintf(stderr, __VA_ARGS__); \
|
fprintf(stderr, __VA_ARGS__); \
|
||||||
fprintf(stderr, "\n"); \
|
fprintf(stderr, "\n"); \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define debug(...) \
|
#define debug(...) \
|
||||||
if (enable_debug) { \
|
if (enable_debug) { \
|
||||||
fprintf(stdout, __VA_ARGS__); \
|
fprintf(stdout, __VA_ARGS__); \
|
||||||
fprintf(stdout, "\n"); \
|
fprintf(stdout, "\n"); \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define return_error return -1;
|
#define return_error return -1;
|
||||||
#define return_success return 0;
|
#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 vector<string> t, const char delm = ' ');
|
||||||
|
|
||||||
string join2str(const int argc, char **argv, const char delm = ' ');
|
string join2str(const int argc, char **argv, const char delm = ' ');
|
||||||
|
|
||||||
bool validCgroup(const string cgroup);
|
bool validCgroup(const string cgroup);
|
||||||
|
|
||||||
bool validCgroup(const vector<string> cgroup);
|
bool validCgroup(const vector<string> cgroup);
|
||||||
|
|
||||||
bool validPid(const string pid);
|
bool validPid(const string pid);
|
||||||
|
|
||||||
bool validPort(const int port);
|
bool validPort(const int port);
|
||||||
|
|
||||||
#endif
|
#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 add2json(v) j[#v] = v;
|
||||||
#define tryassign(v) \
|
#define tryassign(v) \
|
||||||
@@ -29,13 +34,13 @@ void Config::toEnv() {
|
|||||||
int Config::saveToFile(const string f) {
|
int Config::saveToFile(const string f) {
|
||||||
ofstream o(f);
|
ofstream o(f);
|
||||||
if (!o.is_open()) return FILE_ERROR;
|
if (!o.is_open()) return FILE_ERROR;
|
||||||
json j = toJson();
|
string js = toJsonStr();
|
||||||
o << setw(4) << j << endl;
|
o << setw(4) << js << endl;
|
||||||
o.close();
|
o.close();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
json Config::toJson() {
|
string Config::toJsonStr() {
|
||||||
json j;
|
json j;
|
||||||
add2json(cgroup_proxy);
|
add2json(cgroup_proxy);
|
||||||
add2json(cgroup_noproxy);
|
add2json(cgroup_noproxy);
|
||||||
@@ -46,34 +51,35 @@ json Config::toJson() {
|
|||||||
add2json(enable_udp);
|
add2json(enable_udp);
|
||||||
add2json(enable_ipv4);
|
add2json(enable_ipv4);
|
||||||
add2json(enable_ipv6);
|
add2json(enable_ipv6);
|
||||||
return j;
|
return j.dump();
|
||||||
}
|
}
|
||||||
|
|
||||||
int Config::loadFromFile(const string f) {
|
int Config::loadFromFile(const string f) {
|
||||||
debug("loading config: %s", f.c_str());
|
debug("loading config: %s", f.c_str());
|
||||||
ifstream ifs(f);
|
ifstream ifs(f);
|
||||||
if (ifs.is_open()) {
|
if (ifs.is_open()) {
|
||||||
json j;
|
string js;
|
||||||
try {
|
try {
|
||||||
ifs >> j;
|
ifs >> js;
|
||||||
} catch (exception &e) {
|
} catch (exception &e) {
|
||||||
error("parse error: %s", f.c_str());
|
error("parse error: %s", f.c_str());
|
||||||
ifs.close();
|
ifs.close();
|
||||||
return PARSE_ERROR;
|
return PARSE_ERROR;
|
||||||
}
|
}
|
||||||
ifs.close();
|
ifs.close();
|
||||||
return loadFromJson(j);
|
return loadFromJsonStr(js);
|
||||||
} else {
|
} else {
|
||||||
error("open failed: %s", f.c_str());
|
error("open failed: %s", f.c_str());
|
||||||
return FILE_ERROR;
|
return FILE_ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int Config::loadFromJson(const json &j) {
|
int Config::loadFromJsonStr(const string js) {
|
||||||
if (!validateJson(j)) {
|
if (!validateJsonStr(js)) {
|
||||||
error("json validate fail");
|
error("json validate fail");
|
||||||
return PARAM_ERROR;
|
return PARAM_ERROR;
|
||||||
}
|
}
|
||||||
|
json j = json::parse(js);
|
||||||
tryassign(cgroup_proxy);
|
tryassign(cgroup_proxy);
|
||||||
tryassign(cgroup_noproxy);
|
tryassign(cgroup_noproxy);
|
||||||
tryassign(enable_gateway);
|
tryassign(enable_gateway);
|
||||||
@@ -91,7 +97,8 @@ void Config::mergeReserved() {
|
|||||||
merge(cgroup_noproxy);
|
merge(cgroup_noproxy);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Config::validateJson(const json &j) {
|
bool Config::validateJsonStr(const string js) {
|
||||||
|
json j = json::parse(js);
|
||||||
bool status = true;
|
bool status = true;
|
||||||
const set<string> boolset = {"enable_gateway", "enable_dns", "enable_tcp",
|
const set<string> boolset = {"enable_gateway", "enable_dns", "enable_tcp",
|
||||||
"enable_udp", "enable_ipv4", "enable_ipv6"};
|
"enable_udp", "enable_ipv4", "enable_ipv6"};
|
||||||
|
|||||||
@@ -1,18 +1,10 @@
|
|||||||
#ifndef CONFIG_H
|
#ifndef CONFIG_H
|
||||||
#define CONFIG_H
|
#define CONFIG_H
|
||||||
#include "common.hpp"
|
#include "common.h"
|
||||||
#include <fstream>
|
|
||||||
#include <iomanip>
|
|
||||||
#include <iostream>
|
|
||||||
#include <nlohmann/json.hpp>
|
|
||||||
#include <set>
|
|
||||||
#include <sstream>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using json = nlohmann::json;
|
|
||||||
|
|
||||||
namespace CGPROXY::CONFIG {
|
namespace CGPROXY::CONFIG {
|
||||||
|
|
||||||
@@ -21,7 +13,6 @@ public:
|
|||||||
const string cgroup_proxy_preserved = CGROUP_PROXY_PRESVERED;
|
const string cgroup_proxy_preserved = CGROUP_PROXY_PRESVERED;
|
||||||
const string cgroup_noproxy_preserved = CGROUP_NOPROXY_PRESVERED;
|
const string cgroup_noproxy_preserved = CGROUP_NOPROXY_PRESVERED;
|
||||||
|
|
||||||
private:
|
|
||||||
vector<string> cgroup_proxy;
|
vector<string> cgroup_proxy;
|
||||||
vector<string> cgroup_noproxy;
|
vector<string> cgroup_noproxy;
|
||||||
bool enable_gateway = false;
|
bool enable_gateway = false;
|
||||||
@@ -32,14 +23,16 @@ private:
|
|||||||
bool enable_ipv4 = true;
|
bool enable_ipv4 = true;
|
||||||
bool enable_ipv6 = true;
|
bool enable_ipv6 = true;
|
||||||
|
|
||||||
public:
|
|
||||||
void toEnv();
|
void toEnv();
|
||||||
int saveToFile(const string f);
|
int saveToFile(const string f);
|
||||||
json toJson();
|
string toJsonStr();
|
||||||
int loadFromFile(const string f);
|
int loadFromFile(const string f);
|
||||||
int loadFromJson(const json &j);
|
int loadFromJsonStr(const string js);
|
||||||
|
|
||||||
|
private:
|
||||||
void mergeReserved();
|
void mergeReserved();
|
||||||
bool validateJson(const json &j);
|
bool validateJsonStr(const string js);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace CGPROXY::CONFIG
|
} // 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 {
|
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 {
|
namespace CGPROXY::SOCKET {
|
||||||
|
|
||||||
void SocketServer::socketListening(function<int(char *)> callback) {
|
void SocketServer::socketListening(function<int(char *)> callback) {
|
||||||
debug("starting socket listening");
|
debug("starting socket listening");
|
||||||
sfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
sfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
@@ -40,15 +48,18 @@ void SocketServer::socketListening(function<int(char *)> callback) {
|
|||||||
continue_if_error(flag, "write back");
|
continue_if_error(flag, "write back");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void *SocketServer::startThread(void *arg) {
|
void *SocketServer::startThread(void *arg) {
|
||||||
thread_arg *p = (thread_arg *)arg;
|
thread_arg *p = (thread_arg *)arg;
|
||||||
SocketServer server;
|
SocketServer server;
|
||||||
server.socketListening(p->handle_msg);
|
server.socketListening(p->handle_msg);
|
||||||
return (void *)0;
|
return (void *)0;
|
||||||
}
|
}
|
||||||
|
|
||||||
SocketServer::~SocketServer() {
|
SocketServer::~SocketServer() {
|
||||||
close(sfd);
|
close(sfd);
|
||||||
close(cfd);
|
close(cfd);
|
||||||
unlink(SOCKET_PATH);
|
unlink(SOCKET_PATH);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace CGPROXY::SOCKET
|
} // namespace CGPROXY::SOCKET
|
||||||
@@ -1,21 +1,10 @@
|
|||||||
#ifndef SOCKET_SERVER_H
|
#ifndef SOCKET_SERVER_H
|
||||||
#define SOCKET_SERVER_H
|
#define SOCKET_SERVER_H
|
||||||
|
|
||||||
#include "common.hpp"
|
|
||||||
#include <filesystem>
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <iostream>
|
|
||||||
#include <signal.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
#include <unistd.h>
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
namespace fs = std::filesystem;
|
|
||||||
|
|
||||||
namespace CGPROXY::SOCKET {
|
namespace CGPROXY::SOCKET {
|
||||||
|
|
||||||
@@ -3,4 +3,5 @@ include_directories(${PROJECT_SOURCE_DIR}/src)
|
|||||||
|
|
||||||
find_package(nlohmann_json REQUIRED)
|
find_package(nlohmann_json REQUIRED)
|
||||||
add_executable(client_test socket_client_test.cpp)
|
add_executable(client_test socket_client_test.cpp)
|
||||||
|
target_link_libraries(client_test cgproxy-lib)
|
||||||
target_link_libraries(client_test nlohmann_json::nlohmann_json)
|
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>
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
using namespace CGPROXY;
|
using namespace CGPROXY;
|
||||||
|
using namespace CGPROXY::CONFIG;
|
||||||
|
|
||||||
void test_json() {
|
void send_config(Config &config, int &status) {
|
||||||
json j;
|
json j;
|
||||||
j["type"] = MSG_TYPE_JSON;
|
j["type"] = MSG_TYPE_CONFIG_JSON;
|
||||||
j["data"]["cgroup_proxy"] = "/";
|
j["data"] = config.toJsonStr();
|
||||||
j["data"]["enable_dns"] = false;
|
|
||||||
int status;
|
|
||||||
SOCKET::send(j.dump(), status);
|
SOCKET::send(j.dump(), status);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_json_array() {
|
void send_config_path(const string s, int &status) {
|
||||||
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() {
|
|
||||||
json j;
|
json j;
|
||||||
j["type"] = MSG_TYPE_CONFIG_PATH;
|
j["type"] = MSG_TYPE_CONFIG_PATH;
|
||||||
j["data"] = "/etc/cgproxy.conf";
|
j["data"] = s;
|
||||||
int status;
|
|
||||||
SOCKET::send(j.dump(), status);
|
SOCKET::send(j.dump(), status);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_pid() {
|
void send_pid(const pid_t pid, bool proxy, int &status) {
|
||||||
json j;
|
json j;
|
||||||
j["type"] = MSG_TYPE_PROXY_PID;
|
j["type"] = proxy ? MSG_TYPE_PROXY_PID : MSG_TYPE_NOPROXY_PID;
|
||||||
j["data"] = "9999";
|
j["data"] = pid;
|
||||||
int status;
|
|
||||||
SOCKET::send(j.dump(), status);
|
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() {
|
int main() {
|
||||||
test_json_array();
|
test_config();
|
||||||
test_file();
|
|
||||||
test_json();
|
|
||||||
test_pid();
|
|
||||||
return 0;
|
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})
|
||||||
include_directories(${PROJECT_SOURCE_DIR}/src)
|
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})
|
install(TARGETS cgattach DESTINATION /usr/bin PERMISSIONS ${basic_permission})
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "cgroup_attach.hpp"
|
#include "cgroup_attach.h"
|
||||||
#include "common.hpp"
|
#include "common.h"
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <unistd.h>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void print_usage() { fprintf(stdout, "usage: cgattach <pid> <cgroup>\n"); }
|
void print_usage() { fprintf(stdout, "usage: cgattach <pid> <cgroup>\n"); }
|
||||||
|
|||||||
Reference in New Issue
Block a user