修改: Makefile

修改:     README.md
	修改:     conf.c
	修改:     conf.h
	修改:     conf/cproxy.ini
	修改:     cproxy.c
	修改:     cproxy.h
	新文件:   cproxy_help.c
	新文件:   cproxy_help.h
	修改:     cproxy_request.c
	修改:     cproxy_request.h
	修改:     log/cproxy.pid
This commit is contained in:
aixiao 2019-02-07 17:26:48 +08:00
parent 266aade849
commit e0261c8942
12 changed files with 463 additions and 452 deletions

View File

@ -3,10 +3,10 @@ CFLAGS = -g -Wall -Werror -I../iniparser/src -L../iniparser
LIBS = -liniparser LIBS = -liniparser
OBJ := cproxy OBJ := cproxy
all: cproxy.o cproxy_request.o conf.o all: cproxy.o conf.o cproxy_request.o cproxy_help.o
$(CC) $(CFLAGS) -o $(OBJ) $^ $(LIBS) $(CC) $(CFLAGS) -o $(OBJ) $^ $(LIBS)
strip $(OBJ) strip $(OBJ)
-chmod 755 $(OBJ) -chmod a+x $(OBJ)
.c.o: .c.o:
$(CC) $(CFLAGS) -c $< $(LIBS) $(CC) $(CFLAGS) -c $< $(LIBS)

View File

@ -1,6 +1,7 @@
# cproxy # cproxy
基于mproxy修改后的Android本地代理. Android本地二级代理.
可以修改非Http tunnel消息头(request). 可以修改HTTP协议消息头(request).
可以修改HTTP协议CONNECT方法消息头.
# Build # Build

5
conf.c
View File

@ -1,6 +1,6 @@
#include "conf.h" #include "conf.h"
void read_conf(char *file, conf * p) void read_conf(char *file, conf *p)
{ {
dictionary *ini = iniparser_load(file); dictionary *ini = iniparser_load(file);
@ -101,7 +101,7 @@ err:
iniparser_freedict(ini); iniparser_freedict(ini);
} }
void free_conf(conf * p) void free_conf(conf *p)
{ {
free(p->server_pid_file); free(p->server_pid_file);
free(p->http_ip); free(p->http_ip);
@ -111,3 +111,4 @@ void free_conf(conf * p)
free(p->https_del); free(p->https_del);
free(p->https_first); free(p->https_first);
} }

1
conf.h
View File

@ -5,3 +5,4 @@
#include "cproxy.h" #include "cproxy.h"
#endif #endif

View File

@ -12,4 +12,4 @@ http_first="[M] [U] [V]\r\n.aixiao.me\rx-online-host: [host]\r\nhost: iread.wo.c
https_ip=10.0.0.172; https_ip=10.0.0.172;
https_port=80; https_port=80;
https_del="Host"; https_del="Host";
https_first="[M] [U] [V]\r\nHost: [host]\r\n"; https_first="[M] [U]?wap.10010.com [V]\r\nHost: wap.10010.com:[port]\r\nX-Online-Host: [host]:[port]\r\n";

485
cproxy.c
View File

