Fix getting the host and port functions extract_ Host(), do not process post header temporarily.

This commit is contained in:
aixiao 2020-08-16 07:58:53 +08:00
parent 2e19c24eff
commit 3c8a3747ef
16 changed files with 253 additions and 286 deletions

View File

@ -8,21 +8,21 @@ global {
}
http {
http_ip=47.240.75.93;
http_ip=8.210.203.112;
http_port=124;
http_del="x-online-host,X-Online-Host,host,Host";
http_first="[M] [U] [V]\r\nHost: [H]\r\n";
http_first="[M] http://[host][U] [V]\r\nHost: [host]\r\n";
//strrep="Windows NT 10.0->Linux";
//regrep="Host*.+?->Host: hu60.cn:443";
//regrep="User-Agent:*.+?->User-Agent: aixiao.me";
}
https {
https_ip=47.240.75.93;
https_ip=8.210.203.112;
https_port=124;
https_del="Host,host,x-online-host";
https_first="[M] [U] [V]\r\nHost: [H]\r\n";
strrep="Windows NT 10.0->Linux";
//regrep="Host*.+?->Host: hu60.cn:443";
https_first="[M] [U] [V]\r\nHost: [host]\r\n";
//strrep="Windows NT 10.0->Linux";
//regrep="Host*.+?->Host: [H]";
}
httpdns {

View File

@ -1,7 +1,7 @@
CROSS_COMPILE ?=
CC := $(CROSS_COMPILE)gcc
STRIP := $(CROSS_COMPILE)strip
CFLAGS += -g -O2 -Wall -pthread -static
CFLAGS += -g -O2 -Wall -pthread -fcommon
LIBS =
OBJ := CProxy
@ -14,3 +14,5 @@ clean:
rm -rf *.o
rm $(OBJ)
android:
ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=Application.mk APP_BUILD_SCRIPT=Android.mk

View File

@ -2,7 +2,7 @@
EPOLL多路复用IO, Android/Linux本地二级代理.
可以修改HTTP协议消息头(request).
可以修改HTTP协议CONNECT方法消息头.
可以修改HTTP协议GET方法消息头.
### Build
Linux编译:
@ -23,11 +23,9 @@
Usage: [-?hpt] [-s signal] [-c filename]
Options:
-l --local_address : localip:localport
-f --remote_address : remoteip:remote:port
-p --process : process number, default: 2
-t --timeout : timeout minute, default: no timeout
-e --coding : ssl coding, default: [0-128]
-p --process : process number
-t --timeout : timeout minute
-e --coding : ssl coding, [0-128]
-s --signal : send signal to a master process: stop, quit, restart, reload, status
-c --config : set configuration file, default: CProxy.conf
-? -h --? --help : help information
@ -35,13 +33,13 @@
Mar 22 2020 09:29:11 Compile、link.
#启动
./CProxy -c CProxy.ini
./CProxy -c CProxy.conf
#关闭
./CProxy -s stop
#重启
./CProxy -s reload -c CProxy.ini
./CProxy -s reload -c CProxy.conf
or
./CProxy -s restart -c CProxy.ini
./CProxy -s restart -c CProxy.conf
#状态(只打印 Pid)
./CProxy -s status

139
conf.c
View File

@ -142,11 +142,13 @@ static void parse_http_module(char *content, conf * p)
} else if (strcasecmp(var, "http_del") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->http_del = (char *)malloc(val_begin_len);
memset(p->http_del, 0, val_begin_len);
memcpy(p->http_del, val_begin, val_begin_len);
p->http_del_len = val_begin_len;
} else if (strcasecmp(var, "http_first") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->http_first = (char *)malloc(val_begin_len);
memset(p->http_first, 0, val_begin_len);
memcpy(p->http_first, val_begin, val_begin_len);
p->http_first_len = val_begin_len;
} else if (strcasecmp(var, "strrep") == 0) {
@ -155,19 +157,21 @@ static void parse_http_module(char *content, conf * p)
p->http_strrep = (char *)malloc(val_begin_len);
if (p->http_strrep == NULL)
free(p->http_strrep);
memset(p->http_strrep, 0, val_begin_len);
memcpy(p->http_strrep, val_begin, val_begin_len);
char *p1 = strstr(val_begin, "->");
printf("p1 %s\n", p1);
p->http_strrep_aim = (char *)malloc(val_begin_len - strlen(p1 + 2) - 2 + 1);
if (p->http_strrep_aim == NULL) {
free(p->http_strrep_aim);
}
memset(p->http_strrep_aim, 0, val_begin_len - strlen(p1 + 2) - 2 + 1);
strncpy_(p->http_strrep_aim, val_begin, val_begin_len - strlen(p1 + 2) - 3); // 实际 val_begin_len 多1
p->http_strrep_obj = (char *)malloc(strlen(p1 + 2) + 1);
if (p->http_strrep_obj == NULL) {
free(p->http_strrep_obj);
}
memset(p->http_strrep_obj, 0, strlen(p1 + 2) + 1);
strncpy_(p->http_strrep_obj, p1 + 2, strlen(p1 + 2));
p->http_strrep_aim_len = strlen(p->http_strrep_aim);
p->http_strrep_obj_len = strlen(p->http_strrep_obj);
@ -177,6 +181,7 @@ static void parse_http_module(char *content, conf * p)
p->http_regrep = (char *)malloc(val_begin_len);
if (p->http_regrep == NULL)
free(p->http_regrep);
memset(p->http_regrep, 0, val_begin_len);
memcpy(p->http_regrep, val_begin, val_begin_len);
char *p1 = strstr(val_begin, "->");
@ -184,11 +189,13 @@ static void parse_http_module(char *content, conf * p)
if (p->http_regrep_aim == NULL) {
free(p->http_regrep_aim);
}
memset(p->http_regrep_aim, 0, val_begin_len - strlen(p1 + 2) - 2 + 1);
strncpy_(p->http_regrep_aim, val_begin, val_begin_len - strlen(p1 + 2) - 3);
p->http_regrep_obj = (char *)malloc(strlen(p1 + 2) + 1);
if (p->http_regrep_obj == NULL) {
free(p->http_regrep_obj);
}
memset(p->http_regrep_obj, 0, strlen(p1 + 2) + 1);
strncpy_(p->http_regrep_obj, p1 + 2, strlen(p1 + 2));
p->http_regrep_aim_len = strlen(p->http_regrep_aim);
p->http_regrep_obj_len = strlen(p->http_regrep_obj);
@ -207,17 +214,20 @@ static void parse_https_module(char *content, conf * p)
if (strcasecmp(var, "https_ip") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->https_ip = (char *)malloc(val_begin_len);
memset(p->https_ip, 0, val_begin_len);
memcpy(p->https_ip, val_begin, val_begin_len);
} else if (strcasecmp(var, "https_port") == 0) {
p->https_port = atoi(val_begin);
} else if (strcasecmp(var, "https_del") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->https_del = (char *)malloc(val_begin_len);
memset(p->https_del, 0, val_begin_len);
memcpy(p->https_del, val_begin, val_begin_len);
p->https_del_len = val_begin_len;
} else if (strcasecmp(var, "https_first") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->https_first = (char *)malloc(val_begin_len);
memset(p->https_first, 0, val_begin_len);
memcpy(p->https_first, val_begin, val_begin_len);
p->https_first_len = val_begin_len;
} else if (strcasecmp(var, "strrep") == 0) {
@ -226,6 +236,7 @@ static void parse_https_module(char *content, conf * p)
p->https_strrep = (char *)malloc(val_begin_len);
if (p->https_strrep == NULL)
free(p->https_strrep);
memset(p->https_strrep, 0, val_begin_len);
memcpy(p->https_strrep, val_begin, val_begin_len);
char *p1 = strstr(val_begin, "->");
@ -233,11 +244,13 @@ static void parse_https_module(char *content, conf * p)
if (p->https_strrep_aim == NULL) {
free(p->https_strrep_aim);
}
memset(p->https_strrep_aim, 0, val_begin_len - strlen(p1 + 2) - 2 + 1);
strncpy_(p->https_strrep_aim, val_begin, val_begin_len - strlen(p1 + 2) - 3);
p->https_strrep_obj = (char *)malloc(strlen(p1 + 2) + 1);
if (p->https_strrep_obj == NULL) {
free(p->https_strrep_obj);
}
memset(p->https_strrep_obj, 0, strlen(p1 + 2) + 1);
strncpy_(p->https_strrep_obj, p1 + 2, strlen(p1 + 2));
p->https_strrep_aim_len = strlen(p->https_strrep_aim);
p->https_strrep_obj_len = strlen(p->https_strrep_obj);
@ -247,16 +260,19 @@ static void parse_https_module(char *content, conf * p)
p->https_regrep = (char *)malloc(val_begin_len);
if (p->https_regrep == NULL)
free(p->https_regrep);
memset(p->https_regrep, 0, val_begin_len);
memcpy(p->https_regrep, val_begin, val_begin_len);
char *p1 = strstr(val_begin, "->");
p->https_regrep_aim = (char *)malloc(val_begin_len - strlen(p1 + 2) - 2 + 1);
if (p->https_regrep_aim == NULL)
free(p->https_regrep_aim);
memset(p->https_regrep_aim, 0, val_begin_len - strlen(p1 + 2) - 2 + 1);
strncpy_(p->https_regrep_aim, val_begin, val_begin_len - strlen(p1 + 2) - 3);
p->https_regrep_obj = (char *)malloc(strlen(p1 + 2) + 1);
if (p->https_regrep_obj == NULL)
free(p->https_regrep_obj);
memset(p->https_regrep_obj, 0, strlen(p1 + 2) + 1);
strncpy_(p->https_regrep_obj, p1 + 2, strlen(p1 + 2));
p->https_regrep_aim_len = strlen(p->https_regrep_aim);
p->https_regrep_obj_len = strlen(p->https_regrep_obj);
@ -275,10 +291,12 @@ static void parse_httpdns_module(char *content, conf * p)
if (strcasecmp(var, "addr") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->addr = (char *)malloc(val_begin_len);
memset(p->addr, 0, val_begin_len);
memcpy(p->addr, val_begin, val_begin_len);
} else if (strcasecmp(var, "http_req") == 0) {
val_begin_len = strlen(val_begin) + 1;
p->http_req = (char *)malloc(val_begin_len);
memset(p->http_req, 0, val_begin_len);
memcpy(p->http_req, val_begin, val_begin_len);
p->http_req_len = val_begin_len;
} else if (strcasecmp(var, "encode") == 0) {
@ -290,30 +308,48 @@ static void parse_httpdns_module(char *content, conf * p)
void free_conf(conf * p)
{
free(p->server_pid_file);
if (p->http_ip)
free(p->http_ip);
if (p->http_del)
free(p->http_del);
if (p->http_first)
free(p->http_first);
if (p->http_strrep)
free(p->http_strrep);
if (p->http_strrep_aim)
free(p->http_strrep_aim);
if (p->http_strrep_obj)
free(p->http_strrep_obj);
if (p->http_regrep)
free(p->http_regrep);
if (p->http_regrep_aim)
free(p->http_regrep_aim);
if (p->http_regrep_obj)
free(p->http_regrep_obj);
free(p->http_ip);
free(p->http_del);
free(p->http_first);
free(p->http_strrep);
free(p->http_strrep_aim);
free(p->http_strrep_obj);
free(p->http_regrep);
free(p->http_regrep_aim);
free(p->http_regrep_obj);
if (p->https_ip)
free(p->https_ip);
if (p->https_del)
free(p->https_del);
if (p->https_first)
free(p->https_first);
if (p->https_strrep)
free(p->https_strrep);
if (p->https_strrep_aim)
free(p->https_strrep_aim);
if (p->https_strrep_obj)
free(p->https_strrep_obj);
if (p->https_regrep)
free(p->https_regrep);
if (p->https_regrep_aim)
free(p->https_regrep_aim);
if (p->https_regrep_obj)
free(p->https_regrep_obj);
free(p->https_ip);
free(p->https_del);
free(p->https_first);
free(p->https_strrep);
free(p->https_strrep_aim);
free(p->https_strrep_obj);
free(p->https_regrep);
free(p->https_regrep_aim);
free(p->https_regrep_obj);
free(p->addr);
free(p->http_req);
if (p->addr)
free(p->addr);
if (p->http_req)
free(p->http_req);
return;
}
@ -360,60 +396,3 @@ void read_conf(char *filename, conf * configure)
parse_httpdns_module(httpdns_content, configure);
free(httpdns_content);
}
void printfconf(conf * configure)
{
printf("%d\n", configure->uid);
printf("%d\n", configure->process);
printf("%d\n", configure->timeout);
printf("%d\n", configure->sslencoding);
printf("%d\n", configure->tcp_listen);
printf("%d\n", configure->dns_listen);
printf("\n");
if (configure->http_ip)
printf("%s\n", configure->http_ip);
printf("%d\n", configure->http_port);
if (configure->http_del)
printf("%s\n", configure->http_del);
if (configure->http_first)
printf("%s\n", configure->http_first);
if (configure->http_strrep)
printf("%s\n", configure->http_strrep);
if (configure->http_strrep_aim)
printf("%s\n", configure->http_strrep_aim);
if (configure->http_strrep_obj)
printf("%s\n", configure->http_strrep_obj);
if (configure->http_regrep)
printf("%s\n", configure->http_regrep);
if (configure->http_regrep_aim)
printf("%s\n", configure->http_regrep_aim);
if (configure->http_regrep_obj)
printf("%s\n", configure->http_regrep_obj);
printf("\n");
if (configure->https_ip)
printf("%s\n", configure->https_ip);
printf("%d\n", configure->https_port);
if (configure->https_del)
printf("%s\n", configure->https_del);
if (configure->https_first)
printf("%s\n", configure->https_first);
if (configure->https_strrep)
printf("%s\n", configure->https_strrep);
if (configure->https_strrep_aim)
printf("%s\n", configure->https_strrep_aim);
if (configure->https_strrep_obj)
printf("%s\n", configure->https_strrep_obj);
if (configure->https_regrep)
printf("%s\n", configure->https_regrep);
if (configure->https_regrep_aim)
printf("%s\n", configure->https_regrep_aim);
if (configure->https_regrep_obj)
printf("%s\n", configure->https_regrep_obj);
printf("\n");
if (configure->addr)
printf("%s\n", configure->addr);
if (configure->http_req)
printf("%s\n", configure->http_req);
}

3
conf.h
View File

@ -17,8 +17,6 @@ typedef struct CONF {
//int server_port;
int tcp_listen;
int dns_listen;
char *server_pid_file;
int server_pid_file_len; // length
// http module
int http_port;
@ -58,6 +56,5 @@ char *strncpy_(char *dest, const char *src, size_t n);
void read_conf(char *file, conf * p);
void free_conf(conf * p);
void printfconf(conf * configure);
#endif

2
help.c
View File

@ -14,8 +14,6 @@ char help_information(void)
static const char *s_help[] = {
"",
"Options:",
" -l --local_address : localIP:localPORT",
" -f --remote_address : remoteIP:remotePORT",
" -p --process : process number",
" -t --timeout : timeout minute",
" -e --coding : ssl coding, [0-128]",

View File

@ -91,24 +91,6 @@ static void serverToClient(conn * server)
}
void clienttoserver(conn * in)
{
int write_len;
conn *remote;
remote = in + 1;
write_len = write(remote->fd, in->header_buffer, in->header_buffer_len);
if (write_len == in->header_buffer_len) {
in->header_buffer_len = 0;
in->header_buffer = NULL;
} else {
close_connection(remote);
}
return;
}
// 判断请求类型
static int8_t request_type(char *data)
{
@ -119,6 +101,8 @@ static int8_t request_type(char *data)
void tcp_in(conn * in, conf * configure)
{
conn *remote;
if (in->fd < 0)
return;
// 如果in - cts是奇数,那么是服务端触发事件
@ -127,23 +111,28 @@ void tcp_in(conn * in, conf * configure)
serverToClient(in);
return;
}
remote = in + 1;
in->timer = (in + 1)->timer = 0;
in->header_buffer = read_data(in, in->header_buffer, &in->header_buffer_len);
if (in->header_buffer != NULL) {
if (in->header_buffer == NULL) {
close_connection(in);
return;
} else if (in->header_buffer != NULL) {
if (request_type(in->header_buffer) == HTTP_TYPE) {
in->header_buffer = request_head(in, configure);
struct epoll_event epollEvent;
conn *remote;
remote = in + 1;
remote->fd = create_connection(remote_host, remote_port);
epollEvent.events = EPOLLIN | EPOLLOUT | EPOLLET;
epollEvent.data.ptr = remote;
epoll_ctl(epollfd, EPOLL_CTL_ADD, remote->fd, &epollEvent);
}
}
dataEncode(in->header_buffer, in->header_buffer_len);
clienttoserver(in);
if (remote->fd >= 0)
tcp_out(remote);
return;
}

View File

@ -1,5 +1,5 @@
#ifndef HTTP_H
#define HTTP_H
#ifndef HTTP_PROXY_H
#define HTTP_PROXY_H
#include "conf.h"
#include "main.h"
@ -16,12 +16,12 @@ typedef struct conn_t {
int fd;
char *header_buffer;
int header_buffer_len, sent_len, timer;
unsigned request_type :1;
} conn;
extern conn cts[MAX_CONNECTION];
extern void tcp_in(conn * in, conf * configure);
extern void tcp_out(conn * out);
extern void clienttoserver(conn * in);
extern void close_connection(conn * conn);
extern char *request_head(conn * in, conf * configure);

View File

@ -122,40 +122,42 @@ static char *regrep(char *str, int *str_len, const char *src, char *dest, int de
// 删除字符串head中第一位到 character 处并拼接 string, character 为空返回原字符串.(string 字符替换第一个字符到 character 处)
char *splice_head(char *head, const char *character, char *string)
{
int len_first = strlen(string);
char *p1 = strstr(head, character);
if (p1 == NULL) {
int first_len = strlen(string);
char *_p1 = strstr(head, character);
if (_p1 == NULL) {
return head;
}
p1 = p1 + 1;
char new_string[len_first + strlen(p1) + 1];
strcpy(new_string, string);
return strcat(new_string, p1);
_p1 = _p1 + 1;
char temporary[first_len + strlen(_p1) + 1];
memset(temporary, 0, (first_len + strlen(_p1) + 1));
strcpy(temporary, string);
strcat(temporary, _p1);
memset(head, 0, strlen(head));
return strcpy(head, temporary);
}
// 删除字符串 head 中 character 到 string 处, character 为空返回原字符串.
char *delete_head(char *head, const char *character, int string)
{
int head_len = strlen(head);
char *p1 = strstr(head, character);
if (p1 == NULL) {
char *_p1 = strstr(head, character);
if (_p1 == NULL) {
return head;
}
char *p2 = strchr(p1, string);
if (p2 == NULL) {
char *_p2 = strchr(_p1, string);
if (_p2 == NULL) {
return head;
}
char tmp[head_len];
strncpy_(tmp, head, head_len - strlen(p1) - 1);
strcat(tmp, p2);
return strcpy(head, tmp);
char temporary[head_len];
memset(temporary, 0, head_len);
memcpy(temporary, head, (head_len - strlen(_p1) - 1));
strcat(temporary, _p2);
memset(head, 0, strlen(head));
return memcpy(head, temporary, head_len);
}
int extract_host(char *header, char *host, char *port)
{
memset(host, 0, strlen(host));
memset(port, 0, strlen(port));
char *_p = strstr(header, "CONNECT"); // 在 CONNECT 方法中解析 隧道主机名称及端口号
if (_p) {
char *_p1 = strchr(_p, ' ');
@ -163,15 +165,10 @@ int extract_host(char *header, char *host, char *port)
char *_p3 = strchr(_p1 + 1, ' ');
if (_p2) {
char s_port[10];
bzero(s_port, 10);
strncpy_(host, _p1 + 1, (int)(_p2 - _p1) - 1);
strncpy_(s_port, _p2 + 1, (int)(_p3 - _p2) - 1);
strcpy(port, s_port);
} else {
strncpy(host, _p1 + 1, (int)(_p3 - _p1) - 1);
strcpy(port, "80");
memcpy(host, _p1 + 1, (int)(_p2 - _p1) - 1);
memcpy(port, _p2 + 1, (int)(_p3 - _p2) - 1);
} else { // 如果_p2等于空就返回-1
return -1;
}
return 0;
}
@ -187,107 +184,125 @@ int extract_host(char *header, char *host, char *port)
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';
strcpy(port, s_port);
int h_len = (int)(p2 - p - 5 - 1);
strncpy(host, p + 5 + 1, h_len); // Host:
host[h_len] = '\0';
memcpy(port, p2 + 1, (int)(p1 - p2 - 1));
memcpy(host, p + 5 + 1, (int)(p2 - p - 5 - 1)); // Host:
} else {
int h_len = (int)(p1 - p - 5 - 1 - 1);
strncpy_(host, p + 5 + 1, h_len);
host[h_len] = '\0';
strcpy(port, "80");
memset(port, 0, strlen(port));
memset(host, 0, strlen(host));
memcpy(host, p + 5 + 1, (int)(p1 - p - 5 - 1 - 1));
memcpy(port, "80", 2);
}
return 0;
}
char *get_path(char *url, char *path)
{
char *_p0;
_p0 = NULL;
if (url) {
char *p0 = strstr(url + 7, "/");
if (p0)
return strncpy_(path, p0, strlen(p0));
_p0 = strstr(url + 7, "/");
if (_p0)
return strncpy_(path, _p0, (int)strlen(_p0));
else
return NULL;
}
return NULL;
}
void free_http_request(struct http_request *http_request) {
void free_http_request(struct http_request *http_request)
{
if (http_request->M)
free(http_request->M);
if (http_request->U)
free(http_request->U);
if (http_request->V)
free(http_request->V);
if (http_request->host)
free(http_request->host);
if (http_request->port)
free(http_request->port);
if (http_request->H)
free(http_request->H);
if (http_request->url)
free(http_request->url);
if (http_request->uri)
free(http_request->uri);
}
void parse_request_head(char *http_request_line, struct http_request *http_request) {
void parse_request_head(char *http_request_line, struct http_request *http_request)
{
char *p;
char *head;
size_t head_len;
char *m, *u;
p = strstr(http_request_line, "\r\n"); // 查找"\r\n"
if(p == NULL) {
return ;
p = strstr(http_request_line, "\r\n"); // 查找"\r\n"
if (p == NULL) {
return;
}
head_len = strlen(http_request_line) - strlen(p);
head = (char *)malloc(sizeof(char) * head_len*2);
head = (char *)malloc(sizeof(char) * head_len * 2);
if (head == NULL)
free(head);
memset(head, 0, head_len*2);
memset(head, 0, head_len * 2);
memcpy(head, http_request_line, head_len);
//printf("HEAD: %s\n", head);
http_request->M = (char *)malloc(sizeof(char) * head_len);
http_request->U = (char *)malloc(sizeof(char) * head_len);
http_request->V = (char *)malloc(10);
if (http_request->M == NULL) {
free(http_request->M);
perror("malloc");
}
if (http_request->U == NULL) {
free(http_request->M);
perror("malloc");
}
if (http_request->V == NULL) {
free(http_request->M);
perror("malloc");
}
memset(http_request->M, 0, head_len);
memset(http_request->U, 0, head_len);
memset(http_request->V, 0, 10);
m = strstr(head, " ");
http_request->M_len = strlen(head) - strlen(m);
memset(http_request->M, 0, head_len);
memcpy(http_request->M, head, http_request->M_len);
u = strstr(m+1, " ");
http_request->U_len = strlen(m+1) - strlen(u);
memset(http_request->U, 0, head_len);
memcpy(http_request->U, m+1, http_request->U_len);
memset(http_request->V, 0, 8);
memcpy(http_request->V, u+1, 8);
u = strstr(m + 1, " ");
http_request->U_len = strlen(m + 1) - strlen(u);
memcpy(http_request->U, m + 1, http_request->U_len);
memcpy(http_request->V, u + 1, 8);
http_request->V_len = 8;
free(head);
printf("%s\n", http_request->M);
printf("%s\n", http_request->U);
printf("%s\n", http_request->V);
printf("%d\n", http_request->M_len);
printf("%d\n", http_request->U_len);
printf("%d\n", http_request->V_len);
if (http_request->U_len < 0)
return;
http_request->host = (char *)malloc(sizeof(char) * head_len);
http_request->port = (char *)malloc(sizeof(char) * head_len);
http_request->url = (char *)malloc(sizeof(char) * head_len);
http_request->uri = (char *)malloc(sizeof(char) * head_len);
http_request->H = (char *)malloc(sizeof(char) * head_len * 2);
memset(http_request->host, '\0', head_len);
memset(http_request->port, '\0', head_len);
memset(http_request->url, '\0', head_len);
memset(http_request->uri, '\0', head_len);
memset(http_request->H, '\0', head_len * 2);
if (extract_host(http_request_line, http_request->host, http_request->port) == -1)
return;
http_request->host_len = (int)strlen(http_request->host);
http_request->port_len = (int)strlen(http_request->port);
memcpy(http_request->H, http_request->host, http_request->host_len);
strcat(http_request->H, ":");
strcat(http_request->H, http_request->port);
memcpy(http_request->url, http_request->U, http_request->U_len);
get_path(http_request->url, http_request->uri);
http_request->url_len = (int)strlen(http_request->url);
http_request->uri_len = (int)strlen(http_request->uri);
http_request->H_len = (int)strlen(http_request->H);
free(head);
return;
}
char *request_head(conn * in, conf * configure)
@ -296,26 +311,22 @@ char *request_head(conn * in, conf * configure)
http_request = (struct http_request *)malloc(sizeof(struct http_request));
parse_request_head(in->header_buffer, http_request);
//printfconf(configure); // 打印配置
if (strncmp(http_request->M, "CONNECT", 7) == 0) {
char host[http_request->U_len], port[http_request->U_len], H[http_request->U_len * 2];
if (strncmp(in->header_buffer, "CONNECT", 7) == 0) {
char *incomplete_head;
int incomplete_head_len;
char https_del_copy[configure->https_del_len * 2];
char *result = NULL;
extract_host(in->header_buffer, host, port);
if (configure->https_port > 0)
remote_port = configure->https_port;
if (configure->https_ip != NULL)
strcpy(remote_host, configure->https_ip);
incomplete_head = (char *)malloc(sizeof(char) * (1024 + BUFFER_SIZE));
incomplete_head = (char *)malloc(sizeof(char) * (BUFFER_SIZE));
if (incomplete_head == NULL) {
free(incomplete_head);
perror("malloc");
}
memset(incomplete_head, 0, sizeof(char) * (1024 + BUFFER_SIZE));
memset(incomplete_head, 0, sizeof(char) * (BUFFER_SIZE));
memcpy(incomplete_head, in->header_buffer, strlen(in->header_buffer));
memcpy(https_del_copy, configure->https_del, configure->https_del_len);
@ -324,8 +335,12 @@ char *request_head(conn * in, conf * configure)
delete_head(incomplete_head, result, '\n');
result = strtok(NULL, ",");
}
strcpy(incomplete_head, splice_head(incomplete_head, "\n", configure->https_first));
splice_head(incomplete_head, "\n", configure->https_first);
incomplete_head_len = strlen(incomplete_head);
if (configure->https_strrep)
incomplete_head = replace(incomplete_head, &incomplete_head_len, configure->https_strrep_aim, configure->https_strrep_aim_len, configure->https_strrep_obj, configure->https_strrep_obj_len);
if (configure->https_regrep)
incomplete_head = regrep(incomplete_head, &incomplete_head_len, configure->https_regrep_aim, configure->https_regrep_obj, configure->https_regrep_obj_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\r", 2, "\r", 1);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\n", 2, "\n", 1);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\b", 2, "\b", 1);
@ -340,60 +355,46 @@ char *request_head(conn * in, conf * configure)
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[U]", 3, http_request->U, http_request->U_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[V]", 3, http_request->V, http_request->V_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[version]", 9, http_request->V, http_request->V_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[host]", 6, host, (int)strlen(host));
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[port]", 6, port, (int)strlen(port));
memset(H, 0, strlen(H));
strcpy(H, host);
strcat(H, ":");
strcat(H, port);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[H]", 3, H, (int)strlen(H));
if (configure->https_strrep) {
incomplete_head = replace(incomplete_head, &incomplete_head_len, configure->https_strrep_aim, configure->https_strrep_aim_len, configure->https_strrep_obj, configure->https_strrep_obj_len);
}
if (configure->https_regrep) {
incomplete_head = regrep(incomplete_head, &incomplete_head_len, configure->https_regrep_aim, configure->https_regrep_obj, configure->https_regrep_obj_len);
}
printf("%s", incomplete_head); // 打印HTTP HEADER
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[host]", 6, http_request->host, http_request->host_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[port]", 6, http_request->port, http_request->port_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[H]", 3, http_request->H, http_request->H_len);
//printf("%s", incomplete_head); // 打印HTTP HEADER
memset(in->header_buffer, 0, strlen(in->header_buffer));
strcpy(in->header_buffer, incomplete_head);
in->header_buffer_len = strlen(in->header_buffer);
free(incomplete_head);
} else {
char host[http_request->U_len], port[http_request->U_len], H[http_request->U_len * 2];
char url[http_request->U_len], uri[http_request->U_len];
int uri_len;
} else if (strncmp(in->header_buffer, "GET", 3) == 0) {
char *incomplete_head;
int incomplete_head_len;
char https_del_copy[configure->http_del_len];
char *result = NULL;
extract_host(in->header_buffer, host, port);
strcpy(url, http_request->U);
get_path(url, uri);
uri_len = strlen(uri);
if (configure->http_port > 0)
remote_port = configure->http_port;
if (configure->https_ip != NULL)
strcpy(remote_host, configure->http_ip);
incomplete_head = (char *)malloc(sizeof(char) * (1024 + BUFFER_SIZE));
if (configure->http_ip != NULL)
memmove(remote_host, configure->http_ip, strlen(configure->http_ip));
incomplete_head = (char *)malloc(sizeof(char) * (BUFFER_SIZE));
if (incomplete_head == NULL) {
free(incomplete_head);
perror("malloc");
}
memset(incomplete_head, 0, sizeof(char) * (1024 + BUFFER_SIZE));
strcpy(incomplete_head, in->header_buffer);
strcpy(https_del_copy, configure->http_del);
memset(incomplete_head, 0, sizeof(char) * (BUFFER_SIZE));
memcpy(incomplete_head, in->header_buffer, strlen(in->header_buffer));
memcpy(https_del_copy, configure->https_del, configure->https_del_len);
result = strtok(https_del_copy, ",");
while (result != NULL) {
delete_head(incomplete_head, result, '\n');
result = strtok(NULL, ",");
}
strcpy(incomplete_head, splice_head(incomplete_head, "\n", configure->http_first));
splice_head(incomplete_head, "\n", configure->http_first);
incomplete_head_len = strlen(incomplete_head);
if (configure->http_strrep)
incomplete_head = replace(incomplete_head, &incomplete_head_len, configure->http_strrep_aim, configure->http_strrep_aim_len, configure->http_strrep_obj, configure->http_strrep_obj_len);
if (configure->http_regrep)
incomplete_head = regrep(incomplete_head, &incomplete_head_len, configure->http_regrep_aim, configure->http_regrep_obj, configure->http_regrep_obj_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\r", 2, "\r", 1);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\n", 2, "\n", 1);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "\\b", 2, "\b", 1);
@ -406,29 +407,22 @@ char *request_head(conn * in, conf * configure)
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[M]", 3, http_request->M, http_request->M_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[method]", 8, http_request->M, http_request->M_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[U]", 3, http_request->U, http_request->U_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[uri]", 5, uri, uri_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[V]", 3, http_request->V, http_request->V_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[version]", 9, http_request->V, http_request->V_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[host]", 6, host, (int)strlen(host));
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[port]", 6, port, (int)strlen(port));
memset(H, 0, strlen(H));
strcpy(H, host);
strcat(H, ":");
strcat(H, port);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[H]", 3, H, (int)strlen(H));
if (configure->http_strrep) {
incomplete_head = replace(incomplete_head, &incomplete_head_len, configure->http_strrep_aim, configure->http_strrep_aim_len, configure->http_strrep_obj, configure->http_strrep_obj_len);
}
if (configure->http_regrep) {
incomplete_head = regrep(incomplete_head, &incomplete_head_len, configure->http_regrep_aim, configure->http_regrep_obj, configure->http_regrep_obj_len);
}
printf("%s", incomplete_head);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[url]", 5, http_request->url, http_request->url_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[uri]", 5, http_request->uri, http_request->uri_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[host]", 6, http_request->host, http_request->host_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[port]", 6, http_request->port, http_request->port_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[H]", 3, http_request->H, http_request->H_len);
//printf("%s", incomplete_head);
incomplete_head_len = strlen(incomplete_head);
memset(in->header_buffer, 0, strlen(in->header_buffer));
strcpy(in->header_buffer, incomplete_head);
memmove(in->header_buffer, incomplete_head, incomplete_head_len);
in->header_buffer_len = strlen(in->header_buffer);
free(incomplete_head);
}
free_http_request(http_request);
free(http_request);
return in->header_buffer;

View File

@ -1,5 +1,5 @@
#ifndef REQUEST_H
#define REQUEST_H
#ifndef HTTP_REQUEST_H
#define HTTP_REQUEST_H
#include <string.h>
#include <stdio.h>
@ -10,7 +10,12 @@
struct http_request {
char *M, *U, *V;
char *host, *port, *H;
char *url, *uri;
int M_len, U_len, V_len;
int host_len, port_len, H_len;
int url_len, uri_len;
};
void *memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen);

View File

@ -14,7 +14,6 @@ struct dns_cache *cache, *cache_temp;
socklen_t addr_len = sizeof(dst_addr);
unsigned int cache_using, cacheLimit;
int read_cache_file()
{
char *buff, *answer, *question;

View File

@ -23,6 +23,7 @@
#define DATA_SIZE 512
#define HTTP_RSP_SIZE 1024
typedef struct dns_connection {
char dns_req[DATA_SIZE];
struct sockaddr_in src_addr;

32
main.c
View File

@ -19,7 +19,8 @@ int create_connection(char *remote_host, int remote_port)
{
struct sockaddr_in server_addr;
struct hostent *server;
int sock;
int sock = -1;
server = NULL;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
return -1;
@ -33,7 +34,7 @@ int create_connection(char *remote_host, int remote_port)
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);
memmove(&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) {
perror("connect");
@ -125,7 +126,7 @@ void *start_server(conf * configure)
int n;
pthread_t thread_id;
if (timeout_minute)
pthread_create(&thread_id, NULL, &close_timeout_connectionLoop, NULL);
pthread_create(&thread_id, NULL, &tcp_timeout_check, NULL);
while (1) {
n = epoll_wait(epollfd, events, MAX_CONNECTION, -1);
@ -150,16 +151,17 @@ int process_signal(int signal, char *process_name)
char bufer[PATH_SIZE];
char comm[PATH_SIZE];
char proc_comm_name[PATH_SIZE];
int num[PATH_SIZE] = { 0 };
int number[PATH_SIZE] = { 0 };
int n = 0;
FILE *fp;
DIR *dir;
struct dirent *ptr;
dir = opendir("/proc");
bzero(bufer, 0);
bzero(comm, 0);
bzero(proc_comm_name, 0);
while ((ptr = readdir(dir)) != NULL) {
if (ptr->d_type == DT_DIR && strcasecmp(ptr->d_name, ".")
&& strcasecmp(ptr->d_name, "..")) {
bzero(bufer, 0);
if (ptr->d_type == DT_DIR && strcasecmp(ptr->d_name, ".") && strcasecmp(ptr->d_name, "..")) {
sprintf(comm, "/proc/%s/comm", ptr->d_name);
if (access(comm, F_OK) == 0) {
fp = fopen(comm, "r");
@ -169,7 +171,7 @@ int process_signal(int signal, char *process_name)
}
sscanf(bufer, "%s", proc_comm_name);
if (!strcmp(process_name, proc_comm_name)) {
num[n] = atoi(ptr->d_name);
number[n] = atoi(ptr->d_name);
n += 1;
}
fclose(fp);
@ -179,10 +181,10 @@ int process_signal(int signal, char *process_name)
}
closedir(dir);
if (signal == SERVER_STATUS) { // 状态
n -= 2; // 去除最后一个搜索时的本身进程
for (; n >= 0; n--) {
printf("\t%d\n", num[n]);
if (signal == SERVER_STATUS) { // 状态
n -= 2; // 去除最后一个搜索时的本身进程和最后加一后未使用的
for (; n >= 0; n--) { // 依据数组从大到小的下标打印PID
printf("\t%d\n", number[n]);
}
}
if (signal == SERVER_STOP || signal == SERVER_RELOAD) { // 关闭
@ -339,6 +341,9 @@ void _main(int argc, char *argv[])
exit(1);
server_ini(); // 初始化http_proxy
//start_server(configure);
//httpdns_loop(configure);
pthread_t thread_id = 0;
sigset_t signal_mask;
sigemptyset(&signal_mask);
@ -346,9 +351,8 @@ void _main(int argc, char *argv[])
if (pthread_sigmask(SIG_BLOCK, &signal_mask, NULL) != 0) {
printf("block sigpipe error\n");
}
pthread_detach(thread_id);
if (timeout_minute)
pthread_create(&thread_id, NULL, &close_timeout_connectionLoop, NULL);
pthread_create(&thread_id, NULL, &tcp_timeout_check, NULL);
if (pthread_create(&thread_id, NULL, &http_proxy_loop, (void *)configure) != 0)
perror("pthread_create");
if (pthread_create(&thread_id, NULL, &httpdns_loop, (void *)configure) != 0)

7
main.h
View File

@ -1,5 +1,5 @@
#ifndef PROXY_H
#define PROXY_H
#ifndef MAIN_H
#define MAIN_H
#include <stdio.h>
#include <stdlib.h>
@ -16,13 +16,14 @@
#include <pthread.h>
#include <pwd.h>
#include <dirent.h>
#include <pthread.h>
#include <sched.h>
#include <getopt.h>
#include <netinet/in.h>
#define MAX_CONNECTION 1020
#define BUFFER_SIZE 8192
#define PATH_SIZE 270
#define CACHE_SIZE 270
int local_port;
char local_host[128];

View File

@ -4,7 +4,7 @@
int timeout_minute;
void *close_timeout_connectionLoop(void *nullPtr)
void *tcp_timeout_check(void *nullPtr)
{
int i;
@ -12,8 +12,9 @@ void *close_timeout_connectionLoop(void *nullPtr)
sleep(60);
for (i = 0; i < MAX_CONNECTION; i += 2)
if (cts[i].fd > -1) {
if (cts[i].timer >= timeout_minute)
if (cts[i].timer >= timeout_minute) {
close_connection(cts + i);
}
else
cts[i].timer++;
}

View File

@ -2,7 +2,6 @@
#define TIME_H
extern int timeout_minute;
void *close_timeout_connectionLoop(void *nullPtr);
void *tcp_timeout_check(void *nullPtr);
#endif