From 9ba3f7dcf1c121f0508916a174e8b29facc4fcf7 Mon Sep 17 00:00:00 2001 From: aixiao Date: Wed, 26 Aug 2020 20:40:35 +0800 Subject: [PATCH] optimization --- CProxy.conf | 6 ++--- http_proxy.c | 65 ++++++++++++++++++++++++++++++++++++++++---------- http_request.c | 8 +++---- 3 files changed, 60 insertions(+), 19 deletions(-) diff --git a/CProxy.conf b/CProxy.conf index 5a99e8a..74cfc63 100644 --- a/CProxy.conf +++ b/CProxy.conf @@ -8,16 +8,16 @@ global { } http { - http_ip=47.240.75.93; + http_ip=8.210.203.112; http_port=124; - http_del="x-online-host,X-Online-Host,host,Host"; + http_del="Host"; http_first="[M] http://[host][U] [V]\r\nHost: [host]\r\n"; //strrep="Windows NT 10.0->Linux"; //regrep="Host:*.+?->Host: [host]:80"; } https { - https_ip=47.240.75.93; + https_ip=8.210.203.112; https_port=124; https_del="Host,host,x-online-host"; https_first="[M] [U] [V]\r\nHost: [host]\r\n"; diff --git a/http_proxy.c b/http_proxy.c index c9aa99a..514d6a5 100644 --- a/http_proxy.c +++ b/http_proxy.c @@ -91,10 +91,42 @@ static void serverToClient(conn * server) } + +void clientToserver(conn * in) +{ + int write_len; + conn *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; + } else { + close_connection(remote); + } + + return; + +} + // 判断请求类型 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; } @@ -111,27 +143,36 @@ void tcp_in(conn * in, conf * configure) serverToClient(in); return; } - remote = in + 1; + in->timer = (in + 1)->timer = 0; in->header_buffer = read_data(in, in->header_buffer, &in->header_buffer_len); if (in->header_buffer == NULL) { close_connection(in); return; - } else if (in->header_buffer != NULL) { - if (request_type(in->header_buffer) == HTTP_TYPE) { - in->header_buffer = request_head(in, configure); - struct epoll_event epollEvent; - remote->fd = create_connection(remote_host, remote_port); - epollEvent.events = EPOLLIN | EPOLLOUT | EPOLLET; - epollEvent.data.ptr = remote; - epoll_ctl(epollfd, EPOLL_CTL_ADD, remote->fd, &epollEvent); - } + } + + remote = in + 1; + if (in->request_type == OTHER_TYPE) + { + 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_connection(remote_host, remote_port); + epollEvent.events = EPOLLIN | EPOLLOUT | EPOLLET; + epollEvent.data.ptr = remote; + epoll_ctl(epollfd, EPOLL_CTL_ADD, remote->fd, &epollEvent); } dataEncode(in->header_buffer, in->header_buffer_len); - if (remote->fd >= 0) + handle_data_complete: + if (remote->fd >= 0) { + //clientToserver(in); tcp_out(remote); + } return; } diff --git a/http_request.c b/http_request.c index e89203c..c348669 100644 --- a/http_request.c +++ b/http_request.c @@ -367,10 +367,10 @@ char *request_head(conn * in, conf * configure) in->header_buffer_len = strlen(in->header_buffer); free(incomplete_head); - } else if (strncmp(in->header_buffer, "GET", 3) == 0) { + } else if (strncmp(in->header_buffer, "GET", 3) == 0 || strncmp(in->header_buffer, "POST", 4) == 0) { char *incomplete_head; int incomplete_head_len; - char https_del_copy[configure->http_del_len]; + char http_del_copy[configure->http_del_len]; char *result = NULL; if (configure->http_port > 0) @@ -384,9 +384,9 @@ char *request_head(conn * in, conf * configure) } memset(incomplete_head, 0, sizeof(char) * (BUFFER_SIZE)); memcpy(incomplete_head, in->header_buffer, strlen(in->header_buffer)); - memcpy(https_del_copy, configure->https_del, configure->https_del_len); + memcpy(http_del_copy, configure->http_del, configure->http_del_len); - result = strtok(https_del_copy, ","); + result = strtok(http_del_copy, ","); while (result != NULL) { delete_head(incomplete_head, result, '\n'); result = strtok(NULL, ",");