Fix illegal reading and writing.

This commit is contained in:
aixiao 2020-08-21 17:51:19 +08:00
parent 3c8a3747ef
commit 4b40444806
3 changed files with 40 additions and 26 deletions

View File

@ -8,16 +8,16 @@ global {
} }
http { http {
http_ip=8.210.203.112; http_ip=47.240.75.93;
http_port=124; http_port=124;
http_del="x-online-host,X-Online-Host,host,Host"; http_del="x-online-host,X-Online-Host,host,Host";
http_first="[M] http://[host][U] [V]\r\nHost: [host]\r\n"; http_first="[M] http://[host][U] [V]\r\nHost: [host]\r\n";
//strrep="Windows NT 10.0->Linux"; //strrep="Windows NT 10.0->Linux";
//regrep="User-Agent:*.+?->User-Agent: aixiao.me"; //regrep="Host:*.+?->Host: [host]:80";
} }
https { https {
https_ip=8.210.203.112; https_ip=47.240.75.93;
https_port=124; https_port=124;
https_del="Host,host,x-online-host"; https_del="Host,host,x-online-host";
https_first="[M] [U] [V]\r\nHost: [host]\r\n"; https_first="[M] [U] [V]\r\nHost: [host]\r\n";

View File

@ -276,16 +276,16 @@ void parse_request_head(char *http_request_line, struct http_request *http_reque
if (http_request->U_len < 0) if (http_request->U_len < 0)
return; return;
http_request->host = (char *)malloc(sizeof(char) * head_len); http_request->host = (char *)malloc(sizeof(char) * head_len + CACHE_SIZE);
http_request->port = (char *)malloc(sizeof(char) * head_len); http_request->port = (char *)malloc(sizeof(char) * head_len);
http_request->url = (char *)malloc(sizeof(char) * head_len); http_request->url = (char *)malloc(sizeof(char) * head_len);
http_request->uri = (char *)malloc(sizeof(char) * head_len); http_request->uri = (char *)malloc(sizeof(char) * head_len);
http_request->H = (char *)malloc(sizeof(char) * head_len * 2); http_request->H = (char *)malloc(sizeof(char) * head_len * 2);
memset(http_request->host, '\0', head_len); memset(http_request->host, 0, head_len + CACHE_SIZE);
memset(http_request->port, '\0', head_len); memset(http_request->port, 0, head_len);
memset(http_request->url, '\0', head_len); memset(http_request->url, 0, head_len);
memset(http_request->uri, '\0', head_len); memset(http_request->uri, 0, head_len);
memset(http_request->H, '\0', head_len * 2); memset(http_request->H, 0, head_len * 2);
if (extract_host(http_request_line, http_request->host, http_request->port) == -1) if (extract_host(http_request_line, http_request->host, http_request->port) == -1)
return; return;
@ -309,6 +309,8 @@ char *request_head(conn * in, conf * configure)
{ {
struct http_request *http_request; struct http_request *http_request;
http_request = (struct http_request *)malloc(sizeof(struct http_request)); http_request = (struct http_request *)malloc(sizeof(struct http_request));
memset(http_request, 0, sizeof(struct http_request));
parse_request_head(in->header_buffer, http_request); parse_request_head(in->header_buffer, http_request);
if (strncmp(in->header_buffer, "CONNECT", 7) == 0) { if (strncmp(in->header_buffer, "CONNECT", 7) == 0) {
@ -391,10 +393,6 @@ char *request_head(conn * in, conf * configure)
} }
splice_head(incomplete_head, "\n", configure->http_first); splice_head(incomplete_head, "\n", configure->http_first);
incomplete_head_len = strlen(incomplete_head); incomplete_head_len = strlen(incomplete_head);
if (configure->http_strrep)
incomplete_head = replace(incomplete_head, &incomplete_head_len, configure->http_strrep_aim, configure->http_strrep_aim_len, configure->http_strrep_obj, configure->http_strrep_obj_len);
if (configure->http_regrep)
incomplete_head = regrep(incomplete_head, &incomplete_head_len, configure->http_regrep_aim, configure->http_regrep_obj, configure->http_regrep_obj_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\r", 2, "\r", 1); incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\r", 2, "\r", 1);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\n", 2, "\n", 1); incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\n", 2, "\n", 1);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\b", 2, "\b", 1); incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\b", 2, "\b", 1);
@ -414,11 +412,16 @@ char *request_head(conn * in, conf * configure)
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[host]", 6, http_request->host, http_request->host_len); incomplete_head = replace(incomplete_head, &incomplete_head_len, "[host]", 6, http_request->host, http_request->host_len);
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, "[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 = replace(incomplete_head, &incomplete_head_len, "[H]", 3, http_request->H, http_request->H_len);
//printf("%s", incomplete_head); if (configure->http_strrep)
incomplete_head = replace(incomplete_head, &incomplete_head_len, configure->http_strrep_aim, configure->http_strrep_aim_len, configure->http_strrep_obj, configure->http_strrep_obj_len);
if (configure->http_regrep)
incomplete_head = regrep(incomplete_head, &incomplete_head_len, configure->http_regrep_aim, configure->http_regrep_obj, configure->http_regrep_obj_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[host]", 6, http_request->host, http_request->host_len);
incomplete_head_len = strlen(incomplete_head); incomplete_head_len = strlen(incomplete_head);
//printf("%s", incomplete_head);
memset(in->header_buffer, 0, strlen(in->header_buffer)); memset(in->header_buffer, 0, in->header_buffer_len);
memmove(in->header_buffer, incomplete_head, incomplete_head_len); memmove(in->header_buffer, incomplete_head, incomplete_head_len + 1);
in->header_buffer_len = strlen(in->header_buffer); in->header_buffer_len = strlen(in->header_buffer);
free(incomplete_head); free(incomplete_head);
} }

19
main.c
View File

@ -229,6 +229,7 @@ void _main(int argc, char *argv[])
char *inifile = "/CProxy.conf"; char *inifile = "/CProxy.conf";
inifile = strcat(path, inifile); inifile = strcat(path, inifile);
conf *configure = (struct CONF *)malloc(sizeof(struct CONF)); conf *configure = (struct CONF *)malloc(sizeof(struct CONF));
memset(configure, 0, sizeof(struct CONF));
read_conf(inifile, configure); read_conf(inifile, configure);
sslEncodeCode = 0; // 默认SSL不转码 sslEncodeCode = 0; // 默认SSL不转码
@ -241,7 +242,6 @@ void _main(int argc, char *argv[])
if (configure->process > 0) // 如果配置文件有值,优先使用配置文件读取的值 if (configure->process > 0) // 如果配置文件有值,优先使用配置文件读取的值
process = configure->process; process = configure->process;
//char optstring[] = ":l:f:t:p:c:e:s:h?";
int longindex = 0; int longindex = 0;
char optstring[] = ":l:f:t:p:c:e:s:h?"; char optstring[] = ":l:f:t:p:c:e:s:h?";
static struct option longopts[] = { static struct option longopts[] = {
@ -257,6 +257,7 @@ void _main(int argc, char *argv[])
{ 0, 0, 0, 0 } { 0, 0, 0, 0 }
}; };
char *p = NULL; char *p = NULL;
//char optstring[] = ":l:f:t:p:c:e:s:h?";
//while (-1 != (opt = getopt(argc, argv, optstring))) { //while (-1 != (opt = getopt(argc, argv, optstring))) {
while (-1 != (opt = getopt_long(argc, argv, optstring, longopts, &longindex))) { while (-1 != (opt = getopt_long(argc, argv, optstring, longopts, &longindex))) {
switch (opt) { switch (opt) {
@ -313,6 +314,7 @@ void _main(int argc, char *argv[])
} }
} }
server_ini(); // 守护进程
httpdns_initialize(configure); // 初始化http_dns httpdns_initialize(configure); // 初始化http_dns
memset(cts, 0, sizeof(cts)); memset(cts, 0, sizeof(cts));
for (i = MAX_CONNECTION; i--;) for (i = MAX_CONNECTION; i--;)
@ -340,8 +342,7 @@ void _main(int argc, char *argv[])
if (setegid(configure->uid) == -1 || seteuid(configure->uid) == -1) // 设置uid if (setegid(configure->uid) == -1 || seteuid(configure->uid) == -1) // 设置uid
exit(1); exit(1);
server_ini(); // 初始化http_proxy //start_server(configure); // 单线程
//start_server(configure);
//httpdns_loop(configure); //httpdns_loop(configure);
pthread_t thread_id = 0; pthread_t thread_id = 0;
@ -361,7 +362,17 @@ void _main(int argc, char *argv[])
pthread_join(thread_id, NULL); pthread_join(thread_id, NULL);
pthread_exit(NULL); pthread_exit(NULL);
return ; /* 线程分离未使用
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&thread_id, &attr, &tcp_timeout_check, NULL);
pthread_create(&thread_id, &attr, &http_proxy_loop, (void *)configure);
pthread_create(&thread_id, &attr, &httpdns_loop, (void *)configure);
pthread_exit(NULL);
*/
return;
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])