optimization

This commit is contained in:
aixiao 2022-06-04 11:06:12 +08:00
parent 0efa5309a6
commit 9a2c98f059
10 changed files with 275 additions and 368 deletions

View File

@ -7,8 +7,8 @@ global {
} }
http { http {
http_ip="git.aixiao.me"; http_ip="aixiao.me";
http_port=128; http_port=129;
http_del="Host,"; http_del="Host,";
http_first="[M] [U] [V]\r\nHost: [H]\r\n"; http_first="[M] [U] [V]\r\nHost: [H]\r\n";
strrep="Windows NT 10.0" -> "Linux"; strrep="Windows NT 10.0" -> "Linux";
@ -16,15 +16,15 @@ http {
strrep="aixiao.me" -> "AIXIAO.ME"; strrep="aixiao.me" -> "AIXIAO.ME";
regrep="Accept-Encoding*.+?" -> "Accept-Encoding: GZIP, deflate"; regrep="Accept-Encoding*.+?" -> "Accept-Encoding: GZIP, deflate";
regrep="Connection*.+?" -> "Connection: KEEP-alive"; regrep="Connection*.+?" -> "Connection: KEEP-alive";
encode=128; encode=129;
} }
https { https {
https_ip="git.aixiao.me"; https_ip="aixiao.me";
https_port=128; https_port=129;
https_del="Host,host,x-online-host"; https_del="Host,host,x-online-host,Proxy-Connection";
https_first="[M] [U] [V]\r\nHost: [host]:[port]\r\n"; https_first="[M] [U] [V]\r\nHost: [host]:[port]\r\n";
encode=128; encode=129;
} }
httpdns { httpdns {

View File

@ -1,8 +1,8 @@
CROSS_COMPILE ?= CROSS_COMPILE ?=
CC := $(CROSS_COMPILE)gcc CC := $(CROSS_COMPILE)gcc
STRIP := $(CROSS_COMPILE)strip STRIP := $(CROSS_COMPILE)strip
CFLAGS += -g -O2 -Wall -pthread CFLAGS += -g -O2 -Wall -pthread
LIBS = LIBS = -static
OBJ := CProxy OBJ := CProxy
all: main.o http_proxy.o http_request.o httpdns.o httpudp.o conf.o all: main.o http_proxy.o http_request.o httpdns.o httpudp.o conf.o

214
conf.c
View File

@ -1,7 +1,6 @@
#include "conf.h" #include "conf.h"
#include "http_request.h" #include "http_request.h"
/* 字符串预处理,设置转义字符 */ /* 字符串预处理,设置转义字符 */
static void string_pretreatment(char *str, int *len) static void string_pretreatment(char *str, int *len)
{ {
@ -113,41 +112,26 @@ static char *read_module(char *buff, const char *module_name)
return strndup(p, p0 - p); return strndup(p, p0 - p);
} }
static void parse_global_module(char *content, conf *p) static void parse_global_module(char *content, conf * p)
{ {
char *var, *val_begin, *val_end, *lineEnd; char *var, *val_begin, *val_end, *lineEnd;
while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) { while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) {
if (strcasecmp(var, "uid") == 0) if (strcasecmp(var, "uid") == 0) {
{
p->uid = atoi(val_begin); p->uid = atoi(val_begin);
} } else if (strcasecmp(var, "process") == 0) {
else if (strcasecmp(var, "process") == 0)
{
p->process = atoi(val_begin); p->process = atoi(val_begin);
} } else if (strcasecmp(var, "timeout") == 0) {
else if (strcasecmp(var, "timeout") == 0)
{
p->timeout = atoi(val_begin); p->timeout = atoi(val_begin);
} } else if (strcasecmp(var, "encode") == 0) {
else if (strcasecmp(var, "encode") == 0)
{
p->sslencoding = atoi(val_begin); p->sslencoding = atoi(val_begin);
} } else if (strcasecmp(var, "tcp_listen") == 0) {
else if (strcasecmp(var, "tcp_listen") == 0)
{
p->tcp_listen = atoi(val_begin); p->tcp_listen = atoi(val_begin);
} } else if (strcasecmp(var, "tcp6_listen") == 0) {
else if (strcasecmp(var, "tcp6_listen") == 0)
{
p->tcp6_listen = atoi(val_begin); p->tcp6_listen = atoi(val_begin);
} } else if (strcasecmp(var, "dns_listen") == 0) {
else if (strcasecmp(var, "dns_listen") == 0)
{
p->dns_listen = atoi(val_begin); p->dns_listen = atoi(val_begin);
} } else if (strcasecmp(var, "udp_listen") == 0) {
else if (strcasecmp(var, "udp_listen") == 0)
{
p->udp_listen = atoi(val_begin);; p->udp_listen = atoi(val_begin);;
} }
@ -155,7 +139,7 @@ static void parse_global_module(char *content, conf *p)
} }
} }
static void parse_http_module(char *content, conf *p) static void parse_http_module(char *content, conf * p)
{ {
char *var, *val_begin, *val_end, *lineEnd; char *var, *val_begin, *val_end, *lineEnd;
tcp *http_node = NULL; tcp *http_node = NULL;
@ -163,37 +147,26 @@ static void parse_http_module(char *content, conf *p)
char *p2 = NULL; char *p2 = NULL;
while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) { while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) {
if (strcasecmp(var, "http_ip") == 0) if (strcasecmp(var, "http_ip") == 0) {
{
p->http_ip_len = val_end - val_begin; p->http_ip_len = val_end - val_begin;
if (copy_new_mem(val_begin, p->http_ip_len, &p->http_ip) != 0) if (copy_new_mem(val_begin, p->http_ip_len, &p->http_ip) != 0)
return ; return;
} } else if (strcasecmp(var, "encode") == 0) {
else if (strcasecmp(var, "encode") == 0)
{
p->http_encode = atoi(val_begin); p->http_encode = atoi(val_begin);
} } else if (strcasecmp(var, "http_port") == 0) {
else if (strcasecmp(var, "http_port") == 0)
{
p->http_port = atoi(val_begin); p->http_port = atoi(val_begin);
} } else if (strcasecmp(var, "http_del") == 0) {
else if (strcasecmp(var, "http_del") == 0)
{
p->http_del_len = val_end - val_begin; p->http_del_len = val_end - val_begin;
if (copy_new_mem(val_begin, p->http_del_len, &p->http_del) != 0) if (copy_new_mem(val_begin, p->http_del_len, &p->http_del) != 0)
return ; return;
} } else if (strcasecmp(var, "http_first") == 0) {
else if (strcasecmp(var, "http_first") == 0)
{
p->http_first_len = val_end - val_begin; p->http_first_len = val_end - val_begin;
if (copy_new_mem(val_begin, p->http_first_len, &p->http_first) != 0) if (copy_new_mem(val_begin, p->http_first_len, &p->http_first) != 0)
return ; return;
} } else if (strcasecmp(var, "strrep") == 0) {
else if (strcasecmp(var, "strrep") == 0) http_node = (tcp *) malloc(sizeof(struct tcp));
{
http_node = (tcp *)malloc(sizeof(struct tcp));
if (http_node == NULL) if (http_node == NULL)
return ; return;
memset(http_node, 0, sizeof(struct tcp)); memset(http_node, 0, sizeof(struct tcp));
http_node->strrep = strdup(val_begin); http_node->strrep = strdup(val_begin);
http_node->strrep_len = val_end - val_begin; http_node->strrep_len = val_end - val_begin;
@ -201,7 +174,7 @@ static void parse_http_module(char *content, conf *p)
p1 = strstr(val_begin, "->"); p1 = strstr(val_begin, "->");
for (t = p1; *t != '"'; ++t) ; for (t = p1; *t != '"'; ++t) ;
http_node->strrep_t = strdup(t + 1); http_node->strrep_t = strdup(t + 1);
p2 = strchr(t+1, '\0'); p2 = strchr(t + 1, '\0');
http_node->strrep_t_len = p2 - (t + 1); http_node->strrep_t_len = p2 - (t + 1);
for (s = p1 - 1; *s == ' '; s--) { for (s = p1 - 1; *s == ' '; s--) {
@ -213,34 +186,34 @@ static void parse_http_module(char *content, conf *p)
http_node->strrep_s = strndup(val_begin, s - val_begin + 1); http_node->strrep_s = strndup(val_begin, s - val_begin + 1);
http_node->strrep_s_len = s - val_begin + 1; http_node->strrep_s_len = s - val_begin + 1;
http_node->next = NULL; http_node->next = NULL;
if (http_head_strrep == NULL) { if (http_head_strrep == NULL) {
http_head_strrep = http_node; http_head_strrep = http_node;
} else { } else {
http_node->next = http_head_strrep; http_node->next = http_head_strrep;
http_head_strrep = http_node; http_head_strrep = http_node;
//http_node->next = http_head_strrep->next; //http_node->next = http_head_strrep->next;
//http_head_strrep->next = http_node; //http_head_strrep->next = http_node;
} }
} else if (strcasecmp(var, "regrep") == 0) { } else if (strcasecmp(var, "regrep") == 0) {
http_node = (tcp *) malloc(sizeof(struct tcp)); http_node = (tcp *) malloc(sizeof(struct tcp));
if (http_node == NULL) if (http_node == NULL)
return ; return;
memset(http_node, 0, sizeof(struct tcp)); memset(http_node, 0, sizeof(struct tcp));
http_node->regrep = strdup(val_begin); http_node->regrep = strdup(val_begin);
http_node->regrep_len = val_end - val_begin; http_node->regrep_len = val_end - val_begin;
p1 = strstr(val_begin, "->"); p1 = strstr(val_begin, "->");
for (t = p1; *t != '"'; ++t) ; for (t = p1; *t != '"'; ++t) ;
http_node->regrep_t = strdup(t + 1); http_node->regrep_t = strdup(t + 1);
p2 = strchr(t+1, '\0'); p2 = strchr(t + 1, '\0');
http_node->regrep_t_len = p2 - (t + 1); http_node->regrep_t_len = p2 - (t + 1);
for (s = p1 - 1; *s == ' '; s--) { for (s = p1 - 1; *s == ' '; s--) {
if (s == val_begin) if (s == val_begin)
return ; return;
} }
if (*s == '"') if (*s == '"')
s--; s--;
@ -254,7 +227,7 @@ static void parse_http_module(char *content, conf *p)
} else { } else {
http_node->next = http_head_regrep; http_node->next = http_head_regrep;
http_head_regrep = http_node; http_head_regrep = http_node;
//http_node->next = http_head_regrep->next; //http_node->next = http_head_regrep->next;
//http_head_regrep->next = http_node; //http_head_regrep->next = http_node;
} }
@ -264,7 +237,7 @@ static void parse_http_module(char *content, conf *p)
} }
} }
static void parse_https_module(char *content, conf *p) static void parse_https_module(char *content, conf * p)
{ {
char *var, *val_begin, *val_end, *lineEnd; char *var, *val_begin, *val_end, *lineEnd;
tcp *https_node = NULL; tcp *https_node = NULL;
@ -272,40 +245,29 @@ static void parse_https_module(char *content, conf *p)
char *p2 = NULL; char *p2 = NULL;
while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) { while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) {
if (strcasecmp(var, "https_ip") == 0) if (strcasecmp(var, "https_ip") == 0) {
{
p->https_ip_len = val_end - val_begin; p->https_ip_len = val_end - val_begin;
if (copy_new_mem(val_begin, p->https_ip_len, &p->https_ip) != 0) if (copy_new_mem(val_begin, p->https_ip_len, &p->https_ip) != 0)
return ; return;
} } else if (strcasecmp(var, "encode") == 0) {
else if (strcasecmp(var, "encode") == 0)
{
p->https_encode = atoi(val_begin); p->https_encode = atoi(val_begin);
} } else if (strcasecmp(var, "https_port") == 0) {
else if (strcasecmp(var, "https_port") == 0)
{
p->https_port = atoi(val_begin); p->https_port = atoi(val_begin);
} } else if (strcasecmp(var, "https_del") == 0) {
else if (strcasecmp(var, "https_del") == 0)
{
p->https_del_len = val_end - val_begin; p->https_del_len = val_end - val_begin;
if (copy_new_mem(val_begin, p->https_del_len, &p->https_del) != 0) if (copy_new_mem(val_begin, p->https_del_len, &p->https_del) != 0)
return ; return;
} } else if (strcasecmp(var, "https_first") == 0) {
else if (strcasecmp(var, "https_first") == 0)
{
p->https_first_len = val_end - val_begin; p->https_first_len = val_end - val_begin;
if (copy_new_mem(val_begin, p->https_first_len, &p->https_first) != 0) if (copy_new_mem(val_begin, p->https_first_len, &p->https_first) != 0)
return ; return;
} } else if (strcasecmp(var, "strrep") == 0) {
else if (strcasecmp(var, "strrep") == 0)
{
// 链表操作,支持多个相同配置KEY // 链表操作,支持多个相同配置KEY
https_node = (tcp *)malloc(sizeof(struct tcp)); https_node = (tcp *) malloc(sizeof(struct tcp));
if (https_node == NULL) if (https_node == NULL)
return ; return;
memset(https_node, 0, sizeof(struct tcp)); memset(https_node, 0, sizeof(struct tcp));
https_node->strrep = strdup(val_begin); https_node->strrep = strdup(val_begin);
https_node->strrep_len = val_end - val_begin; https_node->strrep_len = val_end - val_begin;
@ -313,7 +275,7 @@ static void parse_https_module(char *content, conf *p)
p1 = strstr(val_begin, "->"); p1 = strstr(val_begin, "->");
for (t = p1; *t != '"'; ++t) ; for (t = p1; *t != '"'; ++t) ;
https_node->strrep_t = strdup(t + 1); https_node->strrep_t = strdup(t + 1);
p2 = strchr(t+1, '\0'); p2 = strchr(t + 1, '\0');
https_node->strrep_t_len = p2 - (t + 1); https_node->strrep_t_len = p2 - (t + 1);
for (s = p1 - 1; *s == ' '; s--) { for (s = p1 - 1; *s == ' '; s--) {
@ -325,37 +287,35 @@ static void parse_https_module(char *content, conf *p)
https_node->strrep_s = strndup(val_begin, s - val_begin + 1); https_node->strrep_s = strndup(val_begin, s - val_begin + 1);
https_node->strrep_s_len = s - val_begin + 1; https_node->strrep_s_len = s - val_begin + 1;
https_node->next = NULL; https_node->next = NULL;
if (https_head_strrep == NULL) { if (https_head_strrep == NULL) {
https_head_strrep = https_node; https_head_strrep = https_node;
} else { } else {
https_node->next = https_head_strrep; https_node->next = https_head_strrep;
https_head_strrep = https_node; https_head_strrep = https_node;
//https_node->next = https_head_strrep->next; //https_node->next = https_head_strrep->next;
//https_head_strrep->next = https_node; //https_head_strrep->next = https_node;
} }
} } else if (strcasecmp(var, "regrep") == 0) {
else if (strcasecmp(var, "regrep") == 0)
{
https_node = (tcp *) malloc(sizeof(struct tcp)); https_node = (tcp *) malloc(sizeof(struct tcp));
if (https_node == NULL) if (https_node == NULL)
return ; return;
memset(https_node, 0, sizeof(struct tcp)); memset(https_node, 0, sizeof(struct tcp));
https_node->regrep = strdup(val_begin); https_node->regrep = strdup(val_begin);
https_node->regrep_len = val_end - val_begin; https_node->regrep_len = val_end - val_begin;
p1 = strstr(val_begin, "->"); p1 = strstr(val_begin, "->");
for (t = p1; *t != '"'; ++t) ; for (t = p1; *t != '"'; ++t) ;
https_node->regrep_t = strdup(t + 1); https_node->regrep_t = strdup(t + 1);
p2 = strchr(t+1, '\0'); p2 = strchr(t + 1, '\0');
https_node->regrep_t_len = p2 - (t + 1); https_node->regrep_t_len = p2 - (t + 1);
for (s = p1 - 1; *s == ' '; s--) { for (s = p1 - 1; *s == ' '; s--) {
if (s == val_begin) if (s == val_begin)
return ; return;
} }
if (*s == '"') if (*s == '"')
s--; s--;
@ -369,7 +329,7 @@ static void parse_https_module(char *content, conf *p)
} else { } else {
https_node->next = https_head_regrep; https_node->next = https_head_regrep;
https_head_regrep = https_node; https_head_regrep = https_node;
//https_node->next = https_head_regrep->next; //https_node->next = https_head_regrep->next;
//https_head_regrep->next = https_node; //https_head_regrep->next = https_node;
} }
@ -380,7 +340,7 @@ static void parse_https_module(char *content, conf *p)
} }
// 打印tcp链表 // 打印tcp链表
void print_tcp(tcp *p) void print_tcp(tcp * p)
{ {
tcp *temp = p; tcp *temp = p;
while (temp) { while (temp) {
@ -402,8 +362,7 @@ void print_tcp(tcp *p)
} }
} }
tcp *local_reverse(tcp * head)
tcp *local_reverse(tcp *head)
{ {
tcp *beg = NULL; tcp *beg = NULL;
tcp *end = NULL; tcp *end = NULL;
@ -425,21 +384,20 @@ tcp *local_reverse(tcp *head)
} }
// Free tcp 链表 // Free tcp 链表
void free_tcp(tcp **conf_head) void free_tcp(tcp ** conf_head)
{ {
tcp *t; tcp *t;
while(*conf_head != NULL) while (*conf_head != NULL) {
{ t = *conf_head;
t=*conf_head; *conf_head = t->next;
*conf_head=t->next;
if (t->strrep) if (t->strrep)
free(t->strrep); free(t->strrep);
if (t->strrep_s) if (t->strrep_s)
free(t->strrep_s); free(t->strrep_s);
if (t->strrep_t) if (t->strrep_t)
free(t->strrep_t); free(t->strrep_t);
if (t->regrep) if (t->regrep)
free(t->regrep); free(t->regrep);
if (t->regrep_s) if (t->regrep_s)
@ -451,59 +409,49 @@ void free_tcp(tcp **conf_head)
} }
} }
static void parse_httpdns_module(char *content, conf *p) static void parse_httpdns_module(char *content, conf * p)
{ {
char *var, *val_begin, *val_end, *lineEnd; char *var, *val_begin, *val_end, *lineEnd;
while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) { while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) {
if (strcasecmp(var, "addr") == 0) if (strcasecmp(var, "addr") == 0) {
{
p->httpdns_addr_len = val_end - val_begin; p->httpdns_addr_len = val_end - val_begin;
if (copy_new_mem(val_begin, p->httpdns_addr_len, &p->httpdns_addr) != 0) if (copy_new_mem(val_begin, p->httpdns_addr_len, &p->httpdns_addr) != 0)
return ; return;
} } else if (strcasecmp(var, "http_req") == 0) {
else if (strcasecmp(var, "http_req") == 0)
{
p->httpdns_http_req_len = val_end - val_begin; p->httpdns_http_req_len = val_end - val_begin;
if (copy_new_mem(val_begin, p->httpdns_http_req_len, &p->httpdns_http_req) != 0) if (copy_new_mem(val_begin, p->httpdns_http_req_len, &p->httpdns_http_req) != 0)
return ; return;
} } else if (strcasecmp(var, "encode") == 0) {
else if (strcasecmp(var, "encode") == 0)
{
p->encode = atoi(val_begin); p->encode = atoi(val_begin);
} }
content = strchr(lineEnd + 1, '\n'); content = strchr(lineEnd + 1, '\n');
} }
} }
static void parse_httpudp_module(char *content, conf *p) static void parse_httpudp_module(char *content, conf * p)
{ {
char *var, *val_begin, *val_end, *lineEnd; char *var, *val_begin, *val_end, *lineEnd;
while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) { while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) {
if (strcasecmp(var, "addr") == 0) if (strcasecmp(var, "addr") == 0) {
{
p->httpudp_addr_len = val_end - val_begin; p->httpudp_addr_len = val_end - val_begin;
if (copy_new_mem(val_begin, p->httpudp_addr_len, &p->httpudp_addr) != 0) if (copy_new_mem(val_begin, p->httpudp_addr_len, &p->httpudp_addr) != 0)
return ; return;
} } else if (strcasecmp(var, "http_req") == 0) {
else if (strcasecmp(var, "http_req") == 0)
{
p->httpudp_http_req_len = val_end - val_begin; p->httpudp_http_req_len = val_end - val_begin;
if (copy_new_mem(val_begin, p->httpudp_http_req_len, &p->httpudp_http_req) != 0) if (copy_new_mem(val_begin, p->httpudp_http_req_len, &p->httpudp_http_req) != 0)
return ; return;
} } else if (strcasecmp(var, "encode") == 0) {
else if (strcasecmp(var, "encode") == 0)
{
p->httpudp_encode = atoi(val_begin); p->httpudp_encode = atoi(val_begin);
} }
content = strchr(lineEnd + 1, '\n'); content = strchr(lineEnd + 1, '\n');
} }
} }
void free_conf(conf *p) void free_conf(conf * p)
{ {
// http module // http module
if (p->http_ip) if (p->http_ip)
@ -526,17 +474,17 @@ void free_conf(conf *p)
free(p->httpdns_addr); free(p->httpdns_addr);
if (p->httpdns_http_req) if (p->httpdns_http_req)
free(p->httpdns_http_req); free(p->httpdns_http_req);
// httpudp module // httpudp module
if(p->httpudp_addr) if (p->httpudp_addr)
free(p->httpudp_addr); free(p->httpudp_addr);
if (p->httpudp_http_req) if (p->httpudp_http_req)
free(p->httpudp_http_req); free(p->httpudp_http_req);
return; return;
} }
void read_conf(char *filename, conf *configure) void read_conf(char *filename, conf * configure)
{ {
char *buff, *global_content, *http_content, *https_content, *httpdns_content, *httpudp_content; char *buff, *global_content, *http_content, *https_content, *httpdns_content, *httpudp_content;
FILE *file; FILE *file;
@ -582,7 +530,7 @@ void read_conf(char *filename, conf *configure)
else else
parse_httpdns_module(httpdns_content, configure); parse_httpdns_module(httpdns_content, configure);
free(httpdns_content); free(httpdns_content);
if ((httpudp_content = read_module(buff, "httpudp")) == NULL) if ((httpudp_content = read_module(buff, "httpudp")) == NULL)
perror("read httpdns module error"); perror("read httpdns module error");
else else

13
conf.h
View File

@ -38,7 +38,7 @@ typedef struct CONF {
int httpdns_addr_len; int httpdns_addr_len;
int httpdns_http_req_len; int httpdns_http_req_len;
int encode; int encode;
// httpudp module // httpudp module
char *httpudp_addr; char *httpudp_addr;
char *httpudp_http_req; char *httpudp_http_req;
@ -69,13 +69,12 @@ extern tcp *https_head_strrep;
extern tcp *https_head_regrep; extern tcp *https_head_regrep;
extern tcp *https_node; extern tcp *https_node;
extern void print_tcp(tcp *p); extern void print_tcp(tcp * p);
extern void free_tcp(tcp **p); extern void free_tcp(tcp ** p);
extern tcp *local_reverse(tcp *head); extern tcp *local_reverse(tcp * head);
char *strncpy_(char *dest, const char *src, size_t n); char *strncpy_(char *dest, const char *src, size_t n);
void read_conf(char *file, conf *p); void read_conf(char *file, conf * p);
void free_conf(conf *p); void free_conf(conf * p);
#endif #endif

View File

@ -16,41 +16,36 @@ void dataEncode(char *data, int data_len, unsigned code)
void *tcp_timeout_check(void *nullPtr) void *tcp_timeout_check(void *nullPtr)
{ {
int i; int i;
for (i = 0; i < MAX_CONNECTION; i += 2) { for (i = 0; i < MAX_CONNECTION; i += 2) {
if (cts[i].fd > -1) if (cts[i].fd > -1) {
{
if (cts[i].timer >= timeout_minute) { if (cts[i].timer >= timeout_minute) {
printf("关闭连接\n"); printf("关闭连接\n");
close_connection(cts + i); close_connection(cts + i);
} } else
else
cts[i].timer++; cts[i].timer++;
} }
} }
return NULL; return NULL;
} }
static char *read_data(conn_t *in, char *data, int *data_len) static char *read_data(conn_t * in, char *data, int *data_len)
{ {
char *new_data; char *new_data;
int read_len; int read_len;
do { do {
new_data = (char *)realloc(data, *data_len + BUFFER_SIZE + 1); new_data = (char *)realloc(data, *data_len + BUFFER_SIZE + 1);
if (new_data == NULL) if (new_data == NULL) {
{
free(data); free(data);
return NULL; return NULL;
} }
data = new_data; data = new_data;
read_len = read(in->fd, data + *data_len, BUFFER_SIZE); read_len = read(in->fd, data + *data_len, BUFFER_SIZE);
/* 判断是否关闭连接 */ /* 判断是否关闭连接 */
if (read_len <= 0) if (read_len <= 0) {
{ if (read_len == 0 || *data_len == 0 || errno != EAGAIN) {
if (read_len == 0 || *data_len == 0 || errno != EAGAIN)
{
free(data); free(data);
return NULL; return NULL;
} }
@ -60,7 +55,7 @@ static char *read_data(conn_t *in, char *data, int *data_len)
} while (read_len == BUFFER_SIZE); } while (read_len == BUFFER_SIZE);
*(data + *data_len) = '\0'; *(data + *data_len) = '\0';
return data; return data;
} }
void close_connection(conn_t * conn) void close_connection(conn_t * conn)
@ -73,12 +68,12 @@ void close_connection(conn_t * conn)
server_data = conn->ready_data; server_data = conn->ready_data;
memset(conn, 0, sizeof(conn_t)); memset(conn, 0, sizeof(conn_t));
conn->ready_data = server_data; conn->ready_data = server_data;
conn-- ->fd = -1; conn--->fd = -1;
} else { } else {
free(conn->ready_data); free(conn->ready_data);
free(conn->incomplete_data); free(conn->incomplete_data);
memset(conn, 0, sizeof(conn_t)); memset(conn, 0, sizeof(conn_t));
conn++ ->fd = -1; conn++->fd = -1;
} }
if (conn->fd >= 0) if (conn->fd >= 0)
close_connection(conn); close_connection(conn);
@ -137,20 +132,7 @@ void clientToserver(conn_t * in)
// 判断请求类型 // 判断请求类型
static int8_t request_type(char *data) static int8_t request_type(char *data)
{ {
if (strncmp(data, "GET", 3) == 0 || if (strncmp(data, "GET", 3) == 0 || strncmp(data, "POST", 4) == 0 || strncmp(data, "CONNECT", 7) == 0 || strncmp(data, "HEAD", 4) == 0 || strncmp(data, "PUT", 3) == 0 || strncmp(data, "OPTIONS", 7) == 0 || strncmp(data, "MOVE", 4) == 0 || strncmp(data, "COPY", 4) == 0 || strncmp(data, "TRACE", 5) == 0 || strncmp(data, "DELETE", 6) == 0 || strncmp(data, "LINK", 4) == 0 || strncmp(data, "UNLINK", 6) == 0 || strncmp(data, "PATCH", 5) == 0 || strncmp(data, "WRAPPED", 7) == 0)
strncmp(data, "POST", 4) == 0 ||
strncmp(data, "CONNECT", 7) == 0 ||
strncmp(data, "HEAD", 4) == 0 ||
strncmp(data, "PUT", 3) == 0 ||
strncmp(data, "OPTIONS", 7) == 0 ||
strncmp(data, "MOVE", 4) == 0 ||
strncmp(data, "COPY", 4) == 0 ||
strncmp(data, "TRACE", 5) == 0 ||
strncmp(data, "DELETE", 6) == 0 ||
strncmp(data, "LINK", 4) == 0 ||
strncmp(data, "UNLINK", 6) == 0 ||
strncmp(data, "PATCH", 5) == 0 ||
strncmp(data, "WRAPPED", 7) == 0)
return HTTP_TYPE; return HTTP_TYPE;
return OTHER_TYPE; return OTHER_TYPE;
} }
@ -167,7 +149,7 @@ int check_ipversion(char *address)
return AF_INET6; return AF_INET6;
} }
} }
return 0; return 0;
} }
@ -192,18 +174,18 @@ int create_connection6(char *remote_host, int remote_port)
hints.ai_family = validfamily; hints.ai_family = validfamily;
hints.ai_flags |= AI_NUMERICHOST; hints.ai_flags |= AI_NUMERICHOST;
} }
if (getaddrinfo(remote_host, portstr, &hints, &res) != 0) { if (getaddrinfo(remote_host, portstr, &hints, &res) != 0) {
errno = EFAULT; errno = EFAULT;
perror("getaddrinfo"); perror("getaddrinfo");
return -1; return -1;
} }
if ((sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) { if ((sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
perror("socket"); perror("socket");
return -1; return -1;
} }
if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) { if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
perror("connect"); perror("connect");
return -1; return -1;
@ -212,7 +194,6 @@ int create_connection6(char *remote_host, int remote_port)
if (res != NULL) if (res != NULL)
freeaddrinfo(res); freeaddrinfo(res);
/* /*
//通用sockaddr_storage结构体 //通用sockaddr_storage结构体
struct sockaddr_storage remote_addr; struct sockaddr_storage remote_addr;
@ -242,7 +223,7 @@ int create_connection6(char *remote_host, int remote_port)
return -1; return -1;
} }
*/ */
/* /*
//普通方法 //普通方法
if (validfamily == AF_INET) { if (validfamily == AF_INET) {
@ -283,6 +264,8 @@ int create_connection6(char *remote_host, int remote_port)
return sock; return sock;
} }
*/ */
fcntl(sock, F_SETFL, O_NONBLOCK);
return sock; return sock;
} }
@ -290,9 +273,8 @@ int create_connection6(char *remote_host, int remote_port)
static int8_t copy_data(conn_t * ct) static int8_t copy_data(conn_t * ct)
{ {
dataEncode(ct->incomplete_data, ct->incomplete_data_len, sslEncodeCode); dataEncode(ct->incomplete_data, ct->incomplete_data_len, sslEncodeCode);
if (ct->ready_data) if (ct->ready_data) {
{
char *new_data; char *new_data;
new_data = (char *)realloc(ct->ready_data, ct->ready_data_len + ct->incomplete_data_len); new_data = (char *)realloc(ct->ready_data, ct->ready_data_len + ct->incomplete_data_len);
@ -302,9 +284,7 @@ static int8_t copy_data(conn_t * ct)
memcpy(new_data + ct->ready_data_len, ct->incomplete_data, ct->incomplete_data_len); memcpy(new_data + ct->ready_data_len, ct->incomplete_data, ct->incomplete_data_len);
ct->ready_data_len += ct->incomplete_data_len; ct->ready_data_len += ct->incomplete_data_len;
free(ct->incomplete_data); free(ct->incomplete_data);
} } else {
else
{
ct->ready_data = ct->incomplete_data; ct->ready_data = ct->incomplete_data;
ct->ready_data_len = ct->incomplete_data_len; ct->ready_data_len = ct->incomplete_data_len;
} }
@ -322,71 +302,66 @@ void tcp_in(conn_t * in, conf * configure)
if (in->fd < 0) if (in->fd < 0)
return; return;
//如果in - cts是奇数那么是服务端触发事件 //如果in - cts是奇数那么是服务端触发事件
if ((in - cts) & 1) if ((in - cts) & 1) {
{ in->timer = (in - 1)->timer = 0;
in->timer = (in-1)->timer = 0;
if (in->ready_data_len <= 0) if (in->ready_data_len <= 0)
serverToClient(in); serverToClient(in);
return; return;
} }
in->timer = (in+1)->timer = 0; in->timer = (in + 1)->timer = 0;
in->incomplete_data = read_data(in, in->incomplete_data, &in->incomplete_data_len); in->incomplete_data = read_data(in, in->incomplete_data, &in->incomplete_data_len);
if (in->incomplete_data == NULL) { if (in->incomplete_data == NULL) {
close_connection(in); close_connection(in);
return; return;
} }
server = in + 1; server = in + 1;
server->request_type = in->request_type = request_type(in->incomplete_data); server->request_type = in->request_type = request_type(in->incomplete_data);
if (in->request_type == OTHER_TYPE) if (in->request_type == OTHER_TYPE) {
{
//如果是第一次读取数据并且不是HTTP请求的关闭连接。复制数据失败的也关闭连接 //如果是第一次读取数据并且不是HTTP请求的关闭连接。复制数据失败的也关闭连接
if (in->reread_data == 0 || copy_data(in) != 0) if (in->reread_data == 0 || copy_data(in) != 0) {
{
close_connection(in); close_connection(in);
return; return;
} }
goto handle_data_complete; goto handle_data_complete;
} }
headerEnd = strstr(in->incomplete_data, "\n\r"); headerEnd = strstr(in->incomplete_data, "\n\r");
//请求头不完整,等待下次读取 //请求头不完整,等待下次读取
if (headerEnd == NULL) if (headerEnd == NULL)
return; return;
if (in->reread_data == 0) if (in->reread_data == 0) {
{
in->reread_data = 1; in->reread_data = 1;
in->incomplete_data = request_head(in, configure); in->incomplete_data = request_head(in, configure);
server->fd = create_connection6(remote_host, remote_port); server->fd = create_connection6(remote_host, remote_port);
if (server->fd < 0) { if (server->fd < 0) {
printf("remote->fd ERROR!\n"); printf("remote->fd ERROR!\n");
close_connection(in); close_connection(in);
return ; return;
} }
fcntl(server->fd, F_SETFL, O_NONBLOCK);
ev.events = EPOLLIN | EPOLLOUT | EPOLLET; ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
ev.data.ptr = server; ev.data.ptr = server;
epoll_ctl(epollfd, EPOLL_CTL_ADD, server->fd, &ev); epoll_ctl(epollfd, EPOLL_CTL_ADD, server->fd, &ev);
} }
if (in->incomplete_data == NULL || copy_data(in) != 0) { if (in->incomplete_data == NULL || copy_data(in) != 0) {
close_connection(server); close_connection(server);
return; return;
} }
// 数据处理完毕,可以发送 // 数据处理完毕,可以发送
handle_data_complete: handle_data_complete:
// 这个判断是防止 多次读取客户端数据,但是没有和服务端建立连接,导致报错 // 这个判断是防止 多次读取客户端数据,但是没有和服务端建立连接,导致报错
if (server->fd >= 0) if (server->fd >= 0)
tcp_out(server); tcp_out(server);
} }
void tcp_out(conn_t *to) void tcp_out(conn_t * to)
{ {
conn_t *from; conn_t *from;
int write_len; int write_len;
@ -400,38 +375,29 @@ void tcp_out(conn_t *to)
from->timer = to->timer = 0; from->timer = to->timer = 0;
write_len = write(to->fd, from->ready_data + from->sent_len, from->ready_data_len - from->sent_len); write_len = write(to->fd, from->ready_data + from->sent_len, from->ready_data_len - from->sent_len);
if (write_len == from->ready_data_len - from->sent_len) if (write_len == from->ready_data_len - from->sent_len) {
{
//服务端的数据可能没全部写入到客户端 //服务端的数据可能没全部写入到客户端
if ((from - cts) & 1) if ((from - cts) & 1) {
{
serverToClient(from); serverToClient(from);
if (from->fd >= 0 && from->ready_data_len == 0) if (from->fd >= 0 && from->ready_data_len == 0) {
{ ev.events = EPOLLIN | EPOLLET;
ev.events = EPOLLIN|EPOLLET;
ev.data.ptr = to; ev.data.ptr = to;
epoll_ctl(epollfd, EPOLL_CTL_MOD, to->fd, &ev); epoll_ctl(epollfd, EPOLL_CTL_MOD, to->fd, &ev);
} }
} } else {
else ev.events = EPOLLIN | EPOLLET;
{
ev.events = EPOLLIN|EPOLLET;
ev.data.ptr = to; ev.data.ptr = to;
epoll_ctl(epollfd, EPOLL_CTL_MOD, to->fd, &ev); epoll_ctl(epollfd, EPOLL_CTL_MOD, to->fd, &ev);
free(from->ready_data); free(from->ready_data);
from->ready_data = NULL; from->ready_data = NULL;
from->ready_data_len = 0; from->ready_data_len = 0;
} }
} } else if (write_len > 0) {
else if (write_len > 0)
{
from->sent_len += write_len; from->sent_len += write_len;
ev.events = EPOLLIN|EPOLLOUT|EPOLLET; ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
ev.data.ptr = to; ev.data.ptr = to;
epoll_ctl(epollfd, EPOLL_CTL_MOD, to->fd, &ev); epoll_ctl(epollfd, EPOLL_CTL_MOD, to->fd, &ev);
} } else if (errno != EAGAIN) {
else if (errno != EAGAIN)
{
close_connection(to); close_connection(to);
} }
} }

