diff --git a/Makefile b/Makefile index 98bd589..21efba5 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CROSS_COMPILE ?= CC := $(CROSS_COMPILE)gcc STRIP := $(CROSS_COMPILE)strip CFLAGS += -g -Wall -I../iniparser/src -L../iniparser -LIBS = -liniparser +LIBS = -liniparser -static OBJ := cproxy all: cproxy.o conf.o cproxy_request.o cproxy_help.o kill.o diff --git a/conf.c b/conf.c index 10dd7e0..a1a56e4 100644 --- a/conf.c +++ b/conf.c @@ -10,16 +10,21 @@ void read_conf(char *file, conf *p) dictionary *ini = iniparser_load(file); // server module - p->server_port = iniparser_getint(ini, "server:PORT", 0); - p->len_server_pid_file = strlen(iniparser_getstring(ini, "server:PID_FILE", NULL)) + 1; + // uid + p->uid = iniparser_getint(ini, "server:uid", 0); + //local_port + p->server_port = iniparser_getint(ini, "server:local_port", 0); + //pid_file + p->len_server_pid_file = strlen(iniparser_getstring(ini, "server:pid_file", NULL)) + 1; p->server_pid_file = (char *)malloc(p->len_server_pid_file); if (p->server_pid_file == NULL) { goto err; } memset(p->server_pid_file, 0, p->len_server_pid_file); - memcpy(p->server_pid_file, iniparser_getstring(ini, "server:PID_FILE", NULL), p->len_server_pid_file); + memcpy(p->server_pid_file, iniparser_getstring(ini, "server:pid_file", NULL), p->len_server_pid_file); //printf("%s\n", p->server_pid_file); + // http module // http ip p->len_http_ip = strlen(iniparser_getstring(ini, "http:http_ip", NULL)) + 1; p->http_ip = (char *)malloc(p->len_http_ip); @@ -50,7 +55,7 @@ void read_conf(char *file, conf *p) memset(p->http_first, 0, p->len_http_first); memcpy(p->http_first, iniparser_getstring(ini, "http:http_first", NULL), p->len_http_first); - + // https module // https ip p->len_https_ip = strlen(iniparser_getstring(ini, "https:https_ip", NULL)) + 1; p->https_ip = (char *)malloc(p->len_https_ip); diff --git a/conf/cproxy.ini b/conf/cproxy.ini index 507c7e9..3f9e665 100644 --- a/conf/cproxy.ini +++ b/conf/cproxy.ini @@ -1,6 +1,7 @@ [server] -PORT=9606; -PID_FILE=log/cproxy.pid; +uid=3004; +local_port=9606; +pid_file=log/cproxy.pid; [http] http_ip=10.0.0.172; @@ -16,4 +17,3 @@ https_del=",Host"; https_first="[M] iread.wo.cn//https://[host]:[port]#iread.wo.cn [V]\r\nhost: iread.wo.cn:443\r\n"; strrep = "Mi MIX 2->Linux"; regrep = "Host*.+?->Host: iread.wo.cn:443"; - diff --git a/conf/cproxy.ini.explain b/conf/cproxy.ini.explain index 9c7df31..2722451 100644 --- a/conf/cproxy.ini.explain +++ b/conf/cproxy.ini.explain @@ -1,18 +1,30 @@ 模块: [server], [http], [https] -[server]模块 -PORT 端口 -PID_FILE pid文件 - [http]、[https]模块关键字: [M], [U], [V], [host], [port], \r, \n, \v, \f, \b, \t, \a. 如果原本请求头含有关键字也会被替换. + +[server]模块 +uid 设置UID +local_port 端口 +pid_file pid文件 + +[http]模块 +[M] 原请求方法 +[U] 原请求url +[V] 原请求协议版本 +[host] 原请求host +[port] 原请求端口 +关键字strrep替换字符串指令. +strrep = "Mi MIX 2->Linux"; 以"->"为分界符,"Mi MIX 2"字符串替换为"Linux"字符串. +关键字regrep正则匹配替换字符串. +regrep = "Host*.+?->Host: iread.wo.cn:443"; 以"->"为分界符,匹配到的内容"Host*.+?"替换为"Host: iread.wo.cn:443"字符串. + +[https]模块 [M] 原请求方法 [U] 原请求url [V] 原请求协议版本 [host] 原请求host [port] 原请求端口 - 关键字strrep替换字符串指令. strrep = "Mi MIX 2->Linux"; 以"->"为分界符,"Mi MIX 2"字符串替换为"Linux"字符串. - 关键字regrep正则匹配替换字符串. regrep = "Host*.+?->Host: iread.wo.cn:443"; 以"->"为分界符,匹配到的内容"Host*.+?"替换为"Host: iread.wo.cn:443"字符串. diff --git a/conf/cproxy.transparent.ini b/conf/cproxy.transparent.ini new file mode 100644 index 0000000..9d61ce0 --- /dev/null +++ b/conf/cproxy.transparent.ini @@ -0,0 +1,21 @@ +[server] +uid=3004; +local_port=9606; +pid_file=log/cproxy.pid; + +[http] +http_ip=10.0.0.172; +http_port=80; +http_del="x-online-host,X-Online-Host,host,Host"; +;http_first="[M] [U] [V]\r\n.aixiao.me\rx-online-host: [host]\r\nhost: iread.wo.cn\r\n"; +http_first="[M] [U] [V]\r\nhost: [host]:[port]\r\n"; +;strrep = "Mi MIX 2->Linux"; + +[https] +https_ip=10.0.0.172; +https_port=80; +https_del=",Host"; +;https_first="[M] iread.wo.cn//https://[host]:[port]#iread.wo.cn [V]\r\nhost: iread.wo.cn:443\r\n"; +https_first="[M] [U] [V]\r\nhost: [host]:[port]\r\n"; +;strrep = "Mi MIX 2->Linux"; +;regrep = "Host*.+?->Host: iread.wo.cn:443"; diff --git a/cproxy.c b/cproxy.c index b6e6c5a..8e45c86 100644 --- a/cproxy.c +++ b/cproxy.c @@ -70,7 +70,8 @@ void handle_client(int client_sock, struct sockaddr_in client_addr, conf *config if (fork() == 0) { if (SIGN == HTTP_CONNECT) { - servertoclient(remote_sock, client_sock, complete_data, &len_complete_data); + //servertoclient(remote_sock, client_sock, complete_data, &len_complete_data); + forward_data(remote_sock, client_sock); } else if (SIGN == HTTP_OTHERS || SIGN == HTTP) { forward_data(remote_sock, client_sock); } @@ -297,8 +298,8 @@ int _main(int argc, char *argv[]) header_buffer = (char *)malloc(BUF_SIZE); len_header_buffer = strlen(header_buffer); - complete_data = (char *)malloc(BUF_SIZES); - len_complete_data = strlen(complete_data); + //complete_data = (char *)malloc(BUF_SIZES); + //len_complete_data = strlen(complete_data); char *inifile = "conf/cproxy.ini"; char path[PATH_SIZE] = { 0 }; @@ -323,8 +324,10 @@ int _main(int argc, char *argv[]) init_daemon(1, 1, configure, path); break; case 's': - if (strcasecmp(optarg, "stop") == 0) + if (strcasecmp(optarg, "stop") == 0) { + free(header_buffer); stop(1, executable_filename); + } exit(0); break; case 'c': @@ -340,10 +343,13 @@ int _main(int argc, char *argv[]) ; } } + + if (setegid(configure->uid) == -1 || seteuid(configure->uid) == -1) // 设置uid + exit(1); start_server(configure); free_conf(configure); - free(complete_data); + //free(complete_data); free(header_buffer); return 0; } diff --git a/cproxy.h b/cproxy.h index 46cb38b..4d35e52 100644 --- a/cproxy.h +++ b/cproxy.h @@ -42,13 +42,14 @@ int remote_sock; char *header_buffer; int len_header_buffer; -char *complete_data; -int len_complete_data; +//char *complete_data; +//int len_complete_data; int SIGN; // 配置文件结构 typedef struct CONF { + int uid; int server_port; // server module char *server_pid_file; diff --git a/log/cproxy.pid b/log/cproxy.pid index c2a7542..f578672 100644 --- a/log/cproxy.pid +++ b/log/cproxy.pid @@ -1 +1 @@ -8604 \ No newline at end of file +24157 \ No newline at end of file