Remove multiple thread detection timeout errors, optimize the processing of strrep and regrep configurations, and optimize the acquisition of host and port functions.

This commit is contained in:
aixiao 2022-02-06 19:04:04 +08:00
parent 2881609c0c
commit 890afe93c8
48 changed files with 154 additions and 232 deletions

7
13.txt
View File

@ -1,7 +0,0 @@
==411== Memcheck, a memory error detector
==411== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==411== Using Valgrind-3.17.0 and LibVEX; rerun with -h for copyright info
==411== Command: ./CProxy -c CProxy.conf
==411== Parent PID: 159
==411==
==411== error calling PR_SET_PTRACER, vgdb might block

BIN
CProxy

Binary file not shown.

View File

@ -8,12 +8,16 @@ global {
}
http {
http_ip="47.240.75.93";
http_ip="2001:19f0:4401:2f:5400:3ff:fec4:e376";
http_port=129;
http_del="Host,";
http_first="[M] [U] [V]\r\nHost: [H]\r\n";
//strrep="Windows NT 10.0" -> "Linux";
//regrep="Host*.+?" -> "Host: [H]";
strrep="Windows NT 10.0" -> "Linux";
strrep="Linux" -> "aixiao.me";
strrep="aixiao.me" -> "AIXIAO.ME";
regrep="Accept-Encoding*.+?" -> "Accept-Encoding: GZIP, deflate";
regrep="Connection*.+?" -> "Connection: KEEP-alive";
}
https {
@ -21,8 +25,12 @@ https {
https_port=129;
https_del="Host,host,x-online-host";
https_first="[M] [U] [V]\r\nHost: [host]\r\n";
//strrep="Windows NT 10.0" -> "Linux";
//regrep="Host*.+?" -> "host: [host]:443";
strrep="Windows NT 10.0" -> "Linux";
strrep="Linux" -> "aixiao.me";
strrep="aixiao.me" -> "AIXIAO.ME";
regrep="Host*.+?" -> "host: [host]:443";
regrep="host*.+?" -> "Host: [H]";
regrep="host*.+?" -> "Host: [host]";
}
httpdns {

View File

@ -1,6 +1,5 @@
global {
uid=3004;
process=2;
timeout=7;
encode=128;
tcp_listen=0124;
@ -9,21 +8,29 @@ global {
}
http {
http_ip="47.240.75.93";
http_port=127;
http_del="Host";
http_first="[M] http://[host][U] [V]\r\nHost: [H]\r\n";
//strrep="Windows NT 10.0" -> "Linux";
//regrep="Host:*.+?" -> "Host: [host]:80";
http_ip="2001:19f0:4401:2f:5400:3ff:fec4:e376";
http_port=129;
http_del="Host,";
http_first="[M] [U] [V]\r\nHost: [H]\r\n";
strrep="Windows NT 10.0" -> "Linux";
strrep="Linux" -> "aixiao.me";
strrep="aixiao.me" -> "AIXIAO.ME";
regrep="Accept-Encoding*.+?" -> "Accept-Encoding: GZIP, deflate";
regrep="Connection*.+?" -> "Connection: KEEP-alive";
}
https {
https_ip="47.240.75.93";
https_port=127;
https_ip="2001:19f0:4401:2f:5400:3ff:fec4:e376";
https_port=129;
https_del="Host,host,x-online-host";
https_first="[M] [U] [V]\r\nHost: [host]\r\n";
//strrep="Windows NT 10.0" -> "Linux";
//regrep="Host*.+?" -> "host: [host]:443";
strrep="Windows NT 10.0" -> "Linux";
strrep="Linux" -> "aixiao.me";
strrep="aixiao.me" -> "AIXIAO.ME";
regrep="Host*.+?" -> "host: [host]:443";
regrep="host*.+?" -> "Host: [H]";
regrep="host*.+?" -> "Host: [host]";
}
httpdns {

View File

@ -32,7 +32,6 @@
-c --config : set configuration file, default: CProxy.conf
-? -h --? --help : help information
Dec 19 2020 09:18:15 Compile、link.
#启动
./CProxy -c CProxy.conf

68
conf.c
View File

@ -225,11 +225,14 @@ static void parse_http_module(char *content, conf * p)
http_node->strrep_s_len = s - val_begin + 1;
http_node->next = NULL;
if (http_head == NULL) {
http_head = http_node;
if (http_head_strrep == NULL) {
http_head_strrep = http_node;
} else {
http_node->next = http_head;
http_head = http_node;
http_node->next = http_head_strrep;
http_head_strrep = http_node;
//http_node->next = http_head_strrep->next;
//http_head_strrep->next = http_node;
}
} else if (strcasecmp(var, "regrep") == 0) {
http_node = (tcp *) malloc(sizeof(struct tcp));
@ -256,11 +259,14 @@ static void parse_http_module(char *content, conf * p)
http_node->regrep_s_len = s - val_begin + 1;
http_node->next = NULL;
if (http_head == NULL) {
http_head = http_node;
if (http_head_regrep == NULL) {
http_head_regrep = http_node;
} else {
http_node->next = http_head;
http_head = http_node;
http_node->next = http_head_regrep;
http_head_regrep = http_node;
//http_node->next = http_head_regrep->next;
//http_head_regrep->next = http_node;
}
}
@ -328,14 +334,14 @@ static void parse_https_module(char *content, conf * p)
https_node->next = NULL;
if (https_head == NULL) {
https_head = https_node;
if (https_head_strrep == NULL) {
https_head_strrep = https_node;
} else {
//https_node->next = https_head;
//https_head = https_node;
https_node->next = https_head_strrep;
https_head_strrep = https_node;
https_node->next = https_head->next;
https_head->next = https_node;
//https_node->next = https_head_strrep->next;
//https_head_strrep->next = https_node;
}
}
else if (strcasecmp(var, "regrep") == 0)
@ -364,14 +370,14 @@ static void parse_https_module(char *content, conf * p)
https_node->regrep_s_len = s - val_begin + 1;
https_node->next = NULL;
if (https_head == NULL) {
https_head = https_node;
if (https_head_regrep == NULL) {
https_head_regrep = https_node;
} else {
https_node->next = https_head;
https_head = https_node;
https_node->next = https_head_regrep;
https_head_regrep = https_node;
//https_node->next = https_head->next;
//https_head->next = https_node;
//https_node->next = https_head_regrep->next;
//https_head_regrep->next = https_node;
}
}
@ -402,6 +408,28 @@ void print_tcp(tcp * p)
}
}
tcp *local_reverse(tcp *head)
{
tcp *beg = NULL;
tcp *end = NULL;
if (head == NULL || head->next == NULL) {
return head;
}
beg = head;
end = head->next;
while (end != NULL) {
//将 end 从链表中摘除
beg->next = end->next;
//将 end 移动至链表头
end->next = head;
head = end;
//调整 end 的指向,另其指向 beg 后的一个节点,为反转下一个节点做准备
end = beg->next;
}
return head;
}
// Free tcp 链表
void free_tcp(tcp **conf_head)
{

8
conf.h
View File

@ -59,13 +59,17 @@ typedef struct tcp {
struct tcp *next;
} tcp;
extern tcp *http_head;
extern tcp *http_head_strrep;
extern tcp *http_head_regrep;
extern tcp *http_node;
extern tcp *https_head;
extern tcp *https_head_strrep;
extern tcp *https_head_regrep;
extern tcp *https_node;
extern void print_tcp(tcp *p);
extern void free_tcp(tcp **p);
extern tcp *local_reverse(tcp *head);
char *strncpy_(char *dest, const char *src, size_t n);

BIN
conf.o

Binary file not shown.

BIN
help.o

Binary file not shown.

Binary file not shown.

View File

@ -476,25 +476,6 @@ void parse_request_head(char *http_request_line, struct http_request *http_reque
return;
}
char *conf_handle(char *str, int str_len, tcp *p)
{
//print_tcp(p);
while (p) {
if (p->strrep) {
str = replace(str, &str_len, p->strrep_s, p->strrep_s_len, p->strrep_t, p->strrep_t_len);
}
if (p->regrep) {
str = regrep(str, &str_len, p->regrep_s, p->regrep_t, p->regrep_t_len);
}
p = p->next;
}
return str;
}
static char *splice_head(char *head, const char *needle, char *string)
{
char *tail_head;
@ -560,6 +541,34 @@ static char *delete_head(char *head, const char *needle, int string)
return memmove(head, temp_stack, temp_stack_len);
}
static char *conf_handle_strrep(char *str, int str_len, tcp *temp)
{
tcp *p = temp;
while (p) {
if (p->strrep) {
str = replace(str, &str_len, p->strrep_s, p->strrep_s_len, p->strrep_t, p->strrep_t_len);
}
p = p->next;
}
return str;
}
static char *conf_handle_regrep(char *str, int str_len, tcp *temp)
{
tcp *p = temp;
while (p) {
if (p->regrep) {
str = regrep(str, &str_len, p->regrep_s, p->regrep_t, p->regrep_t_len);
}
p = p->next;
}
return str;
}
char *request_head(conn_t * in, conf * configure)
{
char *result = NULL;
@ -578,7 +587,6 @@ char *request_head(conn_t * in, conf * configure)
{
char https_del_copy[configure->https_del_len+1];
memset(remote_host, 0, CACHE_SIZE);
if (configure->https_port > 0) {
remote_port = configure->https_port;
@ -603,9 +611,13 @@ char *request_head(conn_t * in, conf * configure)
}
splice_head(incomplete_head, "\n", configure->https_first);
incomplete_head_len = strlen(incomplete_head);
incomplete_head = conf_handle(incomplete_head, incomplete_head_len, https_head);
incomplete_head_len = strlen(incomplete_head);
incomplete_head_len = strlen(incomplete_head); // 更新HTTPS HEADER长度
incomplete_head = conf_handle_strrep(incomplete_head, incomplete_head_len, https_head_strrep);
incomplete_head_len = strlen(incomplete_head) + 1; // 更新HTTPS HEADER长度
incomplete_head = conf_handle_regrep(incomplete_head, incomplete_head_len, https_head_regrep);
incomplete_head_len = strlen(incomplete_head); // 更新HTTPS HEADER长度
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[M]", 3, http_request->method, http_request->method_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[method]", 8, http_request->method, http_request->method_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[U]", 3, http_request->U, http_request->U_len);
@ -615,8 +627,7 @@ char *request_head(conn_t * in, conf * configure)
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[port]", 6, http_request->port, http_request->port_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[H]", 3, http_request->H, http_request->H_len);
incomplete_head_len = strlen(incomplete_head); // 更新HTTPS HEADER长度
printf("%s", incomplete_head); // 打印HTTPS HEADER
//printf("%s", incomplete_head); // 打印HTTPS HEADER
char *new_incomplete_data;
new_incomplete_data = (char *)realloc(in->incomplete_data, incomplete_head_len + 1);
@ -635,7 +646,6 @@ char *request_head(conn_t * in, conf * configure)
if (strncmp(in->incomplete_data, "GET", 3) == 0 || strncmp(in->incomplete_data, "POST", 4) == 0)
{
char http_del_copy[configure->http_del_len + 1];
memset(remote_host, 0, CACHE_SIZE);
@ -661,7 +671,9 @@ char *request_head(conn_t * in, conf * configure)
splice_head(incomplete_head, "\n", configure->http_first);
incomplete_head_len = strlen(incomplete_head); // 更新HTTP HEADER长度
incomplete_head = conf_handle(incomplete_head, incomplete_head_len, http_head);
incomplete_head = conf_handle_strrep(incomplete_head, incomplete_head_len, http_head_strrep);
incomplete_head_len = strlen(incomplete_head) + 1; // 更新HTTP HEADER长度
incomplete_head = conf_handle_regrep(incomplete_head, incomplete_head_len, http_head_regrep);
incomplete_head_len = strlen(incomplete_head); // 更新HTTP HEADER长度
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[M]", 3, http_request->method, http_request->method_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[method]", 8, http_request->method, http_request->method_len);
@ -674,8 +686,8 @@ char *request_head(conn_t * in, conf * configure)
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[port]", 6, http_request->port, http_request->port_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[H]", 3, http_request->H, http_request->H_len);
incomplete_head_len = strlen(incomplete_head); // 更新HTTP HEADER长度
printf("%s", incomplete_head); // 打印HTTP HEADER
//printf("%s", incomplete_head); // 打印HTTP HEADER
char *new_incomplete_data;
new_incomplete_data = (char *)realloc(in->incomplete_data, incomplete_head_len + 1);
if (new_incomplete_data == NULL) {

Binary file not shown.

BIN
httpdns.o

Binary file not shown.

BIN
httpudp.o

Binary file not shown.

Binary file not shown.

Binary file not shown.

43
main.c
View File

@ -18,8 +18,11 @@ char *default_ssl_request;
int default_ssl_request_len;
uint16_t tcp_listen_port;
tcp *http_head = NULL;
tcp *https_head = NULL;
tcp *http_head_strrep;
tcp *http_head_regrep;
tcp *https_head_strrep;
tcp *https_head_regrep;
struct epoll_event ev, events[MAX_CONNECTION + 1];
int epollfd, server_sock, server_sock6, local_port, process;
@ -455,15 +458,25 @@ void _main(int argc, char *argv[])
conf *configure = (struct CONF *)malloc(sizeof(struct CONF));
memset(configure, 0, sizeof(struct CONF));
read_conf(inifile, configure);
// 反转链表,使读取的配置正序
http_head_strrep = local_reverse(http_head_strrep);
http_head_regrep = local_reverse(http_head_regrep);
https_head_strrep = local_reverse(https_head_strrep);
https_head_regrep = local_reverse(https_head_regrep);
/*
printf("%s", configure->http_first);
printf("%s\n", configure->http_del);
printf("%s", configure->https_first);
printf("%s\n", configure->https_del);
print_tcp(https_head);
free_tcp(&https_head);
print_tcp(http_head);
free_tcp(&http_head);
print_tcp(https_head_strrep);
print_tcp(https_head_regrep);
free_tcp(&https_head_strrep);
free_tcp(&https_head_regrep);
print_tcp(http_head_strrep);
print_tcp(http_head_regrep);
free_tcp(&http_head_strrep);
free_tcp(&http_head_regrep);
free_conf(configure);
free(configure);
exit(0);
@ -522,8 +535,10 @@ void _main(int argc, char *argv[])
process = atoi(optarg);
break;
case 'c':
free_tcp(&https_head);
free_tcp(&http_head);
free_tcp(&http_head_strrep);
free_tcp(&http_head_regrep);
free_tcp(&https_head_strrep);
free_tcp(&https_head_regrep);
free_conf(configure);
memset(configure, 0, sizeof(struct CONF));
read_conf(optarg, configure);
@ -554,9 +569,7 @@ void _main(int argc, char *argv[])
}
//server_ini(); // 守护进程
server_ini(); // 守护进程
rt.rlim_max = rt.rlim_cur = MAX_CONNECTION * 2; // 设置每个进程允许打开的最大文件数
if (setrlimit(RLIMIT_NOFILE, &rt) == -1)

BIN
main.o

Binary file not shown.

Binary file not shown.

View File

@ -1,10 +0,0 @@
./obj/local/arm64-v8a/objs/CProxy/conf.o: conf.c conf.h http_request.h \
http_proxy.h main.h
conf.h:
http_request.h:
http_proxy.h:
main.h:

View File

@ -1,3 +0,0 @@
./obj/local/arm64-v8a/objs/CProxy/help.o: help.c help.h
help.h:

View File

@ -1,8 +0,0 @@
./obj/local/arm64-v8a/objs/CProxy/http_proxy.o: http_proxy.c http_proxy.h \
conf.h main.h
http_proxy.h:
conf.h:
main.h:

View File

@ -1,10 +0,0 @@
./obj/local/arm64-v8a/objs/CProxy/http_request.o: http_request.c \
http_request.h http_proxy.h conf.h main.h
http_request.h:
http_proxy.h:
conf.h:
main.h:

View File

@ -1,12 +0,0 @@
./obj/local/arm64-v8a/objs/CProxy/httpdns.o: httpdns.c httpdns.h main.h \
http_request.h http_proxy.h conf.h
httpdns.h:
main.h:
http_request.h:
http_proxy.h:
conf.h:

View File

@ -1,12 +0,0 @@
./obj/local/arm64-v8a/objs/CProxy/httpudp.o: httpudp.c http_request.h \
http_proxy.h conf.h main.h httpudp.h
http_request.h:
http_proxy.h:
conf.h:
main.h:
httpudp.h:

View File

@ -1,16 +0,0 @@
./obj/local/arm64-v8a/objs/CProxy/main.o: main.c main.h http_proxy.h \
conf.h http_request.h httpdns.h httpudp.h help.h
main.h:
http_proxy.h:
conf.h:
http_request.h:
httpdns.h:
httpudp.h:
help.h:

Binary file not shown.

View File

@ -1,10 +0,0 @@
./obj/local/armeabi-v7a/objs/CProxy/conf.o: conf.c conf.h http_request.h \
http_proxy.h main.h
conf.h:
http_request.h:
http_proxy.h:
main.h:

View File

@ -1,3 +0,0 @@
./obj/local/armeabi-v7a/objs/CProxy/help.o: help.c help.h
help.h:

View File

@ -1,8 +0,0 @@
./obj/local/armeabi-v7a/objs/CProxy/http_proxy.o: http_proxy.c \
http_proxy.h conf.h main.h
http_proxy.h:
conf.h:
main.h:

View File

@ -1,10 +0,0 @@
./obj/local/armeabi-v7a/objs/CProxy/http_request.o: http_request.c \
http_request.h http_proxy.h conf.h main.h
http_request.h:
http_proxy.h:
conf.h:
main.h:

View File

@ -1,12 +0,0 @@
./obj/local/armeabi-v7a/objs/CProxy/httpdns.o: httpdns.c httpdns.h main.h \
http_request.h http_proxy.h conf.h
httpdns.h:
main.h:
http_request.h:
http_proxy.h:
conf.h:

View File

@ -1,12 +0,0 @@
./obj/local/armeabi-v7a/objs/CProxy/httpudp.o: httpudp.c http_request.h \
http_proxy.h conf.h main.h httpudp.h
http_request.h:
http_proxy.h:
conf.h:
main.h:
httpudp.h:

View File

@ -1,16 +0,0 @@
./obj/local/armeabi-v7a/objs/CProxy/main.o: main.c main.h http_proxy.h \
conf.h http_request.h httpdns.h httpudp.h help.h
main.h:
http_proxy.h:
conf.h:
http_request.h:
httpdns.h:
httpudp.h:
help.h: