From a2f0dc399275aec66cf194f27b17577a4866c0ed Mon Sep 17 00:00:00 2001 From: aixiao Date: Sat, 16 Feb 2019 17:28:47 +0800 Subject: [PATCH] =?UTF-8?q?=09=E4=BF=AE=E6=94=B9=EF=BC=9A=20=20=20=20=20Ma?= =?UTF-8?q?kefile=20=09=E4=BF=AE=E6=94=B9=EF=BC=9A=20=20=20=20=20conf.h=20?= =?UTF-8?q?=09=E4=BF=AE=E6=94=B9=EF=BC=9A=20=20=20=20=20conf/cproxy.ini=20?= =?UTF-8?q?=09=E6=96=B0=E6=96=87=E4=BB=B6=EF=BC=9A=20=20=20conf/cproxy.ini?= =?UTF-8?q?.explain=20=09=E4=BF=AE=E6=94=B9=EF=BC=9A=20=20=20=20=20cproxy.?= =?UTF-8?q?c=20=09=E4=BF=AE=E6=94=B9=EF=BC=9A=20=20=20=20=20cproxy.h=20=09?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=9A=20=20=20=20=20cproxy=5Fhelp.h=20=09?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=9A=20=20=20=20=20cproxy=5Frequest.c=20?= =?UTF-8?q?=09=E4=BF=AE=E6=94=B9=EF=BC=9A=20=20=20=20=20cproxy=5Frequest.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 2 +- conf.h | 2 ++ conf/cproxy.ini | 6 ++-- conf/cproxy.ini.explain | 8 +++++ cproxy.c | 16 ++++------ cproxy.h | 20 +++++++----- cproxy_help.h | 1 + cproxy_request.c | 67 ++++++++++++++++++++++++++++------------- cproxy_request.h | 13 ++++++++ 9 files changed, 93 insertions(+), 42 deletions(-) create mode 100644 conf/cproxy.ini.explain diff --git a/Makefile b/Makefile index 235b6d2..2b20426 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ LIBS = -liniparser OBJ := cproxy all: cproxy.o conf.o cproxy_request.o cproxy_help.o - $(CC) $(CFLAGS) -o $(OBJ) $^ $(LIBS) + $(CC) $(CFLAGS) -o $(OBJ) $^ $(LIBS) strip $(OBJ) -chmod a+x $(OBJ) .c.o: diff --git a/conf.h b/conf.h index 3cac86a..132835d 100644 --- a/conf.h +++ b/conf.h @@ -3,6 +3,8 @@ #include "iniparser.h" #include "cproxy.h" +void read_conf(char *file, conf *p); +void free_conf(conf *p); #endif diff --git a/conf/cproxy.ini b/conf/cproxy.ini index 2037b43..cce4c1d 100644 --- a/conf/cproxy.ini +++ b/conf/cproxy.ini @@ -6,10 +6,10 @@ PID_FILE=log/cproxy.pid; http_ip=10.0.0.172; http_port=80; http_del="x-online-host,X-Online-Host,host,Host"; -http_first="[M] [U] [V]\r\n.aixiao.me\rx-online-host: [host]\r\nhost: iread.wo.com.cn\r\n"; +http_first="[M] [U] [V]\r\n.aixiao.me\rx-online-host: [host]\r\nhost: iread.wo.cn\r\n"; [https] https_ip=10.0.0.172; https_port=80; -https_del="Host"; -https_first="[M] [U]?wap.10010.com [V]\r\nHost: wap.10010.com:[port]\r\nX-Online-Host: [host]:[port]\r\n"; +https_del=",Host"; +https_first="[M] iread.wo.cn//https://[host]:[port]:iread.wo.cn [V]\r\nHost: iread.wo.cn\r\n"; diff --git a/conf/cproxy.ini.explain b/conf/cproxy.ini.explain new file mode 100644 index 0000000..41a1b99 --- /dev/null +++ b/conf/cproxy.ini.explain @@ -0,0 +1,8 @@ +模块: [server], [http], [https] + +[http]、[https]模块关键字: [M], [U], [V], [host], [port], \r, \n, \v, \f, \b, \t, \a. 如果原本请求头含有关键字也会被替换. +[M] 原请求方法 +[U] 原请求url +[V] 原请求协议版本 +[host] 原请求host +[port] 原请求端口 \ No newline at end of file diff --git a/cproxy.c b/cproxy.c index 25c4c7c..4f8cb37 100644 --- a/cproxy.c +++ b/cproxy.c @@ -50,8 +50,6 @@ void handle_client(int client_sock, struct sockaddr_in client_addr, conf *config replacement_http_head(header_buffer, remote_host, &remote_port, &SIGN, configure); //printf("%s", header_buffer); - //printf("%s\n", remote_host); - //printf("%d\n", remote_port); if ((remote_sock = create_connection(configure, SIGN)) < 0) { return; @@ -103,7 +101,7 @@ void forward_data(int source_sock, int destination_sock) shutdown(source_sock, SHUT_RDWR); } -int create_connection(conf * configure, int SIGN) +int create_connection(conf *configure, int SIGN) { struct sockaddr_in server_addr; struct hostent *server; @@ -173,7 +171,7 @@ int create_server_socket(int port) } // 守护 -int init_daemon(int nochdir, int noclose, conf * configure) +int init_daemon(int nochdir, int noclose, conf *configure) { FILE *fp = fopen(configure->server_pid_file, "w"); int pid; @@ -225,20 +223,18 @@ void server_loop(conf * configure) socklen_t addrlen = sizeof(client_addr); while (1) { - client_sock = - accept(server_sock, (struct sockaddr *)&client_addr, &addrlen); + client_sock = accept(server_sock, (struct sockaddr *)&client_addr, &addrlen); if (fork() == 0) { // 创建子进程处理客户端连接请求 + close(server_sock); handle_client(client_sock, client_addr, configure); - close(client_sock); exit(0); } + close(client_sock); } - - close(server_sock); } -void start_server(conf * configure) +void start_server(conf *configure) { signal(SIGCHLD, sigchld_handler); // 防止子进程变成僵尸进程 diff --git a/cproxy.h b/cproxy.h index 8eaa9a9..382239f 100644 --- a/cproxy.h +++ b/cproxy.h @@ -66,17 +66,23 @@ typedef struct CONF { #define HTTP_OTHERS 3 #define HTTP_CONNECT 4 -void read_conf(char *file, conf *p); -void free_conf(conf * p); - -void server_loop(conf * configure); +char *read_data(int client_sock, char *data, int *data_len); +void servertoclient(int remote_sock, int client_sock, char *complete_data, int *len_complete_data); +void clienttoserver(int remote_sock, char *complete_data, int *len_complete_data); void handle_client(int client_sock, struct sockaddr_in client_addr, conf *configure); -void forward_data(int source_sock, int destination_sock); int send_data(int socket, char *buffer, int len); int receive_data(int socket, char *buffer, int len); - -int create_connection(conf * configure, int SIGN); +void forward_data(int source_sock, int destination_sock); +int create_connection(conf *configure, int SIGN); +int create_server_socket(int port); +int init_daemon(int nochdir, int noclose, conf * configure); +void sigchld_handler(int signal); +void server_loop(conf *configure); +void start_server(conf * configure); int _main(int argc, char *argv[]); + +void read_conf(char *file, conf *p); +void free_conf(conf *p); int extract_host(char *header); int replacement_http_head(char *header_buffer, char *remote_host, int *remote_port, int *SIGN, conf *p); uint8_t request_type(char *req); diff --git a/cproxy_help.h b/cproxy_help.h index 3c8dcfb..886e659 100644 --- a/cproxy_help.h +++ b/cproxy_help.h @@ -5,6 +5,7 @@ #include "cproxy.h" #define BUILD(fmt...) do { fprintf(stderr,"%s %s ",__DATE__,__TIME__); fprintf(stderr, ##fmt); } while(0) +char help_information(void); #endif diff --git a/cproxy_request.c b/cproxy_request.c index 7e4f163..5a93b90 100644 --- a/cproxy_request.c +++ b/cproxy_request.c @@ -1,7 +1,5 @@ #include "cproxy_request.h" -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) { @@ -95,15 +93,15 @@ uint8_t request_type(char *req) else if (strncmp(req, "CONNECT", 7) == 0) return HTTP_CONNECT; else if (strncmp(req, "HEAD", 4) == 0 || - strncmp(req, "PUT", 3) == 0 || - strncmp(req, "OPTIONS", 7) == 0 || - strncmp(req, "MOVE", 4) == 0 || - strncmp(req, "COPY", 4) == 0 || - strncmp(req, "TRACE", 5) == 0 || - strncmp(req, "DELETE", 6) == 0 || - strncmp(req, "LINK", 4) == 0 || - strncmp(req, "UNLINK", 6) == 0 || - strncmp(req, "PATCH", 5) == 0 || strncmp(req, "WRAPPED", 7) == 0) + strncmp(req, "PUT", 3) == 0 || + strncmp(req, "OPTIONS", 7) == 0 || + strncmp(req, "MOVE", 4) == 0 || + strncmp(req, "COPY", 4) == 0 || + strncmp(req, "TRACE", 5) == 0 || + strncmp(req, "DELETE", 6) == 0 || + strncmp(req, "LINK", 4) == 0 || + strncmp(req, "UNLINK", 6) == 0 || + strncmp(req, "PATCH", 5) == 0 || strncmp(req, "WRAPPED", 7) == 0) return HTTP_OTHERS; else return OTHER; @@ -193,6 +191,17 @@ void rewrite_header() } } +// 判断数字有几位 +int numbin(int n) +{ + int sum = 0; + while (n) { + sum++; + n /= 10; + } + return sum; +} + // 删除字符串header_buffer中第一位到character处,并拼接string,character必须存在.(string替换第一个字符到character处) char *splice_head(char *header_buffer, const char *character, char *string) { @@ -218,7 +227,7 @@ char *delete_header(char *header_buffer, const char *character, int string) return strcat(header_buffer, p2 + 1); } -int replacement_http_head(char *header_buffer, char *remote_host, int *remote_port, int *SIGN, conf * p) +int replacement_http_head(char *header_buffer, char *remote_host, int *remote_port, int *SIGN, conf *p) { char *http_firsts = (char *)malloc(strlen(p->http_first) + 1); strcpy(http_firsts, p->http_first); // 拷贝http_first @@ -231,7 +240,7 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po char *new_http_del = malloc(strlen(p->http_del) + 1); // 拷贝http_del strcpy(new_http_del, p->http_del); - char *new_https_del = malloc(strlen(p->https_del) + 1); // // 拷贝https_del + char *new_https_del = malloc(strlen(p->https_del) + 1); // 拷贝https_del strcpy(new_https_del, p->https_del); if (*SIGN == HTTP) { @@ -273,7 +282,6 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po //printf("%s", V); char *new_header_buffer = (char *)malloc(strlen(splice_head(header_buffer_backup, "\n", http_firsts)) + 1); - //char *new_header_buffer = (char *)malloc(BUF_SIZES); strcpy(new_header_buffer, splice_head(header_buffer_backup, "\n", http_firsts)); int len = strlen(new_header_buffer); @@ -286,6 +294,19 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po new_header_buffer = replace(new_header_buffer, &len, "[U]", 3, U, len_u); new_header_buffer = replace(new_header_buffer, &len, "[V]", 3, V, len_v); new_header_buffer = replace(new_header_buffer, &len, "[host]", 6, remote_host, len_remote_host); + + char port_copy[numbin(*remote_port) + 1]; + sprintf(port_copy, "%d", *remote_port); + int len_remote_port = strlen(port_copy); + new_header_buffer = replace(new_header_buffer, &len, "[port]", 6, port_copy, len_remote_port); + + new_header_buffer = replace(new_header_buffer, &len, "\\r", 2, "\r", 1); + new_header_buffer = replace(new_header_buffer, &len, "\\n", 2, "\n", 1); + new_header_buffer = replace(new_header_buffer, &len, "\\b", 2, "\b", 1); + new_header_buffer = replace(new_header_buffer, &len, "\\v", 2, "\v", 1); + new_header_buffer = replace(new_header_buffer, &len, "\\f", 2, "\f", 1); + new_header_buffer = replace(new_header_buffer, &len, "\\a", 2, "\a", 1); + new_header_buffer = replace(new_header_buffer, &len, "\\t", 2, "\t", 1); new_header_buffer = replace(new_header_buffer, &len, "\\r", 2, "\r", 1); new_header_buffer = replace(new_header_buffer, &len, "\\n", 2, "\n", 1); @@ -332,15 +353,12 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po // V p4 = p4 + 1; - //del_chr(p4, '\r'); - //del_chr(p4, '\n'); l = strlen(p4); char *V = (char *)malloc(l); strncpy_(V, p4, 8); //printf("%s", V); char *new_header_buffer = (char *) malloc(strlen(splice_head(header_buffer_backup, "\n", https_firsts)) + 1); - //char *new_header_buffer = (char *) malloc(BUF_SIZES); strcpy(new_header_buffer, splice_head(header_buffer_backup, "\n", https_firsts)); int len = strlen(new_header_buffer); @@ -353,11 +371,18 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po new_header_buffer = replace(new_header_buffer, &len, "[V]", 3, V, len_v); new_header_buffer = replace(new_header_buffer, &len, "[host]", 6, remote_host, len_remote_host); - char port_num[9]; - sprintf(port_num, "%d", *remote_port); - int len_remote_port = strlen(port_num); - new_header_buffer = replace(new_header_buffer, &len, "[port]", 6, port_num, len_remote_port); + char port_copy[numbin(*remote_port) + 1]; + sprintf(port_copy, "%d", *remote_port); + int len_remote_port = strlen(port_copy); + new_header_buffer = replace(new_header_buffer, &len, "[port]", 6, port_copy, len_remote_port); + new_header_buffer = replace(new_header_buffer, &len, "\\r", 2, "\r", 1); + new_header_buffer = replace(new_header_buffer, &len, "\\n", 2, "\n", 1); + new_header_buffer = replace(new_header_buffer, &len, "\\b", 2, "\b", 1); + new_header_buffer = replace(new_header_buffer, &len, "\\v", 2, "\v", 1); + new_header_buffer = replace(new_header_buffer, &len, "\\f", 2, "\f", 1); + new_header_buffer = replace(new_header_buffer, &len, "\\a", 2, "\a", 1); + new_header_buffer = replace(new_header_buffer, &len, "\\t", 2, "\t", 1); new_header_buffer = replace(new_header_buffer, &len, "\\r", 2, "\r", 1); new_header_buffer = replace(new_header_buffer, &len, "\\n", 2, "\n", 1); diff --git a/cproxy_request.h b/cproxy_request.h index 6e47650..2c1cf44 100644 --- a/cproxy_request.h +++ b/cproxy_request.h @@ -4,7 +4,20 @@ #include #include #include +#include #include "cproxy.h" +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); +void del_chr(char *s, char ch); +char *strncpy_(char *dest, const char *src, size_t n); +uint8_t request_type(char *req); +int extract_host(char *header); +void forward_header(int destination_sock); +void rewrite_header(); +int numbin(int n); +char *splice_head(char *header_buffer, const char *character, char *string); +char *delete_header(char *header_buffer, const char *character, int string); +int replacement_http_head(char *header_buffer, char *remote_host, int *remote_port, int *SIGN, conf *p); #endif