@ -1,151 +1,97 @@
#include "cproxy.h" #include "cproxy.h"
ssize_t readLine(int fd, void *buffer, size_t n) char *read_data(int client_sock, char *data, int *data_len)
{ {
ssize_t numRead; char *new_data;
size_t totRead; int read_len;
char *buf;
char ch;
if (n <= 0 || buffer == NULL) { do {
errno = EINVAL; new_data = (char *)realloc(data, *data_len + BUF_SIZE + 1);
return -1; if (new_data == NULL) {
free(data);
return NULL;
} }
data = new_data;
buf = buffer; read_len = read(client_sock, data + *data_len, BUF_SIZE);
if (read_len <= 0) {
totRead = 0; if (read_len == 0 || *data_len == 0 || errno != EAGAIN) {
for (;;) { free(data);
numRead = receive_data(fd, &ch, 1); return NULL;
if (numRead == -1) {
if (errno == EINTR)
continue;
else
return -1; // 未知错误
} else if (numRead == 0) { // EOF
if (totRead == 0) // No bytes read; return 0
return 0;
else // Some bytes read; add '\0'
break;
} else {
if (totRead < n - 1) { // Discard > (n - 1) bytes
totRead++;
*buf++ = ch;
} }
if (ch == '\n')
break; break;
} }
} *data_len += read_len;
} while (read_len == BUF_SIZE);
*(data + *data_len) = '\0';
*buf = '\0'; return data;
return totRead;
} }
// 读取header void servertoclient(int remote_sock, int client_sock, char *complete_data, int *len_complete_data)
int read_header(int fd, void *buffer)
{ {
memset(header_buffer, 0, MAX_HEADER_SIZE); while ((*len_complete_data = read(remote_sock, complete_data, BUF_SIZE)) > 0) {
char line_buffer[2048]; write(client_sock, complete_data, *len_complete_data);
char *base_ptr = header_buffer;
for (;;) {
memset(line_buffer, 0, 2048);
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 {
return -1;
}
// 读到了空行http头结束
if (strcmp(line_buffer, "\r\n") == 0 || strcmp(line_buffer, "\n") == 0) {
break;
}
}
return 0;
} }
// 提取主机、端口 void clienttoserver(int remote_sock, char *complete_data, int *len_complete_data)
int extract_host(char *header)
{ {
char *_p = strstr(header, "CONNECT"); // 在 CONNECT 方法中解析 隧道主机名称及端口号 write(remote_sock, complete_data, *len_complete_data);
if (_p) { complete_data = NULL;
char *_p1 = strchr(_p, ' '); complete_data = 0;
char *_p2 = strchr(_p1 + 1, ':');
char *_p3 = strchr(_p1 + 1, ' ');
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);
remote_port = atoi(s_port);
} else {
strncpy(remote_host, _p1 + 1, (int)(_p3 - _p1) - 1);
remote_port = 80;
}
return 0;
}
char *p = strstr(header, "Host:");
if (!p) {
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 s_port[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:
remote_host[h_len] = '\0';
} 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;
}
return 0;
} }
// 响应隧道连接请求 // 处理客户端的连接
int send_tunnel_ok(int client_sock) void handle_client(int client_sock, struct sockaddr_in client_addr, conf *configure)
{ {
char *resp = "HTTP/1.1 200 Connection Established\r\n\r\n"; // 把字符串写入client_sock流 read_data(client_sock, header_buffer, &len_header_buffer); // 第一次读取客户端(浏览器)数据
int len = strlen(resp); SIGN = request_type(header_buffer); // 获取请求消息类型
char buffer[len + 1]; extract_host(header_buffer); // 提取真实Host
strcpy(buffer, resp); replacement_http_head(header_buffer, remote_host, &remote_port, &SIGN, configure);
if (send_data(client_sock, buffer, len) < 0) {
return -1; //printf("%s", header_buffer);
//printf("%s\n", remote_host);
//printf("%d\n", remote_port);
if ((remote_sock = create_connection(configure, SIGN)) < 0) {
return;
} }
return 0;
if (fork() == 0) {
if (SIGN == HTTP_CONNECT) {
clienttoserver(remote_sock, header_buffer, &len_header_buffer);
forward_data(client_sock, remote_sock);
} else if (SIGN == HTTP_OTHERS || SIGN == HTTP) {
forward_header(remote_sock); //普通的http请求先转发header
forward_data(client_sock, remote_sock);
}
exit(0);
}
if (fork() == 0) {
if (SIGN == HTTP_CONNECT) {
servertoclient(remote_sock, client_sock, complete_data, &len_complete_data);
} else if (SIGN == HTTP_OTHERS || SIGN == HTTP) {
forward_data(remote_sock, client_sock);
}
exit(0);
}
close(client_sock);
close(remote_sock);
}
int send_data(int socket, char *buffer, int len)
{
return send(socket, buffer, len, 0);
}
int receive_data(int socket, char *buffer, int len)
{
int n = recv(socket, buffer, len, 0);
return n;
} }
// 转发数据,读取source_sock流发送到destination_sock流
void forward_data(int source_sock, int destination_sock) void forward_data(int source_sock, int destination_sock)
{ {
char buffer[BUF_SIZE]; char buffer[BUF_SIZE];
@ -157,134 +103,79 @@ void forward_data(int source_sock, int destination_sock)
shutdown(source_sock, SHUT_RDWR); shutdown(source_sock, SHUT_RDWR);
} }
// 转发头字符串到destination_sock int create_connection(conf * configure, int SIGN)
void forward_header(int destination_sock)
{
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';
}
}
}
// 接收数据
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 sockaddr_in server_addr;
struct hostent *server; struct hostent *server;
int sock; int sock;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
return -1; return CLIENT_SOCKET_ERROR;
} }
if ((server = gethostbyname(remote_host)) == NULL) {
if (SIGN == HTTP_CONNECT) {
if ((server = gethostbyname(configure->https_ip)) == NULL) {
errno = EFAULT; errno = EFAULT;
return -1; return CLIENT_RESOLVE_ERROR;
} }
} else if (SIGN == HTTP_OTHERS || SIGN == HTTP) {
if ((server = gethostbyname(configure->http_ip)) == NULL) {
errno = EFAULT;
return CLIENT_RESOLVE_ERROR;
}
}
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;
memcpy(&server_addr.sin_addr.s_addr, server->h_addr, server->h_length); memcpy(&server_addr.sin_addr.s_addr, server->h_addr, server->h_length);
server_addr.sin_port = htons(remote_port);
if (SIGN == HTTP_CONNECT) {
server_addr.sin_port = htons(configure->https_port);
} else if (SIGN == HTTP_OTHERS || SIGN == HTTP) {
server_addr.sin_port = htons(configure->http_port);
}
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
return -1; return CLIENT_CONNECT_ERROR;
} }
return sock; return sock;
} }
// 处理客户端的连接 int create_server_socket(int port)
void handle_client(int client_sock, struct sockaddr_in client_addr, conf *m)
{ {
int HTTPS = 0; int server_sock, optval;
read_header(client_sock, header_buffer); struct sockaddr_in server_addr;
extract_host(header_buffer);
replacement_http_head(header_buffer, remote_host, &remote_port, &HTTPS, m);
printf("%s\n", remote_host); if ((server_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("%d\n", remote_port); return SERVER_SOCKET_ERROR;
printf("%d\n", HTTPS);
printf("%s", header_buffer);
if ((remote_sock = create_connection()) < 0) { // 先创建连接
return;
} }
if (fork() == 0) { // 创建子进程用于从客户端转发数据到远端socket接口 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
if (HTTPS == 0) { return SERVER_SETSOCKOPT_ERROR;
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);
}
}
if (HTTPS == 1) {
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);
}
}
exit(0);
}
if (fork() == 0) { // 创建子进程用于转发从远端socket接口过来的数据到客户端
if (HTTPS == 1) {
send_tunnel_ok(client_sock);
}
forward_data(remote_sock, client_sock);
exit(0);
} }
close(remote_sock); memset(&server_addr, 0, sizeof(server_addr));
close(client_sock); 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) {
return SERVER_BIND_ERROR;
}
if (listen(server_sock, 20) < 0) {
return SERVER_LISTEN_ERROR;
}
return server_sock;
} }
// 守护 // 守护
int init_daemon(int nochdir, int noclose, conf * p) int init_daemon(int nochdir, int noclose, conf * configure)
{ {
FILE *fp = fopen(p->server_pid_file, "w"); FILE *fp = fopen(configure->server_pid_file, "w");
int pid; int pid;
if ((pid = fork()) < 0) { if ((pid = fork()) < 0) {
@ -312,14 +203,6 @@ int init_daemon(int nochdir, int noclose, conf * p)
if (0 == nochdir) { if (0 == nochdir) {
chdir("/"); chdir("/");
} }
//close off file descriptors
/*
int i;
for (i = 0; i < 64; i++) {
close(i);
}
*/
//redirect stdin,stdout,stderror to "/dev/null" //redirect stdin,stdout,stderror to "/dev/null"
if (0 == noclose) { if (0 == noclose) {
open("/dev/null", O_RDONLY); open("/dev/null", O_RDONLY);
@ -330,107 +213,81 @@ int init_daemon(int nochdir, int noclose, conf * p)
return 0; return 0;
} }
// 判断pid文件存放目录 // 处理僵尸进程
void log_dir(conf *p)
{
char *d = strchr(p->server_pid_file, '/');
if (d != NULL) {
int l_d = p->len_server_pid_file - strlen(d);
char pid_dir[l_d];
strncpy(pid_dir, p->server_pid_file, l_d - 1);
printf("%d\n", l_d);
printf("%s\n", pid_dir);
if (access(pid_dir, F_OK) != 0) {
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");
}
}
}
void sigchld_handler(int signal) void sigchld_handler(int signal)
{ {
while (waitpid(-1, NULL, WNOHANG) > 0) ; while (waitpid(-1, NULL, WNOHANG) > 0) ;
} }
// 主函数 void server_loop(conf * configure)
{
struct sockaddr_in client_addr;
socklen_t addrlen = sizeof(client_addr);
while (1) {
client_sock =
accept(server_sock, (struct sockaddr *)&client_addr, &addrlen);
if (fork() == 0) { // 创建子进程处理客户端连接请求
handle_client(client_sock, client_addr, configure);
close(client_sock);
exit(0);
}
}
close(server_sock);
}
void start_server(conf * configure)
{
signal(SIGCHLD, sigchld_handler); // 防止子进程变成僵尸进程
if ((server_sock = create_server_socket(local_port)) < 0) {
exit(server_sock);
}
server_loop(configure);
}
int _main(int argc, char *argv[]) int _main(int argc, char *argv[])
{ {
//初始化全局变量
header_buffer = (char *)malloc(BUF_SIZE);
len_header_buffer = strlen(header_buffer);
complete_data = (char *)malloc(BUF_SIZES);
len_complete_data = strlen(complete_data);
char *inifile = "conf/cproxy.ini"; char *inifile = "conf/cproxy.ini";
conf *m = (struct CONF *)malloc(sizeof(struct CONF)); conf *configure = (struct CONF *)malloc(sizeof(struct CONF));
read_conf(inifile, m); read_conf(inifile, configure);
int PORT = m->server_port;
/* local_port = configure->server_port;
printf("%d\n", m->server_port);
printf("%s\n", m->server_pid_file);
printf("%s\n", m->http_ip);
printf("%d\n", m->http_port);
printf("%s\n", m->http_del);
printf("%s\n", m->http_first);
printf("%s\n", m->https_ip);
printf("%d\n", m->https_port);
printf("%s\n", m->https_del);
printf("%s\n", m->https_first);
*/
int opt; int opt;
char optstrs[] = ":l:d"; char optstrs[] = ":l:dh";
while (-1 != (opt = getopt(argc, argv, optstrs))) { while (-1 != (opt = getopt(argc, argv, optstrs))) {
switch (opt) { switch (opt) {
case 'l': case 'l':
PORT = atoi(optarg); local_port = atoi(optarg);
break; break;
case 'd':{ case 'd':
init_daemon(1, 1, m); init_daemon(1, 1, configure);
} break;
case 'h':
help_information();
exit(0);
break; break;
default: default:
; ;
} }
} }
header_buffer = (char *)malloc(MAX_HEADER_SIZE); start_server(configure);
free_conf(configure);
signal(SIGCHLD, sigchld_handler); // 防止子进程变成僵尸进程 free(complete_data);
free(header_buffer);
int server_sock, optval; return 0;
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) {
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) {
return -1;
}
if (listen(server_sock, 17) < 0) {
return -1;
}
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);
if (fork() == 0) { // 创建子进程处理客户端连接请求
close(server_sock);
handle_client(client_socket, clnt_addr, m);
exit(0);
}
close(client_socket);
}
free(header_buffer); // 收回内存
free_conf(m);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])

View File

@ -14,17 +14,37 @@
#include <unistd.h> #include <unistd.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <netinet/in.h> #include <netinet/in.h>
#include<sys/types.h> #include<sys/types.h>
#include<sys/stat.h> #include<sys/stat.h>
#include<fcntl.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 MAX_HEADER_SIZE 8192 #define BUF_SIZES 1024
#define SERVER_SOCKET_ERROR -1
#define SERVER_SETSOCKOPT_ERROR -2
#define SERVER_BIND_ERROR -3
#define SERVER_LISTEN_ERROR -4
#define CLIENT_SOCKET_ERROR -5
#define CLIENT_RESOLVE_ERROR -6
#define CLIENT_CONNECT_ERROR -7
char remote_host[128];
int remote_port;
int local_port;
int server_sock;
int client_sock;
int remote_sock;
char *header_buffer;
int len_header_buffer;
char *complete_data;
int len_complete_data;
int SIGN;
// 配置文件结构
typedef struct CONF { typedef struct CONF {
int server_port; // server module int server_port; // server module
char *server_pid_file; char *server_pid_file;
@ -40,34 +60,28 @@ typedef struct CONF {
int len_https_ip, len_https_del, len_https_first; int len_https_ip, len_https_del, len_https_first;
} conf; } conf;
void read_conf(char *file, conf * p); // 请求类型
#define OTHER 1
#define HTTP 2
#define HTTP_OTHERS 3
#define HTTP_CONNECT 4
void read_conf(char *file, conf *p);
void free_conf(conf * p); void free_conf(conf * p);
char remote_host[128]; void server_loop(conf * configure);
int remote_port; void handle_client(int client_sock, struct sockaddr_in client_addr, conf *configure);
int local_port;
int server_sock;
int client_sock;
int remote_sock;
char *header_buffer;
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_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);
int _main(int argc, char *argv[]);
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);
void forward_header(int destination_sock); void forward_header(int destination_sock);
void rewrite_header(); void rewrite_header();
int receive_data(int socket, char *buffer, int len); char help_information(void);
int send_data(int socket, char *buffer, int len);
int create_connection();
void handle_client(int client_sock, struct sockaddr_in client_addr, conf * p);
int init_daemon(int nochdir, int noclose, conf *p);
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, conf * p);
#endif #endif

