Add files via upload

This commit is contained in:
mmmdbybyd 2018-12-07 09:29:41 +08:00 committed by GitHub
parent f5cb1c8e02
commit 94d106a7a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 66 additions and 79 deletions

View File

@ -8,7 +8,7 @@ ifeq ($(ANDROID_DATA),/data)
endif endif
all : main.o http.o dns.o all : main.o http.o dns.o timeout.o
$(CC) $(CFLAGS) $(DEFS) -o $(OBJ) $^ $(CC) $(CFLAGS) $(DEFS) -o $(OBJ) $^
strip $(OBJ) strip $(OBJ)
-chmod 777 $(OBJ) 2>&- -chmod 777 $(OBJ) 2>&-

2
dns.h
View File

@ -3,6 +3,8 @@
#include "main.h" #include "main.h"
#define DEFAULT_DNS_IP "114.114.114.114"
struct dns { struct dns {
char request[512]; //UDP的DNS请求不超512字节 char request[512]; //UDP的DNS请求不超512字节
uint16_t request_len, sent_len; uint16_t request_len, sent_len;

75
http.c
View File

@ -1,21 +1,15 @@
#include "http.h" #include "http.h"
#include "dns.h" #include "dns.h"
#include "timeout.h"
#define SSL_RSP "HTTP/1.1 200 Connection established\r\n\r\n"
#define HTTP_TYPE 0 #define HTTP_TYPE 0
#define OTHER_TYPE 1 #define OTHER_TYPE 1
conn_t cts[MAX_CONNECTION]; conn_t cts[MAX_CONNECTION];
static char ssl_rsp[] = "HTTP/1.1 200 Connection established\r\n\r\n";
char *local_header, *proxy_header, *ssl_proxy; char *local_header, *proxy_header, *ssl_proxy;
int lisFd, proxy_header_len, local_header_len; int lisFd, proxy_header_len, local_header_len;
uint8_t strict_spilce, encodeCode; uint8_t strict_spilce;
/* 对数据进行编码 */
static void dataEncode(char *data, int data_len)
{
while (data_len-- > 0)
data[data_len] ^= encodeCode;
}
int8_t connectionToServer(char *ip, conn_t *server) int8_t connectionToServer(char *ip, conn_t *server)
{ {
@ -146,19 +140,10 @@ static char *get_host(char *data)
while (*host == ' ') while (*host == ' ')
host++; host++;
hostEnd = strchr(host, '\r'); hostEnd = strchr(host, '\r');
if (hostEnd) if (hostEnd)
{
if (encodeCode)
dataEncode(host, hostEnd - host);
return strndup(host, hostEnd - host); return strndup(host, hostEnd - host);
}
else else
{
if (encodeCode)
dataEncode(host, strlen(host));
return strdup(host); return strdup(host);
}
} }
/* 删除请求头中的头域 */ /* 删除请求头中的头域 */
@ -192,24 +177,6 @@ static void del_hdr(char *header, int *header_len)
} }
} }
/* 对请求头中的Referer头域进行编码 */
static void encodeReferer(char *request)
{
char *referer_key, *referer_value, *line, *pr;
for (line = strchr(request, '\n'); (referer_key = strstr(line, "\nReferer:")) != NULL || (referer_key = strstr(line, "\nreferer:")) != NULL; line = pr + 1)
{
referer_value = referer_key + sizeof("\nReferer:") - 1;
while (*referer_value == ' ')
referer_value++;
pr = strchr(referer_value, '\r');
if (pr)
dataEncode(referer_value, pr - referer_value);
else
dataEncode(referer_value, strlen(referer_value));
}
}
/* 构建新请求头 */ /* 构建新请求头 */
static char *build_request(char *client_data, int *data_len, char *host) static char *build_request(char *client_data, int *data_len, char *host)
{ {
@ -225,7 +192,6 @@ static char *build_request(char *client_data, int *data_len, char *host)
lf = strchr(header, '\n'); lf = strchr(header, '\n');
if (url == NULL || lf == NULL || lf - 10 <= header) if (url == NULL || lf == NULL || lf - 10 <= header)
return client_data; return client_data;
if (url < lf && *(++url) != '/') if (url < lf && *(++url) != '/')
{ {
uri = strchr(url + 7, '/'); uri = strchr(url + 7, '/');
@ -243,12 +209,6 @@ static char *build_request(char *client_data, int *data_len, char *host)
*data_len -= p - url; *data_len -= p - url;
lf -= p - url; lf -= p - url;
} }
if (encodeCode)
dataEncode(url + 1, lf - 10 - (url + 1));
}
else if (url < lf && encodeCode)
{
dataEncode(url + 1, lf - 10 - (url + 1));
} }
*data_len += strlen(proxy_host) + 8; //8为 "Host: " + "\r\n"的长度 *data_len += strlen(proxy_host) + 8; //8为 "Host: " + "\r\n"的长度
@ -273,8 +233,6 @@ static char *build_request(char *client_data, int *data_len, char *host)
free(client_data); free(client_data);
if (proxy_host != host) if (proxy_host != host)
free(proxy_host); free(proxy_host);
if (encodeCode)
encodeReferer(new_data);
if (strict_spilce == 0) if (strict_spilce == 0)
return new_data; return new_data;
client_data = new_data; client_data = new_data;
@ -352,8 +310,6 @@ static void serverToClient(conn_t *server)
client = server - 1; client = server - 1;
while ((server->ready_data_len = read(server->fd, server->ready_data, BUFFER_SIZE)) > 0) while ((server->ready_data_len = read(server->fd, server->ready_data, BUFFER_SIZE)) > 0)
{ {
if (encodeCode) //对服务端的数据编码
dataEncode(server->ready_data, server->ready_data_len);
write_len = write(client->fd, server->ready_data, server->ready_data_len); write_len = write(client->fd, server->ready_data, server->ready_data_len);
if (write_len == -1) if (write_len == -1)
{ {
@ -392,6 +348,7 @@ void tcp_out(conn_t *to)
from = to - 1; from = to - 1;
else else
from = to + 1; from = to + 1;
from->last_event_time = to->last_event_time = time(NULL);
write_len = write(to->fd, from->ready_data + from->sent_len, from->ready_data_len - from->sent_len); write_len = write(to->fd, from->ready_data + from->sent_len, from->ready_data_len - from->sent_len);
if (write_len == from->ready_data_len - from->sent_len) if (write_len == from->ready_data_len - from->sent_len)
{ {
@ -439,11 +396,13 @@ void tcp_in(conn_t *in)
//如果in - cts是奇数那么是服务端触发事件 //如果in - cts是奇数那么是服务端触发事件
if ((in - cts) & 1) if ((in - cts) & 1)
{ {
in->last_event_time = (in-1)->last_event_time = time(NULL);
if (in->ready_data_len == 0) if (in->ready_data_len == 0)
serverToClient(in); serverToClient(in);
return; return;
} }
in->last_event_time = (in+1)->last_event_time = time(NULL);
in->incomplete_data = read_data(in, in->incomplete_data, &in->incomplete_data_len); in->incomplete_data = read_data(in, in->incomplete_data, &in->incomplete_data_len);
if (in->incomplete_data == NULL) if (in->incomplete_data == NULL)
{ {
@ -454,8 +413,6 @@ void tcp_in(conn_t *in)
server->request_type = in->request_type = request_type(in->incomplete_data); server->request_type = in->request_type = request_type(in->incomplete_data);
if (in->request_type == OTHER_TYPE) if (in->request_type == OTHER_TYPE)
{ {
if (encodeCode)
dataEncode(in->incomplete_data, in->incomplete_data_len);
//如果是第一次读取数据并且不是HTTP请求的关闭连接。复制数据失败的也关闭连接 //如果是第一次读取数据并且不是HTTP请求的关闭连接。复制数据失败的也关闭连接
if (in->reread_data == 0 || copy_data(in) != 0) if (in->reread_data == 0 || copy_data(in) != 0)
{ {
@ -464,13 +421,10 @@ void tcp_in(conn_t *in)
} }
goto handle_data_complete; goto handle_data_complete;
} }
headerEnd = strstr(in->incomplete_data, "\n\r\n"); headerEnd = strstr(in->incomplete_data, "\n\r");
//请求头不完整,等待下次读取 //请求头不完整,等待下次读取
if (headerEnd == NULL) if (headerEnd == NULL)
return; return;
headerEnd += 3;
if (encodeCode)
dataEncode(headerEnd, in->incomplete_data_len - (headerEnd - in->incomplete_data));
host = get_host(in->incomplete_data); host = get_host(in->incomplete_data);
if (host == NULL) if (host == NULL)
{ {
@ -491,12 +445,13 @@ void tcp_in(conn_t *in)
{ {
server->is_ssl = in->is_ssl = 1; server->is_ssl = in->is_ssl = 1;
/* 这时候即使fd是非阻塞也只需要判断返回值是否小于0 */ /* 这时候即使fd是非阻塞也只需要判断返回值是否小于0 */
if (write(in->fd, ssl_rsp, sizeof(ssl_rsp) - 1) < 0) if (write(in->fd, SSL_RSP, sizeof(SSL_RSP)-1) < 0)
{ {
free(host); free(host);
close_connection(in); close_connection(in);
return; return;
} }
headerEnd += 3;
if (headerEnd - in->incomplete_data < in->incomplete_data_len) if (headerEnd - in->incomplete_data < in->incomplete_data_len)
{ {
in->incomplete_data_len -= headerEnd - in->incomplete_data; in->incomplete_data_len -= headerEnd - in->incomplete_data;
@ -507,16 +462,6 @@ void tcp_in(conn_t *in)
free(host); free(host);
return; return;
} }
else
{
//如果是CONNECT代理HTTP,需要重新获取host
char *save_host = host;
host = get_host(in->incomplete_data);
if (!host)
host = save_host;
else
free(save_host);
}
} }
else else
{ {
@ -547,8 +492,6 @@ void *accept_loop(void *ptr)
struct epoll_event epollEvent; struct epoll_event epollEvent;
conn_t *client; conn_t *client;
if (encodeCode)
dataEncode(ssl_rsp, sizeof(ssl_rsp) - 1);
epollEvent.events = EPOLLIN|EPOLLET; epollEvent.events = EPOLLIN|EPOLLET;
while (1) while (1)
{ {

4
http.h
View File

@ -6,9 +6,11 @@
typedef struct tcp_connection { typedef struct tcp_connection {
char *ready_data, *incomplete_data; char *ready_data, *incomplete_data;
int fd, ready_data_len, incomplete_data_len, sent_len; int fd, ready_data_len, incomplete_data_len, sent_len;
unsigned int last_event_time;
uint16_t destPort; uint16_t destPort;
unsigned reread_data :1; unsigned reread_data :1;
unsigned request_type :1; unsigned request_type :1;
unsigned keep_alive :1;
unsigned is_ssl :1; unsigned is_ssl :1;
} conn_t; } conn_t;
@ -22,6 +24,6 @@ extern void tcp_out(conn_t *ct);
extern conn_t cts[MAX_CONNECTION]; extern conn_t cts[MAX_CONNECTION];
extern char *local_header, *proxy_header, *ssl_proxy; extern char *local_header, *proxy_header, *ssl_proxy;
extern int lisFd, local_header_len, proxy_header_len; extern int lisFd, local_header_len, proxy_header_len;
extern uint8_t strict_spilce, isEncode, encodeCode; extern uint8_t strict_spilce;
#endif #endif

17
main.c
View File

@ -1,5 +1,6 @@
#include "main.h" #include "main.h"
#include "http.h" #include "http.h"
#include "timeout.h"
#include "dns.h" #include "dns.h"
#include <pthread.h> #include <pthread.h>
@ -19,8 +20,8 @@ static void usage()
" -L local proxy header \033[35G default is 'Local'\n" " -L local proxy header \033[35G default is 'Local'\n"
" -d dns query address \033[35G default is " DEFAULT_DNS_IP "\n" " -d dns query address \033[35G default is " DEFAULT_DNS_IP "\n"
" -s ssl proxy string \033[35G default is 'CONNECT'\n" " -s ssl proxy string \033[35G default is 'CONNECT'\n"
" -t timeout \033[35G default is 35s\n"
" -u uid \033[35G running uid\n" " -u uid \033[35G running uid\n"
" -e \033[35G set encode data code(1-127)\n"
" -a \033[35G all http requests repeat spilce\n" " -a \033[35G all http requests repeat spilce\n"
" -h display this infomaction\n" " -h display this infomaction\n"
" -w worker process\n"); " -w worker process\n");
@ -80,7 +81,7 @@ static void initializate(int argc, char **argv)
dnsAddr.sin_addr.s_addr = inet_addr(DEFAULT_DNS_IP); dnsAddr.sin_addr.s_addr = inet_addr(DEFAULT_DNS_IP);
dnsAddr.sin_port = htons(53); dnsAddr.sin_port = htons(53);
dns_connect(&dnsAddr); //主进程中的fd dns_connect(&dnsAddr); //主进程中的fd
strict_spilce = encodeCode = 0; strict_spilce = timeout_seconds = 0;
local_header = NULL; local_header = NULL;
ssl_proxy = (char *)"CONNECT"; ssl_proxy = (char *)"CONNECT";
local_header = (char *)"\nLocal:"; local_header = (char *)"\nLocal:";
@ -88,7 +89,7 @@ static void initializate(int argc, char **argv)
proxy_header_len = strlen(proxy_header); proxy_header_len = strlen(proxy_header);
local_header_len = strlen(local_header); local_header_len = strlen(local_header);
/* 处理命令行参数 */ /* 处理命令行参数 */
while ((opt = getopt(argc, argv, "d:l:e:p:s:w:u:L:ah")) != -1) while ((opt = getopt(argc, argv, "d:l:p:s:w:t:u:L:ah")) != -1)
{ {
switch (opt) switch (opt)
{ {
@ -116,10 +117,6 @@ static void initializate(int argc, char **argv)
} }
break; break;
case 'e':
encodeCode = (int8_t)atoi(optarg);
break;
case 'p': case 'p':
//假如选项值为 "Proxy", proxy_header设置为 "\nProxy:" //假如选项值为 "Proxy", proxy_header设置为 "\nProxy:"
proxy_header_len = strlen(optarg) + 2; proxy_header_len = strlen(optarg) + 2;
@ -155,6 +152,10 @@ static void initializate(int argc, char **argv)
strict_spilce = 1; strict_spilce = 1;
break; break;
case 't':
timeout_seconds = (unsigned int)atoi(optarg);
break;
case 'w': case 'w':
workers = atoi(optarg); workers = atoi(optarg);
break; break;
@ -228,7 +229,7 @@ static void initializate(int argc, char **argv)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
initializate(argc, argv); initializate(argc, argv);
if (daemon(1, 0)) if (daemon(1, 1))
{ {
perror("daemon"); perror("daemon");
return 1; return 1;

2
main.h
View File

@ -16,7 +16,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <signal.h> #include <signal.h>
#define BUFFER_SIZE 4096 #define BUFFER_SIZE 10240
#define MAX_CONNECTION 1020 #define MAX_CONNECTION 1020
extern struct epoll_event evs[MAX_CONNECTION + 1], ev; extern struct epoll_event evs[MAX_CONNECTION + 1], ev;

27
timeout.c Normal file
View File

@ -0,0 +1,27 @@
#include "main.h"
#include "http.h"
#include "time.h"
unsigned int timeout_seconds;
void *close_timeout_connectionLoop(void *nullPtr)
{
time_t currentTime;
int i, recv_return;
char c;
while (1)
{
sleep(1);
currentTime = time(NULL);
for (i = 0; i < MAX_CONNECTION; i += 2)
{
if (cts[i].fd > -1 && cts[i].is_ssl == 0 && cts[i].last_event_time != 0 && currentTime - cts[i].last_event_time >= (time_t)timeout_seconds)
{
recv_return = recv(cts[i].fd, &c, 1, MSG_PEEK);
if (recv_return == 0 && errno != EAGAIN)
close_connection(cts + i);
}
}
}
}

12
timeout.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef TIME_H
#define TIME_H
#define DEFAULT_TIMEOUT 35
#include <time.h>
extern unsigned int timeout_seconds;
void *close_timeout_connectionLoop(void *nullPtr);
#endif