mirror of
https://github.com/springzfx/cgproxy.git
synced 2026-02-09 21:14:57 +08:00
fix typo, and clang-format cpp
This commit is contained in:
141
cgattach.cpp
141
cgattach.cpp
@@ -1,80 +1,83 @@
|
||||
#include <iostream>
|
||||
#include <errno.h>
|
||||
#include <fstream>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <iostream>
|
||||
#include <regex>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
using namespace std;
|
||||
|
||||
void print_usage(){
|
||||
fprintf(stderr, "usage: cgattach <pid> <cgroup>\n");
|
||||
void print_usage() { fprintf(stderr, "usage: cgattach <pid> <cgroup>\n"); }
|
||||
|
||||
bool exist(string path) {
|
||||
struct stat st;
|
||||
if (stat(path.c_str(), &st) != -1) {
|
||||
return S_ISDIR(st.st_mode);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool exist(string path){
|
||||
struct stat st;
|
||||
if (stat(path.c_str(),&st)!=-1){
|
||||
return S_ISDIR(st.st_mode);
|
||||
}
|
||||
return false;
|
||||
bool validate(string pid, string cgroup) {
|
||||
bool pid_v = regex_match(pid, regex("^[0-9]+$"));
|
||||
bool cg_v = regex_match(cgroup, regex("^\\/[a-zA-Z0-9\\-_./@]+$"));
|
||||
if (pid_v && cg_v)
|
||||
return true;
|
||||
// cout<<pid_v<<" "<<cg_v<<endl;
|
||||
print_usage();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
bool validate(string pid, string cgroup){
|
||||
bool pid_v=regex_match(pid,regex("^[0-9]+$"));
|
||||
bool cg_v=regex_match(cgroup,regex("^\\/[a-zA-Z0-9\\-_./@]+$"));
|
||||
if (pid_v && cg_v) return true;
|
||||
// cout<<pid_v<<" "<<cg_v<<endl;
|
||||
int main(int argc, char *argv[]) {
|
||||
setuid(0);
|
||||
setgid(0);
|
||||
if (getuid() != 0 || getgid() != 0) {
|
||||
fprintf(stderr, "cgattach need setuid sticky bit\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (argc != 3) {
|
||||
print_usage();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int main(int argc,char *argv[]){
|
||||
setuid(0);
|
||||
setgid(0);
|
||||
if (getuid()!=0||getgid()!=0){
|
||||
fprintf(stderr, "cgattach need setuid sticky bit\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (argc!=3){
|
||||
print_usage();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
string pid=string(argv[1]);
|
||||
string cgroup_target=string(argv[2]);
|
||||
validate(pid, cgroup_target);
|
||||
string cgroup_mount_point="/sys/fs/cgroup";
|
||||
string cgroup_target_path=cgroup_mount_point+cgroup_target;
|
||||
string cgroup_target_procs=cgroup_target_path+"/cgroup.procs"; // only support cgroup v2
|
||||
|
||||
// check if exist, we won't create it if not exist
|
||||
if (!exist(cgroup_target_path)){
|
||||
if (mkdir(cgroup_target_path.c_str(), S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)==0){
|
||||
fprintf(stdout, "created cgroup %s success\n",cgroup_target.c_str());
|
||||
}else{
|
||||
fprintf(stderr, "created cgroup %s failed\n",cgroup_target.c_str());
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// fprintf(stderr, "cgroup %s not exist\n",cgroup_target.c_str());
|
||||
// exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// put pid to target cgroup
|
||||
ofstream procs(cgroup_target_procs,ofstream::app);
|
||||
if (!procs.is_open()){
|
||||
fprintf(stderr, "open file %s failed\n",cgroup_target_procs.c_str());
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
procs<<pid.c_str()<<endl;
|
||||
procs.close();
|
||||
|
||||
// maybe there some write error, for example process pid not exist
|
||||
if (!procs){
|
||||
fprintf(stderr, "write %s to %s failed, maybe process %s not exist\n",pid.c_str(),cgroup_target_procs.c_str(),pid.c_str());
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
string pid = string(argv[1]);
|
||||
string cgroup_target = string(argv[2]);
|
||||
validate(pid, cgroup_target);
|
||||
string cgroup_mount_point = "/sys/fs/cgroup";
|
||||
string cgroup_target_path = cgroup_mount_point + cgroup_target;
|
||||
string cgroup_target_procs = cgroup_target_path + "/cgroup.procs";
|
||||
|
||||
// check if exist, we won't create it if not exist
|
||||
if (!exist(cgroup_target_path)) {
|
||||
if (mkdir(cgroup_target_path.c_str(),
|
||||
S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
|
||||
fprintf(stdout, "created cgroup %s success\n", cgroup_target.c_str());
|
||||
} else {
|
||||
fprintf(stderr, "created cgroup %s failed, errno %d\n",
|
||||
cgroup_target.c_str(), errno);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// fprintf(stderr, "cgroup %s not exist\n",cgroup_target.c_str());
|
||||
// exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// put pid to target cgroup
|
||||
ofstream procs(cgroup_target_procs, ofstream::app);
|
||||
if (!procs.is_open()) {
|
||||
fprintf(stderr, "open file %s failed\n", cgroup_target_procs.c_str());
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
procs << pid.c_str() << endl;
|
||||
procs.close();
|
||||
|
||||
// maybe there some write error, for example process pid not exist
|
||||
if (!procs) {
|
||||
fprintf(stderr, "write %s to %s failed, maybe process %s not exist\n",
|
||||
pid.c_str(), cgroup_target_procs.c_str(), pid.c_str());
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -20,5 +20,4 @@ v2ray_so_mark=255
|
||||
## do not modify this if you don't known what you are doing
|
||||
table=100
|
||||
mark=100
|
||||
mark_newin=1
|
||||
v2ray_so_mark=255
|
||||
mark_newin=1
|
||||
@@ -51,7 +51,7 @@ It is alreay in [archlinux AUR](https://aur.archlinux.org/packages/cgproxy/).
|
||||
sudo systemctl status cgproxy.service
|
||||
```
|
||||
|
||||
- Then prefix with cgproxy with you command, just like proxychains
|
||||
- Then prefix with cgproxy with your command, just like proxychains
|
||||
|
||||
```
|
||||
cgproxy <CMD>
|
||||
@@ -89,7 +89,6 @@ v2ray_so_mark=255
|
||||
table=100
|
||||
mark=100
|
||||
mark_newin=1
|
||||
v2ray_so_mark=255
|
||||
```
|
||||
|
||||
If you changed config, remember to restart service
|
||||
@@ -111,9 +110,9 @@ sudo systemctl restart cgproxy.service
|
||||
- `run_in_cgroup` run command in specific cgroup which will create if not exist , cgroup can be only one level down exist cgroup, otherwise created fail.
|
||||
|
||||
```bash
|
||||
run_in_cgroup --cggroup=CGROUP <COMMAND>
|
||||
run_in_cgroup --cgroup=CGROUP <COMMAND>
|
||||
# example
|
||||
run_in_cgroup --cggroup=/mycgroup.slice ping 127.0.0.1
|
||||
run_in_cgroup --cgroup=/mycgroup.slice ping 127.0.0.1
|
||||
```
|
||||
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
print_help(){
|
||||
cat << 'DOC'
|
||||
usage:
|
||||
run_in_cgroup --cggroup=CGROUP <COMMAND>
|
||||
run_in_cgroup --cgroup=CGROUP <COMMAND>
|
||||
run_in_cgroup --help
|
||||
note:
|
||||
CGROUP must start will slash '/' , and no special character
|
||||
|
||||
Reference in New Issue
Block a user