View File

@ -136,23 +136,20 @@ static char *regrep(char *str, int *str_len, const char *src, char *dest, int de
return str; return str;
} }
int extract_host(char *header, char *host, char *port) int extract_host(char *header, char *host, int *host_len, char *port, int *port_len)
{ {
memset(port, 0, strlen(port));
memset(host, 0, strlen(host));
//printf("%s\n", header); //printf("%s\n", header);
char *_p = strstr(header, "CONNECT"); // 在 CONNECT 方法中解析 隧道主机名称及端口号 char *_p = strstr(header, "CONNECT"); // 在 CONNECT 方法中解析 隧道主机名称及端口号
if (_p) if (_p) {
{
if (strchr(header, '[') || strchr(header, ']')) { // IPv6 if (strchr(header, '[') || strchr(header, ']')) { // IPv6
char *_p1 = strchr(header, '['); char *_p1 = strchr(header, '[');
char *_p2 = strchr(_p1 + 1, ']'); char *_p2 = strchr(_p1 + 1, ']');
strncpy(host, _p1 + 1, (int)(_p2 - _p1) - 1); memcpy(host, _p1 + 1, (int)(_p2 - _p1) - 1);
char *_p3 = strchr(_p2 + 1, ' '); char *_p3 = strchr(_p2 + 1, ' ');
strncpy(port, _p2 + 2, (int)(_p3 - _p2) - 1); memcpy(port, _p2 + 2, (int)(_p3 - _p2) - 1);
return 0; return 0;
} }
@ -167,7 +164,7 @@ int extract_host(char *header, char *host, char *port)
} else { // 如果_p2等于空就返回-1 } else { // 如果_p2等于空就返回-1
return -1; return -1;
} }
return 0; return 0;
} else { } else {
@ -175,11 +172,11 @@ int extract_host(char *header, char *host, char *port)
if (_p == NULL) { if (_p == NULL) {
_p = strstr(header, "host:"); _p = strstr(header, "host:");
} }
if (_p == NULL) { // 都为空时 if (_p == NULL) { // 都为空时
return -1; return -1;
} }
char *_p1 = strchr(_p, '\n'); // 指向末尾'\n' char *_p1 = strchr(_p, '\n'); // 指向末尾'\n'
if (!_p1) { if (!_p1) {
return -1; return -1;
} }
@ -187,14 +184,14 @@ int extract_host(char *header, char *host, char *port)
char *_p2 = strchr(_p + 5, ':'); // 5是指'Host:'的长度 char *_p2 = strchr(_p + 5, ':'); // 5是指'Host:'的长度
int h_len = (int)(_p1 - _p - 6); int h_len = (int)(_p1 - _p - 6);
char s_host[h_len]; char s_host[h_len];
strncpy(s_host, _p + 6, _p1 - _p - 6); memcpy(s_host, _p + 6, _p1 - _p - 6);
s_host[h_len] = '\0'; s_host[h_len] = '\0';
char *_p3 = strchr(s_host, ':'); char *_p3 = strchr(s_host, ':');
char *_p4 = NULL; char *_p4 = NULL;
if (_p3) if (_p3)
_p4 = strchr(_p3 + 1, ':'); // 二次确认':' _p4 = strchr(_p3 + 1, ':'); // 二次确认':'
{ // IPV6 { // IPV6
if (_p4 != NULL) { if (_p4 != NULL) {
char *_p5 = NULL; char *_p5 = NULL;
char *_p6 = NULL; char *_p6 = NULL;
@ -204,48 +201,48 @@ int extract_host(char *header, char *host, char *port)
char url[_p6 - _p5 - 1]; char url[_p6 - _p5 - 1];
memset(url, 0, _p6 - _p5 - 1); memset(url, 0, _p6 - _p5 - 1);
strncpy(url, _p5 + 1, _p6 - _p5 - 1); memcpy(url, _p5 + 1, _p6 - _p5 - 1);
url[_p6 - _p5 - 1] = '\0'; url[_p6 - _p5 - 1] = '\0';
if (strstr(url, "http") != NULL) { // 去除 'http://' if (strstr(url, "http") != NULL) { // 去除 'http://'
memcpy(url, url + 7, (_p6 - _p5 - 1) - 7); memcpy(url, url + 7, (_p6 - _p5 - 1) - 7);
url[(_p6 - _p5 - 1) - 7] = '\0'; url[(_p6 - _p5 - 1) - 7] = '\0';
char *_p7 = strchr(url, '/'); char *_p7 = strchr(url, '/');
if (_p7) // 去除 uri if (_p7) // 去除 uri
url[_p7 - url] = '\0'; url[_p7 - url] = '\0';
char *_p8 = strchr(url, ']'); char *_p8 = strchr(url, ']');
char *_p9 = strchr(url, '\0'); char *_p9 = strchr(url, '\0');
if (_p8) { if (_p8) {
if ((_p9-_p8) == 1) { // 如果不带端口就默认80, 并结束 if ((_p9 - _p8) == 1) { // 如果不带端口就默认80, 并结束
memcpy(port, "80", 2); memcpy(port, "80", 2);
strncpy(host, url + 1, _p8 - (url+1)); memcpy(host, url + 1, _p8 - (url + 1));
return 0; return 0;
} }
memcpy(port, _p8 + 2, _p9-(_p8+2)); memcpy(port, _p8 + 2, _p9 - (_p8 + 2));
strncpy(host, url + 1, _p8 - (url+1)); memcpy(host, url + 1, _p8 - (url + 1));
} }
return 0; return 0;
} else { // HTTP头为不规范的url时处理Host, 主要Proxifier转发url为'/'时 } else { // HTTP头为不规范的url时处理Host, 主要Proxifier转发url为'/'时
//printf("s_host: %s\n", s_host); //printf("s_host: %s\n", s_host);
char *_p1 = strchr(s_host, '['); char *_p1 = strchr(s_host, '[');
if (_p1 == NULL) // 涉及到自定义的Host, 不带'['、']'时, 默认截取最后为端口 if (_p1 == NULL) // 涉及到自定义的Host, 不带'['、']'时, 默认截取最后为端口
{ {
char *_p2 = strrchr(s_host, ':'); char *_p2 = strrchr(s_host, ':');
remote_port = atoi(_p2+1); remote_port = atoi(_p2 + 1);
strncpy(remote_host, s_host, _p2-s_host); memcpy(remote_host, s_host, _p2 - s_host);
return 0; return 0;
} }
char *_p2 = strchr(_p1 + 1, ']'); char *_p2 = strchr(_p1 + 1, ']');
char *_p3 = strchr(s_host, '\0'); char *_p3 = strchr(s_host, '\0');
if (_p1 && _p2) { if (_p1 && _p2) {
memcpy(host, _p1 + 1, _p2 - _p1 - 1); memcpy(host, _p1 + 1, _p2 - (_p1 + 1));
if (strlen(_p2) < 3) { if (_p3 - (_p2 + 1) < 2) {
memcpy(port, "80", 2); memcpy(port, "80", 2);
} else { } else {
memcpy(port, _p2+2, _p3-(_p2+2)); memcpy(port, _p2 + 2, _p3 - (_p2 + 2));
} }
} }
@ -257,10 +254,10 @@ int extract_host(char *header, char *host, char *port)
} }
// HTTP 非 CONNECT 方法 // HTTP 非 CONNECT 方法
{ {
if (_p2 && _p2 < _p1) { // 带端口, p2指向':', p1指向末尾'\n', _p指向Host if (_p2 && _p2 < _p1) { // 带端口, p2指向':', p1指向末尾'\n', _p指向Host
memcpy(port, _p2 + 1, (int)(_p1 - _p2 - 1)); memcpy(port, _p2 + 1, (int)(_p1 - _p2 - 1));
memcpy(host, _p + 5 + 1, (int)(_p2 - _p - 5 - 1)); memcpy(host, _p + 5 + 1, (int)(_p2 - _p - 5 - 1));
} else { // 不带端口 } else { // 不带端口
if (0 < (int)(_p1 - _p - 5 - 1 - 1)) { if (0 < (int)(_p1 - _p - 5 - 1 - 1)) {
memcpy(host, _p + 5 + 1, (_p1 - _p - 5 - 1 - 1)); memcpy(host, _p + 5 + 1, (_p1 - _p - 5 - 1 - 1));
memcpy(port, "80", 2); memcpy(port, "80", 2);
@ -281,14 +278,14 @@ char *get_http_path(char *url, int url_len, char *path)
{ {
char *_p0, *_p1; char *_p0, *_p1;
_p0 = _p1 = NULL; _p0 = _p1 = NULL;
_p1 = strchr(url, '\0'); _p1 = strchr(url, '\0');
if (url_len > 7) { if (url_len > 7) {
if (url) { if (url) {
_p0 = strstr(url + 7, "/"); _p0 = strstr(url + 7, "/");
if (_p0) if (_p0)
return memcpy(path, _p0, _p1-_p0); return memcpy(path, _p0, _p1 - _p0);
else else
return memcpy(path, "/", 1); // 如果没有资源路径就默认"/" return memcpy(path, "/", 1); // 如果没有资源路径就默认"/"
} }
@ -338,7 +335,7 @@ void get_http_host_port_len(char *head, int *host_len, int *port_len)
} }
} else { // 未找到时使用HTTP_HEAD_CACHE_SIZE大小 } else { // 未找到时使用HTTP_HEAD_CACHE_SIZE大小
*host_len = HTTP_HEAD_HOST_CACHE_SIZE; *host_len = HTTP_HEAD_HOST_CACHE_SIZE;
*port_len = HTTP_HEAD_HOST_CACHE_SIZE/10; *port_len = HTTP_HEAD_HOST_CACHE_SIZE / 10;
} }
} }
@ -400,14 +397,13 @@ void parse_request_head(char *http_request_line, struct http_request *http_reque
m = strchr(head, ' '); m = strchr(head, ' ');
http_request->method_len = m - head; http_request->method_len = m - head;
memmove(http_request->method, head, http_request->method_len); memmove(http_request->method, head, http_request->method_len);
u = strchr(m + 1, ' '); u = strchr(m + 1, ' ');
memmove(http_request->U, m + 1, u - (m+1)); memmove(http_request->U, m + 1, u - (m + 1));
http_request->U_len = u - ( m + 1); http_request->U_len = u - (m + 1);
memmove(http_request->version, u + 1, 8); memmove(http_request->version, u + 1, 8);
http_request->version_len = 8; http_request->version_len = 8;
// 获取Host、Port长度 // 获取Host、Port长度
get_http_host_port_len(http_request_line, &host_len, &port_len); get_http_host_port_len(http_request_line, &host_len, &port_len);
@ -418,13 +414,13 @@ void parse_request_head(char *http_request_line, struct http_request *http_reque
char *_p1 = strchr(http_request->U + 7, '/'); char *_p1 = strchr(http_request->U + 7, '/');
char *_p2 = strchr(http_request->U + 7, '\0'); char *_p2 = strchr(http_request->U + 7, '\0');
if (_p1) { if (_p1) {
uri_len = _p2-_p1; uri_len = _p2 - _p1;
} }
} else { // 非标准头 } else { // 非标准头
char *_p1 = strchr(http_request->U, '/'); char *_p1 = strchr(http_request->U, '/');
char *_p2 = strchr(http_request->U, '\0'); char *_p2 = strchr(http_request->U, '\0');
if (_p1) { if (_p1) {
uri_len = _p2-_p1; uri_len = _p2 - _p1;
} else { } else {
uri_len = 1; // 没有uri时 uri_len = 1; // 没有uri时
} }
@ -451,7 +447,7 @@ void parse_request_head(char *http_request_line, struct http_request *http_reque
memset(http_request->url, 0, http_request->U_len + 1); memset(http_request->url, 0, http_request->U_len + 1);
memset(http_request->uri, 0, uri_len + 1); memset(http_request->uri, 0, uri_len + 1);
memset(http_request->H, 0, host_len + port_len + 1); memset(http_request->H, 0, host_len + port_len + 1);
if (extract_host(http_request_line, http_request->host, http_request->port) == -1) if (extract_host(http_request_line, http_request->host, &host_len, http_request->port, &port_len) == -1)
return; return;
http_request->host_len = (int)strlen(http_request->host); http_request->host_len = (int)strlen(http_request->host);
http_request->port_len = (int)strlen(http_request->port); http_request->port_len = (int)strlen(http_request->port);
@ -460,23 +456,23 @@ void parse_request_head(char *http_request_line, struct http_request *http_reque
strncat(http_request->H, http_request->port, http_request->port_len); strncat(http_request->H, http_request->port, http_request->port_len);
memcpy(http_request->url, http_request->U, http_request->U_len); memcpy(http_request->url, http_request->U, http_request->U_len);
http_request->url_len = http_request->U_len; http_request->url_len = http_request->U_len;
if (get_http_path(http_request->url, http_request->url_len, http_request->uri) == NULL ) if (get_http_path(http_request->url, http_request->url_len, http_request->uri) == NULL)
return; return;
http_request->uri_len = uri_len; http_request->uri_len = uri_len;
http_request->H_len = http_request->host_len + http_request->port_len + 1; http_request->H_len = http_request->host_len + http_request->port_len + 1;
/* /*
// 调试 // 调试
printf("%s %d\n", http_request->method, http_request->method_len); printf("%s %d\n", http_request->method, http_request->method_len);
printf("%s %d\n", http_request->U, http_request->U_len); printf("%s %d\n", http_request->U, http_request->U_len);
printf("%s %d\n", http_request->version, http_request->version_len); printf("%s %d\n", http_request->version, http_request->version_len);
printf("%s %d\n", http_request->host, http_request->host_len); printf("%s %d\n", http_request->host, http_request->host_len);
printf("%s %d\n", http_request->port, http_request->port_len); printf("%s %d\n", http_request->port, http_request->port_len);
printf("%s %d\n", http_request->H, http_request->H_len); printf("%s %d\n", http_request->H, http_request->H_len);
printf("%s %d\n", http_request->url, http_request->url_len); printf("%s %d\n", http_request->url, http_request->url_len);
printf("%s %d\n", http_request->uri, http_request->uri_len); printf("%s %d\n", http_request->uri, http_request->uri_len);
*/ */
free(head); free(head);
return; return;
@ -484,28 +480,28 @@ void parse_request_head(char *http_request_line, struct http_request *http_reque
static char *splice_head(char *head, int *head_len, const char *needle, char *string, int string_len) static char *splice_head(char *head, int *head_len, const char *needle, char *string, int string_len)
{ {
char *tail_head; char *temp_stack;
char *_p0; char *_p0;
char *_p1; char *_p1;
_p1 = strstr(head, needle); _p1 = strstr(head, needle);
if (_p1 == NULL) { if (_p1 == NULL) {
return head; return head;
} }
_p1 = _p1 + 1; _p1 = _p1 + 1;
_p0 = strchr(_p1, '\0'); _p0 = strchr(_p1, '\0');
tail_head = (char *)alloca((_p0 - _p1) + 1); temp_stack = (char *)alloca((_p0 - _p1) + 1);
if (tail_head == NULL) { if (temp_stack == NULL) {
perror("alloca"); perror("alloca");
return head; return head;
} }
memset(tail_head, 0, (_p0 - _p1) + 1); memset(temp_stack, 0, (_p0 - _p1) + 1);
memcpy(tail_head, _p1, (_p0 - _p1)); memcpy(temp_stack, _p1, (_p0 - _p1));
memset(head, 0, *head_len); memset(head, 0, *head_len);
memcpy(head, string, string_len); memcpy(head, string, string_len);
strncat(head, tail_head, (_p0 - _p1)); strncat(head, temp_stack, (_p0 - _p1));
*head_len = string_len + (_p0 - _p1); *head_len = string_len + (_p0 - _p1);
return head; return head;
} }
@ -517,43 +513,42 @@ static char *delete_head(char *head, int *head_len, const char *needle, int stri
_p1 = _p2 = _p3 = NULL; _p1 = _p2 = _p3 = NULL;
int temp_stack_len; int temp_stack_len;
_p1 = strstr(head, needle); // _p1指向head字符串中的"needle"字符处(needle字符串的第一个字符) _p1 = strstr(head, needle); // _p1指向head字符串中的"needle"字符处(needle字符串的第一个字符)
if (_p1 == NULL) { if (_p1 == NULL) {
//perror("_p1 HEAD NULL"); //perror("_p1 HEAD NULL");
return head; return head;
} }
_p2 = strchr(_p1, string); // _p2指向head字符串中"string"字符到末尾中的'\0' _p2 = strchr(_p1, string); // _p2指向head字符串中"string"字符到末尾中的'\0'
if (_p2 == NULL) { if (_p2 == NULL) {
//perror("_p2 HEAD NULL"); //perror("_p2 HEAD NULL");
return head; return head;
} }
_p3 = strchr(_p1, '\0'); // _p3指向head字符串末尾的'\0' _p3 = strchr(_p1, '\0'); // _p3指向head字符串末尾的'\0'
if (_p3 == NULL) { if (_p3 == NULL) {
//perror("_p3 HEAD NULL"); //perror("_p3 HEAD NULL");
return head; return head;
} }
temp_stack_len = (_p1 - head) + (_p3 - _p2); temp_stack_len = (_p1 - head) + (_p3 - _p2);
temp_stack = (char *)alloca(temp_stack_len + 1); // 分配临时栈内存长度是去除needle到string处 temp_stack = (char *)alloca(temp_stack_len + 1); // 分配临时栈内存长度是去除needle到string处
if (temp_stack == NULL) { if (temp_stack == NULL) {
perror("alloca"); perror("alloca");
return head; return head;
} }
memset(temp_stack, 0, temp_stack_len + 1); memset(temp_stack, 0, temp_stack_len + 1);
memmove(temp_stack, head, (_p1 - head) - 1); memmove(temp_stack, head, (_p1 - head) - 1);
strncat(temp_stack, _p2, _p3-_p2);
strncat(temp_stack, _p2, _p3 - _p2);
memset(head, 0, *head_len); memset(head, 0, *head_len);
*head_len = temp_stack_len; *head_len = temp_stack_len;
return memmove(head, temp_stack, temp_stack_len); return memmove(head, temp_stack, temp_stack_len);
} }
static char *conf_handle_strrep(char *str, int *str_len, tcp *temp) static char *conf_handle_strrep(char *str, int *str_len, tcp * temp)
{ {
tcp *p = temp; tcp *p = temp;
while (p) while (p) {
{
if (p->strrep) { if (p->strrep) {
str = replace(str, str_len, p->strrep_s, p->strrep_s_len, p->strrep_t, p->strrep_t_len); str = replace(str, str_len, p->strrep_s, p->strrep_s_len, p->strrep_t, p->strrep_t_len);
} }
@ -564,12 +559,11 @@ static char *conf_handle_strrep(char *str, int *str_len, tcp *temp)
return str; return str;
} }
static char *conf_handle_regrep(char *str, int *str_len, tcp *temp) static char *conf_handle_regrep(char *str, int *str_len, tcp * temp)
{ {
tcp *p = temp; tcp *p = temp;
while (p) while (p) {
{
if (p->regrep) { if (p->regrep) {
str = regrep(str, str_len, p->regrep_s, p->regrep_t, p->regrep_t_len); str = regrep(str, str_len, p->regrep_s, p->regrep_t, p->regrep_t_len);
} }
@ -594,12 +588,11 @@ char *request_head(conn_t * in, conf * configure)
parse_request_head(in->incomplete_data, http_request); parse_request_head(in->incomplete_data, http_request);
if ((return_val = strncmp(in->incomplete_data, "CONNECT", 7)) == 0) if ((return_val = strncmp(in->incomplete_data, "CONNECT", 7)) == 0) {
{
sslEncodeCode = configure->https_encode; sslEncodeCode = configure->https_encode;
char https_del_copy[configure->https_del_len+1]; char https_del_copy[configure->https_del_len + 1];
memset(remote_host, 0, CACHE_SIZE); memset(remote_host, 0, CACHE_SIZE);
if (configure->https_port > 0) { if (configure->https_port > 0) {
remote_port = configure->https_port; remote_port = configure->https_port;
@ -607,7 +600,7 @@ char *request_head(conn_t * in, conf * configure)
if (configure->https_ip != NULL) { if (configure->https_ip != NULL) {
memcpy(remote_host, configure->https_ip, configure->https_ip_len); memcpy(remote_host, configure->https_ip, configure->https_ip_len);
} }
incomplete_head = (char *)malloc(sizeof(char) * (BUFFER_SIZE)); incomplete_head = (char *)malloc(sizeof(char) * (BUFFER_SIZE));
if (incomplete_head == NULL) { if (incomplete_head == NULL) {
free(incomplete_head); free(incomplete_head);
@ -615,18 +608,17 @@ char *request_head(conn_t * in, conf * configure)
} }
memset(incomplete_head, 0, sizeof(char) * (BUFFER_SIZE)); memset(incomplete_head, 0, sizeof(char) * (BUFFER_SIZE));
memmove(incomplete_head, in->incomplete_data, in->incomplete_data_len); memmove(incomplete_head, in->incomplete_data, in->incomplete_data_len);
memmove(https_del_copy, configure->https_del, configure->https_del_len+1); memmove(https_del_copy, configure->https_del, configure->https_del_len + 1);
result = strtok_r(https_del_copy, delim, &saveptr); result = strtok_r(https_del_copy, delim, &saveptr);
while (result != NULL) { while (result != NULL) {
delete_head(incomplete_head, &in->incomplete_data_len, result, '\n'); delete_head(incomplete_head, &in->incomplete_data_len, result, '\n');
result = strtok_r(NULL, delim, &saveptr); result = strtok_r(NULL, delim, &saveptr);
} }
splice_head(incomplete_head, &in->incomplete_data_len, "\n", configure->https_first, configure->https_first_len); splice_head(incomplete_head, &in->incomplete_data_len, "\n", configure->https_first, configure->https_first_len);
incomplete_head_len = in->incomplete_data_len; // 更新HTTPS HEADER长度 incomplete_head_len = in->incomplete_data_len; // 更新HTTPS HEADER长度
incomplete_head = conf_handle_strrep(incomplete_head, &incomplete_head_len, https_head_strrep); incomplete_head = conf_handle_strrep(incomplete_head, &incomplete_head_len, https_head_strrep);
incomplete_head = conf_handle_regrep(incomplete_head, &incomplete_head_len, https_head_regrep); incomplete_head = conf_handle_regrep(incomplete_head, &incomplete_head_len, https_head_regrep);
@ -639,7 +631,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, "[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); // 打印HTTPS HEADER printf("%s", incomplete_head); // 打印HTTPS HEADER
char *new_incomplete_data; char *new_incomplete_data;
new_incomplete_data = (char *)realloc(in->incomplete_data, incomplete_head_len + 1); new_incomplete_data = (char *)realloc(in->incomplete_data, incomplete_head_len + 1);
@ -649,17 +641,16 @@ char *request_head(conn_t * in, conf * configure)
return in->incomplete_data; return in->incomplete_data;
} }
in->incomplete_data = new_incomplete_data; in->incomplete_data = new_incomplete_data;
memset(in->incomplete_data, 0, incomplete_head_len + 1); // 清空incomplete_data数据 memset(in->incomplete_data, 0, incomplete_head_len + 1); // 清空incomplete_data数据
memmove(in->incomplete_data, incomplete_head, incomplete_head_len); // 更新incomplete_data数据 memmove(in->incomplete_data, incomplete_head, incomplete_head_len); // 更新incomplete_data数据
in->incomplete_data_len = incomplete_head_len; // 更新incomplete_data长度 in->incomplete_data_len = incomplete_head_len; // 更新incomplete_data长度
free(incomplete_head); // 释放incomplete_head内存 free(incomplete_head); // 释放incomplete_head内存
} }
if (strncmp(in->incomplete_data, "GET", 3) == 0 || strncmp(in->incomplete_data, "POST", 4) == 0) if (strncmp(in->incomplete_data, "GET", 3) == 0 || strncmp(in->incomplete_data, "POST", 4) == 0) {
{
sslEncodeCode = configure->http_encode; sslEncodeCode = configure->http_encode;
char http_del_copy[configure->http_del_len + 1]; char http_del_copy[configure->http_del_len + 1];
memset(remote_host, 0, CACHE_SIZE); memset(remote_host, 0, CACHE_SIZE);
@ -669,7 +660,7 @@ char *request_head(conn_t * in, conf * configure)
if (configure->http_ip != NULL) { if (configure->http_ip != NULL) {
memcpy(remote_host, configure->http_ip, configure->http_ip_len); memcpy(remote_host, configure->http_ip, configure->http_ip_len);
} }
incomplete_head = (char *)malloc(sizeof(char) * (BUFFER_SIZE)); incomplete_head = (char *)malloc(sizeof(char) * (BUFFER_SIZE));
if (incomplete_head == NULL) { if (incomplete_head == NULL) {
perror("malloc"); perror("malloc");
@ -678,7 +669,7 @@ char *request_head(conn_t * in, conf * configure)
memset(incomplete_head, 0, sizeof(char) * (BUFFER_SIZE)); memset(incomplete_head, 0, sizeof(char) * (BUFFER_SIZE));
memmove(incomplete_head, in->incomplete_data, in->incomplete_data_len); memmove(incomplete_head, in->incomplete_data, in->incomplete_data_len);
memmove(http_del_copy, configure->http_del, configure->http_del_len+1); memmove(http_del_copy, configure->http_del, configure->http_del_len + 1);
result = strtok_r(http_del_copy, delim, &saveptr); result = strtok_r(http_del_copy, delim, &saveptr);
while (result != NULL) { while (result != NULL) {
@ -687,10 +678,10 @@ char *request_head(conn_t * in, conf * configure)
} }
splice_head(incomplete_head, &in->incomplete_data_len, "\n", configure->http_first, configure->http_first_len); splice_head(incomplete_head, &in->incomplete_data_len, "\n", configure->http_first, configure->http_first_len);
incomplete_head_len = in->incomplete_data_len; // 更新HTTP HEADER长度 incomplete_head_len = in->incomplete_data_len; // 更新HTTP HEADER长度
incomplete_head = conf_handle_strrep(incomplete_head, &incomplete_head_len, http_head_strrep); incomplete_head = conf_handle_strrep(incomplete_head, &incomplete_head_len, http_head_strrep);
incomplete_head = conf_handle_regrep(incomplete_head, &incomplete_head_len, http_head_regrep); incomplete_head = conf_handle_regrep(incomplete_head, &incomplete_head_len, http_head_regrep);
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, "[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, "[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); incomplete_head = replace(incomplete_head, &incomplete_head_len, "[U]", 3, http_request->U, http_request->U_len);
@ -702,7 +693,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, "[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); // 打印HTTP HEADER printf("%s", incomplete_head); // 打印HTTP HEADER
char *new_incomplete_data; char *new_incomplete_data;
new_incomplete_data = (char *)realloc(in->incomplete_data, incomplete_head_len + 1); new_incomplete_data = (char *)realloc(in->incomplete_data, incomplete_head_len + 1);
@ -712,11 +703,11 @@ char *request_head(conn_t * in, conf * configure)
return in->incomplete_data; return in->incomplete_data;
} }
in->incomplete_data = new_incomplete_data; in->incomplete_data = new_incomplete_data;
memset(in->incomplete_data, 0, incomplete_head_len + 1); // 清空incomplete_data数据 memset(in->incomplete_data, 0, incomplete_head_len + 1); // 清空incomplete_data数据
memmove(in->incomplete_data, incomplete_head, incomplete_head_len); // 更新incomplete_data数据 memmove(in->incomplete_data, incomplete_head, incomplete_head_len); // 更新incomplete_data数据
in->incomplete_data_len = incomplete_head_len; // 更新incomplete_data长度 in->incomplete_data_len = incomplete_head_len; // 更新incomplete_data长度
free(incomplete_head); // 释放incomplete_head内存 free(incomplete_head); // 释放incomplete_head内存
} }
free_http_request(http_request); free_http_request(http_request);

View File

@ -49,7 +49,7 @@ int8_t read_cache_file()
char *buff, *cache_ptr; char *buff, *cache_ptr;
struct dns_cache *cache_temp; struct dns_cache *cache_temp;
long file_size; long file_size;
int len, fl=0; int len, fl = 0;
cache_temp = NULL; cache_temp = NULL;
cache_using = 0; cache_using = 0;
@ -69,8 +69,7 @@ int8_t read_cache_file()
if ((buff = (char *)alloca(file_size)) == NULL) if ((buff = (char *)alloca(file_size)) == NULL)
return 1; return 1;
rewind(cfp); rewind(cfp);
if ((fl = fread(buff, file_size, 1, cfp)) != 1) if ((fl = fread(buff, file_size, 1, cfp)) != 1) ;
;
fclose(cfp); fclose(cfp);
for (cache_ptr = buff; cache_ptr - buff < file_size; cache_ptr += len) { for (cache_ptr = buff; cache_ptr - buff < file_size; cache_ptr += len) {

View File

@ -78,9 +78,9 @@ static int createRspFd(info_t * client)
bind(client->responseClientFd, (struct sockaddr *)&client->toaddr, sizeof(struct sockaddr_in)); bind(client->responseClientFd, (struct sockaddr *)&client->toaddr, sizeof(struct sockaddr_in));
//切换回用户设置的uid //切换回用户设置的uid
if (-1 == setegid(global.uid)) if (-1 == setegid(global.uid))
perror("setegid"); perror("setegid");
if (-1 == seteuid(global.uid)) if (-1 == seteuid(global.uid))
perror("seteuid"); perror("seteuid");
return 0; return 0;
} }
@ -372,14 +372,14 @@ static void http_udp_req_init()
udp.http_request = replace(udp.http_request, &udp.http_request_len, "[url]", 5, "/", 1); udp.http_request = replace(udp.http_request, &udp.http_request_len, "[url]", 5, "/", 1);
udp.http_request = replace(udp.http_request, &udp.http_request_len, "[U]", 3, "/", 1); udp.http_request = replace(udp.http_request, &udp.http_request_len, "[U]", 3, "/", 1);
} else { /* 默认使用CONNECT请求 */ } else { /* 默认使用CONNECT请求 */
if (https.encodeCode) { if (https.encodeCode) {
dataEncode(dest, strlen(dest), https.encodeCode); dataEncode(dest, strlen(dest), https.encodeCode);
udp.httpsProxy_encodeCode = https.encodeCode; udp.httpsProxy_encodeCode = https.encodeCode;
} }
udp.http_request_len = default_ssl_request_len; udp.http_request_len = default_ssl_request_len;
copy_new_mem(default_ssl_request, default_ssl_request_len, &udp.http_request); copy_new_mem(default_ssl_request, default_ssl_request_len, &udp.http_request);
udp.http_request = replace(udp.http_request, &udp.http_request_len, "[H]", 3, dest, strlen(dest)); udp.http_request = replace(udp.http_request, &udp.http_request_len, "[H]", 3, dest, strlen(dest));
memcpy(&udp.dst, &https.dst, sizeof(udp.dst)); memcpy(&udp.dst, &https.dst, sizeof(udp.dst));
} }
if (udp.http_request == NULL) if (udp.http_request == NULL)
errors("out of memory."); errors("out of memory.");

43
main.c
View File

@ -84,7 +84,7 @@ int create_server_socket(int port)
perror("bind"); perror("bind");
return -1; return -1;
} }
if (listen(server_sock, 500) < 0) { if (listen(server_sock, 200) < 0) {
perror("listen"); perror("listen");
return -1; return -1;
} }
@ -121,7 +121,7 @@ int create_server_socket6(int port)
return -1; return -1;
} }
if (listen(server_sock, 500) < 0) { if (listen(server_sock, 200) < 0) {
perror("listen"); perror("listen");
return -1; return -1;
} }
@ -177,7 +177,7 @@ void accept_client6()
void *http_proxy_loop(void *p) void *http_proxy_loop(void *p)
{ {
conf *configure = (conf *)p; conf *configure = (conf *) p;
int n; int n;
ev.events = EPOLLIN; ev.events = EPOLLIN;
@ -192,14 +192,20 @@ void *http_proxy_loop(void *p)
exit(1); exit(1);
} }
while (1) { while (1)
{
n = epoll_wait(epollfd, events, MAX_CONNECTION, -1); n = epoll_wait(epollfd, events, MAX_CONNECTION, -1);
while (n-- > 0) { while (n-- > 0)
if (events[n].data.fd == server_sock6) { {
accept_client6(); if (events[n].data.fd == server_sock) {
} else if (events[n].data.fd == server_sock) {
accept_client(); accept_client();
} else { }
else if (events[n].data.fd == server_sock6)
{
accept_client6();
}
else
{
if (events[n].events & EPOLLIN) { if (events[n].events & EPOLLIN) {
tcp_in((conn_t *) events[n].data.ptr, configure); tcp_in((conn_t *) events[n].data.ptr, configure);
} }
@ -328,7 +334,7 @@ void *timeout_check(void *nullPtr)
return NULL; return NULL;
} }
void initialize(conf *configure) void initialize(conf * configure)
{ {
int i; int i;
char *p; char *p;
@ -389,7 +395,7 @@ void initialize(conf *configure)
} }
void thread_loop(conf *configure) void thread_loop(conf * configure)
{ {
pthread_t thread_id = 0; pthread_t thread_id = 0;
sigset_t signal_mask; sigset_t signal_mask;
@ -444,10 +450,9 @@ void _main(int argc, char *argv[])
conf *configure = (struct CONF *)malloc(sizeof(struct CONF)); conf *configure = (struct CONF *)malloc(sizeof(struct CONF));
memset(configure, 0, sizeof(struct CONF)); memset(configure, 0, sizeof(struct CONF));
read_conf(inifile, configure); read_conf(inifile, configure);
sslEncodeCode = 0; // 默认SSL不转码 sslEncodeCode = 0; // 默认SSL不转码
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[] = {
{ "local_address", required_argument, 0, 'l' }, { "local_address", required_argument, 0, 'l' },
@ -522,11 +527,11 @@ void _main(int argc, char *argv[])
} }
#if DAEMON #if DAEMON
// 守护进程 // 守护进程
int nochdir = 0; int nochdir = 1;
int noclose = 0; int noclose = 1;
int pid; int pid;
if ((pid = fork()) < 0) { if ((pid = fork()) < 0) {
return ; return;
} else if (0 != pid) { } else if (0 != pid) {
free_tcp(&http_head_strrep); free_tcp(&http_head_strrep);
free_tcp(&http_head_regrep); free_tcp(&http_head_regrep);
@ -538,12 +543,12 @@ void _main(int argc, char *argv[])
} }
if (setsid() < 0) { if (setsid() < 0) {
return ; return;
} }
signal(SIGHUP, SIG_IGN); signal(SIGHUP, SIG_IGN);
if ((pid = fork()) < 0) { if ((pid = fork()) < 0) {
return ; return;
} else if (0 != pid) { } else if (0 != pid) {
free_tcp(&http_head_strrep); free_tcp(&http_head_strrep);
free_tcp(&http_head_regrep); free_tcp(&http_head_regrep);
@ -571,7 +576,7 @@ void _main(int argc, char *argv[])
https_head_strrep = local_reverse(https_head_strrep); https_head_strrep = local_reverse(https_head_strrep);
https_head_regrep = local_reverse(https_head_regrep); https_head_regrep = local_reverse(https_head_regrep);
/* /*
print_tcp(https_head_strrep); print_tcp(https_head_strrep);
print_tcp(https_head_regrep); print_tcp(https_head_regrep);

1
main.h
View File

@ -22,7 +22,6 @@
#include <sys/time.h> #include <sys/time.h>
#include <sys/resource.h> #include <sys/resource.h>
#define MAX_CONNECTION 1020 #define MAX_CONNECTION 1020
#define BUFFER_SIZE 8192 #define BUFFER_SIZE 8192
#define CACHE_SIZE 270 #define CACHE_SIZE 270