Remove unsafe functions.

This commit is contained in:
aixiao 2022-04-20 17:12:58 +08:00
parent 5e3b8d0194
commit f6800178f4
7 changed files with 180 additions and 197 deletions

View File

@ -1,14 +1,14 @@
global {
uid=3004;
timeout=7;
encode=128;
timeout=60;
encode=129;
tcp_listen=0124;
dns_listen=0126;
udp_listen = 10010;
}
http {
http_ip="174.137.54.215";
http_ip="172.22.86.139";
http_port=129;
http_del="Host,";
http_first="[M] [U] [V]\r\nHost: [H]\r\n";
@ -17,23 +17,15 @@ http {
strrep="aixiao.me" -> "AIXIAO.ME";
regrep="Accept-Encoding*.+?" -> "Accept-Encoding: GZIP, deflate";
regrep="Connection*.+?" -> "Connection: KEEP-alive";
encode=129;
}
https {
https_ip="174.137.54.215";
https_ip="172.22.86.139";
https_port=129;
https_del="Host,host,x-online-host";
https_first="[M] [U] [V]\r\nHost: [host]\r\n";
strrep="Windows NT 10.0" -> "aaaaaaaaaaaaaaaaaaa";
strrep="aaaaaaaaaaaaaaaaaaa" -> "bbbbbbbbbbbbbbbb";
strrep="bbbbbbbbbbbbbbbb" -> "cccccccccccccccccc";
strrep="cccccccccccccccccc" -> "ddddddddddddddddddddddd";
regrep="Host*.+?" -> "host: [host]:443";
regrep="host*.+?" -> "Host: [H]";
regrep="host*.+?" -> "Host: [host]";
https_first="[M] [U] [V]\r\nHost: [host]:[port]\r\n";
encode=129;
}
httpdns {

22
conf.c
View File

@ -1,20 +1,6 @@
#include "conf.h"
#include "http_request.h"
char *strncpy_(char *dest, const char *src, size_t n)
{
int size = sizeof(char) * (n + 1);
char *tmp = (char *)malloc(size); // 开辟大小为n+1的临时内存tmp
if (tmp) {
memset(tmp, '\0', size); // 将内存初始化为0
memcpy(tmp, src, size - 1); // 将src的前n个字节拷贝到tmp
memcpy(dest, tmp, size); // 将临时空间tmp的内容拷贝到dest
free(tmp); // 释放内存
return dest;
} else {
return NULL;
}
}
/* 字符串预处理,设置转义字符 */
static void string_pretreatment(char *str, int *len)
@ -183,6 +169,10 @@ static void parse_http_module(char *content, conf * p)
if (copy_new_mem(val_begin, p->http_ip_len, &p->http_ip) != 0)
return ;
}
else if (strcasecmp(var, "encode") == 0)
{
p->http_encode = atoi(val_begin);
}
else if (strcasecmp(var, "http_port") == 0)
{
p->http_port = atoi(val_begin);
@ -288,6 +278,10 @@ static void parse_https_module(char *content, conf * p)
if (copy_new_mem(val_begin, p->https_ip_len, &p->https_ip) != 0)
return ;
}
else if (strcasecmp(var, "encode") == 0)
{
p->https_encode = atoi(val_begin);
}
else if (strcasecmp(var, "https_port") == 0)
{
p->https_port = atoi(val_begin);

2
conf.h
View File

@ -24,11 +24,13 @@ typedef struct CONF {
int http_port;
char *http_ip, *http_del, *http_first;
int http_ip_len, http_del_len, http_first_len;
int http_encode;
// https module
int https_port;
char *https_ip, *https_del, *https_first;
int https_ip_len, https_del_len, https_first_len;
int https_encode;
// httpdns module
char *addr;

View File

@ -16,11 +16,15 @@ void dataEncode(char *data, int data_len, unsigned code)
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++;
}
}
@ -28,22 +32,25 @@ 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;
}
@ -53,7 +60,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)
@ -66,12 +73,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);
@ -183,12 +190,9 @@ int create_connection6(char *remote_host, int remote_port)
// check for numeric IP to specify IPv6 or IPv4 socket
if ((validfamily = check_ipversion(remote_host)) != 0) {
hints.ai_family = validfamily;
hints.ai_flags |= AI_NUMERICHOST; // remote_host是有效的数字ip跳过解析
hints.ai_flags |= AI_NUMERICHOST;
}
//getaddrinfo方法
// 检查指定的主机是否有效。 如果remote_host是主机名尝试解析地址
if (getaddrinfo(remote_host, portstr, &hints, &res) != 0) {
errno = EFAULT;
perror("getaddrinfo");
@ -199,13 +203,12 @@ int create_connection6(char *remote_host, int remote_port)
perror("socket");
return -1;
}
if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
perror("connect");
return -1;
}
if (res != NULL)
freeaddrinfo(res);
@ -287,7 +290,9 @@ int create_connection6(char *remote_host, int remote_port)
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);
@ -297,11 +302,12 @@ 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;
}
ct->incomplete_data = NULL;
ct->incomplete_data_len = 0;
@ -310,25 +316,27 @@ static int8_t copy_data(conn_t * ct)
void tcp_in(conn_t * in, conf * configure)
{
char *headerEnd;
conn_t *server;
char *headerEnd;
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);
return;
}
server = in + 1;
server->request_type = in->request_type = request_type(in->incomplete_data);
if (in->request_type == OTHER_TYPE)
@ -341,28 +349,34 @@ void tcp_in(conn_t * in, conf * configure)
}
goto handle_data_complete;
}
headerEnd = strstr(in->incomplete_data, "\n\r");
//请求头不完整,等待下次读取
if (headerEnd == NULL)
return;
if (in->request_type == HTTP_TYPE) {
if (in->reread_data == 0)
{
in->reread_data = 1;
in->incomplete_data = request_head(in, configure);
server->fd = create_connection6(remote_host, remote_port);
if (server->fd < 0)
if (server->fd < 0) {
printf("remote->fd ERROR!\n");
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->reread_data == 0)
{
in->reread_data = 1;
}
if (in->incomplete_data == NULL || copy_data(in) != 0) {
close_connection(in);
close_connection(server);
return;
}
// 数据处理完毕,可以发送
@ -372,7 +386,7 @@ void tcp_in(conn_t * in, conf * configure)
tcp_out(server);
}
void tcp_out(conn_t * to)
void tcp_out(conn_t *to)
{
conn_t *from;
int write_len;
@ -384,31 +398,40 @@ void tcp_out(conn_t * to)
else
from = to + 1;
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

@ -5,6 +5,10 @@
#include "main.h"
#include <arpa/inet.h>
#define SSL_RSP_CONNECT "HTTP/1.1 200 Connection established\r\nServer: SpecialProxy_CuteBi\r\nConnection: keep-alive\r\n\r\n"
#define SSL_RSP_HTTP "HTTP/1.1 200 OK\r\nContent-length: 99999999\r\nServer: SpecialProxy_CuteBi\r\nContent-Type: text/plain; charset=utf-8\r\nConnection: keep-alive\r\n\r\n"
#define SSL_RSP_WEBSOCKET "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: SpecialProxy_CuteBi\r\n\r\n"
#define HTTP_TYPE 0
#define OTHER_TYPE 1

View File

@ -138,6 +138,7 @@ static char *regrep(char *str, int *str_len, const char *src, char *dest, int de
int extract_host(char *header, char *host, char *port)
{
memset(port, 0, strlen(port));
memset(host, 0, strlen(host));
//printf("%s\n", header);
@ -188,10 +189,11 @@ int extract_host(char *header, char *host, char *port)
char s_host[h_len];
strncpy(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, ':');
_p4 = strchr(_p3 + 1, ':'); // 二次确认':'
{ // IPV6
if (_p4 != NULL) {
char *_p5 = NULL;
@ -215,13 +217,14 @@ int extract_host(char *header, char *host, char *port)
char *_p8 = strchr(url, ']');
char *_p9 = strchr(url, '\0');
if (_p8) {
strcpy(port, _p8 + 2);
strncpy(host, url + 1, _p8 - (url+1));
if ((_p9-_p8) == 1) {
strcpy(port, "80");
if ((_p9-_p8) == 1) { // 如果不带端口就默认80, 并结束
memcpy(port, "80", 2);
strncpy(host, url + 1, _p8 - (url+1));
return 0;
}
memcpy(port, _p8 + 2, _p9-(_p8+2));
strncpy(host, url + 1, _p8 - (url+1));
}
return 0;
} else { // HTTP头为不规范的url时处理Host, 主要Proxifier转发url为'/'时
@ -236,12 +239,13 @@ int extract_host(char *header, char *host, char *port)
}
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) {
strcpy(port, "80");
memcpy(port, "80", 2);
} else {
strcpy(port, _p2 + 2);
memcpy(port, _p2+2, _p3-(_p2+2));
}
}
@ -253,7 +257,7 @@ int extract_host(char *header, char *host, char *port)
}
// HTTP 非 CONNECT 方法
{
if (_p2 && _p2 < _p1) { // 带端口, p2 指向':' p1 指向末尾'\n'
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 { // 不带端口
@ -273,24 +277,23 @@ int extract_host(char *header, char *host, char *port)
return 0;
}
char *get_http_path(char *url, char *path)
char *get_http_path(char *url, int url_len, char *path)
{
char *_p0;
_p0 = NULL;
int url_len;
url_len = 0;
url_len = strlen(url);
char *_p0, *_p1;
_p0 = _p1 = NULL;
_p1 = strchr(url, '\0');
if (url_len > 7) {
if (url) {
_p0 = strstr(url + 7, "/");
if (_p0)
return memcpy(path, _p0, (int)strlen(_p0));
return memcpy(path, _p0, _p1-_p0);
else
memcpy(path, "/", 1); // 如果没有资源路径就默认"/"
return memcpy(path, "/", 1); // 如果没有资源路径就默认"/"
}
} else {
memcpy(path, "/", 1);
return memcpy(path, "/", 1);
}
return NULL;
@ -310,8 +313,9 @@ void get_http_host_port_len(char *head, int *host_len, int *port_len)
host[*host_len] = '\0';
char *_p3 = strrchr(host, ':');
char *_p4 = strchr(host, '\0');
if (_p3) {
*port_len = strlen(_p3 + 1);
*port_len = _p4 - (_p3 + 1);
} else {
*port_len = *host_len;
}
@ -326,8 +330,9 @@ void get_http_host_port_len(char *head, int *host_len, int *port_len)
host[*host_len] = '\0';
char *_p3 = strrchr(host, ':');
char *_p4 = strchr(host, '\0');
if (_p3) {
*port_len = strlen(_p3 + 1);
*port_len = _p4 - (_p3 + 1);
} else {
*port_len = *host_len;
}
@ -382,15 +387,12 @@ void parse_request_head(char *http_request_line, struct http_request *http_reque
http_request->method = (char *)malloc(sizeof(char) * 8);
http_request->U = (char *)malloc(sizeof(char) * head_len);
http_request->version = (char *)malloc(10);
if (http_request->method == NULL) {
if (http_request->method == NULL)
perror("malloc");
}
if (http_request->U == NULL) {
if (http_request->U == NULL)
perror("malloc");
}
if (http_request->version == NULL) {
if (http_request->version == NULL)
perror("malloc");
}
memset(http_request->method, 0, 8);
memset(http_request->U, 0, sizeof(char) * head_len);
memset(http_request->version, 0, 10);
@ -401,10 +403,11 @@ void parse_request_head(char *http_request_line, struct http_request *http_reque
u = strchr(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;
http_request->U_len = (int)strlen(http_request->U);
// 获取Host、Port长度
get_http_host_port_len(http_request_line, &host_len, &port_len);
@ -413,13 +416,15 @@ void parse_request_head(char *http_request_line, struct http_request *http_reque
char *_p0 = strstr(http_request->U, "http://");
if (_p0) { // 标准头
char *_p1 = strchr(http_request->U + 7, '/');
char *_p2 = strchr(http_request->U + 7, '\0');
if (_p1) {
uri_len = (int)strlen(_p1);
uri_len = _p2-_p1;
}
} else { // 非标准头
char *_p1 = strchr(http_request->U, '/');
char *_p2 = strchr(http_request->U, '\0');
if (_p1) {
uri_len = (int)strlen(_p1);
uri_len = _p2-_p1;
} else {
uri_len = 1; // 没有uri时
}
@ -446,37 +451,38 @@ 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)
return;
http_request->host_len = (int)strlen(http_request->host);
http_request->port_len = (int)strlen(http_request->port);
memcpy(http_request->H, http_request->host, http_request->host_len);
strcat(http_request->H, ":");
strcat(http_request->H, http_request->port);
strncat(http_request->H, ":", 2);
strncat(http_request->H, http_request->port, http_request->port_len);
memcpy(http_request->url, http_request->U, http_request->U_len);
get_http_path(http_request->url, http_request->uri);
http_request->url_len = http_request->U_len;
if (get_http_path(http_request->url, http_request->url_len, http_request->uri) == NULL )
return;
http_request->U_len = (int)strlen(http_request->U);
http_request->url_len = (int)strlen(http_request->url);
http_request->uri_len = (int)strlen(http_request->uri);
http_request->H_len = (int)strlen(http_request->H);
/*
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);
//printf("%s %d\n", http_request->uri);
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;
}
static char *splice_head(char *head, const char *needle, char *string)
static char *splice_head(char *head, int *head_len, const char *needle, char *string, int string_len)
{
char *tail_head;
char *_p0;
@ -495,16 +501,16 @@ static char *splice_head(char *head, const char *needle, char *string)
return head;
}
memset(tail_head, 0, (_p0 - _p1) + 1);
strcpy(tail_head, _p1);
memcpy(tail_head, _p1, (_p0 - _p1));
memset(head, 0, strlen(head));
strcpy(head, string);
strcat(head, tail_head);
memset(head, 0, *head_len);
memcpy(head, string, string_len);
strncat(head, tail_head, (_p0 - _p1));
*head_len = string_len + (_p0 - _p1);
return head;
}
static char *delete_head(char *head, const char *needle, int string)
static char *delete_head(char *head, int *head_len, const char *needle, int string)
{
char *temp_stack;
char *_p1, *_p2, *_p3;
@ -534,20 +540,22 @@ static char *delete_head(char *head, const char *needle, int string)
}
memset(temp_stack, 0, temp_stack_len + 1);
memmove(temp_stack, head, (_p1 - head) - 1);
strcat(temp_stack, _p2);
strncat(temp_stack, _p2, _p3-_p2);
memset(head, 0, strlen(head));
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);
str = replace(str, str_len, p->strrep_s, p->strrep_s_len, p->strrep_t, p->strrep_t_len);
}
p = p->next;
@ -556,12 +564,14 @@ 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);
str = regrep(str, str_len, p->regrep_s, p->regrep_t, p->regrep_t_len);
}
p = p->next;
@ -586,6 +596,8 @@ char *request_head(conn_t * in, conf * configure)
if ((return_val = strncmp(in->incomplete_data, "CONNECT", 7)) == 0)
{
sslEncodeCode = configure->https_encode;
char https_del_copy[configure->https_del_len+1];
memset(remote_host, 0, CACHE_SIZE);
@ -593,8 +605,9 @@ char *request_head(conn_t * in, conf * configure)
remote_port = configure->https_port;
}
if (configure->https_ip != NULL) {
strcpy(remote_host, configure->https_ip);
memcpy(remote_host, configure->https_ip, configure->https_ip_len);
}
incomplete_head = (char *)malloc(sizeof(char) * (BUFFER_SIZE));
if (incomplete_head == NULL) {
free(incomplete_head);
@ -607,21 +620,16 @@ char *request_head(conn_t * in, conf * configure)
result = strtok_r(https_del_copy, delim, &saveptr);
while (result != NULL) {
delete_head(incomplete_head, result, '\n');
delete_head(incomplete_head, &in->incomplete_data_len, result, '\n');
result = strtok_r(NULL, delim, &saveptr);
}
splice_head(incomplete_head, "\n", configure->https_first);
incomplete_head_len = strlen(incomplete_head); // 更新HTTPS HEADER长度
splice_head(incomplete_head, &in->incomplete_data_len, "\n", configure->https_first, configure->https_first_len);
incomplete_head = conf_handle_strrep(incomplete_head, incomplete_head_len, https_head_strrep);
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);
incomplete_head_len = strlen(incomplete_head) + 1; // 更新HTTPS HEADER长度
incomplete_head = conf_handle_regrep(incomplete_head, incomplete_head_len, https_head_regrep);
incomplete_head_len = strlen(incomplete_head); // 更新HTTPS HEADER长度
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[M]", 3, http_request->method, http_request->method_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[method]", 8, http_request->method, http_request->method_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[U]", 3, http_request->U, http_request->U_len);
@ -630,7 +638,7 @@ char *request_head(conn_t * in, conf * configure)
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[host]", 6, http_request->host, http_request->host_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[port]", 6, http_request->port, http_request->port_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[H]", 3, http_request->H, http_request->H_len);
incomplete_head_len = strlen(incomplete_head); // 更新HTTPS HEADER长度
//printf("%s", incomplete_head); // 打印HTTPS HEADER
char *new_incomplete_data;
@ -650,13 +658,18 @@ char *request_head(conn_t * in, conf * configure)
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];
memset(remote_host, 0, CACHE_SIZE);
if (configure->http_port > 0)
if (configure->http_port > 0) {
remote_port = configure->http_port;
if (configure->http_ip != NULL)
strcpy(remote_host, configure->http_ip);
}
if (configure->http_ip != NULL) {
memcpy(remote_host, configure->http_ip, configure->http_ip_len);
}
incomplete_head = (char *)malloc(sizeof(char) * (BUFFER_SIZE));
if (incomplete_head == NULL) {
perror("malloc");
@ -669,16 +682,15 @@ char *request_head(conn_t * in, conf * configure)
result = strtok_r(http_del_copy, delim, &saveptr);
while (result != NULL) {
delete_head(incomplete_head, result, '\n');
delete_head(incomplete_head, &in->incomplete_data_len, result, '\n');
result = strtok_r(NULL, delim, &saveptr);
}
splice_head(incomplete_head, "\n", configure->http_first);
incomplete_head_len = strlen(incomplete_head); // 更新HTTP HEADER长度
incomplete_head = conf_handle_strrep(incomplete_head, incomplete_head_len, http_head_strrep);
incomplete_head_len = strlen(incomplete_head) + 1; // 更新HTTP HEADER长度
incomplete_head = conf_handle_regrep(incomplete_head, incomplete_head_len, http_head_regrep);
incomplete_head_len = strlen(incomplete_head); // 更新HTTP HEADER长度
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 = conf_handle_strrep(incomplete_head, &incomplete_head_len, http_head_strrep);
incomplete_head = conf_handle_regrep(incomplete_head, &incomplete_head_len, http_head_regrep);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[M]", 3, http_request->method, http_request->method_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[method]", 8, http_request->method, http_request->method_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[U]", 3, http_request->U, http_request->U_len);
@ -689,9 +701,9 @@ char *request_head(conn_t * in, conf * configure)
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[host]", 6, http_request->host, http_request->host_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[port]", 6, http_request->port, http_request->port_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[H]", 3, http_request->H, http_request->H_len);
incomplete_head_len = strlen(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);
if (new_incomplete_data == NULL) {
@ -703,7 +715,7 @@ char *request_head(conn_t * in, conf * configure)
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内存
}

48
main.c
View File

@ -50,7 +50,7 @@ int create_server_socket(int port)
perror("bind");
return -1;
}
if (listen(server_sock, 50) < 0) {
if (listen(server_sock, 500) < 0) {
perror("listen");
return -1;
}
@ -87,7 +87,7 @@ int create_server_socket6(int port)
return -1;
}
if (listen(server_sock, 20) < 0) {
if (listen(server_sock, 500) < 0) {
perror("listen");
return -1;
}
@ -176,50 +176,6 @@ void *http_proxy_loop(void *p)
}
}
close(epollfd);
return NULL;
}
void *start_server(conf * configure)
{
int n;
pthread_t thread_id;
ev.events = EPOLLIN;
ev.data.fd = server_sock;
if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, server_sock, &ev)) {
perror("epoll_ctl");
exit(1);
}
ev.events = EPOLLIN;
ev.data.fd = server_sock6;
if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, server_sock6, &ev)) {
perror("epoll_ctl");
exit(1);
}
if (timeout_minute)
pthread_create(&thread_id, NULL, &tcp_timeout_check, NULL);
while (1) {
n = epoll_wait(epollfd, events, MAX_CONNECTION, -1);
while (n-- > 0) {
if (events[n].data.fd == server_sock) {
accept_client();
} 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);
}
if (events[n].events & EPOLLOUT) {
tcp_out((conn_t *) events[n].data.ptr);
}
}
}
}
close(epollfd);
return NULL;
}