optimization
This commit is contained in:
parent
0efa5309a6
commit
9a2c98f059
14
CProxy.conf
14
CProxy.conf
@ -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 {
|
||||
|
2
Makefile
2
Makefile
@ -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
|
||||
|
108
conf.c
108
conf.c
@ -1,7 +1,6 @@
|
||||
#include "conf.h"
|
||||
#include "http_request.h"
|
||||
|
||||
|
||||
/* 字符串预处理,设置转义字符 */
|
||||
static void string_pretreatment(char *str, int *len)
|
||||
{
|
||||
@ -118,36 +117,21 @@ 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);;
|
||||
}
|
||||
|
||||
@ -163,34 +147,23 @@ 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)
|
||||
{
|
||||
} 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)
|
||||
{
|
||||
} 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)
|
||||
{
|
||||
} else if (strcasecmp(var, "strrep") == 0) {
|
||||
http_node = (tcp *) malloc(sizeof(struct tcp));
|
||||
if (http_node == NULL)
|
||||
return;
|
||||
@ -272,36 +245,25 @@ 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)
|
||||
{
|
||||
} 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)
|
||||
{
|
||||
} 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;
|
||||
|
||||
}
|
||||
else if (strcasecmp(var, "strrep") == 0)
|
||||
{
|
||||
} else if (strcasecmp(var, "strrep") == 0) {
|
||||
// 链表操作,支持多个相同配置KEY
|
||||
https_node = (tcp *) malloc(sizeof(struct tcp));
|
||||
if (https_node == NULL)
|
||||
@ -337,9 +299,7 @@ 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;
|
||||
@ -402,7 +362,6 @@ void print_tcp(tcp *p)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
tcp *local_reverse(tcp * head)
|
||||
{
|
||||
tcp *beg = NULL;
|
||||
@ -428,8 +387,7 @@ tcp *local_reverse(tcp *head)
|
||||
void free_tcp(tcp ** conf_head)
|
||||
{
|
||||
tcp *t;
|
||||
while(*conf_head != NULL)
|
||||
{
|
||||
while (*conf_head != NULL) {
|
||||
t = *conf_head;
|
||||
*conf_head = t->next;
|
||||
|
||||
@ -456,20 +414,15 @@ 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)
|
||||
{
|
||||
} 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)
|
||||
{
|
||||
} else if (strcasecmp(var, "encode") == 0) {
|
||||
p->encode = atoi(val_begin);
|
||||
}
|
||||
|
||||
@ -482,20 +435,15 @@ 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)
|
||||
{
|
||||
} 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)
|
||||
{
|
||||
} else if (strcasecmp(var, "encode") == 0) {
|
||||
p->httpudp_encode = atoi(val_begin);
|
||||
}
|
||||
|
||||
|
1
conf.h
1
conf.h
@ -73,7 +73,6 @@ 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);
|
||||
|
76
http_proxy.c
76
http_proxy.c
@ -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++;
|
||||
}
|
||||
}
|
||||
@ -39,18 +37,15 @@ static char *read_data(conn_t *in, char *data, int *data_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;
|
||||
}
|
||||
@ -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,8 +302,7 @@ void tcp_in(conn_t * in, conf * configure)
|
||||
if (in->fd < 0)
|
||||
return;
|
||||
//如果in - cts是奇数,那么是服务端触发事件
|
||||
if ((in - cts) & 1)
|
||||
{
|
||||
if ((in - cts) & 1) {
|
||||
in->timer = (in - 1)->timer = 0;
|
||||
if (in->ready_data_len <= 0)
|
||||
serverToClient(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);
|
||||
@ -367,12 +343,11 @@ void tcp_in(conn_t * in, conf * configure)
|
||||
close_connection(in);
|
||||
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) {
|
||||
@ -400,21 +375,16 @@ 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)
|
||||
{
|
||||
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
|
||||
{
|
||||
} else {
|
||||
ev.events = EPOLLIN | EPOLLET;
|
||||
ev.data.ptr = to;
|
||||
epoll_ctl(epollfd, EPOLL_CTL_MOD, to->fd, &ev);
|
||||
@ -422,16 +392,12 @@ void tcp_out(conn_t *to)
|
||||
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.data.ptr = to;
|
||||
epoll_ctl(epollfd, EPOLL_CTL_MOD, to->fd, &ev);
|
||||
}
|
||||
else if (errno != EAGAIN)
|
||||
{
|
||||
} else if (errno != EAGAIN) {
|
||||
close_connection(to);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
@ -187,7 +184,7 @@ 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, ':');
|
||||
@ -204,7 +201,7 @@ 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://'
|
||||
@ -219,12 +216,12 @@ int extract_host(char *header, char *host, char *port)
|
||||
if (_p8) {
|
||||
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(host, url + 1, _p8 - (url + 1));
|
||||
}
|
||||
return 0;
|
||||
} else { // HTTP头为不规范的url时处理Host, 主要Proxifier转发url为'/'时
|
||||
@ -234,15 +231,15 @@ int extract_host(char *header, char *host, char *port)
|
||||
{
|
||||
char *_p2 = strrchr(s_host, ':');
|
||||
remote_port = atoi(_p2 + 1);
|
||||
strncpy(remote_host, s_host, _p2-s_host);
|
||||
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));
|
||||
@ -408,7 +405,6 @@ void parse_request_head(char *http_request_line, struct http_request *http_reque
|
||||
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);
|
||||
|
||||
@ -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);
|
||||
@ -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;
|
||||
}
|
||||
@ -540,8 +536,8 @@ static char *delete_head(char *head, int *head_len, const char *needle, int stri
|
||||
}
|
||||
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;
|
||||
@ -552,8 +548,7 @@ 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);
|
||||
}
|
||||
@ -568,8 +563,7 @@ 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,8 +588,7 @@ 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];
|
||||
@ -617,7 +610,6 @@ char *request_head(conn_t * in, conf * configure)
|
||||
memmove(incomplete_head, in->incomplete_data, in->incomplete_data_len);
|
||||
memmove(https_del_copy, configure->https_del, configure->https_del_len + 1);
|
||||
|
||||
|
||||
result = strtok_r(https_del_copy, delim, &saveptr);
|
||||
while (result != NULL) {
|
||||
delete_head(incomplete_head, &in->incomplete_data_len, result, '\n');
|
||||
@ -656,8 +648,7 @@ char *request_head(conn_t * in, conf * configure)
|
||||
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];
|
||||
|
@ -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) {
|
||||
|
27
main.c
27
main.c
@ -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;
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
@ -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,8 +527,8 @@ 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;
|
||||
|
Loading…
Reference in New Issue
Block a user