修改: 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
conf.h
2
conf.h
@ -3,6 +3,8 @@
|
||||
|
||||
#include "iniparser.h"
|
||||
#include "cproxy.h"
|
||||
void read_conf(char *file, conf *p);
|
||||
void free_conf(conf *p);
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -6,10 +6,10 @@ PID_FILE=log/cproxy.pid;
|
||||
http_ip=10.0.0.172;
|
||||
http_port=80;
|
||||
http_del="x-online-host,X-Online-Host,host,Host";
|
||||
http_first="[M] [U] [V]\r\n.aixiao.me\rx-online-host: [host]\r\nhost: iread.wo.com.cn\r\n";
|
||||
http_first="[M] [U] [V]\r\n.aixiao.me\rx-online-host: [host]\r\nhost: iread.wo.cn\r\n";
|
||||
|
||||
[https]
|
||||
https_ip=10.0.0.172;
|
||||
https_port=80;
|
||||
https_del="Host";
|
||||
https_first="[M] [U]?wap.10010.com [V]\r\nHost: wap.10010.com:[port]\r\nX-Online-Host: [host]:[port]\r\n";
|
||||
https_del=",Host";
|
||||
https_first="[M] iread.wo.cn//https://[host]:[port]:iread.wo.cn [V]\r\nHost: iread.wo.cn\r\n";
|
||||
|
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] 原请求端口
|
10
cproxy.c
10
cproxy.c
@ -50,8 +50,6 @@ void handle_client(int client_sock, struct sockaddr_in client_addr, conf *config
|
||||
replacement_http_head(header_buffer, remote_host, &remote_port, &SIGN, configure);
|
||||
|
||||
//printf("%s", header_buffer);
|
||||
//printf("%s\n", remote_host);
|
||||
//printf("%d\n", remote_port);
|
||||
|
||||
if ((remote_sock = create_connection(configure, SIGN)) < 0) {
|
||||
return;
|
||||
@ -225,17 +223,15 @@ void server_loop(conf * configure)
|
||||
socklen_t addrlen = sizeof(client_addr);
|
||||
|
||||
while (1) {
|
||||
client_sock =
|
||||
accept(server_sock, (struct sockaddr *)&client_addr, &addrlen);
|
||||
client_sock = accept(server_sock, (struct sockaddr *)&client_addr, &addrlen);
|
||||
|
||||
if (fork() == 0) { // 创建子进程处理客户端连接请求
|
||||
close(server_sock);
|
||||
handle_client(client_sock, client_addr, configure);
|
||||
close(client_sock);
|
||||
exit(0);
|
||||
}
|
||||
close(client_sock);
|
||||
}
|
||||
|
||||
close(server_sock);
|
||||
}
|
||||
|
||||
void start_server(conf *configure)
|
||||
|
18
cproxy.h
18
cproxy.h
@ -66,17 +66,23 @@ typedef struct CONF {
|
||||
#define HTTP_OTHERS 3
|
||||
#define HTTP_CONNECT 4
|
||||
|
||||
void read_conf(char *file, conf *p);
|
||||
void free_conf(conf * p);
|
||||
|
||||
void server_loop(conf * configure);
|
||||
char *read_data(int client_sock, char *data, int *data_len);
|
||||
void servertoclient(int remote_sock, int client_sock, char *complete_data, int *len_complete_data);
|
||||
void clienttoserver(int remote_sock, char *complete_data, int *len_complete_data);
|
||||
void handle_client(int client_sock, struct sockaddr_in client_addr, conf *configure);
|
||||
void forward_data(int source_sock, int destination_sock);
|
||||
int send_data(int socket, char *buffer, int len);
|
||||
int receive_data(int socket, char *buffer, int len);
|
||||
|
||||
void forward_data(int source_sock, int destination_sock);
|
||||
int create_connection(conf *configure, int SIGN);
|
||||
int create_server_socket(int port);
|
||||
int init_daemon(int nochdir, int noclose, conf * configure);
|
||||
void sigchld_handler(int signal);
|
||||
void server_loop(conf *configure);
|
||||
void start_server(conf * configure);
|
||||
int _main(int argc, char *argv[]);
|
||||
|
||||
void read_conf(char *file, conf *p);
|
||||
void free_conf(conf *p);
|
||||
int extract_host(char *header);
|
||||
int replacement_http_head(char *header_buffer, char *remote_host, int *remote_port, int *SIGN, conf *p);
|
||||
uint8_t request_type(char *req);
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include "cproxy.h"
|
||||
|
||||
#define BUILD(fmt...) do { fprintf(stderr,"%s %s ",__DATE__,__TIME__); fprintf(stderr, ##fmt); } while(0)
|
||||
char help_information(void);
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -1,7 +1,5 @@
|
||||
#include "cproxy_request.h"
|
||||
|
||||
void *memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen);
|
||||
|
||||
// 字符串替换
|
||||
char *replace(char *replace_memory, int *replace_memory_len, const char *src, const int src_len, const char *dest, const int dest_len)
|
||||
{
|
||||
@ -193,6 +191,17 @@ void rewrite_header()
|
||||
}
|
||||
}
|
||||
|
||||
// 判断数字有几位
|
||||
int numbin(int n)
|
||||
{
|
||||
int sum = 0;
|
||||
while (n) {
|
||||
sum++;
|
||||
n /= 10;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
// 删除字符串header_buffer中第一位到character处,并拼接string,character必须存在.(string替换第一个字符到character处)
|
||||
char *splice_head(char *header_buffer, const char *character, char *string)
|
||||
{
|
||||
@ -231,7 +240,7 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po
|
||||
char *new_http_del = malloc(strlen(p->http_del) + 1); // 拷贝http_del
|
||||
strcpy(new_http_del, p->http_del);
|
||||
|
||||
char *new_https_del = malloc(strlen(p->https_del) + 1); // // 拷贝https_del
|
||||
char *new_https_del = malloc(strlen(p->https_del) + 1); // 拷贝https_del
|
||||
strcpy(new_https_del, p->https_del);
|
||||
|
||||
if (*SIGN == HTTP) {
|
||||
@ -273,7 +282,6 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po
|
||||
//printf("%s", V);
|
||||
|
||||
char *new_header_buffer = (char *)malloc(strlen(splice_head(header_buffer_backup, "\n", http_firsts)) + 1);
|
||||
//char *new_header_buffer = (char *)malloc(BUF_SIZES);
|
||||
strcpy(new_header_buffer, splice_head(header_buffer_backup, "\n", http_firsts));
|
||||
|
||||
int len = strlen(new_header_buffer);
|
||||
@ -286,6 +294,19 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po
|
||||
new_header_buffer = replace(new_header_buffer, &len, "[U]", 3, U, len_u);
|
||||
new_header_buffer = replace(new_header_buffer, &len, "[V]", 3, V, len_v);
|
||||
new_header_buffer = replace(new_header_buffer, &len, "[host]", 6, remote_host, len_remote_host);
|
||||
|
||||
char port_copy[numbin(*remote_port) + 1];
|
||||
sprintf(port_copy, "%d", *remote_port);
|
||||
int len_remote_port = strlen(port_copy);
|
||||
new_header_buffer = replace(new_header_buffer, &len, "[port]", 6, port_copy, len_remote_port);
|
||||
|
||||
new_header_buffer = replace(new_header_buffer, &len, "\\r", 2, "\r", 1);
|
||||
new_header_buffer = replace(new_header_buffer, &len, "\\n", 2, "\n", 1);
|
||||
new_header_buffer = replace(new_header_buffer, &len, "\\b", 2, "\b", 1);
|
||||
new_header_buffer = replace(new_header_buffer, &len, "\\v", 2, "\v", 1);
|
||||
new_header_buffer = replace(new_header_buffer, &len, "\\f", 2, "\f", 1);
|
||||
new_header_buffer = replace(new_header_buffer, &len, "\\a", 2, "\a", 1);
|
||||
new_header_buffer = replace(new_header_buffer, &len, "\\t", 2, "\t", 1);
|
||||
new_header_buffer = replace(new_header_buffer, &len, "\\r", 2, "\r", 1);
|
||||
new_header_buffer = replace(new_header_buffer, &len, "\\n", 2, "\n", 1);
|
||||
|
||||
@ -332,15 +353,12 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po
|
||||
|
||||
// V
|
||||
p4 = p4 + 1;
|
||||
//del_chr(p4, '\r');
|
||||
//del_chr(p4, '\n');
|
||||
l = strlen(p4);
|
||||
char *V = (char *)malloc(l);
|
||||
strncpy_(V, p4, 8);
|
||||
//printf("%s", V);
|
||||
|
||||
char *new_header_buffer = (char *) malloc(strlen(splice_head(header_buffer_backup, "\n", https_firsts)) + 1);
|
||||
//char *new_header_buffer = (char *) malloc(BUF_SIZES);
|
||||
strcpy(new_header_buffer, splice_head(header_buffer_backup, "\n", https_firsts));
|
||||
|
||||
int len = strlen(new_header_buffer);
|
||||
@ -353,11 +371,18 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po
|
||||
new_header_buffer = replace(new_header_buffer, &len, "[V]", 3, V, len_v);
|
||||
new_header_buffer = replace(new_header_buffer, &len, "[host]", 6, remote_host, len_remote_host);
|
||||
|
||||
char port_num[9];
|
||||
sprintf(port_num, "%d", *remote_port);
|
||||
int len_remote_port = strlen(port_num);
|
||||
new_header_buffer = replace(new_header_buffer, &len, "[port]", 6, port_num, len_remote_port);
|
||||
char port_copy[numbin(*remote_port) + 1];
|
||||
sprintf(port_copy, "%d", *remote_port);
|
||||
int len_remote_port = strlen(port_copy);
|
||||
new_header_buffer = replace(new_header_buffer, &len, "[port]", 6, port_copy, len_remote_port);
|
||||
|
||||
new_header_buffer = replace(new_header_buffer, &len, "\\r", 2, "\r", 1);
|
||||
new_header_buffer = replace(new_header_buffer, &len, "\\n", 2, "\n", 1);
|
||||
new_header_buffer = replace(new_header_buffer, &len, "\\b", 2, "\b", 1);
|
||||
new_header_buffer = replace(new_header_buffer, &len, "\\v", 2, "\v", 1);
|
||||
new_header_buffer = replace(new_header_buffer, &len, "\\f", 2, "\f", 1);
|
||||
new_header_buffer = replace(new_header_buffer, &len, "\\a", 2, "\a", 1);
|
||||
new_header_buffer = replace(new_header_buffer, &len, "\\t", 2, "\t", 1);
|
||||
new_header_buffer = replace(new_header_buffer, &len, "\\r", 2, "\r", 1);
|
||||
new_header_buffer = replace(new_header_buffer, &len, "\\n", 2, "\n", 1);
|
||||
|
||||
|
@ -4,7 +4,20 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <regex.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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user