36
cproxy_help.c Normal file
View File

@ -0,0 +1,36 @@
#include "cproxy_help.h"
char help_information(void)
{
static const char name[] = "cproxy";
static const char subject[] = "proxy server";
static const struct {
const char *a, *b, *c, *d;
} author = {
"aixiao@aixiao.me", "aixiao", "Author:", "Email :",};
static const char usage[] =
"usage: [-d] [-h]";
static const char *s_help[] = {
" -h: help information",
" -d: daemon",
"",
0
};
fprintf(stderr, "%s %s\n", name, subject);
fprintf(stderr, "%s %s\n", author.c, author.b);
fprintf(stderr, "%s %s\n", author.d, author.a);
fprintf(stderr, "%s %s\n", name, usage);
int l;
for (l = 0; s_help[l]; l++) {
fprintf(stderr, "%s\n", s_help[l]);
}
BUILD("Compile、link.\n");
return 0;
}

10
cproxy_help.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef CPROXY_HELP
#define CPROXY_HELP
#include <stdio.h>
#include "cproxy.h"
#define BUILD(fmt...) do { fprintf(stderr,"%s %s ",__DATE__,__TIME__); fprintf(stderr, ##fmt); } while(0)
#endif

View File

@ -1,11 +1,9 @@
#include "cproxy_request.h" #include "cproxy_request.h"
void *memmem(const void *haystack, size_t haystacklen, const void *needle, void *memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen);
size_t needlelen);
// 字符串替换 // 字符串替换
char *replace(char *replace_memory, int *replace_memory_len, const char *src, char *replace(char *replace_memory, int *replace_memory_len, const char *src, const int src_len, const char *dest, const int dest_len)
const int src_len, const char *dest, const int dest_len)
{ {
if (!replace_memory || !src || !dest) if (!replace_memory || !src || !dest)
return replace_memory; return replace_memory;
@ -64,14 +62,14 @@ char *replace(char *replace_memory, int *replace_memory_len, const char *src,
// 删除单个字符 // 删除单个字符
void del_chr(char *s, char ch) void del_chr(char *s, char ch)
{ {
char *t = s; //目标指针先指向原串头 char *t = s; // 目标指针先指向原串头
while (*s != '\0') //遍历字符串s while (*s != '\0') // 遍历字符串s
{ {
if (*s != ch) //如果当前字符不是要删除的,则保存到目标串中 if (*s != ch) // 如果当前字符不是要删除的,则保存到目标串中
*t++ = *s; *t++ = *s;
s++; //检查下一个字符 s++; // 检查下一个字符
} }
*t = '\0'; //置目标串结束符。 *t = '\0'; // 置目标串结束符。
} }
// strncpy()封装 // strncpy()封装
@ -90,42 +88,145 @@ char *strncpy_(char *dest, const char *src, size_t n)
} }
} }
// 删除字符串header_buffer中第一位到ch处,并拼接str,ch必须存在. uint8_t request_type(char *req)
char *splice_head(char *header_buffer, const char *ch, char *str)
{ {
int len_http_first = strlen(str); if (strncmp(req, "GET", 3) == 0 || strncmp(req, "POST", 4) == 0)
char *p1 = strstr(header_buffer, ch); return HTTP;
p1 = p1 + 1; else if (strncmp(req, "CONNECT", 7) == 0)
return HTTP_CONNECT;
char *s = (char *)malloc(len_http_first + strlen(p1) + 1); else if (strncmp(req, "HEAD", 4) == 0 ||
strcpy(s, str); // 拼接 strncmp(req, "PUT", 3) == 0 ||
return strcat(s, p1); 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;
} }
// 删除字符串header_buffer中ch到str处,ch为空返回原字符串. int extract_host(char *header)
char *delete_header(char *header_buffer, const char *ch, int str) {
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];
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);
remote_port = 80;
}
return 0;
}
char *p = strstr(header, "Host:");
if (!p) {
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 s_port[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:
remote_host[h_len] = '\0';
} 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;
}
return 0;
}
// 转发头字符串到destination_sock
void forward_header(int destination_sock)
{
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';
}
}
}
// 删除字符串header_buffer中第一位到character处,并拼接string,character必须存在.(string替换第一个字符到character处)
char *splice_head(char *header_buffer, const char *character, char *string)
{
int len_first = strlen(string);
char *p1 = strstr(header_buffer, character);
p1 = p1 + 1;
char new_string[len_first + strlen(p1) + 1];
strcpy(new_string, string); // 拼接
return strcat(new_string, p1);
}
// 删除字符串header_buffer中character到string处,character为空返回原字符串.
char *delete_header(char *header_buffer, const char *character, int string)
{ {
int len_header_buffer = strlen(header_buffer); int len_header_buffer = strlen(header_buffer);
char *p1 = strstr(header_buffer, ch); char *p1 = strstr(header_buffer, character);
if (p1 == NULL) { if (p1 == NULL) {
return header_buffer; return header_buffer;
} }
char *p2 = strchr(p1, str); char *p2 = strchr(p1, string);
int l = len_header_buffer - strlen(p1); int l = len_header_buffer - strlen(p1);
header_buffer[l] = '\0'; header_buffer[l] = '\0';
return strcat(header_buffer, p2 + 1); return strcat(header_buffer, p2 + 1);
} }
int replacement_http_head(char *header_buffer, char *remote_host, int replacement_http_head(char *header_buffer, char *remote_host, int *remote_port, int *SIGN, conf * p)
int *remote_port, int *HTTPS, conf *p)
{ {
char *http_firsts = (char *)malloc(strlen(p->http_first) + 1); char *http_firsts = (char *)malloc(strlen(p->http_first) + 1);
strcpy(http_firsts, p->http_first); // 拷贝http_first strcpy(http_firsts, p->http_first); // 拷贝http_first
char *https_firsts = (char *)malloc(strlen(p->https_first) + 1); char *https_firsts = (char *)malloc(strlen(p->https_first) + 1);
strcpy(https_firsts, p->https_first); // 拷贝https_first strcpy(https_firsts, p->https_first); // 拷贝https_first
char *header_buffers = (char *)malloc(strlen(header_buffer) + 1); // 拷贝原字符串 char *header_buffer_backup = (char *)malloc(strlen(header_buffer) + 1); // 拷贝原字符串
strcpy(header_buffers, header_buffer); strcpy(header_buffer_backup, header_buffer);
char *new_http_del = malloc(strlen(p->http_del) + 1); // 拷贝http_del char *new_http_del = malloc(strlen(p->http_del) + 1); // 拷贝http_del
strcpy(new_http_del, p->http_del); strcpy(new_http_del, p->http_del);
@ -133,25 +234,19 @@ int replacement_http_head(char *header_buffer, char *remote_host,
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); strcpy(new_https_del, p->https_del);
char *con = strstr(header_buffers, "CONNECT"); // 判断是否是http 隧道请求 if (*SIGN == HTTP) {
if (con == NULL) {
char *result = NULL; char *result = NULL;
result = strtok(new_http_del, ","); result = strtok(new_http_del, ",");
while (result != NULL) { while (result != NULL) {
delete_header(header_buffers, result, '\n'); delete_header(header_buffer_backup, result, '\n');
result = strtok(NULL, ","); 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"); char *p2 = strstr(header_buffer_backup, "\n");
p2 = p2 + 1; p2 = p2 + 1;
int len_http_head = strlen(header_buffers) - strlen(p2); int len_http_head = strlen(header_buffer_backup) - strlen(p2);
char *HTTP_HEAD = (char *)malloc(len_http_head + 1); //http头第一行 char *HTTP_HEAD = (char *)malloc(len_http_head + 1); //http头第一行
strncpy_(HTTP_HEAD, header_buffers, len_http_head); strncpy_(HTTP_HEAD, header_buffer_backup, len_http_head);
// M // M
char *p3 = strstr(HTTP_HEAD, " "); char *p3 = strstr(HTTP_HEAD, " ");
@ -177,55 +272,48 @@ int replacement_http_head(char *header_buffer, char *remote_host,
strcpy(V, p4); strcpy(V, p4);
//printf("%s", V); //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); int len = strlen(new_header_buffer);
int len_m = strlen(M); 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); 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); 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); int len_remote_host = strlen(remote_host);
new_header_buffer =
replace(new_header_buffer, &len, "[host]", 6, remote_host, new_header_buffer = replace(new_header_buffer, &len, "[M]", 3, M, len_m);
len_remote_host); new_header_buffer = replace(new_header_buffer, &len, "[U]", 3, U, len_u);
len = strlen(new_header_buffer); 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);
new_header_buffer = replace(new_header_buffer, &len, "\\r", 2, "\r", 1); 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); new_header_buffer = replace(new_header_buffer, &len, "\\n", 2, "\n", 1);
stpcpy(remote_host, p->http_ip); //stpcpy(p->http_ip, remote_host);
*remote_port = p->http_port; //p->http_port = *remote_port;
memset(header_buffer, 0, strlen(header_buffer)); memset(header_buffer, 0, strlen(header_buffer));
strcpy(header_buffer, new_header_buffer); strcpy(header_buffer, new_header_buffer);
len_header_buffer = strlen(header_buffer);
free(HTTP_HEAD); free(HTTP_HEAD);
free(M); free(M);
free(U); free(U);
free(V); free(V);
free(new_header_buffer); free(new_header_buffer);
*HTTPS = 0;
} else { } else if (*SIGN == HTTP_CONNECT) {
char *result = NULL; char *result = NULL;
result = strtok(new_https_del, ","); result = strtok(new_https_del, ",");
while (result != NULL) { while (result != NULL) {
delete_header(header_buffers, result, '\n'); delete_header(header_buffer_backup, result, '\n');
result = strtok(NULL, ","); 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"); char *p2 = strstr(header_buffer_backup, "\n");
p2 = p2 + 1; p2 = p2 + 1;
int len_https_head = strlen(header_buffers) - strlen(p2); int len_https_head = strlen(header_buffer_backup) - strlen(p2);
char *HTTPS_HEAD = (char *)malloc(len_https_head + 1); //http头第一行 char *HTTPS_HEAD = (char *)malloc(len_https_head + 1); //https头第一行
strncpy_(HTTPS_HEAD, header_buffers, len_https_head); strncpy_(HTTPS_HEAD, header_buffer_backup, len_https_head);
// M // M
char *p3 = strstr(HTTPS_HEAD, " "); char *p3 = strstr(HTTPS_HEAD, " ");
@ -244,51 +332,53 @@ int replacement_http_head(char *header_buffer, char *remote_host,
// V // V
p4 = p4 + 1; p4 = p4 + 1;
del_chr(p4, '\r'); //del_chr(p4, '\r');
del_chr(p4, '\n'); //del_chr(p4, '\n');
l = strlen(p4); l = strlen(p4);
char *V = (char *)malloc(l); char *V = (char *)malloc(l);
strcpy(V, p4); strncpy_(V, p4, 8);
//printf("%s", V); //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); int len = strlen(new_header_buffer);
int len_m = strlen(M); 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); 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); 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); int len_remote_host = strlen(remote_host);
new_header_buffer = new_header_buffer = replace(new_header_buffer, &len, "[M]", 3, M, len_m);
replace(new_header_buffer, &len, "[host]", 6, remote_host, new_header_buffer = replace(new_header_buffer, &len, "[U]", 3, U, len_u);
len_remote_host); new_header_buffer = replace(new_header_buffer, &len, "[V]", 3, V, len_v);
len = strlen(new_header_buffer); 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);
new_header_buffer = replace(new_header_buffer, &len, "\\r", 2, "\r", 1); 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); new_header_buffer = replace(new_header_buffer, &len, "\\n", 2, "\n", 1);
//stpcpy(remote_host, p->https_ip); //stpcpy(p->https_ip, remote_host); // 走真实IP非代理
//*remote_port = p->https_port; //p->https_port = *remote_port;
memset(header_buffer, 0, strlen(header_buffer)); memset(header_buffer, 0, strlen(header_buffer));
strcpy(header_buffer, new_header_buffer); memcpy(header_buffer, new_header_buffer, strlen(new_header_buffer));
len_header_buffer = strlen(header_buffer);
free(HTTPS_HEAD); free(HTTPS_HEAD);
free(M); free(M);
free(U); free(U);
free(V); free(V);
free(new_header_buffer); free(new_header_buffer);
*HTTPS = 1;
} }
free(http_firsts); free(http_firsts);
free(https_firsts); free(https_firsts);
free(new_http_del); free(new_http_del);
free(new_https_del); free(new_https_del);
free(header_buffers); free(header_buffer_backup);
return *HTTPS; return 1;
} }

View File

@ -7,3 +7,4 @@
#include "cproxy.h" #include "cproxy.h"
#endif #endif

View File

@ -1 +1 @@
3006 22338