Optimize and fix some errors.

This commit is contained in:
aixiao 2022-01-19 21:48:02 +08:00
parent c0e238eb0c
commit 2881609c0c
34 changed files with 416 additions and 379 deletions

23
13.txt
View File

@ -1,16 +1,7 @@
==1384== Memcheck, a memory error detector
==1384== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==1384== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==1384== Command: ./CProxy
==1384== Parent PID: 74
==1384==
==1384== error calling PR_SET_PTRACER, vgdb might block
==1384==
==1384== HEAP SUMMARY:
==1384== in use at exit: 0 bytes in 0 blocks
==1384== total heap usage: 43 allocs, 43 frees, 10,762 bytes allocated
==1384==
==1384== All heap blocks were freed -- no leaks are possible
==1384==
==1384== For lists of detected and suppressed errors, rerun with: -s
==1384== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==411== Memcheck, a memory error detector
==411== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==411== Using Valgrind-3.17.0 and LibVEX; rerun with -h for copyright info
==411== Command: ./CProxy -c CProxy.conf
==411== Parent PID: 159
==411==
==411== error calling PR_SET_PTRACER, vgdb might block

BIN
CProxy

Binary file not shown.

View File

@ -1,6 +1,5 @@
global {
uid=3004;
process=2;
timeout=7;
encode=128;
tcp_listen=0124;
@ -11,10 +10,10 @@ global {
http {
http_ip="47.240.75.93";
http_port=129;
http_del="Host";
http_first="[M] http://[host][U] [V]\r\nHost: [H]\r\n";
strrep = "(Windows NT 10.0; Win64; x64)" -> "Android";
regrep = "Host*.+?" -> "Host: [host]";
http_del="Host,";
http_first="[M] [U] [V]\r\nHost: [H]\r\n";
//strrep="Windows NT 10.0" -> "Linux";
//regrep="Host*.+?" -> "Host: [H]";
}
https {
@ -22,11 +21,8 @@ https {
https_port=129;
https_del="Host,host,x-online-host";
https_first="[M] [U] [V]\r\nHost: [host]\r\n";
strrep = "HTTP/1.1" -> "HTTP/1.3";
strrep = "HTTP/1.3" -> "HTTP/1.1";
regrep = "host*.+?" -> "Host: [host]";
regrep = "Host*.+?" -> "host: [host]";
//strrep="Windows NT 10.0" -> "Linux";
//regrep="Host*.+?" -> "host: [host]:443";
}
httpdns {

View File

@ -13,8 +13,8 @@ http {
http_port=127;
http_del="Host";
http_first="[M] http://[host][U] [V]\r\nHost: [H]\r\n";
//strrep="Windows NT 10.0->Linux";
//regrep="Host:*.+?->Host: [host]:80";
//strrep="Windows NT 10.0" -> "Linux";
//regrep="Host:*.+?" -> "Host: [host]:80";
}
https {
@ -22,8 +22,8 @@ https {
https_port=127;
https_del="Host,host,x-online-host";
https_first="[M] [U] [V]\r\nHost: [host]\r\n";
//strrep="Windows NT 10.0->Linux";
//regrep="Host*.+?->host: [host]:443";
//strrep="Windows NT 10.0" -> "Linux";
//regrep="Host*.+?" -> "host: [host]:443";
}
httpdns {

244
conf.c
View File

@ -1,4 +1,5 @@
#include "conf.h"
#include "http_request.h"
char *strncpy_(char *dest, const char *src, size_t n)
{
@ -85,68 +86,6 @@ static char *set_var_val_lineEnd(char *content, char **var, char **val_begin, ch
break;
}
if (*val_end) {
**val_end = '\0';
val_len = *val_end - *val_begin;
lineEnd = *val_end;
} else {
val_len = strlen(*val_begin);
*val_end = lineEnd = *val_begin + val_len;
}
//string_pretreatment(*val_begin, &val_len);
*val_end = *val_begin + val_len;
//printf("var[%s]\nbegin[%s]\n\n", *var, *val_begin);
return lineEnd;
}
/* 在content中设置变量(var)的首地址,值(val)的位置首地址和末地址,返回下一行指针 */
static char *set_var_val_lineEnd2(char *content, char **var, char **val_begin, char **val_end)
{
char *p, *pn, *lineEnd;
;
int val_len;
while (1) {
if (content == NULL)
return NULL;
for (; *content == ' ' || *content == '\t' || *content == '\r' || *content == '\n'; content++) ;
if (*content == '\0')
return NULL;
*var = content;
pn = strchr(content, '\n');
p = strchr(content, '=');
if (p == NULL) {
if (pn) {
content = pn + 1;
continue;
} else
return NULL;
}
content = p;
//将变量以\0结束
for (p--; *p == ' ' || *p == '\t'; p--) ;
*(p + 1) = '\0';
//值的首地址
for (content++; *content == ' ' || *content == '\t'; content++) ;
if (*content == '\0')
return NULL;
//双引号引起来的值支持换行
if (*content == '"') {
*val_begin = content + 1;
*val_end = strstr(*val_begin, "\";");
if (*val_end != NULL)
break;
} else
*val_begin = content;
*val_end = strchr(content, ';');
if (pn && *val_end > pn) {
content = pn + 1;
continue;
}
break;
}
if (*val_end) {
**val_end = '\0';
val_len = *val_end - *val_begin;
@ -193,21 +132,36 @@ 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);;
}
@ -218,32 +172,35 @@ static void parse_global_module(char *content, conf * p)
static void parse_http_module(char *content, conf * p)
{
char *var, *val_begin, *val_end, *lineEnd;
int val_begin_len;
tcp *http_node = NULL;
char *p1 = NULL, *s = NULL, *t = NULL;
char *p2 = NULL;
while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) {
if (strcasecmp(var, "http_ip") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->http_ip = (char *)malloc(val_begin_len);
memset(p->http_ip, 0, val_begin_len);
memcpy(p->http_ip, val_begin, val_begin_len);
} else if (strcasecmp(var, "http_port") == 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, "http_port") == 0)
{
p->http_port = atoi(val_begin);
} else if (strcasecmp(var, "http_del") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->http_del = (char *)malloc(val_begin_len);
memset(p->http_del, 0, val_begin_len);
memcpy(p->http_del, val_begin, val_begin_len);
p->http_del_len = val_begin_len;
} else if (strcasecmp(var, "http_first") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->http_first = (char *)malloc(val_begin_len);
memset(p->http_first, 0, val_begin_len);
memcpy(p->http_first, val_begin, val_begin_len);
p->http_first_len = val_begin_len;
} else if (strcasecmp(var, "strrep") == 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)
{
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));
if (http_node == NULL)
return ;
@ -314,32 +271,37 @@ static void parse_http_module(char *content, conf * p)
static void parse_https_module(char *content, conf * p)
{
char *var, *val_begin, *val_end, *lineEnd;
int val_begin_len;
tcp *https_node = NULL;
char *p1 = NULL, *s = NULL, *t = NULL;
char *p2 = NULL;
while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) {
if (strcasecmp(var, "https_ip") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->https_ip = (char *)malloc(val_begin_len);
memset(p->https_ip, 0, val_begin_len);
memcpy(p->https_ip, val_begin, val_begin_len);
} else if (strcasecmp(var, "https_port") == 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, "https_port") == 0)
{
p->https_port = atoi(val_begin);
} else if (strcasecmp(var, "https_del") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->https_del = (char *)malloc(val_begin_len);
memset(p->https_del, 0, val_begin_len);
memcpy(p->https_del, val_begin, val_begin_len);
p->https_del_len = val_begin_len;
} else if (strcasecmp(var, "https_first") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->https_first = (char *)malloc(val_begin_len);
memset(p->https_first, 0, val_begin_len);
memcpy(p->https_first, val_begin, val_begin_len);
p->https_first_len = val_begin_len;
} else if (strcasecmp(var, "strrep") == 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)
{
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)
{
// 链表操作,支持多个相同配置KEY
https_node = (tcp *)malloc(sizeof(struct tcp));
if (https_node == NULL)
@ -375,7 +337,9 @@ static void parse_https_module(char *content, conf * p)
https_node->next = https_head->next;
https_head->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 ;
@ -468,21 +432,22 @@ void free_tcp(tcp **conf_head)
static void parse_httpdns_module(char *content, conf * p)
{
char *var, *val_begin, *val_end, *lineEnd;
int val_begin_len;
while ((lineEnd = set_var_val_lineEnd2(content, &var, &val_begin, &val_end)) != NULL) {
if (strcasecmp(var, "addr") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->addr = (char *)malloc(val_begin_len);
memset(p->addr, 0, val_begin_len);
memcpy(p->addr, val_begin, val_begin_len);
} else if (strcasecmp(var, "http_req") == 0) {
val_begin_len = val_end - val_begin;
p->http_req = (char *)malloc(val_begin_len + 1);
memset(p->http_req, 0, val_begin_len);
memcpy(p->http_req, val_begin, val_begin_len);
p->http_req_len = val_begin_len;
} else if (strcasecmp(var, "encode") == 0) {
while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) {
if (strcasecmp(var, "addr") == 0)
{
p->addr_len = val_end - val_begin;
if (copy_new_mem(val_begin, p->addr_len, &p->addr) != 0)
return ;
}
else if (strcasecmp(var, "http_req") == 0)
{
p->http_req_len = val_end - val_begin;
if (copy_new_mem(val_begin, p->http_req_len, &p->http_req) != 0)
return ;
}
else if (strcasecmp(var, "encode") == 0)
{
p->encode = atoi(val_begin);
}
@ -493,21 +458,22 @@ static void parse_httpdns_module(char *content, conf * p)
static void parse_httpudp_module(char *content, conf * p)
{
char *var, *val_begin, *val_end, *lineEnd;
int val_begin_len;
while ((lineEnd = set_var_val_lineEnd2(content, &var, &val_begin, &val_end)) != NULL) {
if (strcasecmp(var, "addr") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->httpudp_addr = (char *)malloc(val_begin_len);
memset(p->httpudp_addr, 0, val_begin_len);
memcpy(p->httpudp_addr, val_begin, val_begin_len);
} else if (strcasecmp(var, "http_req") == 0) {
val_begin_len = val_end - val_begin;
p->httpudp_http_req = (char *)malloc(val_begin_len + 1);
memset(p->httpudp_http_req, 0, val_begin_len);
memcpy(p->httpudp_http_req, val_begin, val_begin_len);
p->httpudp_http_req_len = val_begin_len;
} else if (strcasecmp(var, "encode") == 0) {
while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) {
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)
{
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)
{
p->httpudp_encode = atoi(val_begin);
}

2
conf.h
View File

@ -33,12 +33,14 @@ typedef struct CONF {
// httpdns module
char *addr;
char *http_req;
int addr_len;
int http_req_len;
int encode;
// httpudp module
char *httpudp_addr;
char *httpudp_http_req;
int httpudp_addr_len;
int httpudp_http_req_len;
int httpudp_encode;
} conf;

BIN
conf.o

Binary file not shown.

BIN
help.o

Binary file not shown.

View File

@ -16,7 +16,6 @@ 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].timer >= timeout_minute) {
@ -168,7 +167,7 @@ int check_ipversion(char *address)
int create_connection6(char *remote_host, int remote_port)
{
struct addrinfo hints;
//struct addrinfo *res = NULL;
struct addrinfo *res = NULL;
int sock;
int validfamily = 0;
char portstr[CACHE_SIZE];
@ -187,7 +186,7 @@ int create_connection6(char *remote_host, int remote_port)
hints.ai_flags |= AI_NUMERICHOST; // remote_host是有效的数字ip跳过解析
}
/*
//getaddrinfo方法
// 检查指定的主机是否有效。 如果remote_host是主机名尝试解析地址
if (getaddrinfo(remote_host, portstr, &hints, &res) != 0) {
@ -209,8 +208,9 @@ int create_connection6(char *remote_host, int remote_port)
if (res != NULL)
freeaddrinfo(res);
*/
/*
//通用sockaddr_storage结构体
struct sockaddr_storage remote_addr;
memset(&remote_addr, 0, sizeof(struct sockaddr_storage));
@ -238,7 +238,7 @@ int create_connection6(char *remote_host, int remote_port)
perror("connect");
return -1;
}
*/
/*
//普通方法
@ -310,6 +310,7 @@ static int8_t copy_data(conn_t * ct)
void tcp_in(conn_t * in, conf * configure)
{
char *headerEnd;
conn_t *server;
if (in->fd < 0)
@ -324,16 +325,27 @@ void tcp_in(conn_t * in, conf * configure)
in->timer = (in + 1)->timer = 0;
in->incomplete_data = read_data(in, in->incomplete_data, &in->incomplete_data_len);
//printf("%s", in->incomplete_data);
if (in->incomplete_data == NULL) {
close_connection(in);
return;
}
server = in + 1;
server->request_type = in->request_type = request_type(in->incomplete_data);
if (request_type(in->incomplete_data) == HTTP_TYPE) {
if (in->request_type == OTHER_TYPE)
{
//如果是第一次读取数据并且不是HTTP请求的关闭连接。复制数据失败的也关闭连接
if (in->reread_data == 0 || copy_data(in) != 0)
{
close_connection(in);
return;
}
goto handle_data_complete;
}
headerEnd = strstr(in->incomplete_data, "\n\r");
//请求头不完整,等待下次读取
if (headerEnd == NULL)
return;
if (in->request_type == HTTP_TYPE) {
in->incomplete_data = request_head(in, configure);
server->fd = create_connection6(remote_host, remote_port);
@ -343,12 +355,18 @@ void tcp_in(conn_t * in, conf * configure)
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);
return;
}
// 数据处理完毕,可以发送
handle_data_complete:
// 这个判断是防止 多次读取客户端数据,但是没有和服务端建立连接,导致报错
if (server->fd >= 0)
tcp_out(server);
@ -366,6 +384,7 @@ 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) {
//服务端的数据可能没全部写入到客户端

Binary file not shown.

View File

@ -136,56 +136,14 @@ static char *regrep(char *str, int *str_len, const char *src, char *dest, int de
return str;
}
// 删除字符串head中第一位到 character 处并拼接 string, character 为空返回原字符串.(string 字符替换第一个字符到 character 处)
char *splice_head(char *head, const char *character, char *string)
{
int first_len = strlen(string);
char *_p1 = strstr(head, character);
if (_p1 == NULL) {
return head;
}
_p1 = _p1 + 1;
char temporary[first_len + strlen(_p1) + 1];
memset(temporary, 0, (first_len + strlen(_p1) + 1));
strcpy(temporary, string);
strcat(temporary, _p1);
memset(head, 0, strlen(head));
return strcpy(head, temporary);
}
// 删除字符串 head 中 character 到 string 处, character 为空返回原字符串.
char *delete_head(char *head, const char *character, int string)
{
int head_len = strlen(head);
char *_p1, *_p2;
_p1 = _p2 = NULL;
char temporary[head_len];
memset(temporary, 0, head_len);
_p1 = strstr(head, character);
if (_p1 == NULL) {
return head;
}
_p2 = strchr(_p1, string);
if (_p2 == NULL) {
return head;
}
memcpy(temporary, head, _p1 - head - 1);
strcat(temporary, _p2);
memset(head, 0, strlen(head));
//printf("%s\n", temporary);
return memcpy(head, temporary, strlen(temporary));
}
int extract_host(char *header, char *host, char *port)
{
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, '[');
@ -208,63 +166,75 @@ int extract_host(char *header, char *host, char *port)
} else { // 如果_p2等于空就返回-1
return -1;
}
return 0;
} else {
char *p = strstr(header, "Host:");
char *p0 = strstr(header, "host:");
if (!p && !p0) { // 都为空的时候返回 -1
char *_p = strstr(header, "Host:");
if (_p == NULL) {
_p = strstr(header, "host:");
}
if (_p == NULL) { // 都为空时
return -1;
}
char *p1 = strchr(p, '\n');
if (!p1) {
char *_p1 = strchr(_p, '\n'); // 指向末尾'\n'
if (!_p1) {
return -1;
}
char *p2 = strchr(p + 5, ':'); // 5是指'Host:'的长度
int h_len = (int)(p1 - p - 6);
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);
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, ':');
char *_p3 = strchr(s_host, ':');
char *_p4 = NULL;
if (_p3)
_p4 = strchr(_p3 + 1, ':');
{ // IPV6
if (p4 != NULL) {
char *p5 = NULL;
char *p6 = NULL;
p5 = strchr(header, ' ');
if (p5)
p6 = strchr(p5 + 1, ' ');
if (_p4 != NULL) {
char *_p5 = NULL;
char *_p6 = NULL;
_p5 = strchr(header, ' ');
if (_p5)
_p6 = strchr(_p5 + 1, ' ');
char url[p6 - p5 - 1];
memset(url, 0, p6 - p5 - 1);
strncpy(url, p5 + 1, p6 - p5 - 1);
url[p6 - p5 - 1] = '\0';
char url[_p6 - _p5 - 1];
memset(url, 0, _p6 - _p5 - 1);
strncpy(url, _p5 + 1, _p6 - _p5 - 1);
url[_p6 - _p5 - 1] = '\0';
if (strstr(url, "http") != NULL) { // 去除 'http://'
memcpy(url, url + 7, strlen(url) - 7);
url[strlen(url) - 7] = '\0';
char *p7 = strchr(url, '/');
if (p7) // 去除 uri
url[p7 - url] = '\0';
memcpy(url, url + 7, (_p6 - _p5 - 1) - 7);
url[(_p6 - _p5 - 1) - 7] = '\0';
char *_p7 = strchr(url, '/');
if (_p7) // 去除 uri
url[_p7 - url] = '\0';
char *p8 = strchr(url, ']');
if (p8) {
strcpy(port, p8 + 2);
strncpy(host, url + 1, strlen(url) - strlen(p8) - 1);
char *_p8 = strchr(url, ']');
char *_p9 = strchr(url, '\0');
if (_p8) {
strcpy(port, _p8 + 2);
strncpy(host, url + 1, _p8 - (url+1));
if (strlen(p8) < 3) {
if ((_p9-_p8) == 1) {
strcpy(port, "80");
strncpy(host, url + 1, strlen(url) - strlen(p8) - 1);
strncpy(host, url + 1, _p8 - (url+1));
}
}
return 0;
} else { // HTTP头为不规范的url时处理Host, 主要Proxifier转发url为'/'时
//printf("s_host: %s\n", s_host);
char *_p1 = strchr(s_host, '[');
if (_p1 == NULL) // 涉及到自定义的Host, 不带'['、']'时, 默认截取最后为端口
{
char *_p2 = strrchr(s_host, ':');
remote_port = atoi(_p2+1);
strncpy(remote_host, s_host, _p2-s_host);
return 0;
}
char *_p2 = strchr(_p1 + 1, ']');
if (_p1 && _p2) {
memcpy(host, _p1 + 1, _p2 - _p1 - 1);
@ -281,21 +251,21 @@ int extract_host(char *header, char *host, char *port)
return -1;
}
}
if (p2 && p2 < p1) {
memcpy(port, p2 + 1, (int)(p1 - p2 - 1));
memcpy(host, p + 5 + 1, (int)(p2 - p - 5 - 1));
} else {
if (0 < (int)(p1 - p - 5 - 1 - 1)) {
memcpy(host, p + 5 + 1, (p1 - p - 5 - 1 - 1));
// HTTP 非 CONNECT 方法
{
if (_p2 && _p2 < _p1) { // 带端口, p2 指向':' p1 指向末尾'\n'
memcpy(port, _p2 + 1, (int)(_p1 - _p2 - 1));
memcpy(host, _p + 5 + 1, (int)(_p2 - _p - 5 - 1));
} else { // 不带端口
if (0 < (int)(_p1 - _p - 5 - 1 - 1)) {
memcpy(host, _p + 5 + 1, (_p1 - _p - 5 - 1 - 1));
memcpy(port, "80", 2);
} else {
memcpy(host, p + 5 + 1, (strlen(p) - strlen(p1) - 6));
memcpy(host, _p + 5 + 1, (_p1 - _p) - 6);
memcpy(port, "80", 2);
}
}
//printf("%s\n", host);
//printf("%s\n", port);
}
return 0;
}
@ -326,26 +296,6 @@ char *get_http_path(char *url, char *path)
return NULL;
}
void free_http_request(struct http_request *http_request)
{
if (http_request->method)
free(http_request->method);
if (http_request->U)
free(http_request->U);
if (http_request->version)
free(http_request->version);
if (http_request->host)
free(http_request->host);
if (http_request->port)
free(http_request->port);
if (http_request->H)
free(http_request->H);
if (http_request->url)
free(http_request->url);
if (http_request->uri)
free(http_request->uri);
}
void get_http_host_port_len(char *head, int *host_len, int *port_len)
{
*host_len = 0;
@ -382,14 +332,34 @@ void get_http_host_port_len(char *head, int *host_len, int *port_len)
*port_len = *host_len;
}
} else { // 未找到时使用HTTP_HEAD_CACHE_SIZE大小
*host_len = HTTP_HEAD_CACHE_SIZE;
*port_len = HTTP_HEAD_CACHE_SIZE;
*host_len = HTTP_HEAD_HOST_CACHE_SIZE;
*port_len = HTTP_HEAD_HOST_CACHE_SIZE/10;
}
}
return;
}
void free_http_request(struct http_request *http_request)
{
if (http_request->method)
free(http_request->method);
if (http_request->U)
free(http_request->U);
if (http_request->version)
free(http_request->version);
if (http_request->host)
free(http_request->host);
if (http_request->port)
free(http_request->port);
if (http_request->H)
free(http_request->H);
if (http_request->url)
free(http_request->url);
if (http_request->uri)
free(http_request->uri);
}
void parse_request_head(char *http_request_line, struct http_request *http_request)
{
char *p, *head, *m, *u;
@ -402,7 +372,7 @@ void parse_request_head(char *http_request_line, struct http_request *http_reque
return;
}
head_len = strlen(http_request_line) - strlen(p);
head_len = p - http_request_line;
head = (char *)malloc(sizeof(char) * head_len + 1);
if (head == NULL)
free(head);
@ -426,12 +396,13 @@ void parse_request_head(char *http_request_line, struct http_request *http_reque
memset(http_request->version, 0, 10);
m = strchr(head, ' ');
http_request->method_len = strlen(head) - strlen(m);
memcpy(http_request->method, head, http_request->method_len);
http_request->method_len = m - head;
memmove(http_request->method, head, http_request->method_len);
u = strchr(m + 1, ' ');
http_request->U_len = strlen(m + 1) - strlen(u);
memcpy(http_request->U, m + 1, http_request->U_len);
memcpy(http_request->version, u + 1, 8);
memmove(http_request->U, m + 1, u - (m+1));
memmove(http_request->version, u + 1, 8);
http_request->version_len = 8;
http_request->U_len = (int)strlen(http_request->U);
@ -490,6 +461,16 @@ void parse_request_head(char *http_request_line, struct http_request *http_reque
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);
/*
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);
*/
free(head);
return;
@ -497,7 +478,7 @@ void parse_request_head(char *http_request_line, struct http_request *http_reque
char *conf_handle(char *str, int str_len, tcp *p)
{
//print_tcp(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);
@ -507,59 +488,124 @@ char *conf_handle(char *str, int str_len, tcp *p)
str = regrep(str, &str_len, p->regrep_s, p->regrep_t, p->regrep_t_len);
}
p = p->next;
}
return str;
}
static char *splice_head(char *head, const char *needle, char *string)
{
char *tail_head;
char *_p0;
char *_p1;
_p1 = strstr(head, needle);
if (_p1 == NULL) {
return head;
}
_p1 = _p1 + 1;
_p0 = strchr(_p1, '\0');
tail_head = (char *)alloca((_p0 - _p1) + 1);
if (tail_head == NULL) {
perror("alloca");
return head;
}
memset(tail_head, 0, (_p0 - _p1) + 1);
strcpy(tail_head, _p1);
memset(head, 0, strlen(head));
strcpy(head, string);
strcat(head, tail_head);
return head;
}
static char *delete_head(char *head, const char *needle, int string)
{
char *temp_stack;
char *_p1, *_p2, *_p3;
_p1 = _p2 = _p3 = NULL;
int temp_stack_len;
_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'
if (_p2 == NULL) {
//perror("_p2 HEAD NULL");
return head;
}
_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处
if (temp_stack == NULL) {
perror("alloca");
return head;
}
memset(temp_stack, 0, temp_stack_len + 1);
memmove(temp_stack, head, (_p1 - head) - 1);
strcat(temp_stack, _p2);
memset(head, 0, strlen(head));
return memmove(head, temp_stack, temp_stack_len);
}
char *request_head(conn_t * in, conf * configure)
{
char *result = NULL;
char *delim = ",";
char *saveptr = NULL;
char *incomplete_head = NULL;
int incomplete_head_len = 0;
int return_val = 0;
struct http_request *http_request;
http_request = (struct http_request *)malloc(sizeof(struct http_request));
memset(http_request, 0, sizeof(struct http_request));
parse_request_head(in->incomplete_data, http_request);
if (strncmp(in->incomplete_data, "CONNECT", 7) == 0) {
char *incomplete_head;
int incomplete_head_len;
char https_del_copy[configure->https_del_len * 2];
char *result = NULL;
if ((return_val = strncmp(in->incomplete_data, "CONNECT", 7)) == 0)
{
char https_del_copy[configure->https_del_len+1];
memset(remote_host, 0, CACHE_SIZE);
if (configure->https_port > 0)
if (configure->https_port > 0) {
remote_port = configure->https_port;
if (configure->https_ip != NULL)
}
if (configure->https_ip != NULL) {
strcpy(remote_host, configure->https_ip);
}
incomplete_head = (char *)malloc(sizeof(char) * (BUFFER_SIZE));
if (incomplete_head == NULL) {
free(incomplete_head);
perror("malloc");
}
memset(incomplete_head, 0, sizeof(char) * (BUFFER_SIZE));
memcpy(incomplete_head, in->incomplete_data, strlen(in->incomplete_data));
memcpy(https_del_copy, configure->https_del, configure->https_del_len);
memmove(incomplete_head, in->incomplete_data, in->incomplete_data_len);
memmove(https_del_copy, configure->https_del, configure->https_del_len+1);
result = strtok(https_del_copy, ",");
result = strtok_r(https_del_copy, delim, &saveptr);
while (result != NULL) {
delete_head(incomplete_head, result, '\n');
result = strtok(NULL, ",");
result = strtok_r(NULL, delim, &saveptr);
}
splice_head(incomplete_head, "\n", configure->https_first);
incomplete_head_len = strlen(incomplete_head);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\r", 2, "\r", 1);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\n", 2, "\n", 1);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\b", 2, "\b", 1);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\v", 2, "\v", 1);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\f", 2, "\f", 1);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\a", 2, "\a", 1);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\t", 2, "\t", 1);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\r", 2, "\r", 1);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\n", 2, "\n", 1);
incomplete_head = conf_handle(incomplete_head, incomplete_head_len, https_head);
incomplete_head_len = strlen(incomplete_head);
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);
@ -568,26 +614,29 @@ 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 = conf_handle(incomplete_head, incomplete_head_len, https_head);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[H]", 3, http_request->H, http_request->H_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[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_len = strlen(incomplete_head); // 更新HTTPS HEADER长度
//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);
if (new_incomplete_data == NULL) {
free(in->incomplete_data);
perror("realloc");
return in->incomplete_data;
}
in->incomplete_data = new_incomplete_data;
memset(in->incomplete_data, 0, incomplete_head_len + 1); // 清空incomplete_data数据
strcpy(in->incomplete_data, incomplete_head); // 更新incomplete_data数据
in->incomplete_data_len = strlen(in->incomplete_data); // 更新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内存
}
if (strncmp(in->incomplete_data, "GET", 3) == 0 || strncmp(in->incomplete_data, "POST", 4) == 0) {
char *incomplete_head;
int incomplete_head_len;
char http_del_copy[configure->http_del_len];
char *result = NULL;
if (strncmp(in->incomplete_data, "GET", 3) == 0 || strncmp(in->incomplete_data, "POST", 4) == 0)
{
char http_del_copy[configure->http_del_len + 1];
memset(remote_host, 0, CACHE_SIZE);
if (configure->http_port > 0)
@ -601,25 +650,19 @@ char *request_head(conn_t * in, conf * configure)
}
memset(incomplete_head, 0, sizeof(char) * (BUFFER_SIZE));
memcpy(incomplete_head, in->incomplete_data, strlen(in->incomplete_data));
memcpy(http_del_copy, configure->http_del, configure->http_del_len);
memmove(incomplete_head, in->incomplete_data, in->incomplete_data_len);
memmove(http_del_copy, configure->http_del, configure->http_del_len+1);
result = strtok(http_del_copy, ",");
result = strtok_r(http_del_copy, delim, &saveptr);
while (result != NULL) {
delete_head(incomplete_head, result, '\n');
result = strtok(NULL, ",");
result = strtok_r(NULL, delim, &saveptr);
}
splice_head(incomplete_head, "\n", configure->http_first);
incomplete_head_len = strlen(incomplete_head);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\r", 2, "\r", 1);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\n", 2, "\n", 1);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\b", 2, "\b", 1);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\v", 2, "\v", 1);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\f", 2, "\f", 1);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\a", 2, "\a", 1);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\t", 2, "\t", 1);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\r", 2, "\r", 1);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\n", 2, "\n", 1);
incomplete_head_len = strlen(incomplete_head); // 更新HTTP HEADER长度
incomplete_head = conf_handle(incomplete_head, incomplete_head_len, http_head);
incomplete_head_len = strlen(incomplete_head); // 更新HTTP 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);
@ -627,21 +670,24 @@ char *request_head(conn_t * in, conf * configure)
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[version]", 9, http_request->version, http_request->version_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[url]", 5, http_request->url, http_request->url_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[uri]", 5, http_request->uri, http_request->uri_len);
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 = conf_handle(incomplete_head, incomplete_head_len, http_head);
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
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) {
free(in->incomplete_data);
perror("realloc");
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 + 1); // 更新incomplete_data数据
in->incomplete_data_len = strlen(in->incomplete_data); // 更新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内存
}

View File

@ -8,6 +8,8 @@
#include "http_proxy.h"
#include "conf.h"
#define HTTP_HEAD_HOST_CACHE_SIZE 1024
struct http_request {
char *method, *U, *version;
char *host, *port, *H;

Binary file not shown.

BIN
httpdns.o

Binary file not shown.

BIN
httpudp.o

Binary file not shown.

Binary file not shown.

Binary file not shown.

23
main.c
View File

@ -223,10 +223,10 @@ void *start_server(conf * configure)
int process_signal(int signal, char *process_name)
{
char bufer[PATH_SIZE];
char comm[PATH_SIZE];
char proc_comm_name[PATH_SIZE];
//int number[PATH_SIZE] = { 0 };
char bufer[CACHE_SIZE];
char comm[CACHE_SIZE];
char proc_comm_name[CACHE_SIZE];
//int number[CACHE_SIZE] = { 0 };
//int n = 0;
FILE *fp;
DIR *dir;
@ -247,7 +247,7 @@ int process_signal(int signal, char *process_name)
sprintf(comm, "/proc/%s/comm", ptr->d_name);
if (access(comm, F_OK) == 0) {
fp = fopen(comm, "r");
if (fgets(bufer, PATH_SIZE - 1, fp) == NULL) {
if (fgets(bufer, CACHE_SIZE - 1, fp) == NULL) {
fclose(fp);
continue;
}
@ -446,8 +446,8 @@ void thread_loop(conf * configure)
void _main(int argc, char *argv[])
{
int opt;
char path[PATH_SIZE] = { 0 };
char executable_filename[PATH_SIZE] = { 0 };
char path[CACHE_SIZE] = { 0 };
char executable_filename[CACHE_SIZE] = { 0 };
(void)get_executable_path(path, executable_filename, sizeof(path));
char *inifile = "/CProxy.conf";
struct rlimit rt;
@ -455,11 +455,14 @@ void _main(int argc, char *argv[])
conf *configure = (struct CONF *)malloc(sizeof(struct CONF));
memset(configure, 0, sizeof(struct CONF));
read_conf(inifile, configure);
/*
printf("%s", configure->http_first);
printf("%s\n", configure->http_del);
printf("%s", configure->https_first);
printf("%s\n", configure->https_del);
print_tcp(https_head);
free_tcp(&https_head);
//print_tcp(http_head);
print_tcp(http_head);
free_tcp(&http_head);
free_conf(configure);
free(configure);
@ -553,7 +556,7 @@ void _main(int argc, char *argv[])
server_ini(); // 守护进程
//server_ini(); // 守护进程
rt.rlim_max = rt.rlim_cur = MAX_CONNECTION * 2; // 设置每个进程允许打开的最大文件数
if (setrlimit(RLIMIT_NOFILE, &rt) == -1)

2
main.h
View File

@ -25,9 +25,7 @@
#define MAX_CONNECTION 1020
#define BUFFER_SIZE 8192
#define PATH_SIZE 270
#define CACHE_SIZE 270
#define HTTP_HEAD_CACHE_SIZE 1024
#define ERRDEBUG fprintf(stderr,"Error Occured at File: %s, Function: %s, Line: %d, Date: %s, Time: %s.\n", __FILE__, __FUNCTION__, __LINE__, __DATE__, __TIME__);

BIN
main.o

Binary file not shown.

Binary file not shown.

View File

@ -1,3 +1,10 @@
./obj/local/arm64-v8a/objs/CProxy/conf.o: conf.c conf.h
./obj/local/arm64-v8a/objs/CProxy/conf.o: conf.c conf.h http_request.h \
http_proxy.h main.h
conf.h:
http_request.h:
http_proxy.h:
main.h:

Binary file not shown.

View File

@ -1,3 +1,10 @@
./obj/local/armeabi-v7a/objs/CProxy/conf.o: conf.c conf.h
./obj/local/armeabi-v7a/objs/CProxy/conf.o: conf.c conf.h http_request.h \
http_proxy.h main.h
conf.h:
http_request.h:
http_proxy.h:
main.h: