optimization

This commit is contained in:
aixiao 2020-11-26 17:40:17 +08:00
parent 843e5c80b5
commit 3c0d42da76
7 changed files with 275 additions and 255 deletions

View File

@ -9,7 +9,7 @@ global {
}
http {
http_ip="2001:19f0:7001:3bcb:5400:3ff:fe03:860e";
http_ip="47.240.75.93";
http_port=127;
http_del="Host";
http_first="[M] http://[host][U] [V]\r\nHost: [host]\r\n";
@ -18,7 +18,7 @@ http {
}
https {
https_ip="2001:19f0:7001:3bcb:5400:3ff:fe03:860e";
https_ip="47.240.75.93";
https_port=127;
https_del="Host,host,x-online-host";
https_first="[M] [U] [V]\r\nHost: [host]\r\n";

View File

@ -1,7 +1,7 @@
CROSS_COMPILE ?=
CC := $(CROSS_COMPILE)gcc
STRIP := $(CROSS_COMPILE)strip
CFLAGS += -g -O2 -Wall -pthread
CFLAGS += -g -O2 -Wall -pthread -static
LIBS =
OBJ := CProxy

View File

@ -13,7 +13,7 @@ void dataEncode(char *data, int data_len)
data[data_len] ^= sslEncodeCode;
}
static char *read_data(conn * in, char *data, int *data_len)
static char *read_data(conn_t * in, char *data, int *data_len)
{
char *new_data;
int read_len;
@ -41,69 +41,78 @@ static char *read_data(conn * in, char *data, int *data_len)
return data;
}
void close_connection(conn * conn)
void close_connection(conn_t *conn)
{
epoll_ctl(epollfd, EPOLL_CTL_DEL, conn->fd, NULL);
close(conn->fd);
if ((conn - cts) & 1) {
if ((conn - cts) & 1)
{
char *server_data;
server_data = conn->header_buffer;
memset(conn, 0, sizeof(*conn));
conn->header_buffer = server_data;
conn--->fd = -1;
} else {
free(conn->header_buffer);
memset(conn, 0, sizeof(*conn));
conn++->fd = -1;
}
server_data = conn->ready_data;
memset(conn, 0, sizeof(conn_t));
conn->ready_data = server_data;
conn-- ->fd = -1;
}
else
{
free(conn->ready_data);
free(conn->incomplete_data);
memset(conn, 0, sizeof(conn_t));
conn++ ->fd = -1;
}
if (conn->fd >= 0)
close_connection(conn);
}
static void serverToClient(conn * server)
{
int write_len;
conn *client;
client = server - 1;
while ((server->header_buffer_len = read(server->fd, server->header_buffer, BUFFER_SIZE)) > 0) {
dataEncode(server->header_buffer, server->header_buffer_len);
write_len = write(client->fd, server->header_buffer, server->header_buffer_len);
if (write_len <= 0) {
static void serverToClient(conn_t *server)
{
conn_t *client;
int write_len;
client = server - 1;
while ((server->ready_data_len = read(server->fd, server->ready_data, BUFFER_SIZE)) > 0)
{
dataEncode(server->ready_data, server->ready_data_len);
write_len = write(client->fd, server->ready_data, server->ready_data_len);
if (write_len <= 0)
{
if (write_len == 0 || errno != EAGAIN)
close_connection(server);
else
server->sent_len = 0;
return;
} else if (write_len < server->header_buffer_len) {
}
else if (write_len < server->ready_data_len)
{
server->sent_len = write_len;
ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
ev.events = EPOLLIN|EPOLLOUT|EPOLLET;
ev.data.ptr = client;
epoll_ctl(epollfd, EPOLL_CTL_MOD, client->fd, &ev);
return;
}
if (server->header_buffer_len < BUFFER_SIZE)
if (server->ready_data_len < BUFFER_SIZE)
break;
}
if (server->header_buffer_len == 0 || (server->header_buffer_len == -1 && errno != EAGAIN))
//判断是否关闭连接
if (server->ready_data_len == 0 || (server->ready_data_len == -1 && errno != EAGAIN))
close_connection(server);
else
server->header_buffer_len = server->sent_len = 0;
server->ready_data_len = server->sent_len = 0;
}
void clientToserver(conn * in)
void clientToserver(conn_t * in)
{
int write_len;
conn *remote;
conn_t *remote;
remote = in + 1;
write_len = write(remote->fd, in->header_buffer, in->header_buffer_len);
if (write_len == in->header_buffer_len) {
in->header_buffer_len = 0;
in->header_buffer = NULL;
write_len = write(remote->fd, in->ready_data, in->ready_data_len);
if (write_len == in->ready_data_len) {
in->ready_data_len = 0;
in->ready_data = NULL;
} else {
close_connection(remote);
}
@ -133,88 +142,187 @@ static int8_t request_type(char *data)
return OTHER_TYPE;
}
void tcp_in(conn * in, conf * configure)
// Check for valid IPv4 or Iv6 string. Returns AF_INET for IPv4, AF_INET6 for IPv6
int check_ipversion(char * address)
{
conn *remote;
struct in6_addr bindaddr;
if (inet_pton(AF_INET, address, &bindaddr) == 1) {
return AF_INET;
} else {
if (inet_pton(AF_INET6, address, &bindaddr) == 1) {
return AF_INET6;
}
}
return 0;
}
int create_connection6(char *remote_host, int remote_port) {
struct addrinfo hints, *res=NULL;
int sock;
int validfamily=0;
char portstr[270];
memset(&hints, 0x00, sizeof(hints));
hints.ai_flags = AI_NUMERICSERV; // numeric service number, not resolve
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
sprintf(portstr, "%d", 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跳过解析
}
// 检查指定的主机是否有效。 如果remote_host是主机名尝试解析地址
if (getaddrinfo(remote_host, portstr , &hints, &res) != 0) {
errno = EFAULT;
perror("getaddrinfo");
return -1;
}
if ((sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
perror("socket");
return -1;
}
if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
perror("connect");
return -1;
}
if (res != NULL)
freeaddrinfo(res);
return sock;
}
/* 读取到的数据全部就绪将incomplete_data复制到ready_data */
static int8_t copy_data(conn_t *ct)
{
dataEncode(ct->incomplete_data, ct->incomplete_data_len);
if (ct->ready_data)
{
char *new_data;
new_data = (char *)realloc(ct->ready_data, ct->ready_data_len + ct->incomplete_data_len);
if (new_data == NULL)
return 1;
ct->ready_data = new_data;
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
{
ct->ready_data = ct->incomplete_data;
ct->ready_data_len = ct->incomplete_data_len;
}
ct->incomplete_data = NULL;
ct->incomplete_data_len = 0;
return 0;
}
void tcp_in(conn_t * in, conf * configure)
{
conn_t *server;
if (in->fd < 0)
return;
// 如果in - cts是奇数,那么是服务端触发事件
if ((in - cts) & 1) {
in->timer = (in - 1)->timer = 0;
//如果in - cts是奇数那么是服务端触发事件
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->header_buffer = read_data(in, in->header_buffer, &in->header_buffer_len);
if (in->header_buffer == NULL) {
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;
}
remote = in + 1;
if (in->request_type == OTHER_TYPE)
server = in + 1;
server->request_type = in->request_type = request_type(in->incomplete_data);
if (request_type(in->incomplete_data) == HTTP_TYPE) {
in->incomplete_data = request_head(in, configure);
server->fd = create_connection6(remote_host, remote_port);
if (server->fd < 0)
printf("remote->fd ERROR!\n");
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)
{
goto handle_data_complete;
}
if (request_type(in->header_buffer) == HTTP_TYPE) {
in->header_buffer = request_head(in, configure);
struct epoll_event epollEvent;
remote->fd = create_connection6(remote_host, remote_port);
epollEvent.events = EPOLLIN | EPOLLOUT | EPOLLET;
epollEvent.data.ptr = remote;
epoll_ctl(epollfd, EPOLL_CTL_ADD, remote->fd, &epollEvent);
}
//printf("%s", in->header_buffer);
dataEncode(in->header_buffer, in->header_buffer_len);
handle_data_complete:
if (remote->fd >= 0) {
clientToserver(in);
//tcp_out(remote);
}
close_connection(in);
return;
}
// 这个判断是防止 多次读取客户端数据,但是没有和服务端建立连接,导致报错
if (server->fd >= 0)
tcp_out(server);
}
void tcp_out(conn * out)
void tcp_out(conn_t *to)
{
conn *from;
conn_t *from;
int write_len;
if (out->fd == -1)
if (to->fd == -1)
return;
else if ((out - cts) & 1)
from = out - 1;
else if ((to - cts) & 1)
from = to - 1;
else
from = out + 1;
from->timer = out->timer = 0;
write_len = write(out->fd, from->header_buffer + from->sent_len, from->header_buffer_len - from->sent_len);
if (write_len == from->header_buffer_len - from->sent_len) {
// 服务端的数据可能没全部写入到客户端
if ((from - cts) & 1) {
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 ((from - cts) & 1)
{
serverToClient(from);
if (from->fd >= 0 && from->header_buffer == 0) {
ev.events = EPOLLIN | EPOLLET;
ev.data.ptr = out;
epoll_ctl(epollfd, EPOLL_CTL_MOD, out->fd, &ev);
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;
ev.data.ptr = out;
epoll_ctl(epollfd, EPOLL_CTL_MOD, out->fd, &ev);
free(from->header_buffer);
from->header_buffer = NULL;
from->header_buffer_len = 0;
}
} else if (write_len > 0) {
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)
{
from->sent_len += write_len;
ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
ev.data.ptr = out;
epoll_ctl(epollfd, EPOLL_CTL_MOD, out->fd, &ev);
} else if (errno != EAGAIN) {
close_connection(out);
ev.events = EPOLLIN|EPOLLOUT|EPOLLET;
ev.data.ptr = to;
epoll_ctl(epollfd, EPOLL_CTL_MOD, to->fd, &ev);
}
else if (errno != EAGAIN)
{
close_connection(to);
}
return;
}

View File

@ -3,6 +3,7 @@
#include "conf.h"
#include "main.h"
#include <arpa/inet.h>
#define HTTP_TYPE 0
#define OTHER_TYPE 1
@ -11,19 +12,22 @@ extern int remote_port;
extern char remote_host[270];
extern int sslEncodeCode;
typedef struct conn_t {
int fd;
char *header_buffer;
int header_buffer_len, sent_len, timer;
unsigned request_type :1;
} conn;
typedef struct tcp_connection {
char *ready_data, *incomplete_data;
int fd, ready_data_len, incomplete_data_len, sent_len, timer;
uint16_t destPort;
unsigned reread_data :1,
request_type :1,
keep_alive :1;
} conn_t;
extern conn cts[MAX_CONNECTION];
extern void tcp_in(conn * in, conf * configure);
extern void tcp_out(conn * out);
extern void close_connection(conn * conn);
extern char *request_head(conn * in, conf * configure);
extern conn_t cts[MAX_CONNECTION];
extern void tcp_in(conn_t * in, conf * configure);
extern void tcp_out(conn_t * out);
extern void close_connection(conn_t * conn);
extern char *request_head(conn_t * in, conf * configure);
void dataEncode(char *data, int data_len);
#endif

View File

@ -305,15 +305,15 @@ void parse_request_head(char *http_request_line, struct http_request *http_reque
return;
}
char *request_head(conn * in, conf * configure)
char *request_head(conn_t * in, conf * configure)
{
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->header_buffer, http_request);
parse_request_head(in->incomplete_data, http_request);
if (strncmp(in->header_buffer, "CONNECT", 7) == 0) {
if (strncmp(in->incomplete_data, "CONNECT", 7) == 0) {
char *incomplete_head;
int incomplete_head_len;
char https_del_copy[configure->https_del_len * 2];
@ -330,7 +330,7 @@ char *request_head(conn * in, conf * configure)
perror("malloc");
}
memset(incomplete_head, 0, sizeof(char) * (BUFFER_SIZE));
memcpy(incomplete_head, in->header_buffer, strlen(in->header_buffer));
memcpy(incomplete_head, in->incomplete_data, strlen(in->incomplete_data));
memcpy(https_del_copy, configure->https_del, configure->https_del_len);
result = strtok(https_del_copy, ",");
@ -361,11 +361,11 @@ char *request_head(conn * 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);
printf("%s", incomplete_head); // 打印HTTP HEADER
//printf("%s", incomplete_head); // 打印HTTP HEADER
memset(in->header_buffer, 0, strlen(in->header_buffer));
strcpy(in->header_buffer, incomplete_head);
in->header_buffer_len = strlen(in->header_buffer);
memset(in->incomplete_data, 0, strlen(in->incomplete_data));
strcpy(in->incomplete_data, incomplete_head);
in->incomplete_data_len = strlen(in->incomplete_data);
free(incomplete_head);
} else {
@ -386,7 +386,7 @@ char *request_head(conn * in, conf * configure)
perror("malloc");
}
memset(incomplete_head, 0, sizeof(char) * (BUFFER_SIZE));
memcpy(incomplete_head, in->header_buffer, strlen(in->header_buffer));
memcpy(incomplete_head, in->incomplete_data, strlen(in->incomplete_data));
memcpy(http_del_copy, configure->http_del, configure->http_del_len);
result = strtok(http_del_copy, ",");
@ -423,13 +423,13 @@ char *request_head(conn * in, conf * configure)
incomplete_head_len = strlen(incomplete_head);
//printf("%s", incomplete_head);
memset(in->header_buffer, 0, in->header_buffer_len);
memmove(in->header_buffer, incomplete_head, incomplete_head_len + 1);
in->header_buffer_len = strlen(in->header_buffer);
memset(in->incomplete_data, 0, in->incomplete_data_len);
memmove(in->incomplete_data, incomplete_head, incomplete_head_len + 1);
in->incomplete_data_len = strlen(in->incomplete_data);
free(incomplete_head);
}
free_http_request(http_request);
free(http_request);
return in->header_buffer;
return in->incomplete_data;
}

View File

@ -20,6 +20,6 @@ struct http_request {
void *memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen);
char *replace(char *replace_memory, int *replace_memory_len, const char *src, const int src_len, const char *dest, const int dest_len);
char *request_head(conn * in, conf * configure);
char *request_head(conn_t * in, conf * configure);
#endif

160
main.c
View File

@ -13,7 +13,7 @@
struct epoll_event ev, events[MAX_CONNECTION + 1];
int epollfd, server_sock, server_sock6;
conn cts[MAX_CONNECTION];
conn_t cts[MAX_CONNECTION];
int local_port;
char local_host[128];
int process;
@ -49,107 +49,6 @@ int create_connection(char *remote_host, int remote_port)
return sock;
}
int check_ipversion(char * address)
{
/* Check for valid IPv4 or Iv6 string. Returns AF_INET for IPv4, AF_INET6 for IPv6 */
struct in6_addr bindaddr;
if (inet_pton(AF_INET, address, &bindaddr) == 1) {
return AF_INET;
} else {
if (inet_pton(AF_INET6, address, &bindaddr) == 1) {
return AF_INET6;
}
}
return 0;
}
int create_connection6(char *remote_host, int remote_port) {
struct addrinfo hints, *res=NULL;
int sock;
int validfamily=0;
char portstr[12];
memset(&hints, 0x00, sizeof(hints));
hints.ai_flags = AI_NUMERICSERV; /* numeric service number, not resolve */
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
sprintf(portstr, "%d", 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请跳过解析 */
}
/* 检查指定的主机是否有效。 如果remote_host是主机名请尝试解析地址 */
if (getaddrinfo(remote_host, portstr , &hints, &res) != 0) {
errno = EFAULT;
return -1;
}
if ((sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
return -1;
}
if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
return -1;
}
if (res != NULL)
freeaddrinfo(res);
fcntl(sock, F_SETFL, O_NONBLOCK);
return sock;
}
/*
int create_connection6(char *remote_host, int remote_port)
{
char port[270];
int sock = -1;
struct addrinfo *result;
struct addrinfo hints;
bzero(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
memset(port, 0, 270);
sprintf(port, "%d", remote_port); // 转为字符串
if ((getaddrinfo(remote_host, port, &hints, &result)) != 0)
return -1;
//printf("%d\n", result->ai_addrlen);
switch (result->ai_family) {
case AF_INET:{
sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (connect(sock, result->ai_addr, result->ai_addrlen) < 0) {
perror("AF_INET connect");
close(sock);
return -1;
}
break;
}
case AF_INET6:{
sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (connect(sock, result->ai_addr, result->ai_addrlen) < 0) {
perror("AF_INET6 connect");
return -1;
}
break;
}
default:
printf("Unknown\n");
break;
}
freeaddrinfo(result);
fcntl(sock, F_SETFL, O_NONBLOCK);
return sock;
}
*/
int create_server_socket(int port)
{
int server_sock;
@ -220,7 +119,7 @@ void accept_client()
{
struct epoll_event epollEvent;
struct sockaddr_in addr;
conn *client;
conn_t *client;
socklen_t addr_len = sizeof(addr);
// 偶数为客户端,奇数为服务端
@ -243,7 +142,7 @@ void accept_client6()
{
struct epoll_event epollEvent;
struct sockaddr_in6 addr;
conn *client;
conn_t *client;
socklen_t addr_len = sizeof(addr);
// 偶数为客户端,奇数为服务端
@ -267,20 +166,38 @@ void *http_proxy_loop(void *p)
conf *configure = (conf *) p;
int n;
ev.events = EPOLLIN;
ev.data.fd = server_sock;
if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, server_sock, &ev)) {
exit(1);
}
ev.events = EPOLLIN;
ev.data.fd = server_sock6;
if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, server_sock6, &ev)) {
exit(1);
}
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) {
if (events[n].data.fd == server_sock6) {
//printf("accept_client6()!!!\n");
accept_client6();
;
}
else if (events[n].data.fd == server_sock) {
//printf("accept_client()!!!\n");
accept_client();
} else {
if (events[n].events & EPOLLIN) {
tcp_in((conn *) events[n].data.ptr, configure);
//printf("tcp_in()!!!\n");
tcp_in((conn_t *) events[n].data.ptr, configure);
}
if (events[n].events & EPOLLOUT) {
tcp_out((conn *) events[n].data.ptr);
//printf("tcp_out()!!!\n");
tcp_out((conn_t *) events[n].data.ptr);
}
}
}
@ -305,10 +222,10 @@ void *start_server(conf * configure)
accept_client6();
} else {
if (events[n].events & EPOLLIN) {
tcp_in((conn *) events[n].data.ptr, configure);
tcp_in((conn_t *) events[n].data.ptr, configure);
}
if (events[n].events & EPOLLOUT) {
tcp_out((conn *) events[n].data.ptr);
tcp_out((conn_t *) events[n].data.ptr);
}
}
}
@ -383,10 +300,12 @@ int get_executable_path(char *processdir, char *processname, int len)
void server_ini()
{
signal(SIGPIPE, SIG_IGN); // 忽略PIPE信号
if (daemon(1, 1)) {
perror("daemon");
return;
}
//while (process-- > 1 && fork() == 0);
}
@ -491,14 +410,14 @@ void _main(int argc, char *argv[])
}
server_ini(); // 守护进程
httpdns_initialize(configure); // 初始化http_dns
httpdns_initialize(configure); // 初始化httpdns
memset(cts, 0, sizeof(cts));
for (i = MAX_CONNECTION; i--;)
cts[i].fd = -1;
// 为服务端的结构体分配内存
for (i = 1; i < MAX_CONNECTION; i += 2) {
cts[i].header_buffer = (char *)malloc(BUFFER_SIZE);
if (cts[i].header_buffer == NULL) {
cts[i].ready_data = (char *)malloc(BUFFER_SIZE);
if (cts[i].ready_data == NULL) {
fputs("out of memory.", stderr);
exit(1);
}
@ -511,18 +430,7 @@ void _main(int argc, char *argv[])
perror("epoll_create");
exit(1);
}
static struct epoll_event event;
event.events = EPOLLIN;
event.data.fd = server_sock;
if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, server_sock, &event)) {
exit(1);
}
event.events = EPOLLIN;
event.data.fd = server_sock6;
if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, server_sock6, &event)) {
exit(1);
}
if (setegid(configure->uid) == -1 || seteuid(configure->uid) == -1) // 设置uid
exit(1);