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

View File

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

178
conf.c
View File

@ -1,7 +1,6 @@
#include "conf.h"
#include "http_request.h"
/* 字符串预处理,设置转义字符 */
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);
}
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;
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);
}
else if (strcasecmp(var, "process") == 0)
{
} else if (strcasecmp(var, "process") == 0) {
p->process = atoi(val_begin);
}
else if (strcasecmp(var, "timeout") == 0)
{
} else if (strcasecmp(var, "timeout") == 0) {
p->timeout = atoi(val_begin);
}
else if (strcasecmp(var, "encode") == 0)
{
} else if (strcasecmp(var, "encode") == 0) {
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);
}
else if (strcasecmp(var, "tcp6_listen") == 0)
{
} else if (strcasecmp(var, "tcp6_listen") == 0) {
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);
}
else if (strcasecmp(var, "udp_listen") == 0)
{
} else if (strcasecmp(var, "udp_listen") == 0) {
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;
tcp *http_node = NULL;
@ -163,37 +147,26 @@ static void parse_http_module(char *content, conf *p)
char *p2 = 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;
if (copy_new_mem(val_begin, p->http_ip_len, &p->http_ip) != 0)
return ;
}
else if (strcasecmp(var, "encode") == 0)
{
return;
} else if (strcasecmp(var, "encode") == 0) {
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);
}
else if (strcasecmp(var, "http_del") == 0)
{
} else if (strcasecmp(var, "http_del") == 0) {
p->http_del_len = val_end - val_begin;
if (copy_new_mem(val_begin, p->http_del_len, &p->http_del) != 0)
return ;
}
else if (strcasecmp(var, "http_first") == 0)
{
return;
} else if (strcasecmp(var, "http_first") == 0) {
p->http_first_len = val_end - val_begin;
if (copy_new_mem(val_begin, p->http_first_len, &p->http_first) != 0)
return ;
}
else if (strcasecmp(var, "strrep") == 0)
{
http_node = (tcp *)malloc(sizeof(struct tcp));
return;
} else if (strcasecmp(var, "strrep") == 0) {
http_node = (tcp *) malloc(sizeof(struct tcp));
if (http_node == NULL)
return ;
return;
memset(http_node, 0, sizeof(struct tcp));
http_node->strrep = strdup(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, "->");
for (t = p1; *t != '"'; ++t) ;
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);
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) {
http_node = (tcp *) malloc(sizeof(struct tcp));
if (http_node == NULL)
return ;
return;
memset(http_node, 0, sizeof(struct tcp));
http_node->regrep = strdup(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, "->");
for (t = p1; *t != '"'; ++t) ;
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);
for (s = p1 - 1; *s == ' '; s--) {
if (s == val_begin)
return ;
return;
}
if (*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;
tcp *https_node = NULL;
@ -272,40 +245,29 @@ static void parse_https_module(char *content, conf *p)
char *p2 = 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;
if (copy_new_mem(val_begin, p->https_ip_len, &p->https_ip) != 0)
return ;
}
else if (strcasecmp(var, "encode") == 0)
{
return;
} else if (strcasecmp(var, "encode") == 0) {
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);
}
else if (strcasecmp(var, "https_del") == 0)
{
} else if (strcasecmp(var, "https_del") == 0) {
p->https_del_len = val_end - val_begin;
if (copy_new_mem(val_begin, p->https_del_len, &p->https_del) != 0)
return ;
}
else if (strcasecmp(var, "https_first") == 0)
{
return;
} else if (strcasecmp(var, "https_first") == 0) {
p->https_first_len = val_end - val_begin;
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
https_node = (tcp *)malloc(sizeof(struct tcp));
https_node = (tcp *) malloc(sizeof(struct tcp));
if (https_node == NULL)
return ;
return;
memset(https_node, 0, sizeof(struct tcp));
https_node->strrep = strdup(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, "->");
for (t = p1; *t != '"'; ++t) ;
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);
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_head_strrep->next = https_node;
}
}
else if (strcasecmp(var, "regrep") == 0)
{
} else if (strcasecmp(var, "regrep") == 0) {
https_node = (tcp *) malloc(sizeof(struct tcp));
if (https_node == NULL)
return ;
return;
memset(https_node, 0, sizeof(struct tcp));
https_node->regrep = strdup(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, "->");
for (t = p1; *t != '"'; ++t) ;
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);
for (s = p1 - 1; *s == ' '; s--) {
if (s == val_begin)
return ;
return;
}
if (*s == '"')
s--;
@ -380,7 +340,7 @@ static void parse_https_module(char *content, conf *p)
}
// 打印tcp链表
void print_tcp(tcp *p)
void print_tcp(tcp * p)
{
tcp *temp = p;
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 *end = NULL;
@ -425,13 +384,12 @@ tcp *local_reverse(tcp *head)
}
// Free tcp 链表
void free_tcp(tcp **conf_head)
void free_tcp(tcp ** conf_head)
{
tcp *t;
while(*conf_head != NULL)
{
t=*conf_head;
*conf_head=t->next;
while (*conf_head != NULL) {
t = *conf_head;
*conf_head = t->next;
if (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;
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;
if (copy_new_mem(val_begin, p->httpdns_addr_len, &p->httpdns_addr) != 0)
return ;
}
else if (strcasecmp(var, "http_req") == 0)
{
return;
} else if (strcasecmp(var, "http_req") == 0) {
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)
return ;
}
else if (strcasecmp(var, "encode") == 0)
{
return;
} else if (strcasecmp(var, "encode") == 0) {
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;
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;
if (copy_new_mem(val_begin, p->httpudp_addr_len, &p->httpudp_addr) != 0)
return ;
}
else if (strcasecmp(var, "http_req") == 0)
{
return;
} else if (strcasecmp(var, "http_req") == 0) {
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)
return ;
}
else if (strcasecmp(var, "encode") == 0)
{
return;
} else if (strcasecmp(var, "encode") == 0) {
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
if (p->http_ip)
@ -528,7 +476,7 @@ void free_conf(conf *p)
free(p->httpdns_http_req);
// httpudp module
if(p->httpudp_addr)
if (p->httpudp_addr)
free(p->httpudp_addr);
if (p->httpudp_http_req)
free(p->httpudp_http_req);
@ -536,7 +484,7 @@ void free_conf(conf *p)
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;
FILE *file;

11
conf.h
View File

@ -69,13 +69,12 @@ extern tcp *https_head_strrep;
extern tcp *https_head_regrep;
extern tcp *https_node;
extern void print_tcp(tcp *p);
extern void free_tcp(tcp **p);
extern tcp *local_reverse(tcp *head);
extern void print_tcp(tcp * p);
extern void free_tcp(tcp ** p);
extern tcp *local_reverse(tcp * head);
char *strncpy_(char *dest, const char *src, size_t n);
void read_conf(char *file, conf *p);
void free_conf(conf *p);
void read_conf(char *file, conf * p);
void free_conf(conf * p);
#endif

View File

@ -18,13 +18,11 @@ void *tcp_timeout_check(void *nullPtr)
int i;
for (i = 0; i < MAX_CONNECTION; i += 2) {
if (cts[i].fd > -1)
{
if (cts[i].fd > -1) {
if (cts[i].timer >= timeout_minute) {
printf("关闭连接\n");
close_connection(cts + i);
}
else
} else
cts[i].timer++;
}
}
@ -32,25 +30,22 @@ void *tcp_timeout_check(void *nullPtr)
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;
int read_len;
do {
new_data = (char *)realloc(data, *data_len + BUFFER_SIZE + 1);
if (new_data == NULL)
{
if (new_data == NULL) {
free(data);
return NULL;
}
data = new_data;
read_len = read(in->fd, data + *data_len, BUFFER_SIZE);
/* 判断是否关闭连接 */
if (read_len <= 0)
{
if (read_len == 0 || *data_len == 0 || errno != EAGAIN)
{
if (read_len <= 0) {
if (read_len == 0 || *data_len == 0 || errno != EAGAIN) {
free(data);
return NULL;
}
@ -60,7 +55,7 @@ static char *read_data(conn_t *in, char *data, int *data_len)
} while (read_len == BUFFER_SIZE);
*(data + *data_len) = '\0';
return data;
return data;
}
void close_connection(conn_t * conn)
@ -73,12 +68,12 @@ void close_connection(conn_t * conn)
server_data = conn->ready_data;
memset(conn, 0, sizeof(conn_t));
conn->ready_data = server_data;
conn-- ->fd = -1;
conn--->fd = -1;
} else {
free(conn->ready_data);
free(conn->incomplete_data);
memset(conn, 0, sizeof(conn_t));
conn++ ->fd = -1;
conn++->fd = -1;
}
if (conn->fd >= 0)
close_connection(conn);
@ -137,20 +132,7 @@ void clientToserver(conn_t * in)
// 判断请求类型
static int8_t request_type(char *data)
{
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)
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)
return HTTP_TYPE;
return OTHER_TYPE;
}
@ -212,7 +194,6 @@ int create_connection6(char *remote_host, int remote_port)
if (res != NULL)
freeaddrinfo(res);
/*
//通用sockaddr_storage结构体
struct sockaddr_storage remote_addr;
@ -283,6 +264,8 @@ int create_connection6(char *remote_host, int remote_port)
return sock;
}
*/
fcntl(sock, F_SETFL, O_NONBLOCK);
return sock;
}
@ -291,8 +274,7 @@ static int8_t copy_data(conn_t * ct)
{
dataEncode(ct->incomplete_data, ct->incomplete_data_len, sslEncodeCode);
if (ct->ready_data)
{
if (ct->ready_data) {
char *new_data;
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);
ct->ready_data_len += ct->incomplete_data_len;
free(ct->incomplete_data);
}
else
{
} else {
ct->ready_data = ct->incomplete_data;
ct->ready_data_len = ct->incomplete_data_len;
}
@ -322,15 +302,14 @@ void tcp_in(conn_t * in, conf * configure)
if (in->fd < 0)
return;
//如果in - cts是奇数那么是服务端触发事件
if ((in - cts) & 1)
{
in->timer = (in-1)->timer = 0;
if ((in - cts) & 1) {
in->timer = (in - 1)->timer = 0;
if (in->ready_data_len <= 0)
serverToClient(in);
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);
if (in->incomplete_data == NULL) {
close_connection(in);
@ -339,11 +318,9 @@ void tcp_in(conn_t * in, conf * configure)
server = in + 1;
server->request_type = in->request_type = request_type(in->incomplete_data);
if (in->request_type == OTHER_TYPE)
{
if (in->request_type == OTHER_TYPE) {
//如果是第一次读取数据并且不是HTTP请求的关闭连接。复制数据失败的也关闭连接
if (in->reread_data == 0 || copy_data(in) != 0)
{
if (in->reread_data == 0 || copy_data(in) != 0) {
close_connection(in);
return;
}
@ -355,8 +332,7 @@ void tcp_in(conn_t * in, conf * configure)
if (headerEnd == NULL)
return;
if (in->reread_data == 0)
{
if (in->reread_data == 0) {
in->reread_data = 1;
in->incomplete_data = request_head(in, configure);
@ -365,14 +341,13 @@ void tcp_in(conn_t * in, conf * configure)
if (server->fd < 0) {
printf("remote->fd ERROR!\n");
close_connection(in);
return ;
return;
}
fcntl(server->fd, F_SETFL, O_NONBLOCK);
ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
ev.data.ptr = server;
epoll_ctl(epollfd, EPOLL_CTL_ADD, server->fd, &ev);
}
if (in->incomplete_data == NULL || copy_data(in) != 0) {
@ -380,13 +355,13 @@ void tcp_in(conn_t * in, conf * configure)
return;
}
// 数据处理完毕,可以发送
handle_data_complete:
handle_data_complete:
// 这个判断是防止 多次读取客户端数据,但是没有和服务端建立连接,导致报错
if (server->fd >= 0)
tcp_out(server);
}
void tcp_out(conn_t *to)
void tcp_out(conn_t * to)
{
conn_t *from;
int write_len;
@ -400,38 +375,29 @@ void tcp_out(conn_t *to)
from->timer = to->timer = 0;
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);
if (from->fd >= 0 && from->ready_data_len == 0)
{
ev.events = EPOLLIN|EPOLLET;
if (from->fd >= 0 && from->ready_data_len == 0) {
ev.events = EPOLLIN | EPOLLET;
ev.data.ptr = to;
epoll_ctl(epollfd, EPOLL_CTL_MOD, to->fd, &ev);
}
}
else
{
ev.events = EPOLLIN|EPOLLET;
} else {
ev.events = EPOLLIN | EPOLLET;
ev.data.ptr = to;
epoll_ctl(epollfd, EPOLL_CTL_MOD, to->fd, &ev);
free(from->ready_data);
from->ready_data = NULL;
from->ready_data_len = 0;
}
}
else if (write_len > 0)
{
} else if (write_len > 0) {
from->sent_len += write_len;
ev.events = EPOLLIN|EPOLLOUT|EPOLLET;
ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
ev.data.ptr = to;
epoll_ctl(epollfd, EPOLL_CTL_MOD, to->fd, &ev);
}
else if (errno != EAGAIN)
{
} else if (errno != EAGAIN) {
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;
}
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);
char *_p = strstr(header, "CONNECT"); // 在 CONNECT 方法中解析 隧道主机名称及端口号
if (_p)
{
if (_p) {
if (strchr(header, '[') || strchr(header, ']')) { // IPv6
char *_p1 = strchr(header, '[');
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, ' ');
strncpy(port, _p2 + 2, (int)(_p3 - _p2) - 1);
memcpy(port, _p2 + 2, (int)(_p3 - _p2) - 1);
return 0;
}
@ -175,11 +172,11 @@ int extract_host(char *header, char *host, char *port)
if (_p == NULL) {
_p = strstr(header, "host:");
}
if (_p == NULL) { // 都为空时
if (_p == NULL) { // 都为空时
return -1;
}
char *_p1 = strchr(_p, '\n'); // 指向末尾'\n'
char *_p1 = strchr(_p, '\n'); // 指向末尾'\n'
if (!_p1) {
return -1;
}
@ -187,14 +184,14 @@ int extract_host(char *header, char *host, char *port)
char *_p2 = strchr(_p + 5, ':'); // 5是指'Host:'的长度
int h_len = (int)(_p1 - _p - 6);
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';
char *_p3 = strchr(s_host, ':');
char *_p4 = NULL;
if (_p3)
_p4 = strchr(_p3 + 1, ':'); // 二次确认':'
{ // IPV6
_p4 = strchr(_p3 + 1, ':'); // 二次确认':'
{ // IPV6
if (_p4 != NULL) {
char *_p5 = NULL;
char *_p6 = NULL;
@ -204,48 +201,48 @@ int extract_host(char *header, char *host, char *port)
char url[_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';
if (strstr(url, "http") != NULL) { // 去除 'http://'
if (strstr(url, "http") != NULL) { // 去除 'http://'
memcpy(url, url + 7, (_p6 - _p5 - 1) - 7);
url[(_p6 - _p5 - 1) - 7] = '\0';
char *_p7 = strchr(url, '/');
if (_p7) // 去除 uri
if (_p7) // 去除 uri
url[_p7 - url] = '\0';
char *_p8 = strchr(url, ']');
char *_p9 = strchr(url, '\0');
if (_p8) {
if ((_p9-_p8) == 1) { // 如果不带端口就默认80, 并结束
if ((_p9 - _p8) == 1) { // 如果不带端口就默认80, 并结束
memcpy(port, "80", 2);
strncpy(host, url + 1, _p8 - (url+1));
memcpy(host, url + 1, _p8 - (url + 1));
return 0;
}
memcpy(port, _p8 + 2, _p9-(_p8+2));
strncpy(host, url + 1, _p8 - (url+1));
memcpy(port, _p8 + 2, _p9 - (_p8 + 2));
memcpy(host, url + 1, _p8 - (url + 1));
}
return 0;
} else { // HTTP头为不规范的url时处理Host, 主要Proxifier转发url为'/'时
} else { // HTTP头为不规范的url时处理Host, 主要Proxifier转发url为'/'时
//printf("s_host: %s\n", s_host);
char *_p1 = strchr(s_host, '[');
if (_p1 == NULL) // 涉及到自定义的Host, 不带'['、']'时, 默认截取最后为端口
if (_p1 == NULL) // 涉及到自定义的Host, 不带'['、']'时, 默认截取最后为端口
{
char *_p2 = strrchr(s_host, ':');
remote_port = atoi(_p2+1);
strncpy(remote_host, s_host, _p2-s_host);
remote_port = atoi(_p2 + 1);
memcpy(remote_host, s_host, _p2 - s_host);
return 0;
}
char *_p2 = strchr(_p1 + 1, ']');
char *_p3 = strchr(s_host, '\0');
if (_p1 && _p2) {
memcpy(host, _p1 + 1, _p2 - _p1 - 1);
if (strlen(_p2) < 3) {
memcpy(host, _p1 + 1, _p2 - (_p1 + 1));
if (_p3 - (_p2 + 1) < 2) {
memcpy(port, "80", 2);
} 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 方法
{
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(host, _p + 5 + 1, (int)(_p2 - _p - 5 - 1));
} else { // 不带端口
} else { // 不带端口
if (0 < (int)(_p1 - _p - 5 - 1 - 1)) {
memcpy(host, _p + 5 + 1, (_p1 - _p - 5 - 1 - 1));
memcpy(port, "80", 2);
@ -288,7 +285,7 @@ char *get_http_path(char *url, int url_len, char *path)
if (url) {
_p0 = strstr(url + 7, "/");
if (_p0)
return memcpy(path, _p0, _p1-_p0);
return memcpy(path, _p0, _p1 - _p0);
else
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大小
*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);
u = strchr(m + 1, ' ');
memmove(http_request->U, m + 1, u - (m+1));
http_request->U_len = u - ( m + 1);
memmove(http_request->U, m + 1, u - (m + 1));
http_request->U_len = u - (m + 1);
memmove(http_request->version, u + 1, 8);
http_request->version_len = 8;
// 获取Host、Port长度
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 *_p2 = strchr(http_request->U + 7, '\0');
if (_p1) {
uri_len = _p2-_p1;
uri_len = _p2 - _p1;
}
} else { // 非标准头
char *_p1 = strchr(http_request->U, '/');
char *_p2 = strchr(http_request->U, '\0');
if (_p1) {
uri_len = _p2-_p1;
uri_len = _p2 - _p1;
} else {
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->uri, 0, uri_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;
http_request->host_len = (int)strlen(http_request->host);
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);
memcpy(http_request->url, http_request->U, 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;
http_request->uri_len = uri_len;
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->U, http_request->U_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->port, http_request->port_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->uri, http_request->uri_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->version, http_request->version_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->H, http_request->H_len);
printf("%s %d\n", http_request->url, http_request->url_len);
printf("%s %d\n", http_request->uri, http_request->uri_len);
*/
free(head);
return;
@ -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)
{
char *tail_head;
char *temp_stack;
char *_p0;
char *_p1;
@ -495,17 +491,17 @@ static char *splice_head(char *head, int *head_len, const char *needle, char *st
_p1 = _p1 + 1;
_p0 = strchr(_p1, '\0');
tail_head = (char *)alloca((_p0 - _p1) + 1);
if (tail_head == NULL) {
temp_stack = (char *)alloca((_p0 - _p1) + 1);
if (temp_stack == NULL) {
perror("alloca");
return head;
}
memset(tail_head, 0, (_p0 - _p1) + 1);
memcpy(tail_head, _p1, (_p0 - _p1));
memset(temp_stack, 0, (_p0 - _p1) + 1);
memcpy(temp_stack, _p1, (_p0 - _p1));
memset(head, 0, *head_len);
memcpy(head, string, string_len);
strncat(head, tail_head, (_p0 - _p1));
strncat(head, temp_stack, (_p0 - _p1));
*head_len = string_len + (_p0 - _p1);
return head;
}
@ -517,43 +513,42 @@ static char *delete_head(char *head, int *head_len, const char *needle, int stri
_p1 = _p2 = _p3 = NULL;
int temp_stack_len;
_p1 = strstr(head, needle); // _p1指向head字符串中的"needle"字符处(needle字符串的第一个字符)
_p1 = strstr(head, needle); // _p1指向head字符串中的"needle"字符处(needle字符串的第一个字符)
if (_p1 == NULL) {
//perror("_p1 HEAD NULL");
return head;
}
_p2 = strchr(_p1, string); // _p2指向head字符串中"string"字符到末尾中的'\0'
_p2 = strchr(_p1, string); // _p2指向head字符串中"string"字符到末尾中的'\0'
if (_p2 == NULL) {
//perror("_p2 HEAD NULL");
return head;
}
_p3 = strchr(_p1, '\0'); // _p3指向head字符串末尾的'\0'
_p3 = strchr(_p1, '\0'); // _p3指向head字符串末尾的'\0'
if (_p3 == NULL) {
//perror("_p3 HEAD NULL");
return head;
}
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) {
perror("alloca");
return head;
}
memset(temp_stack, 0, temp_stack_len + 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);
*head_len = 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;
while (p)
{
while (p) {
if (p->strrep) {
str = replace(str, str_len, p->strrep_s, p->strrep_s_len, p->strrep_t, p->strrep_t_len);
}
@ -564,12 +559,11 @@ static char *conf_handle_strrep(char *str, int *str_len, tcp *temp)
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;
while (p)
{
while (p) {
if (p->regrep) {
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);
if ((return_val = strncmp(in->incomplete_data, "CONNECT", 7)) == 0)
{
if ((return_val = strncmp(in->incomplete_data, "CONNECT", 7)) == 0) {
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);
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));
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);
while (result != NULL) {
@ -626,7 +618,7 @@ char *request_head(conn_t * in, conf * configure)
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_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, "[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;
new_incomplete_data = (char *)realloc(in->incomplete_data, incomplete_head_len + 1);
@ -649,15 +641,14 @@ char *request_head(conn_t * in, conf * configure)
return in->incomplete_data;
}
in->incomplete_data = new_incomplete_data;
memset(in->incomplete_data, 0, incomplete_head_len + 1); // 清空incomplete_data数据
memmove(in->incomplete_data, incomplete_head, incomplete_head_len); // 更新incomplete_data数据
in->incomplete_data_len = incomplete_head_len; // 更新incomplete_data长度
memset(in->incomplete_data, 0, incomplete_head_len + 1); // 清空incomplete_data数据
memmove(in->incomplete_data, incomplete_head, 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;
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));
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);
while (result != NULL) {
@ -687,7 +678,7 @@ char *request_head(conn_t * in, conf * configure)
}
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_regrep(incomplete_head, &incomplete_head_len, http_head_regrep);
@ -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, "[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;
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;
}
in->incomplete_data = new_incomplete_data;
memset(in->incomplete_data, 0, incomplete_head_len + 1); // 清空incomplete_data数据
memmove(in->incomplete_data, incomplete_head, incomplete_head_len); // 更新incomplete_data数据
in->incomplete_data_len = incomplete_head_len; // 更新incomplete_data长度
memset(in->incomplete_data, 0, incomplete_head_len + 1); // 清空incomplete_data数据
memmove(in->incomplete_data, incomplete_head, 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);

View File

@ -49,7 +49,7 @@ int8_t read_cache_file()
char *buff, *cache_ptr;
struct dns_cache *cache_temp;
long file_size;
int len, fl=0;
int len, fl = 0;
cache_temp = NULL;
cache_using = 0;
@ -69,8 +69,7 @@ int8_t read_cache_file()
if ((buff = (char *)alloca(file_size)) == NULL)
return 1;
rewind(cfp);
if ((fl = fread(buff, file_size, 1, cfp)) != 1)
;
if ((fl = fread(buff, file_size, 1, cfp)) != 1) ;
fclose(cfp);
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));
//切换回用户设置的uid
if (-1 == setegid(global.uid))
perror("setegid");
perror("setegid");
if (-1 == seteuid(global.uid))
perror("seteuid");
perror("seteuid");
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, "[U]", 3, "/", 1);
} else { /* 默认使用CONNECT请求 */
if (https.encodeCode) {
dataEncode(dest, strlen(dest), https.encodeCode);
udp.httpsProxy_encodeCode = https.encodeCode;
}
udp.http_request_len = default_ssl_request_len;
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));
memcpy(&udp.dst, &https.dst, sizeof(udp.dst));
if (https.encodeCode) {
dataEncode(dest, strlen(dest), https.encodeCode);
udp.httpsProxy_encodeCode = https.encodeCode;
}
udp.http_request_len = default_ssl_request_len;
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));
memcpy(&udp.dst, &https.dst, sizeof(udp.dst));
}
if (udp.http_request == NULL)
errors("out of memory.");

39
main.c
View File

@ -84,7 +84,7 @@ int create_server_socket(int port)
perror("bind");
return -1;
}
if (listen(server_sock, 500) < 0) {
if (listen(server_sock, 200) < 0) {
perror("listen");
return -1;
}
@ -121,7 +121,7 @@ int create_server_socket6(int port)
return -1;
}
if (listen(server_sock, 500) < 0) {
if (listen(server_sock, 200) < 0) {
perror("listen");
return -1;
}
@ -177,7 +177,7 @@ void accept_client6()
void *http_proxy_loop(void *p)
{
conf *configure = (conf *)p;
conf *configure = (conf *) p;
int n;
ev.events = EPOLLIN;
@ -192,14 +192,20 @@ void *http_proxy_loop(void *p)
exit(1);
}
while (1) {
while (1)
{
n = epoll_wait(epollfd, events, MAX_CONNECTION, -1);
while (n-- > 0) {
if (events[n].data.fd == server_sock6) {
accept_client6();
} else if (events[n].data.fd == server_sock) {
while (n-- > 0)
{
if (events[n].data.fd == server_sock) {
accept_client();
} else {
}
else if (events[n].data.fd == server_sock6)
{
accept_client6();
}
else
{
if (events[n].events & EPOLLIN) {
tcp_in((conn_t *) events[n].data.ptr, configure);
}
@ -328,7 +334,7 @@ void *timeout_check(void *nullPtr)
return NULL;
}
void initialize(conf *configure)
void initialize(conf * configure)
{
int i;
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;
sigset_t signal_mask;
@ -447,7 +453,6 @@ void _main(int argc, char *argv[])
sslEncodeCode = 0; // 默认SSL不转码
char optstring[] = ":l:f:t:p:c:e:s:h?";
static struct option longopts[] = {
{ "local_address", required_argument, 0, 'l' },
@ -522,11 +527,11 @@ void _main(int argc, char *argv[])
}
#if DAEMON
// 守护进程
int nochdir = 0;
int noclose = 0;
int nochdir = 1;
int noclose = 1;
int pid;
if ((pid = fork()) < 0) {
return ;
return;
} else if (0 != pid) {
free_tcp(&http_head_strrep);
free_tcp(&http_head_regrep);
@ -538,12 +543,12 @@ void _main(int argc, char *argv[])
}
if (setsid() < 0) {
return ;
return;
}
signal(SIGHUP, SIG_IGN);
if ((pid = fork()) < 0) {
return ;
return;
} else if (0 != pid) {
free_tcp(&http_head_strrep);
free_tcp(&http_head_regrep);

1
main.h
View File

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