Add files via upload

This commit is contained in:
mmmdbybyd 2017-07-12 12:52:46 +08:00 committed by GitHub
parent 705cabbd8c
commit 3a7d93668a
4 changed files with 47 additions and 47 deletions

8
dns.c
View File

@ -1,7 +1,7 @@
#include "dns.h"
#include "http.h"
struct dns dns_list[MAX_CONNECTION / 2]; //一个客户端 + 一个服务端 占用一个dns结构体
struct dns dns_list[MAX_CONNECTION / 2]; //一个客户端 + 一个服务端 占用一个dns结构体
int dnsFd;
void read_dns_rsp()
@ -17,10 +17,10 @@ void read_dns_rsp()
memcpy(&dns_flag, rsp_data, 2);
dns = dns_list + dns_flag;
client = cts + (dns_flag << 1);
//判断是否是正常DNS回应是否已关闭连接
//判断是否是正常DNS回应是否已关闭连接
if (dns_flag > MAX_CONNECTION >> 1 || client->fd < 0)
continue;
if (dns->request_len + 12 > len || (unsigned char)rsp_data[3] != 128) //char只有7位可用则正数最高为127
if (dns->request_len + 12 > len || (unsigned char)rsp_data[3] != 128) //char只有7位可用则正数最高为127
{
close_connection(client);
continue;
@ -58,7 +58,7 @@ void read_dns_rsp()
}
}
/* 完全发送返回0发送部分返回1出错返回-1 */
/* 完全发送返回0发送部分返回1出错返回-1 */
static int8_t send_dns_req(struct dns *dns)
{
static int write_len;

2
dns.h
View File

@ -4,7 +4,7 @@
#include "main.h"
struct dns {
char request[512]; //UDP的DNS请求不超512字节
char request[512]; //UDP的DNS请求不超512字节
uint16_t request_len, sent_len;
};

2
http.c
View File

@ -226,10 +226,10 @@ static char *build_request(char *client_data, int *data_len, char *host)
static char *uri, *url, *p, *lf, *header, *new_data, *proxy_host;
static int len;
del_hdr(client_data, data_len);
header = client_data;
proxy_host = host;
do {
del_hdr(client_data, data_len);
/* 将完整url转换为uri */
url = strchr(header, ' ');
lf = strchr(header, '\n');

14
main.c
View File

@ -30,7 +30,7 @@ static void server_loop()
pthread_t thread_id;
int n;
//单独进程accept多进程并发环境下不会惊群
//单独进程accept多进程并发环境下不会惊群
pthread_create(&thread_id, NULL, accept_loop, NULL);
ev.events = EPOLLIN;
ev.data.fd = dnsFd;
@ -73,10 +73,10 @@ static void initializate(int argc, char **argv)
exit(1);
}
dnsAddr.sin_family = addr.sin_family = AF_INET;
//默认dns地址
//默认dns地址
dnsAddr.sin_addr.s_addr = inet_addr(DEFAULT_DNS_IP);
dnsAddr.sin_port = htons(53);
dns_connect(&dnsAddr); //主进程中的fd
dns_connect(&dnsAddr); //主进程中的fd
strict_spilce = 0;
local_header = NULL;
ssl_proxy = (char *)"CONNECT";
@ -84,7 +84,7 @@ static void initializate(int argc, char **argv)
proxy_header = (char *)"\nHost:";
proxy_header_len = strlen(proxy_header);
local_header_len = strlen(local_header);
/* 读取命令行参数 */
/* 读取命令行参数 */
while ((opt = getopt(argc, argv, "d:l:p:s:w:L:ah")) != -1)
{
switch (opt)
@ -118,7 +118,7 @@ static void initializate(int argc, char **argv)
break;
case 'p':
//假如选项值为 "Proxy", proxy_header设置为 "\nProxy:"
//假如选项值为 "Proxy", proxy_header设置为 "\nProxy:"
proxy_header_len = strlen(optarg) + 2;
if (optarg[proxy_header_len] == ':')
optarg[proxy_header_len--] = '\0';
@ -169,7 +169,7 @@ static void initializate(int argc, char **argv)
memset(cts, 0, sizeof(cts));
for (i = MAX_CONNECTION; i--; )
cts[i].fd = -1;
//为服务端的结构体分配内存
//为服务端的结构体分配内存
for (i = 1; i < MAX_CONNECTION; i += 2)
{
cts[i].ready_data = (char *)malloc(BUFFER_SIZE);
@ -196,7 +196,7 @@ static void initializate(int argc, char **argv)
}
signal(SIGPIPE, SIG_IGN);
while (workers-- > 1 && fork() == 0)
//子进程中的dnsFd必须重新申请不然epoll监听可能读取到其他进程得到的数据
//子进程中的dnsFd必须重新申请不然epoll监听可能读取到其他进程得到的数据
dns_connect(&dnsAddr);
}