修改: Makefile
修改: conf.h 修改: conf/cproxy.ini 新文件: conf/cproxy.ini.explain 修改: cproxy.c 修改: cproxy.h 修改: cproxy_help.h 修改: cproxy_request.c 修改: cproxy_request.h
This commit is contained in:
parent
e0261c8942
commit
a2f0dc3992
2
Makefile
2
Makefile
@ -4,7 +4,7 @@ LIBS = -liniparser
|
|||||||
OBJ := cproxy
|
OBJ := cproxy
|
||||||
|
|
||||||
all: cproxy.o conf.o cproxy_request.o cproxy_help.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 a+x $(OBJ)
|
-chmod a+x $(OBJ)
|
||||||
.c.o:
|
.c.o:
|
||||||
|
2
conf.h
2
conf.h
@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#include "iniparser.h"
|
#include "iniparser.h"
|
||||||
#include "cproxy.h"
|
#include "cproxy.h"
|
||||||
|
void read_conf(char *file, conf *p);
|
||||||
|
void free_conf(conf *p);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -6,10 +6,10 @@ PID_FILE=log/cproxy.pid;
|
|||||||
http_ip=10.0.0.172;
|
http_ip=10.0.0.172;
|
||||||
http_port=80;
|
http_port=80;
|
||||||
http_del="x-online-host,X-Online-Host,host,Host";
|
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]
|
||||||
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]?wap.10010.com [V]\r\nHost: wap.10010.com:[port]\r\nX-Online-Host: [host]:[port]\r\n";
|
https_first="[M] iread.wo.cn//https://[host]:[port]:iread.wo.cn [V]\r\nHost: iread.wo.cn\r\n";
|
||||||
|
8
conf/cproxy.ini.explain
Normal file
8
conf/cproxy.ini.explain
Normal file
@ -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] 原请求端口
|
16
cproxy.c
16
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);
|
replacement_http_head(header_buffer, remote_host, &remote_port, &SIGN, configure);
|
||||||
|
|
||||||
//printf("%s", header_buffer);
|
//printf("%s", header_buffer);
|
||||||
//printf("%s\n", remote_host);
|
|
||||||
//printf("%d\n", remote_port);
|
|
||||||
|
|
||||||
if ((remote_sock = create_connection(configure, SIGN)) < 0) {
|
if ((remote_sock = create_connection(configure, SIGN)) < 0) {
|
||||||
return;
|
return;
|
||||||
@ -103,7 +101,7 @@ void forward_data(int source_sock, int destination_sock)
|
|||||||
shutdown(source_sock, SHUT_RDWR);
|
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 sockaddr_in server_addr;
|
||||||
struct hostent *server;
|
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");
|
FILE *fp = fopen(configure->server_pid_file, "w");
|
||||||
int pid;
|
int pid;
|
||||||
@ -225,20 +223,18 @@ void server_loop(conf * configure)
|
|||||||
socklen_t addrlen = sizeof(client_addr);
|
socklen_t addrlen = sizeof(client_addr);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
client_sock =
|
client_sock = accept(server_sock, (struct sockaddr *)&client_addr, &addrlen);
|
||||||
accept(server_sock, (struct sockaddr *)&client_addr, &addrlen);
|
|
||||||
|
|
||||||
if (fork() == 0) { // 创建子进程处理客户端连接请求
|
if (fork() == 0) { // 创建子进程处理客户端连接请求
|
||||||
|
close(server_sock);
|
||||||
handle_client(client_sock, client_addr, configure);
|
handle_client(client_sock, client_addr, configure);
|
||||||
close(client_sock);
|
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
close(client_sock);
|
||||||
}
|
}
|
||||||
|
|
||||||
close(server_sock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void start_server(conf * configure)
|
void start_server(conf *configure)
|
||||||
{
|
{
|
||||||
signal(SIGCHLD, sigchld_handler); // 防止子进程变成僵尸进程
|
signal(SIGCHLD, sigchld_handler); // 防止子进程变成僵尸进程
|
||||||
|
|
||||||
|
20
cproxy.h
20
cproxy.h
@ -66,17 +66,23 @@ typedef struct CONF {
|
|||||||
#define HTTP_OTHERS 3
|
#define HTTP_OTHERS 3
|
||||||
#define HTTP_CONNECT 4
|
#define HTTP_CONNECT 4
|
||||||
|
|
||||||
void read_conf(char *file, conf *p);
|
char *read_data(int client_sock, char *data, int *data_len);
|
||||||
void free_conf(conf * p);
|
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 server_loop(conf * configure);
|
|
||||||
void handle_client(int client_sock, struct sockaddr_in client_addr, conf *configure);
|
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 send_data(int socket, char *buffer, int len);
|
||||||
int receive_data(int socket, char *buffer, int len);
|
int receive_data(int socket, char *buffer, int len);
|
||||||
|
void forward_data(int source_sock, int destination_sock);
|
||||||
int create_connection(conf * configure, int SIGN);
|
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[]);
|
int _main(int argc, char *argv[]);
|
||||||
|
|
||||||
|
void read_conf(char *file, conf *p);
|
||||||
|
void free_conf(conf *p);
|
||||||
int extract_host(char *header);
|
int extract_host(char *header);
|
||||||
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);
|
||||||
uint8_t request_type(char *req);
|
uint8_t request_type(char *req);
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#include "cproxy.h"
|
#include "cproxy.h"
|
||||||
|
|
||||||
#define BUILD(fmt...) do { fprintf(stderr,"%s %s ",__DATE__,__TIME__); fprintf(stderr, ##fmt); } while(0)
|
#define BUILD(fmt...) do { fprintf(stderr,"%s %s ",__DATE__,__TIME__); fprintf(stderr, ##fmt); } while(0)
|
||||||
|
char help_information(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
#include "cproxy_request.h"
|
#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)
|
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)
|
else if (strncmp(req, "CONNECT", 7) == 0)
|
||||||
return HTTP_CONNECT;
|
return HTTP_CONNECT;
|
||||||
else if (strncmp(req, "HEAD", 4) == 0 ||
|
else if (strncmp(req, "HEAD", 4) == 0 ||
|
||||||
strncmp(req, "PUT", 3) == 0 ||
|
strncmp(req, "PUT", 3) == 0 ||
|
||||||
strncmp(req, "OPTIONS", 7) == 0 ||
|
strncmp(req, "OPTIONS", 7) == 0 ||
|
||||||
strncmp(req, "MOVE", 4) == 0 ||
|
strncmp(req, "MOVE", 4) == 0 ||
|
||||||
strncmp(req, "COPY", 4) == 0 ||
|
strncmp(req, "COPY", 4) == 0 ||
|
||||||
strncmp(req, "TRACE", 5) == 0 ||
|
strncmp(req, "TRACE", 5) == 0 ||
|
||||||
strncmp(req, "DELETE", 6) == 0 ||
|
strncmp(req, "DELETE", 6) == 0 ||
|
||||||
strncmp(req, "LINK", 4) == 0 ||
|
strncmp(req, "LINK", 4) == 0 ||
|
||||||
strncmp(req, "UNLINK", 6) == 0 ||
|
strncmp(req, "UNLINK", 6) == 0 ||
|
||||||
strncmp(req, "PATCH", 5) == 0 || strncmp(req, "WRAPPED", 7) == 0)
|
strncmp(req, "PATCH", 5) == 0 || strncmp(req, "WRAPPED", 7) == 0)
|
||||||
return HTTP_OTHERS;
|
return HTTP_OTHERS;
|
||||||
else
|
else
|
||||||
return OTHER;
|
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处)
|
// 删除字符串header_buffer中第一位到character处,并拼接string,character必须存在.(string替换第一个字符到character处)
|
||||||
char *splice_head(char *header_buffer, const char *character, char *string)
|
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);
|
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);
|
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
|
||||||
@ -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
|
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);
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
if (*SIGN == HTTP) {
|
if (*SIGN == HTTP) {
|
||||||
@ -273,7 +282,6 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po
|
|||||||
//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(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));
|
strcpy(new_header_buffer, splice_head(header_buffer_backup, "\n", http_firsts));
|
||||||
|
|
||||||
int len = strlen(new_header_buffer);
|
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, "[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, "[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, "[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, "\\r", 2, "\r", 1);
|
||||||
new_header_buffer = replace(new_header_buffer, &len, "\\n", 2, "\n", 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
|
// V
|
||||||
p4 = p4 + 1;
|
p4 = p4 + 1;
|
||||||
//del_chr(p4, '\r');
|
|
||||||
//del_chr(p4, '\n');
|
|
||||||
l = strlen(p4);
|
l = strlen(p4);
|
||||||
char *V = (char *)malloc(l);
|
char *V = (char *)malloc(l);
|
||||||
strncpy_(V, p4, 8);
|
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(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));
|
strcpy(new_header_buffer, splice_head(header_buffer_backup, "\n", https_firsts));
|
||||||
|
|
||||||
int len = strlen(new_header_buffer);
|
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, "[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, "[host]", 6, remote_host, len_remote_host);
|
||||||
|
|
||||||
char port_num[9];
|
char port_copy[numbin(*remote_port) + 1];
|
||||||
sprintf(port_num, "%d", *remote_port);
|
sprintf(port_copy, "%d", *remote_port);
|
||||||
int len_remote_port = strlen(port_num);
|
int len_remote_port = strlen(port_copy);
|
||||||
new_header_buffer = replace(new_header_buffer, &len, "[port]", 6, port_num, len_remote_port);
|
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, "\\r", 2, "\r", 1);
|
||||||
new_header_buffer = replace(new_header_buffer, &len, "\\n", 2, "\n", 1);
|
new_header_buffer = replace(new_header_buffer, &len, "\\n", 2, "\n", 1);
|
||||||
|
|
||||||
|
@ -4,7 +4,20 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <regex.h>
|
||||||
#include "cproxy.h"
|
#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
|
#endif
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user