optimization
This commit is contained in:
parent
0efa5309a6
commit
9a2c98f059
14
CProxy.conf
14
CProxy.conf
@ -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 {
|
||||||
|
2
Makefile
2
Makefile
@ -2,7 +2,7 @@ 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
|
||||||
|
178
conf.c
178
conf.c
@ -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--) {
|
||||||
@ -227,7 +200,7 @@ static void parse_http_module(char *content, conf *p)
|
|||||||
} 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;
|
||||||
@ -235,12 +208,12 @@ 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->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--;
|
||||||
@ -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--) {
|
||||||
@ -337,12 +299,10 @@ static void parse_https_module(char *content, conf *p)
|
|||||||
//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;
|
||||||
@ -350,12 +310,12 @@ 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->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--;
|
||||||
@ -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,13 +384,12 @@ 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);
|
||||||
@ -451,25 +409,20 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -477,25 +430,20 @@ static void parse_httpdns_module(char *content, conf *p)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -503,7 +451,7 @@ static void parse_httpudp_module(char *content, conf *p)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_conf(conf *p)
|
void free_conf(conf * p)
|
||||||
{
|
{
|
||||||
// http module
|
// http module
|
||||||
if (p->http_ip)
|
if (p->http_ip)
|
||||||
@ -528,7 +476,7 @@ void free_conf(conf *p)
|
|||||||
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);
|
||||||
@ -536,7 +484,7 @@ void free_conf(conf *p)
|
|||||||
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;
|
||||||
|
11
conf.h
11
conf.h
@ -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
|
||||||
|
98
http_proxy.c
98
http_proxy.c
@ -18,13 +18,11 @@ 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++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -32,25 +30,22 @@ void *tcp_timeout_check(void *nullPtr)
|
|||||||
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;
|
||||||
}
|
}
|
||||||
@ -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;
|
||||||
}
|
}
|
||||||
@ -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;
|
||||||
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -291,8 +274,7 @@ 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,15 +302,14 @@ 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);
|
||||||
@ -339,11 +318,9 @@ void tcp_in(conn_t * in, conf * configure)
|
|||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
@ -355,8 +332,7 @@ void tcp_in(conn_t * in, conf * configure)
|
|||||||
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);
|
||||||
@ -365,14 +341,13 @@ void tcp_in(conn_t * in, conf * configure)
|
|||||||
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) {
|
||||||
@ -380,13 +355,13 @@ void tcp_in(conn_t * in, conf * configure)
|
|||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
@ -187,7 +184,7 @@ 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, ':');
|
||||||
@ -204,7 +201,7 @@ 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://'
|
||||||
@ -217,14 +214,14 @@ int extract_host(char *header, char *host, char *port)
|
|||||||
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为'/'时
|
||||||
@ -233,19 +230,19 @@ int extract_host(char *header, char *host, char *port)
|
|||||||
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -288,7 +285,7 @@ char *get_http_path(char *url, int url_len, char *path)
|
|||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -402,13 +399,12 @@ void parse_request_head(char *http_request_line, struct http_request *http_reque
|
|||||||
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,7 +456,7 @@ 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;
|
||||||
@ -484,7 +480,7 @@ 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;
|
||||||
|
|
||||||
@ -495,17 +491,17 @@ static char *splice_head(char *head, int *head_len, const char *needle, char *st
|
|||||||
_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;
|
||||||
}
|
}
|
||||||
@ -540,20 +536,19 @@ static char *delete_head(char *head, int *head_len, const char *needle, int stri
|
|||||||
}
|
}
|
||||||
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,11 +588,10 @@ 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) {
|
||||||
@ -615,8 +608,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(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) {
|
||||||
@ -656,8 +648,7 @@ char *request_head(conn_t * in, conf * configure)
|
|||||||
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];
|
||||||
@ -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) {
|
||||||
|
@ -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) {
|
||||||
|
39
main.c
39
main.c
@ -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;
|
||||||
@ -447,7 +453,6 @@ void _main(int argc, char *argv[])
|
|||||||
|
|
||||||
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);
|
||||||
|
Loading…
Reference in New Issue
Block a user