修改: Makefile

新文件:   README.md
	新文件:   conf/cproxy.ini
	修改:     cproxy.c
	新文件:   cproxy.h
	新文件:   cproxy_request.c
This commit is contained in:
aixiao 2019-01-10 23:12:51 +08:00
parent 8fb56a9b57
commit daa409875b
6 changed files with 628 additions and 261 deletions

View File

@ -1,13 +1,14 @@
CC = gcc CC = gcc
CFLAGS = -g -Wall -Werror CFLAGS = -g -Wall -Werror -I./iniparser/src -L ./iniparser
LIBS = -liniparser
all: cproxy all: cproxy
cproxy: cproxy.o cproxy: cproxy.o
$(CC) $(CFLAGS) cproxy.o -o cproxy $(CC) $(CFLAGS) cproxy.o cproxy_request.o -o cproxy $(LIBS)
cproxy.o: cproxy.o:
$(CC) $(CFLAGS) -c cproxy.c $(CC) $(CFLAGS) -c cproxy.c cproxy_request.c
clean: clean:
rm -rf *.o rm -rf *.o

13
README.md Normal file
View File

@ -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

15
conf/cproxy.ini Normal file
View File

@ -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";

466
cproxy.c
View File

@ -12,8 +12,14 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <netinet/in.h> #include <netinet/in.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <errno.h>
#include "iniparser.h"
#define LOG(fmt...) do { fprintf(stderr,"%s %s ",__DATE__,__TIME__); fprintf(stderr, ##fmt); } while(0)
#define BUF_SIZE 8192 #define BUF_SIZE 8192
#define PORT 9606
#define MAX_HEADER_SIZE 8192 #define MAX_HEADER_SIZE 8192
char remote_host[128]; char remote_host[128];
@ -22,27 +28,11 @@ int local_port;
int server_sock; int server_sock;
int client_sock; int client_sock;
int remote_sock; int remote_sock;
char * header_buffer ; char *header_buffer;
enum int HTTPS = 0;
{
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[]) ;
#include "cproxy.h"
ssize_t readLine(int fd, void *buffer, size_t n) ssize_t readLine(int fd, void *buffer, size_t n)
{ {
@ -90,34 +80,30 @@ ssize_t readLine(int fd, void *buffer, size_t n)
return totRead; 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 line_buffer[2048];
char * base_ptr = header_buffer; char *base_ptr = header_buffer;
for(;;) for (;;) {
{ memset(line_buffer, 0, 2048);
memset(line_buffer,0,2048);
int total_read = readLine(fd,line_buffer,2048); int total_read = readLine(fd, line_buffer, 2048);
if(total_read <= 0) if (total_read <= 0) {
{
return -1; return -1;
} }
// 防止header缓冲区蛮越界 // 防止header缓冲区蛮越界
if(base_ptr + total_read - header_buffer <= MAX_HEADER_SIZE) if (base_ptr + total_read - header_buffer <= MAX_HEADER_SIZE) {
{ strncpy(base_ptr, line_buffer, total_read);
strncpy(base_ptr,line_buffer,total_read);
base_ptr += total_read; base_ptr += total_read;
} else } else {
{
return -1; return -1;
} }
// 读到了空行http头结束 // 读到了空行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; 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 /"); char *_p = strstr(header, "CONNECT"); // 在 CONNECT 方法中解析 隧道主机名称及端口号
if(p) { if (_p) {
char * p1 = strchr(p+4,' '); char *_p1 = strchr(_p, ' ');
strncpy(output,p+4,(int)(p1 - p - 4) ); char *_p2 = strchr(_p1 + 1, ':');
} char *_p3 = strchr(_p1 + 1, ' ');
} if (_p2) {
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)
{
char s_port[10]; char s_port[10];
bzero(s_port,10); bzero(s_port, 10);
strncpy(remote_host,_p1+1,(int)(_p2 - _p1) - 1); strncpy(remote_host, _p1 + 1, (int)(_p2 - _p1) - 1);
strncpy(s_port,_p2+1,(int) (_p3 - _p2) -1); strncpy(s_port, _p2 + 1, (int)(_p3 - _p2) - 1);
remote_port = atoi(s_port); remote_port = atoi(s_port);
} else } else {
{ strncpy(remote_host, _p1 + 1, (int)(_p3 - _p1) - 1);
strncpy(remote_host,_p1+1,(int)(_p3 - _p1) -1);
remote_port = 80; remote_port = 80;
} }
return 0; return 0;
} }
char * p = strstr(header,"Host:"); char *p = strstr(header, "Host:");
if(!p) if (!p) {
{
return -1; return -1;
} }
char * p1 = strchr(p,'\n'); char *p1 = strchr(p, '\n');
if(!p1) if (!p1) {
{
return -1; return -1;
} }
char * p2 = strchr(p + 5,':'); // 5是指'Host:'的长度 char *p2 = strchr(p + 5, ':'); // 5是指'Host:'的长度
if(p2 && p2 < p1) if (p2 && p2 < p1) {
{ int p_len = (int)(p1 - p2 - 1);
int p_len = (int)(p1 - p2 -1);
char s_port[p_len]; char s_port[p_len];
strncpy(s_port,p2+1,p_len); strncpy(s_port, p2 + 1, p_len);
s_port[p_len] = '\0'; s_port[p_len] = '\0';
remote_port = atoi(s_port); remote_port = atoi(s_port);
int h_len = (int)(p2 - p -5 -1 ); int h_len = (int)(p2 - p - 5 - 1);
strncpy(remote_host,p + 5 + 1 ,h_len); // Host: strncpy(remote_host, p + 5 + 1, h_len); // Host:
remote_host[h_len] = '\0'; remote_host[h_len] = '\0';
} else } else {
{ int h_len = (int)(p1 - p - 5 - 1 - 1);
int h_len = (int)(p1 - p - 5 -1 -1); strncpy(remote_host, p + 5 + 1, h_len);
strncpy(remote_host,p + 5 + 1,h_len);
remote_host[h_len] = '\0'; remote_host[h_len] = '\0';
remote_port = 80; remote_port = 80;
} }
@ -200,92 +169,134 @@ int extract_host(const char * header)
// 响应隧道连接请求 // 响应隧道连接请求
int send_tunnel_ok(int client_sock) 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); int len = strlen(resp);
char buffer[len+1]; char buffer[len + 1];
strcpy(buffer,resp); strcpy(buffer, resp);
if(send_data(client_sock,buffer,len) < 0) if (send_data(client_sock, buffer, len) < 0) {
{
return -1; return -1;
} }
return 0; return 0;
} }
void hand_mproxy_info_req(int sock, char * header) { // 转发数据,读取source_sock流发送到destination_sock流
char server_path[255] ; void forward_data(int source_sock, int destination_sock)
char response[8192]; {
extract_server_path(header,server_path); 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]; // 转发头字符串到destination_sock
sprintf(response,"HTTP/1.0 200 OK\nServer: CProxy/0.1\n\ void forward_header(int destination_sock)
Content-type: text/html; charset=utf-8\n\n\ {
<html><body>\ rewrite_header();
<pre>%s</pre>\ int len = strlen(header_buffer);
</body></html>\n",info_buf); 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) void handle_client(int client_sock, struct sockaddr_in client_addr)
{ {
int is_http_tunnel = 0; read_header(client_sock, header_buffer);
if(strlen(remote_host) == 0) // 未指定远端主机名称从http 请求 HOST 字段中获取 extract_host(header_buffer);
{ replacement_http_head(header_buffer, remote_host, &remote_port, &HTTPS);
if(read_header(client_sock,header_buffer) < 0)
{
return;
} else
{
char * p = strstr(header_buffer,"CONNECT"); // 判断是否是http 隧道请求
if(p)
{
is_http_tunnel = 1;
}
if(strstr(header_buffer,"GET /cproxy") > 0) printf("%s\n", remote_host);
{ printf("%d\n", remote_port);
hand_mproxy_info_req(client_sock,header_buffer); printf("%d\n", HTTPS);
return; printf("%s", header_buffer);
}
if(extract_host(header_buffer) < 0) if ((remote_sock = create_connection()) < 0) { // 先创建连接
{
return;
}
}
}
if ((remote_sock = create_connection()) < 0) {
return; return;
} }
if (fork() == 0) { // 创建子进程用于从客户端转发数据到远端socket接口 if (fork() == 0) { // 创建子进程用于从客户端转发数据到远端socket接口
if (HTTPS == 0) {
if(strlen(header_buffer) > 0 && !is_http_tunnel)
{
forward_header(remote_sock); //普通的http请求先转发header forward_header(remote_sock); //普通的http请求先转发header
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);
}
} }
forward_data(client_sock, remote_sock);
exit(0); exit(0);
} }
if (fork() == 0) { // 创建子进程用于转发从远端socket接口过来的数据到客户端 if (fork() == 0) { // 创建子进程用于转发从远端socket接口过来的数据到客户端
if (HTTPS == 1) {
if(io_flag == W_S_ENC)
{
io_flag = R_C_DEC; // 发送请求给服务端进行编码,读取服务端的响应则进行解码
} else if (io_flag == R_C_DEC)
{
io_flag = W_S_ENC; //接收客户端请求进行解码,那么响应客户端请求需要编码
}
if(is_http_tunnel)
{
send_tunnel_ok(client_sock); send_tunnel_ok(client_sock);
} }
forward_data(remote_sock, client_sock); forward_data(remote_sock, client_sock);
exit(0); exit(0);
} }
@ -294,125 +305,112 @@ void handle_client(int client_sock, struct sockaddr_in client_addr)
close(client_sock); close(client_sock);
} }
void forward_data(int source_sock, int destination_sock) { // 守护
char buffer[BUF_SIZE]; int init_daemon(int nochdir, int noclose)
int n; {
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;
while ((n = receive_data(source_sock, buffer, BUF_SIZE)) > 0) if ((pid = fork()) < 0) {
{ return -1;
send_data(destination_sock, buffer, n); } else if (0 != pid) {
exit(0);
}
//child 1 continues...
//become session leader
if (setsid() < 0) {
return -1;
} }
shutdown(destination_sock, SHUT_RDWR); signal(SIGHUP, SIG_IGN);
shutdown(source_sock, SHUT_RDWR); if ((pid = fork()) < 0) {
} return -1;
} else if (0 != pid) {
void forward_header(int destination_sock) fprintf(fp, "%d", pid);
{ exit(0);
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';
} }
} //child 2 continues...
}
int receive_data(int socket, char * buffer, int len) //change working directory
{ if (0 == nochdir) {
int n = recv(socket, buffer, len, 0); chdir("/");
if(io_flag == R_C_DEC && n > 0) }
{ //close off file descriptors
/*
int i; int i;
for(i = 0; i< n; i++ ) for (i = 0; i < 64; i++) {
{ close(i);
buffer[i] ^= 1;
}
} }
*/
return n; //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;
} }
int send_data(int socket,char * buffer,int len) void sigchld_handler(int signal)
{ {
if(io_flag == W_S_ENC) while (waitpid(-1, NULL, WNOHANG) > 0) ;
{
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) {
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;
} }
// 主函数
int _main(int argc, char *argv[]) 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; int server_sock, optval;
struct sockaddr_in server_addr; struct sockaddr_in server_addr;
if ((server_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { if ((server_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
return -1; 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; return -1;
} }
memset(&server_addr, 0, sizeof(server_addr)); memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET; server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT); server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = INADDR_ANY; 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; return -1;
} }
if (listen(server_sock, 17) < 0) { if (listen(server_sock, 17) < 0) {
@ -422,21 +420,21 @@ int _main(int argc, char *argv[])
struct sockaddr_in clnt_addr; struct sockaddr_in clnt_addr;
socklen_t clnt_addr_size = sizeof(clnt_addr); socklen_t clnt_addr_size = sizeof(clnt_addr);
while (1) { while (1) {
int client_socket = int client_socket = accept(server_sock, (struct sockaddr *)&clnt_addr,
accept(server_sock, (struct sockaddr *) &clnt_addr,
&clnt_addr_size); &clnt_addr_size);
if (fork() == 0) { // 创建子进程处理客户端连接请求 if (fork() == 0) { // 创建子进程处理客户端连接请求
close(server_sock); close(server_sock);
handle_client(client_socket, clnt_addr); handle_client(client_socket, clnt_addr);
printf("%s", header_buffer);
exit(0); exit(0);
} }
close(client_socket); close(client_socket);
} }
free(header_buffer); // 收回内存
iniparser_freedict(ini);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
return _main(argc,argv); return _main(argc, argv);
} }

18
cproxy.h Normal file
View File

@ -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);

322
cproxy_request.c Normal file
View File

@ -0,0 +1,322 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#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;
}