From 2adba75b3e803e56cb6399510c385e1617cf6b50 Mon Sep 17 00:00:00 2001 From: fancy Date: Sat, 16 May 2020 11:34:35 +0800 Subject: [PATCH] cgproxyd as class --- .gitignore | 1 + aur-cgproxy-local/PKGBUILD | 40 ------- aur-cgproxy-local/cgproxy.install | 8 -- cgproxyd.cpp | 173 +++++++++++++++++------------- socket_server.hpp | 7 +- 5 files changed, 105 insertions(+), 124 deletions(-) delete mode 100644 aur-cgproxy-local/PKGBUILD delete mode 100644 aur-cgproxy-local/cgproxy.install diff --git a/.gitignore b/.gitignore index eb5df43..6acb7f5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ build .vscode v2ray_config/proxy v2ray_config/06_outbounds_proxy.json +aur-* diff --git a/aur-cgproxy-local/PKGBUILD b/aur-cgproxy-local/PKGBUILD deleted file mode 100644 index 6a86b39..0000000 --- a/aur-cgproxy-local/PKGBUILD +++ /dev/null @@ -1,40 +0,0 @@ -# Maintainer: Fancy Zhang -pkgname=cgproxy-git -pkgver=v4.0.r1.g3fde647 -pkgrel=1 -pkgdesc="A transparent proxy program with cgroup2, like proxychains" -arch=('x86_64') -url="https://github.com/springzfx/cgproxy" -license=('') -groups=('') -makedepends=('cmake') -depends=('systemd') -provides=('cgproxy') -conflicts=('cgproxy') - -curr_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -# source=("${pkgname}::git+file://${curr_dir}/../.git") -source=("${pkgname}::git+file:///home/fancy/workspace/cgproxy#branch=dev") -md5sums=('SKIP') - -pkgver() { - cd "$pkgname" - ( set -o pipefail - git describe --long --tags 2>/dev/null | sed 's/\([^-]*-g\)/r\1/;s/-/./g' || - printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)" - ) -} - -backup=('etc/cgproxy.conf') -install="cgproxy.install" - -build(){ - cd "$pkgname" - mkdir -p build && cd build && cmake .. && make -} - -package_cgproxy-git(){ - cd "$pkgname"/build - make DESTDIR=$pkgdir install -} - diff --git a/aur-cgproxy-local/cgproxy.install b/aur-cgproxy-local/cgproxy.install deleted file mode 100644 index e6081e8..0000000 --- a/aur-cgproxy-local/cgproxy.install +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh - -post_install(){ -cat <<'DOC' - to start service: - systemctl enable --now cgproxy.service -DOC -} diff --git a/cgproxyd.cpp b/cgproxyd.cpp index 7c5a79e..3210d61 100644 --- a/cgproxyd.cpp +++ b/cgproxyd.cpp @@ -18,82 +18,111 @@ using namespace CGPROXY::SOCKET; using namespace CGPROXY::CONFIG; using namespace CGPROXY::CGROUP; -SocketServer sc; -thread_arg arg_t; -Config config; -pthread_t socket_thread_id = -1; +namespace CGPROXY{ -int applyConfig(Config *c) { - system("sh /usr/share/cgproxy/scripts/cgroup-tproxy.sh stop"); - c->toEnv(); - system("sh /usr/share/cgproxy/scripts/cgroup-tproxy.sh"); - return 0; -} - -int handle_msg(char *msg) { - debug("received msg: %s", msg); - json j; - try{ j = json::parse(msg); }catch(exception& e){debug("msg paser error");return MSG_ERROR;} - - int type, status; - int pid, cgroup_target; - try { - type = j.at("type").get(); - switch (type) - { - case MSG_TYPE_JSON: - status=config.loadFromJson(j.at("data")); - if (status==SUCCESS) status=applyConfig(&config); - return status; - break; - case MSG_TYPE_CONFIG_PATH: - status=config.loadFromFile(j.at("data").get()); - if (status==SUCCESS) status=applyConfig(&config); - return status; - break; - case MSG_TYPE_PROXY_PID: - pid=j.at("data").get(); - status=attach(pid, config.cgroup_proxy_preserved); - return status; - break; - case MSG_TYPE_NOPROXY_PID: - pid=j.at("data").get(); - status=attach(pid, config.cgroup_noproxy_preserved); - return status; - break; - default: - return MSG_ERROR; - break; - }; - } catch (out_of_range &e) { - return MSG_ERROR; - } catch (exception &e){ - return ERROR; +class cgproxyd{ + thread_arg arg_t; + Config config; + pthread_t socket_thread_id = -1; + + static cgproxyd* instance; + static int handle_msg_static(char* msg){ + if (!instance) { + error("no cgproxyd instance assigned"); + return ERROR; + } + return instance->handle_msg(msg); } -} -pthread_t startSocketListeningThread() { - arg_t.sc = ≻ - arg_t.handle_msg = &handle_msg; - pthread_t thread_id; - int status = - pthread_create(&thread_id, NULL, &SocketServer::startThread, &arg_t); - if (status != 0) - error("socket thread create failed"); - return thread_id; + int applyConfig(Config *c) { + system("sh /usr/share/cgproxy/scripts/cgroup-tproxy.sh stop"); + c->toEnv(); + system("sh /usr/share/cgproxy/scripts/cgroup-tproxy.sh"); + return 0; + } + + int handle_msg(char *msg) { + debug("received msg: %s", msg); + json j; + try{ j = json::parse(msg); }catch(exception& e){debug("msg paser error");return MSG_ERROR;} + + int type, status; + int pid, cgroup_target; + try { + type = j.at("type").get(); + switch (type) + { + case MSG_TYPE_JSON: + status=config.loadFromJson(j.at("data")); + if (status==SUCCESS) status=applyConfig(&config); + return status; + break; + case MSG_TYPE_CONFIG_PATH: + status=config.loadFromFile(j.at("data").get()); + if (status==SUCCESS) status=applyConfig(&config); + return status; + break; + case MSG_TYPE_PROXY_PID: + pid=j.at("data").get(); + status=attach(pid, config.cgroup_proxy_preserved); + return status; + break; + case MSG_TYPE_NOPROXY_PID: + pid=j.at("data").get(); + status=attach(pid, config.cgroup_noproxy_preserved); + return status; + break; + default: + return MSG_ERROR; + break; + }; + } catch (out_of_range &e) { + return MSG_ERROR; + } catch (exception &e){ + return ERROR; + } + } + + pthread_t startSocketListeningThread() { + arg_t.handle_msg = &handle_msg_static; + pthread_t thread_id; + int status = + pthread_create(&thread_id, NULL, &SocketServer::startThread, &arg_t); + if (status != 0) + error("socket thread create failed"); + return thread_id; + } + + void assignStaticInstance(){ + instance=this; + } + + public: + int start(int argc, char* argv[]) { + int shift=1; + processArgs(argc,argv,shift); + + bool enable_socket = true; + string config_path = DEFAULT_CONFIG_FILE; + config.loadFromFile(config_path); + applyConfig(&config); + if (enable_socket) { + assignStaticInstance(); + socket_thread_id = startSocketListeningThread(); + pthread_join(socket_thread_id, NULL); + } + return 0; + } + ~cgproxyd(){ + + } +}; + +cgproxyd* cgproxyd::instance=NULL; + } int main(int argc, char* argv[]) { - int shift=1; - processArgs(argc,argv,shift); - - bool enable_socket = true; - string config_path = DEFAULT_CONFIG_FILE; - config.loadFromFile(config_path); - applyConfig(&config); - if (enable_socket) { - socket_thread_id = startSocketListeningThread(); - pthread_join(socket_thread_id, NULL); - } - return 0; + CGPROXY::cgproxyd d; + return d.start(argc,argv); } \ No newline at end of file diff --git a/socket_server.hpp b/socket_server.hpp index f4e95d9..a37a6f3 100644 --- a/socket_server.hpp +++ b/socket_server.hpp @@ -25,9 +25,7 @@ namespace CGPROXY::SOCKET{ continue; \ } -class SocketServer; struct thread_arg { - SocketServer *sc; function handle_msg; }; @@ -42,7 +40,7 @@ public: if (fs::exists(SOCKET_PATH)&&unlink(SOCKET_PATH)==-1){ error("%s exist, and can't unlink",SOCKET_PATH); - exit(EXIT_FAILURE); + return; } memset(&unix_socket, '\0', sizeof(struct sockaddr_un)); unix_socket.sun_family = AF_UNIX; @@ -85,7 +83,8 @@ public: static void *startThread(void *arg) { thread_arg *p = (thread_arg *)arg; - p->sc->socketListening(p->handle_msg); + SocketServer server; + server.socketListening(p->handle_msg); return (void *)0; } };