From daa409875b30f38bc27c848361ec39942e2b9c55 Mon Sep 17 00:00:00 2001 From: aixiao Date: Thu, 10 Jan 2019 23:12:51 +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=E6=96=B0=E6=96=87=E4=BB=B6=EF=BC=9A=20=20=20README?= =?UTF-8?q?.md=20=09=E6=96=B0=E6=96=87=E4=BB=B6=EF=BC=9A=20=20=20conf/cpro?= =?UTF-8?q?xy.ini=20=09=E4=BF=AE=E6=94=B9=EF=BC=9A=20=20=20=20=20cproxy.c?= =?UTF-8?q?=20=09=E6=96=B0=E6=96=87=E4=BB=B6=EF=BC=9A=20=20=20cproxy.h=20?= =?UTF-8?q?=09=E6=96=B0=E6=96=87=E4=BB=B6=EF=BC=9A=20=20=20cproxy=5Freques?= =?UTF-8?q?t.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 7 +- README.md | 13 ++ conf/cproxy.ini | 15 ++ cproxy.c | 514 +++++++++++++++++++++++------------------------ cproxy.h | 18 ++ cproxy_request.c | 322 +++++++++++++++++++++++++++++ 6 files changed, 628 insertions(+), 261 deletions(-) create mode 100644 README.md create mode 100644 conf/cproxy.ini create mode 100644 cproxy.h create mode 100644 cproxy_request.c diff --git a/Makefile b/Makefile index 72faa63..968612a 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,14 @@ CC = gcc -CFLAGS = -g -Wall -Werror +CFLAGS = -g -Wall -Werror -I./iniparser/src -L ./iniparser +LIBS = -liniparser all: cproxy cproxy: cproxy.o - $(CC) $(CFLAGS) cproxy.o -o cproxy + $(CC) $(CFLAGS) cproxy.o cproxy_request.o -o cproxy $(LIBS) cproxy.o: - $(CC) $(CFLAGS) -c cproxy.c + $(CC) $(CFLAGS) -c cproxy.c cproxy_request.c clean: rm -rf *.o diff --git a/README.md b/README.md new file mode 100644 index 0000000..bf84192 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# cproxy + 基于mproxy修改后的Android本地代理. + 可以修改非Http tunnel消息头(request). + + +# Build + git clone https://github.com/niuyuling/cproxy.git + cd cproxy + git clone https://github.com/ndevilla/iniparser.git + cd iniparser + make + cd .. + make clean; make diff --git a/conf/cproxy.ini b/conf/cproxy.ini new file mode 100644 index 0000000..58288b5 --- /dev/null +++ b/conf/cproxy.ini @@ -0,0 +1,15 @@ +[server] +PORT=9606; +PID_FILE=log/cproxy.pid; + +[http] +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"; + +[https] +https_ip=10.0.0.172; +https_port=80; +https_del="Host"; +https_first="[M] [U] [V]\r\nHost: [host]\r\n"; diff --git a/cproxy.c b/cproxy.c index c622c45..4fa0465 100644 --- a/cproxy.c +++ b/cproxy.c @@ -12,42 +12,32 @@ #include #include +#include +#include +#include +#include + +#include "iniparser.h" +#define LOG(fmt...) do { fprintf(stderr,"%s %s ",__DATE__,__TIME__); fprintf(stderr, ##fmt); } while(0) #define BUF_SIZE 8192 -#define PORT 9606 #define MAX_HEADER_SIZE 8192 -char remote_host[128]; -int remote_port; +char remote_host[128]; +int remote_port; int local_port; -int server_sock; +int server_sock; int client_sock; int remote_sock; -char * header_buffer ; +char *header_buffer; -enum -{ - FLG_NONE = 0, // 正常数据流不进行编解码 - R_C_DEC = 1, // 读取客户端数据仅进行解码 - W_S_ENC = 2 // 发送到服务端进行编码 -}; - -static int io_flag; // 网络io的一些标志位 - -void handle_client(int client_sock, struct sockaddr_in client_addr); -void forward_header(int destination_sock); -void forward_data(int source_sock, int destination_sock); -void rewrite_header(); -int send_data(int socket,char * buffer,int len ); -int receive_data(int socket, char * buffer, int len); -void hand_mproxy_info_req(int sock,char * header_buffer) ; -int create_connection() ; -int _main(int argc, char *argv[]) ; +int HTTPS = 0; +#include "cproxy.h" ssize_t readLine(int fd, void *buffer, size_t n) { - ssize_t numRead; - size_t totRead; + ssize_t numRead; + size_t totRead; char *buf; char ch; @@ -56,27 +46,27 @@ ssize_t readLine(int fd, void *buffer, size_t n) return -1; } - buf = buffer; + buf = buffer; totRead = 0; for (;;) { numRead = receive_data(fd, &ch, 1); if (numRead == -1) { - if (errno == EINTR) + if (errno == EINTR) continue; else - return -1; // 未知错误 + return -1; // 未知错误 - } else if (numRead == 0) { // EOF - if (totRead == 0) // No bytes read; return 0 + } else if (numRead == 0) { // EOF + if (totRead == 0) // No bytes read; return 0 return 0; - else // Some bytes read; add '\0' + else // Some bytes read; add '\0' break; - } else { - - if (totRead < n - 1) { // Discard > (n - 1) bytes + } else { + + if (totRead < n - 1) { // Discard > (n - 1) bytes totRead++; *buf++ = ch; } @@ -90,34 +80,30 @@ ssize_t readLine(int fd, void *buffer, size_t n) return totRead; } -int read_header(int fd, void * buffer) +// 读取header +int read_header(int fd, void *buffer) { - memset(header_buffer,0,MAX_HEADER_SIZE); + memset(header_buffer, 0, MAX_HEADER_SIZE); char line_buffer[2048]; - char * base_ptr = header_buffer; + char *base_ptr = header_buffer; - for(;;) - { - memset(line_buffer,0,2048); + for (;;) { + memset(line_buffer, 0, 2048); - int total_read = readLine(fd,line_buffer,2048); - if(total_read <= 0) - { + int total_read = readLine(fd, line_buffer, 2048); + if (total_read <= 0) { return -1; } // 防止header缓冲区蛮越界 - if(base_ptr + total_read - header_buffer <= MAX_HEADER_SIZE) - { - strncpy(base_ptr,line_buffer,total_read); - base_ptr += total_read; - } else - { + if (base_ptr + total_read - header_buffer <= MAX_HEADER_SIZE) { + strncpy(base_ptr, line_buffer, total_read); + base_ptr += total_read; + } else { return -1; } // 读到了空行,http头结束 - if(strcmp(line_buffer,"\r\n") == 0 || strcmp(line_buffer,"\n") == 0) - { + if (strcmp(line_buffer, "\r\n") == 0 || strcmp(line_buffer, "\n") == 0) { break; } @@ -126,70 +112,53 @@ int read_header(int fd, void * buffer) } -void extract_server_path(const char * header,char * output) +// 提取主机、端口 +int extract_host(char *header) { - char * p = strstr(header,"GET /"); - if(p) { - char * p1 = strchr(p+4,' '); - strncpy(output,p+4,(int)(p1 - p - 4) ); - } - -} + char *_p = strstr(header, "CONNECT"); // 在 CONNECT 方法中解析 隧道主机名称及端口号 + if (_p) { + char *_p1 = strchr(_p, ' '); + char *_p2 = strchr(_p1 + 1, ':'); + char *_p3 = strchr(_p1 + 1, ' '); -int extract_host(const char * header) -{ - char * _p = strstr(header,"CONNECT"); // 在 CONNECT 方法中解析 隧道主机名称及端口号 - if(_p) - { - char * _p1 = strchr(_p,' '); - char * _p2 = strchr(_p1 + 1,':'); - char * _p3 = strchr(_p1 + 1,' '); - - if(_p2) - { + if (_p2) { char s_port[10]; - bzero(s_port,10); - strncpy(remote_host,_p1+1,(int)(_p2 - _p1) - 1); - strncpy(s_port,_p2+1,(int) (_p3 - _p2) -1); + bzero(s_port, 10); + strncpy(remote_host, _p1 + 1, (int)(_p2 - _p1) - 1); + strncpy(s_port, _p2 + 1, (int)(_p3 - _p2) - 1); remote_port = atoi(s_port); - } else - { - strncpy(remote_host,_p1+1,(int)(_p3 - _p1) -1); + } else { + strncpy(remote_host, _p1 + 1, (int)(_p3 - _p1) - 1); remote_port = 80; } - - + return 0; } - char * p = strstr(header,"Host:"); - if(!p) - { + char *p = strstr(header, "Host:"); + if (!p) { return -1; } - char * p1 = strchr(p,'\n'); - if(!p1) - { - return -1; + char *p1 = strchr(p, '\n'); + if (!p1) { + return -1; } - char * p2 = strchr(p + 5,':'); // 5是指'Host:'的长度 - if(p2 && p2 < p1) - { - int p_len = (int)(p1 - p2 -1); + char *p2 = strchr(p + 5, ':'); // 5是指'Host:'的长度 + if (p2 && p2 < p1) { + int p_len = (int)(p1 - p2 - 1); char s_port[p_len]; - strncpy(s_port,p2+1,p_len); + strncpy(s_port, p2 + 1, p_len); s_port[p_len] = '\0'; remote_port = atoi(s_port); - int h_len = (int)(p2 - p -5 -1 ); - strncpy(remote_host,p + 5 + 1 ,h_len); // Host: + int h_len = (int)(p2 - p - 5 - 1); + strncpy(remote_host, p + 5 + 1, h_len); // Host: remote_host[h_len] = '\0'; - } else - { - int h_len = (int)(p1 - p - 5 -1 -1); - strncpy(remote_host,p + 5 + 1,h_len); + } else { + int h_len = (int)(p1 - p - 5 - 1 - 1); + strncpy(remote_host, p + 5 + 1, h_len); remote_host[h_len] = '\0'; remote_port = 80; } @@ -200,92 +169,134 @@ int extract_host(const char * header) // 响应隧道连接请求 int send_tunnel_ok(int client_sock) { - char * resp = "HTTP/1.1 200 Connection Established\r\n\r\n"; + char *resp = "HTTP/1.1 200 Connection Established\r\n\r\n"; // 把字符串写入client_sock流 int len = strlen(resp); - char buffer[len+1]; - strcpy(buffer,resp); - if(send_data(client_sock,buffer,len) < 0) - { + char buffer[len + 1]; + strcpy(buffer, resp); + if (send_data(client_sock, buffer, len) < 0) { return -1; } return 0; } -void hand_mproxy_info_req(int sock, char * header) { - char server_path[255] ; - char response[8192]; - extract_server_path(header,server_path); +// 转发数据,读取source_sock流发送到destination_sock流 +void forward_data(int source_sock, int destination_sock) +{ + char buffer[BUF_SIZE]; + int n; + while ((n = receive_data(source_sock, buffer, BUF_SIZE)) > 0) { + send_data(destination_sock, buffer, n); + } + shutdown(destination_sock, SHUT_RDWR); + shutdown(source_sock, SHUT_RDWR); +} - char info_buf[1024]; - sprintf(response,"HTTP/1.0 200 OK\nServer: CProxy/0.1\n\ - Content-type: text/html; charset=utf-8\n\n\ - \ -
%s
\ - \n",info_buf); +// 转发头字符串到destination_sock +void forward_header(int destination_sock) +{ + rewrite_header(); + int len = strlen(header_buffer); + send_data(destination_sock, header_buffer, len); +} - write(sock,response,strlen(response)); +// 代理中的完整URL转发前需改成 path 的形式 +void rewrite_header() +{ + char *p = strstr(header_buffer, "http://"); + char *p0 = strchr(p, '\0'); + char *p5 = strstr(header_buffer, "HTTP/"); // "HTTP/" 是协议标识 如 "HTTP/1.1" + int len = strlen(header_buffer); + if (p) { + char *p1 = strchr(p + 7, '/'); + if (p1 && (p5 > p1)) { + // 转换url到 path + memcpy(p, p1, (int)(p0 - p1)); + int l = len - (p1 - p); + header_buffer[l] = '\0'; + } else { + char *p2 = strchr(p, ' '); // GET http://3g.sina.com.cn HTTP/1.1 + memcpy(p + 1, p2, (int)(p0 - p2)); + *p = '/'; // url 没有路径使用根 + int l = len - (p2 - p) + 1; + header_buffer[l] = '\0'; + } + } + +} + +// 接收数据 +int receive_data(int socket, char *buffer, int len) +{ + int n = recv(socket, buffer, len, 0); + return n; +} + +// 发送数据 +int send_data(int socket, char *buffer, int len) +{ + return send(socket, buffer, len, 0); +} + +// 创建连接 +int create_connection() +{ + struct sockaddr_in server_addr; + struct hostent *server; + int sock; + + if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + return -1; + } + if ((server = gethostbyname(remote_host)) == NULL) { + errno = EFAULT; + return -1; + } + memset(&server_addr, 0, sizeof(server_addr)); + server_addr.sin_family = AF_INET; + memcpy(&server_addr.sin_addr.s_addr, server->h_addr, server->h_length); + server_addr.sin_port = htons(remote_port); + + if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { + return -1; + } + + return sock; } // 处理客户端的连接 void handle_client(int client_sock, struct sockaddr_in client_addr) { - int is_http_tunnel = 0; - if(strlen(remote_host) == 0) // 未指定远端主机名称从http 请求 HOST 字段中获取 - { - if(read_header(client_sock,header_buffer) < 0) - { - return; - } else - { - char * p = strstr(header_buffer,"CONNECT"); // 判断是否是http 隧道请求 - if(p) - { - is_http_tunnel = 1; - } + read_header(client_sock, header_buffer); + extract_host(header_buffer); + replacement_http_head(header_buffer, remote_host, &remote_port, &HTTPS); - if(strstr(header_buffer,"GET /cproxy") > 0) - { - hand_mproxy_info_req(client_sock,header_buffer); - return; - } + printf("%s\n", remote_host); + printf("%d\n", remote_port); + printf("%d\n", HTTPS); + printf("%s", header_buffer); - if(extract_host(header_buffer) < 0) - { - return; - } - } - } - - if ((remote_sock = create_connection()) < 0) { + if ((remote_sock = create_connection()) < 0) { // 先创建连接 return; } - if (fork() == 0) { // 创建子进程用于从客户端转发数据到远端socket接口 - - if(strlen(header_buffer) > 0 && !is_http_tunnel) - { + if (fork() == 0) { // 创建子进程用于从客户端转发数据到远端socket接口 + if (HTTPS == 0) { forward_header(remote_sock); //普通的http请求先转发header - } - - forward_data(client_sock, remote_sock); - exit(0); - } - - if (fork() == 0) { // 创建子进程用于转发从远端socket接口过来的数据到客户端 - - if(io_flag == W_S_ENC) - { - io_flag = R_C_DEC; // 发送请求给服务端进行编码,读取服务端的响应则进行解码 - } else if (io_flag == R_C_DEC) - { - io_flag = W_S_ENC; //接收客户端请求进行解码,那么响应客户端请求需要编码 + forward_data(client_sock, remote_sock); + } else { + char buffer[BUF_SIZE]; + int n; + while ((n = read(client_sock, buffer, BUF_SIZE)) > 0) { + write(remote_sock, buffer, n); + } } - if(is_http_tunnel) - { + exit(0); + } + if (fork() == 0) { // 创建子进程用于转发从远端socket接口过来的数据到客户端 + if (HTTPS == 1) { send_tunnel_ok(client_sock); - } - + } forward_data(remote_sock, client_sock); exit(0); } @@ -294,149 +305,136 @@ void handle_client(int client_sock, struct sockaddr_in client_addr) close(client_sock); } -void forward_data(int source_sock, int destination_sock) { - char buffer[BUF_SIZE]; - int n; - - while ((n = receive_data(source_sock, buffer, BUF_SIZE)) > 0) - { - send_data(destination_sock, buffer, n); - } - - shutdown(destination_sock, SHUT_RDWR); - shutdown(source_sock, SHUT_RDWR); -} - -void forward_header(int destination_sock) +// 守护 +int init_daemon(int nochdir, int noclose) { - rewrite_header(); - int len = strlen(header_buffer); - send_data(destination_sock,header_buffer,len) ; -} - -// 代理中的完整URL转发前需改成 path 的形式 -void rewrite_header() -{ - char * p = strstr(header_buffer,"http://"); - char * p0 = strchr(p,'\0'); - char * p5 = strstr(header_buffer,"HTTP/"); // "HTTP/" 是协议标识 如 "HTTP/1.1" - int len = strlen(header_buffer); - if(p) - { - char * p1 = strchr(p + 7,'/'); - if(p1 && (p5 > p1)) - { - // 转换url到 path - memcpy(p,p1,(int)(p0 -p1)); - int l = len - (p1 - p) ; - header_buffer[l] = '\0'; - } else - { - char * p2 = strchr(p,' '); // GET http://3g.sina.com.cn HTTP/1.1 - memcpy(p + 1,p2,(int)(p0-p2)); - *p = '/'; // url 没有路径使用根 - int l = len - (p2 - p ) + 1; - header_buffer[l] = '\0'; - - } + char *inifile = "conf/cproxy.ini"; + dictionary *ini = iniparser_load(inifile); + const char *PID_FILE = iniparser_getstring(ini, "server:PID_FILE", NULL); + FILE *fp = fopen(PID_FILE, "w"); + if (NULL == fp) { + printf("Pid File Can Not Open To Write\n"); + errno = 0; + mkdir("log", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); + perror("mkdir"); } -} + int pid; -int receive_data(int socket, char * buffer, int len) -{ - int n = recv(socket, buffer, len, 0); - if(io_flag == R_C_DEC && n > 0) - { - int i; - for(i = 0; i< n; i++ ) - { - buffer[i] ^= 1; - } + if ((pid = fork()) < 0) { + return -1; + } else if (0 != pid) { + exit(0); } + //child 1 continues... - return n; -} - -int send_data(int socket,char * buffer,int len) -{ - if(io_flag == W_S_ENC) - { - int i; - for(i = 0; i < len ; i++) - { - buffer[i] ^= 1; - } - } - return send(socket,buffer,len,0); -} - -int create_connection() { - struct sockaddr_in server_addr; - struct hostent *server; - int sock; - - if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + //become session leader + if (setsid() < 0) { return -1; } - if ((server = gethostbyname(remote_host)) == NULL) { - errno = EFAULT; + signal(SIGHUP, SIG_IGN); + if ((pid = fork()) < 0) { return -1; + } else if (0 != pid) { + fprintf(fp, "%d", pid); + exit(0); } + //child 2 continues... - memset(&server_addr, 0, sizeof(server_addr)); - server_addr.sin_family = AF_INET; - memcpy(&server_addr.sin_addr.s_addr, server->h_addr, server->h_length); - server_addr.sin_port = htons(remote_port); - - if (connect(sock, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) { - return -1; + //change working directory + if (0 == nochdir) { + chdir("/"); } + //close off file descriptors + /* + int i; + for (i = 0; i < 64; i++) { + close(i); + } + */ - return sock; + //redirect stdin,stdout,stderror to "/dev/null" + if (0 == noclose) { + open("/dev/null", O_RDONLY); + open("/dev/null", O_RDWR); + open("/dev/null", O_RDWR); + } + fclose(fp); + iniparser_freedict(ini); + return 0; } +void sigchld_handler(int signal) +{ + while (waitpid(-1, NULL, WNOHANG) > 0) ; +} + +// 主函数 int _main(int argc, char *argv[]) { - header_buffer = (char *) malloc(MAX_HEADER_SIZE); + char *inifile = "conf/cproxy.ini"; + dictionary *ini = iniparser_load(inifile); + int PORT = iniparser_getint(ini, "server:PORT", 0); + + int opt; + char optstrs[] = ":l:d"; + while (-1 != (opt = getopt(argc, argv, optstrs))) { + switch (opt) { + case 'l': + PORT = atoi(optarg); + break; + case 'd':{ + init_daemon(1, 1); + } + break; + default: + ; + } + } + + header_buffer = (char *)malloc(MAX_HEADER_SIZE); + + signal(SIGCHLD, sigchld_handler); // 防止子进程变成僵尸进程 int server_sock, optval; struct sockaddr_in server_addr; if ((server_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { return -1; } - if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) { + if (setsockopt + (server_sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) { return -1; } memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(PORT); server_addr.sin_addr.s_addr = INADDR_ANY; - if (bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) != 0) { + if (bind(server_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) + != 0) { return -1; } if (listen(server_sock, 17) < 0) { return -1; } - struct sockaddr_in clnt_addr; - socklen_t clnt_addr_size = sizeof(clnt_addr); + struct sockaddr_in clnt_addr; + socklen_t clnt_addr_size = sizeof(clnt_addr); while (1) { - int client_socket = - accept(server_sock, (struct sockaddr *) &clnt_addr, - &clnt_addr_size); + int client_socket = accept(server_sock, (struct sockaddr *)&clnt_addr, + &clnt_addr_size); - if (fork() == 0) { // 创建子进程处理客户端连接请求 + if (fork() == 0) { // 创建子进程处理客户端连接请求 close(server_sock); handle_client(client_socket, clnt_addr); - printf("%s", header_buffer); exit(0); } close(client_socket); } + free(header_buffer); // 收回内存 + iniparser_freedict(ini); } int main(int argc, char *argv[]) { - return _main(argc,argv); + return _main(argc, argv); } diff --git a/cproxy.h b/cproxy.h new file mode 100644 index 0000000..261c89e --- /dev/null +++ b/cproxy.h @@ -0,0 +1,18 @@ +ssize_t readLine(int fd, void *buffer, size_t n); +int read_header(int fd, void *buffer); +void extract_server_path(const char *header, char *output); +int extract_host(char *header); +int send_tunnel_ok(int client_sock); +void hand_cproxy_info_req(int sock, char *header_buffer); +void forward_data(int source_sock, int destination_sock); +void forward_header(int destination_sock); +void rewrite_header(); +int receive_data(int socket, char *buffer, int len); +int send_data(int socket, char *buffer, int len); +int create_connection(); +void handle_client(int client_sock, struct sockaddr_in client_addr); +int init_daemon(int nochdir, int noclose); +void sigchld_handler(int signal); +int _main(int argc, char *argv[]); +int replacement_http_head(char *header_buffer, char *remote_host, + int *remote_port, int *HTTPS); diff --git a/cproxy_request.c b/cproxy_request.c new file mode 100644 index 0000000..a9629ca --- /dev/null +++ b/cproxy_request.c @@ -0,0 +1,322 @@ +#include +#include +#include +#include "iniparser.h" + +const char *http_ip; +int http_port; +const char *http_del; +const char *http_first; + +const char *https_ip; +int https_port; +const char *https_del; +const char *https_first; + +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) +{ + if (!replace_memory || !src || !dest) + return replace_memory; + + char *p; + int diff; + + if (src_len == dest_len) { + for (p = memmem(replace_memory, *replace_memory_len, src, src_len); p; + p = + memmem(p, *replace_memory_len - (p - replace_memory), src, + src_len)) { + memcpy(p, dest, dest_len); + p += dest_len; + } + } else if (src_len < dest_len) { + int before_len; + char *before_end, *new_replace_memory; + + diff = dest_len - src_len; + for (p = memmem(replace_memory, *replace_memory_len, src, src_len); p; + p = + memmem(p, *replace_memory_len - (p - replace_memory), src, + src_len)) { + *replace_memory_len += diff; + before_len = p - replace_memory; + new_replace_memory = + (char *)realloc(replace_memory, *replace_memory_len + 1); + if (new_replace_memory == NULL) { + free(replace_memory); + return NULL; + } + replace_memory = new_replace_memory; + before_end = replace_memory + before_len; + p = before_end + dest_len; + memmove(p, p - diff, *replace_memory_len - (p - replace_memory)); + memcpy(before_end, dest, dest_len); + } + } else if (src_len > dest_len) { + diff = src_len - dest_len; + for (p = memmem(replace_memory, *replace_memory_len, src, src_len); p; + p = + memmem(p, *replace_memory_len - (p - replace_memory), src, + src_len)) { + *replace_memory_len -= diff; + memcpy(p, dest, dest_len); + p += dest_len; + memmove(p, p + diff, *replace_memory_len - (p - replace_memory)); + } + } + + replace_memory[*replace_memory_len] = '\0'; + return replace_memory; +} + +// 删除单个字符 +void del_chr(char *s, char ch) +{ + char *t = s; //目标指针先指向原串头 + while (*s != '\0') //遍历字符串s + { + if (*s != ch) //如果当前字符不是要删除的,则保存到目标串中 + *t++ = *s; + s++; //检查下一个字符 + } + *t = '\0'; //置目标串结束符。 +} + +char *strncpy_(char *dest, const char *src, size_t n) +{ + int size = sizeof(char) * (n + 1); + char *tmp = (char *)malloc(size); // 开辟大小为n+1的临时内存tmp + if (tmp) { + memset(tmp, '\0', size); // 将内存初始化为0 + memcpy(tmp, src, size - 1); // 将src的前n个字节拷贝到tmp + memcpy(dest, tmp, size); // 将临时空间tmp的内容拷贝到dest + free(tmp); // 释放内存 + return dest; + } else { + return NULL; + } +} + +// 删除字符串header_buffer中第一位到ch处,并拼接str,ch必须存在. +char *splice_head(char *header_buffer, const char *ch, char *str) +{ + int len_http_first = strlen(str); + char *p1 = strstr(header_buffer, ch); + p1 = p1 + 1; + + char *s = (char *)malloc(len_http_first + strlen(p1) + 1); + strcpy(s, str); // 拼接 + return strcat(s, p1); +} + +// 删除字符串header_buffer中ch到str处,ch为空返回原字符串. +char *delete_header(char *header_buffer, const char *ch, int str) +{ + int len_header_buffer = strlen(header_buffer); + char *p1 = strstr(header_buffer, ch); + if (p1 == NULL) { + return header_buffer; + } + char *p2 = strchr(p1, str); + int l = len_header_buffer - strlen(p1); + header_buffer[l] = '\0'; + return strcat(header_buffer, p2 + 1); +} + +int replacement_http_head(char *header_buffer, char *remote_host, + int *remote_port, int *HTTPS) +{ + char *file = "conf/cproxy.ini"; + dictionary *ini = iniparser_load(file); + + // 读取配置 + http_ip = iniparser_getstring(ini, "http:http_ip", NULL); + http_port = iniparser_getint(ini, "http:http_port", 0); + http_del = iniparser_getstring(ini, "http:http_del", NULL); + http_first = iniparser_getstring(ini, "http:http_first", NULL); + https_ip = iniparser_getstring(ini, "https:https_ip", NULL); + https_port = iniparser_getint(ini, "https:https_port", 0); + https_del = iniparser_getstring(ini, "https:https_del", NULL); + https_first = iniparser_getstring(ini, "https:https_first", NULL); + + char *http_firsts = (char *)malloc(strlen(http_first) + 1); + strcpy(http_firsts, http_first); // 拷贝http_first + char *https_firsts = (char *)malloc(strlen(https_first) + 1); + strcpy(https_firsts, https_first); // 拷贝https_first + + char *header_buffers = (char *)malloc(strlen(header_buffer) + 1); // 拷贝原字符串 + strcpy(header_buffers, header_buffer); + + char *new_http_del = malloc(strlen(http_del) + 1); // 拷贝http_del + strcpy(new_http_del, http_del); + + char *new_https_del = malloc(strlen(https_del) + 1); // // 拷贝https_del + strcpy(new_https_del, https_del); + + char *p = strstr(header_buffers, "CONNECT"); // 判断是否是http 隧道请求 + if (p == NULL) { + char *result = NULL; + result = strtok(new_http_del, ","); + while (result != NULL) { + delete_header(header_buffers, result, '\n'); + result = strtok(NULL, ","); + } + //delete_header(header_buffers, "Host", '\n'); // 删除HOST,改变原字符串 + char *new_header_buffer = (char *) + malloc(strlen(splice_head(header_buffers, "\n", http_firsts)) + 1); + strcpy(new_header_buffer, + splice_head(header_buffers, "\n", http_firsts)); + + char *p2 = strstr(header_buffers, "\n"); + p2 = p2 + 1; + int len_http_head = strlen(header_buffers) - strlen(p2); + char *HTTP_HEAD = (char *)malloc(len_http_head + 1); //http头第一行 + strncpy_(HTTP_HEAD, header_buffers, len_http_head); + + // M + char *p3 = strstr(HTTP_HEAD, " "); + int l = strlen(HTTP_HEAD) - strlen(p3); + char *M = malloc(l + 1); + strncpy_(M, HTTP_HEAD, l); + //printf("%s", M); + + // U + p3 = p3 + 1; + char *p4 = strstr(p3, " "); + l = strlen(p3) - strlen(p4); + char *U = (char *)malloc(l + 1); + strncpy_(U, p3, l); + //printf("%s", U); + + // V + p4 = p4 + 1; + del_chr(p4, '\r'); + del_chr(p4, '\n'); + l = strlen(p4); + char *V = (char *)malloc(l); + strcpy(V, p4); + //printf("%s", V); + + int len = strlen(new_header_buffer); + int len_m = strlen(M); + new_header_buffer = + replace(new_header_buffer, &len, "[M]", 3, M, len_m); + len = strlen(new_header_buffer); + int len_u = strlen(U); + new_header_buffer = + replace(new_header_buffer, &len, "[U]", 3, U, len_u); + len = strlen(new_header_buffer); + int len_v = strlen(V); + new_header_buffer = + replace(new_header_buffer, &len, "[V]", 3, V, len_v); + len = strlen(new_header_buffer); + int len_remote_host = strlen(remote_host); + new_header_buffer = + replace(new_header_buffer, &len, "[host]", 6, remote_host, + len_remote_host); + len = strlen(new_header_buffer); + new_header_buffer = replace(new_header_buffer, &len, "\\r", 2, "\r", 1); + len = strlen(new_header_buffer); + new_header_buffer = replace(new_header_buffer, &len, "\\n", 2, "\n", 1); + + stpcpy(remote_host, http_ip); + *remote_port = http_port; + memset(header_buffer, 0, strlen(header_buffer)); + strcpy(header_buffer, new_header_buffer); + free(HTTP_HEAD); + free(M); + free(U); + free(V); + free(new_header_buffer); + *HTTPS = 0; + } else { + char *result = NULL; + result = strtok(new_https_del, ","); + while (result != NULL) { + delete_header(header_buffers, result, '\n'); + result = strtok(NULL, ","); + } + //delete_header(header_buffers, "Host", '\n'); // 删除HOST,改变原字符串 + //delete_header(header_buffers, "x-online-host", '\n'); // 删除HOST,改变原字符串 + char *new_header_buffer = (char *) + malloc(strlen(splice_head(header_buffers, "\n", https_firsts)) + 1); + strcpy(new_header_buffer, + splice_head(header_buffers, "\n", https_firsts)); + + char *p2 = strstr(header_buffers, "\n"); + p2 = p2 + 1; + int len_https_head = strlen(header_buffers) - strlen(p2); + char *HTTPS_HEAD = (char *)malloc(len_https_head + 1); //http头第一行 + strncpy_(HTTPS_HEAD, header_buffers, len_https_head); + + // M + char *p3 = strstr(HTTPS_HEAD, " "); + int l = strlen(HTTPS_HEAD) - strlen(p3); + char *M = malloc(l + 1); + strncpy_(M, HTTPS_HEAD, l); + //printf("%s", M); + + // U + p3 = p3 + 1; + char *p4 = strstr(p3, " "); + l = strlen(p3) - strlen(p4); + char *U = (char *)malloc(l + 1); + strncpy_(U, p3, l); + //printf("%s", U); + + // V + p4 = p4 + 1; + del_chr(p4, '\r'); + del_chr(p4, '\n'); + l = strlen(p4); + char *V = (char *)malloc(l); + strcpy(V, p4); + //printf("%s", V); + + int len = strlen(new_header_buffer); + int len_m = strlen(M); + new_header_buffer = + replace(new_header_buffer, &len, "[M]", 3, M, len_m); + len = strlen(new_header_buffer); + int len_u = strlen(U); + new_header_buffer = + replace(new_header_buffer, &len, "[U]", 3, U, len_u); + len = strlen(new_header_buffer); + int len_v = strlen(V); + new_header_buffer = + replace(new_header_buffer, &len, "[V]", 3, V, len_v); + len = strlen(new_header_buffer); + int len_remote_host = strlen(remote_host); + new_header_buffer = + replace(new_header_buffer, &len, "[host]", 6, remote_host, + len_remote_host); + len = strlen(new_header_buffer); + new_header_buffer = replace(new_header_buffer, &len, "\\r", 2, "\r", 1); + len = strlen(new_header_buffer); + new_header_buffer = replace(new_header_buffer, &len, "\\n", 2, "\n", 1); + + //stpcpy(remote_host, https_ip); + //*remote_port = 80; + //memset(header_buffer, 0, strlen(header_buffer)); + //strcpy(header_buffer, new_header_buffer); + + free(HTTPS_HEAD); + free(M); + free(U); + free(V); + free(new_header_buffer); + *HTTPS = 1; + } + free(http_firsts); + free(https_firsts); + free(new_http_del); + free(new_https_del); + free(header_buffers); + iniparser_freedict(ini); + return *HTTPS; +}