optimization
This commit is contained in:
parent
9a2c98f059
commit
1e162d641b
10
CProxy.conf
10
CProxy.conf
@ -1,13 +1,14 @@
|
||||
global {
|
||||
uid=3004;
|
||||
timeout=60;
|
||||
process=2;
|
||||
tcp_listen=0124;
|
||||
dns_listen=0126;
|
||||
udp_listen = 10010;
|
||||
}
|
||||
|
||||
http {
|
||||
http_ip="aixiao.me";
|
||||
http_ip="43.142.66.71";
|
||||
http_port=129;
|
||||
http_del="Host,";
|
||||
http_first="[M] [U] [V]\r\nHost: [H]\r\n";
|
||||
@ -20,10 +21,15 @@ http {
|
||||
}
|
||||
|
||||
https {
|
||||
https_ip="aixiao.me";
|
||||
https_ip="43.142.66.71";
|
||||
https_port=129;
|
||||
https_del="Host,host,x-online-host,Proxy-Connection";
|
||||
https_first="[M] [U] [V]\r\nHost: [host]:[port]\r\n";
|
||||
strrep="Windows NT 10.0" -> "Linux";
|
||||
strrep="Linux" -> "aixiao.me";
|
||||
strrep="aixiao.me" -> "AIXIAO.ME";
|
||||
regrep="Accept-Encoding*.+?" -> "Accept-Encoding: GZIP, deflate";
|
||||
regrep="Connection*.+?" -> "Connection: KEEP-alive";
|
||||
encode=129;
|
||||
}
|
||||
|
||||
|
4
Makefile
4
Makefile
@ -1,8 +1,8 @@
|
||||
CROSS_COMPILE ?=
|
||||
CC := $(CROSS_COMPILE)gcc
|
||||
STRIP := $(CROSS_COMPILE)strip
|
||||
CFLAGS += -g -O2 -Wall -pthread
|
||||
LIBS = -static
|
||||
CFLAGS += -g -O2 -Wall -pthread
|
||||
LIBS =
|
||||
OBJ := CProxy
|
||||
|
||||
all: main.o http_proxy.o http_request.o httpdns.o httpudp.o conf.o
|
||||
|
37
http_proxy.c
37
http_proxy.c
@ -132,7 +132,20 @@ 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;
|
||||
}
|
||||
@ -156,7 +169,7 @@ int check_ipversion(char *address)
|
||||
int create_connection6(char *remote_host, int remote_port)
|
||||
{
|
||||
struct addrinfo hints;
|
||||
struct addrinfo *res = NULL;
|
||||
|
||||
int sock;
|
||||
int validfamily = 0;
|
||||
char portstr[CACHE_SIZE];
|
||||
@ -174,7 +187,10 @@ int create_connection6(char *remote_host, int remote_port)
|
||||
hints.ai_family = validfamily;
|
||||
hints.ai_flags |= AI_NUMERICHOST;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
struct addrinfo *res = NULL;
|
||||
if (getaddrinfo(remote_host, portstr, &hints, &res) != 0) {
|
||||
errno = EFAULT;
|
||||
perror("getaddrinfo");
|
||||
@ -193,8 +209,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));
|
||||
@ -207,7 +224,9 @@ int create_connection6(char *remote_host, int remote_port)
|
||||
perror("socket");
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
struct sockaddr_in6 *addr_v6 = (struct sockaddr_in6 *)&remote_addr;
|
||||
addr_v6->sin6_family = AF_INET6;
|
||||
addr_v6->sin6_port = htons(remote_port);
|
||||
@ -218,11 +237,12 @@ int create_connection6(char *remote_host, int remote_port)
|
||||
}
|
||||
}
|
||||
|
||||
if (connect(sock, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr_storage)) < 0) {
|
||||
if (connect(sock, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr_storage)) < 0)
|
||||
{
|
||||
perror("connect");
|
||||
return -1;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
//普通方法
|
||||
@ -265,7 +285,6 @@ int create_connection6(char *remote_host, int remote_port)
|
||||
}
|
||||
*/
|
||||
|
||||
fcntl(sock, F_SETFL, O_NONBLOCK);
|
||||
return sock;
|
||||
}
|
||||
|
||||
@ -343,7 +362,7 @@ 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);
|
||||
|
@ -5,10 +5,6 @@
|
||||
#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
|
||||
|
||||
|
@ -136,7 +136,7 @@ static char *regrep(char *str, int *str_len, const char *src, char *dest, int de
|
||||
return str;
|
||||
}
|
||||
|
||||
int extract_host(char *header, char *host, int *host_len, char *port, int *port_len)
|
||||
int extract_host(char *header, char *host, char *port)
|
||||
{
|
||||
|
||||
//printf("%s\n", header);
|
||||
@ -238,8 +238,8 @@ int extract_host(char *header, char *host, int *host_len, char *port, int *port_
|
||||
char *_p2 = strchr(_p1 + 1, ']');
|
||||
char *_p3 = strchr(s_host, '\0');
|
||||
if (_p1 && _p2) {
|
||||
memcpy(host, _p1 + 1, _p2 - (_p1 + 1));
|
||||
if (_p3 - (_p2 + 1) < 2) {
|
||||
memcpy(host, _p1 + 1, _p2 - _p1 - 1);
|
||||
if ((_p3-(_p2+1)) < 2) {
|
||||
memcpy(port, "80", 2);
|
||||
} else {
|
||||
memcpy(port, _p2 + 2, _p3 - (_p2 + 2));
|
||||
@ -447,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, &host_len, http_request->port, &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);
|
||||
@ -462,17 +462,17 @@ void parse_request_head(char *http_request_line, struct http_request *http_reque
|
||||
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, http_request->url_len);
|
||||
printf("%s %d\n", http_request->uri, http_request->uri_len);
|
||||
*/
|
||||
/*
|
||||
// 调试
|
||||
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, http_request->url_len);
|
||||
printf("%s %d\n", http_request->uri, http_request->uri_len);
|
||||
*/
|
||||
|
||||
free(head);
|
||||
return;
|
||||
@ -480,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 *temp_stack;
|
||||
char *tail_head;
|
||||
char *_p0;
|
||||
char *_p1;
|
||||
|
||||
@ -491,17 +491,17 @@ static char *splice_head(char *head, int *head_len, const char *needle, char *st
|
||||
_p1 = _p1 + 1;
|
||||
_p0 = strchr(_p1, '\0');
|
||||
|
||||
temp_stack = (char *)alloca((_p0 - _p1) + 1);
|
||||
if (temp_stack == NULL) {
|
||||
tail_head = (char *)alloca((_p0 - _p1) + 1);
|
||||
if (tail_head == NULL) {
|
||||
perror("alloca");
|
||||
return head;
|
||||
}
|
||||
memset(temp_stack, 0, (_p0 - _p1) + 1);
|
||||
memcpy(temp_stack, _p1, (_p0 - _p1));
|
||||
memset(tail_head, 0, (_p0 - _p1) + 1);
|
||||
memcpy(tail_head, _p1, (_p0 - _p1));
|
||||
|
||||
memset(head, 0, *head_len);
|
||||
memcpy(head, string, string_len);
|
||||
strncat(head, temp_stack, (_p0 - _p1));
|
||||
strncat(head, tail_head, (_p0 - _p1));
|
||||
*head_len = string_len + (_p0 - _p1);
|
||||
return head;
|
||||
}
|
||||
@ -536,7 +536,6 @@ 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);
|
||||
|
||||
memset(head, 0, *head_len);
|
||||
@ -641,11 +640,11 @@ char *request_head(conn_t * in, conf * configure)
|
||||
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); // 更新incomplete_data数据
|
||||
in->incomplete_data_len = incomplete_head_len; // 更新incomplete_data长度
|
||||
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内存
|
||||
free(incomplete_head); // 释放incomplete_head内存
|
||||
}
|
||||
|
||||
if (strncmp(in->incomplete_data, "GET", 3) == 0 || strncmp(in->incomplete_data, "POST", 4) == 0) {
|
||||
@ -703,11 +702,11 @@ char *request_head(conn_t * in, conf * configure)
|
||||
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); // 更新incomplete_data数据
|
||||
in->incomplete_data_len = incomplete_head_len; // 更新incomplete_data长度
|
||||
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内存
|
||||
free(incomplete_head); // 释放incomplete_head内存
|
||||
}
|
||||
|
||||
free_http_request(http_request);
|
||||
|
97
main.c
97
main.c
@ -84,7 +84,7 @@ int create_server_socket(int port)
|
||||
perror("bind");
|
||||
return -1;
|
||||
}
|
||||
if (listen(server_sock, 200) < 0) {
|
||||
if (listen(server_sock, 500) < 0) {
|
||||
perror("listen");
|
||||
return -1;
|
||||
}
|
||||
@ -121,7 +121,7 @@ int create_server_socket6(int port)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (listen(server_sock, 200) < 0) {
|
||||
if (listen(server_sock, 500) < 0) {
|
||||
perror("listen");
|
||||
return -1;
|
||||
}
|
||||
@ -175,7 +175,7 @@ void accept_client6()
|
||||
epoll_ctl(epollfd, EPOLL_CTL_ADD, client->fd, &epollEvent);
|
||||
}
|
||||
|
||||
void *http_proxy_loop(void *p)
|
||||
void *tcp_loop(void *p)
|
||||
{
|
||||
conf *configure = (conf *) p;
|
||||
int n;
|
||||
@ -192,20 +192,14 @@ 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_sock) {
|
||||
accept_client();
|
||||
}
|
||||
else if (events[n].data.fd == server_sock6)
|
||||
{
|
||||
while (n-- > 0) {
|
||||
if (events[n].data.fd == server_sock6) {
|
||||
accept_client6();
|
||||
}
|
||||
else
|
||||
{
|
||||
} else if (events[n].data.fd == server_sock) {
|
||||
accept_client();
|
||||
} else {
|
||||
if (events[n].events & EPOLLIN) {
|
||||
tcp_in((conn_t *) events[n].data.ptr, configure);
|
||||
}
|
||||
@ -292,7 +286,8 @@ void server_ini()
|
||||
perror("daemon");
|
||||
return;
|
||||
}
|
||||
//while (process-- > 1 && fork() == 0);
|
||||
|
||||
while (process-- > 1 && fork() == 0);
|
||||
}
|
||||
|
||||
// 监听一个UDP接口
|
||||
@ -387,43 +382,56 @@ void initialize(conf * configure)
|
||||
// global module
|
||||
server_sock = create_server_socket(configure->tcp_listen); // IPV4
|
||||
server_sock6 = create_server_socket6(configure->tcp_listen); // IPV6
|
||||
epollfd = epoll_create(MAX_CONNECTION);
|
||||
if (epollfd == -1) {
|
||||
perror("epoll_create");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void thread_loop(conf * configure)
|
||||
{
|
||||
pthread_t thread_id = 0;
|
||||
/*
|
||||
sigset_t signal_mask;
|
||||
sigemptyset(&signal_mask);
|
||||
sigaddset(&signal_mask, SIGPIPE); // 忽略PIPE信号
|
||||
if (pthread_sigmask(SIG_BLOCK, &signal_mask, NULL) != 0) {
|
||||
printf("block sigpipe error\n");
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
if (global.timeout_m)
|
||||
pthread_create(&thread_id, NULL, &timeout_check, NULL);
|
||||
|
||||
if (pthread_create(&thread_id, NULL, &http_proxy_loop, (void *)configure) != 0)
|
||||
perror("pthread_create");
|
||||
|
||||
if (global.dns_listen_fd >= 0) {
|
||||
|
||||
if (server_sock >= 0)
|
||||
{
|
||||
if (global.dns_listen_fd >= 0) {
|
||||
dns_init();
|
||||
pthread_create(&thread_id, NULL, &dns_loop, NULL);
|
||||
}
|
||||
if (global.udp_listen_fd >= 0) {
|
||||
udp_init();
|
||||
//udp_loop(NULL);
|
||||
pthread_create(&thread_id, NULL, &udp_loop, NULL);
|
||||
}
|
||||
|
||||
if (pthread_create(&thread_id, NULL, &tcp_loop, (void *)configure) != 0)
|
||||
perror("pthread_create");
|
||||
}
|
||||
|
||||
if (global.dns_listen_fd >= 0)
|
||||
{
|
||||
dns_init();
|
||||
pthread_create(&thread_id, NULL, &dns_loop, NULL);
|
||||
if (global.udp_listen_fd >= 0)
|
||||
{
|
||||
udp_init();
|
||||
pthread_create(&thread_id, NULL, &udp_loop, NULL);
|
||||
}
|
||||
dns_loop(NULL);
|
||||
}
|
||||
|
||||
if (global.udp_listen_fd >= 0) {
|
||||
udp_init();
|
||||
udp_loop(NULL);
|
||||
//pthread_create(&thread_id, NULL, &udp_loop, NULL);
|
||||
}
|
||||
udp_init();
|
||||
udp_loop(NULL);
|
||||
|
||||
pthread_join(thread_id, NULL);
|
||||
pthread_exit(NULL);
|
||||
//pthread_join(thread_id, NULL);
|
||||
//pthread_exit(NULL);
|
||||
|
||||
/* 线程分离
|
||||
pthread_attr_t attr;
|
||||
@ -431,7 +439,7 @@ void thread_loop(conf * configure)
|
||||
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
|
||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
||||
pthread_create(&thread_id, &attr, &tcp_timeout_check, NULL);
|
||||
pthread_create(&thread_id, &attr, &http_proxy_loop, (void *)configure);
|
||||
pthread_create(&thread_id, &attr, &tcp_loop, (void *)configure);
|
||||
pthread_exit(NULL);
|
||||
*/
|
||||
}
|
||||
@ -525,6 +533,9 @@ void _main(int argc, char *argv[])
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
#if DAEMON
|
||||
// 守护进程
|
||||
int nochdir = 1;
|
||||
@ -570,6 +581,8 @@ void _main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
// 反转链表,使读取的配置正序
|
||||
http_head_strrep = local_reverse(http_head_strrep);
|
||||
http_head_regrep = local_reverse(http_head_regrep);
|
||||
@ -593,12 +606,24 @@ void _main(int argc, char *argv[])
|
||||
exit(0);
|
||||
*/
|
||||
|
||||
|
||||
rt.rlim_max = rt.rlim_cur = MAX_CONNECTION * 2; // 设置每个进程允许打开的最大文件数
|
||||
if (setrlimit(RLIMIT_NOFILE, &rt) == -1)
|
||||
perror("setrlimit");
|
||||
initialize(configure);
|
||||
|
||||
|
||||
if (setegid(configure->uid) == -1 || seteuid(configure->uid) == -1) // 设置uid
|
||||
exit(1);
|
||||
|
||||
while (configure->process-- > 1 && (child_pid = fork()) == 0);
|
||||
|
||||
epollfd = epoll_create(MAX_CONNECTION);
|
||||
if (epollfd == -1) {
|
||||
perror("epoll_create");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
thread_loop(configure);
|
||||
|
||||
return;
|
||||
|
Loading…
Reference in New Issue
Block a user