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

12
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;
@ -145,12 +145,12 @@ int8_t build_dns_req(struct dns *dns, char *domain)
void dns_connect(struct sockaddr_in *dnsAddr)
{
dnsFd = socket(AF_INET, SOCK_DGRAM, 0);
dnsFd = socket(AF_INET, SOCK_DGRAM, 0);
if (dnsFd < 0)
{
perror("socket");
exit(1);
}
connect(dnsFd, (struct sockaddr *)dnsAddr, sizeof(struct sockaddr_in));
connect(dnsFd, (struct sockaddr *)dnsAddr, sizeof(struct sockaddr_in));
fcntl(dnsFd, F_SETFL, O_NONBLOCK);
}

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;
};

48
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');
@ -435,7 +435,7 @@ void tcp_in(conn_t *in)
if (in->fd < 0)
return;
//如果in - cts是奇数那么是服务端触发事件
//如果in - cts是奇数那么是服务端触发事件
if ((in - cts) & 1)
{
if (in->ready_data_len == 0)
@ -537,28 +537,28 @@ void *accept_loop(void *ptr)
{
conn_t *client;
while (1)
{
/* 偶数为客户端,奇数为服务端 */
for (client = cts; client - cts < MAX_CONNECTION; client += 2)
if (client->fd < 0)
break;
if (client - cts >= MAX_CONNECTION)
{
sleep(3);
continue;
}
client->fd = accept(lisFd, (struct sockaddr *)&addr, &addr_len);
if (client->fd >= 0)
{
fcntl(client->fd, F_SETFL, fcntl(client->fd, F_GETFL)|O_NONBLOCK);
ev.data.ptr = client;
ev.events = EPOLLIN|EPOLLET;
epoll_ctl(efd, EPOLL_CTL_ADD, client->fd, &ev);
}
}
return NULL;
while (1)
{
/* 偶数为客户端,奇数为服务端 */
for (client = cts; client - cts < MAX_CONNECTION; client += 2)
if (client->fd < 0)
break;
if (client - cts >= MAX_CONNECTION)
{
sleep(3);
continue;
}
client->fd = accept(lisFd, (struct sockaddr *)&addr, &addr_len);
if (client->fd >= 0)
{
fcntl(client->fd, F_SETFL, fcntl(client->fd, F_GETFL)|O_NONBLOCK);
ev.data.ptr = client;
ev.events = EPOLLIN|EPOLLET;
epoll_ctl(efd, EPOLL_CTL_ADD, client->fd, &ev);
}
}
return NULL;
}
void create_listen(char *ip, int port)

32
main.c
View File

@ -27,11 +27,11 @@ static void usage()
static void server_loop()
{
pthread_t thread_id;
pthread_t thread_id;
int n;
//单独进程accept多进程并发环境下不会惊群
pthread_create(&thread_id, NULL, accept_loop, NULL);
//单独进程accept多进程并发环境下不会惊群
pthread_create(&thread_id, NULL, accept_loop, NULL);
ev.events = EPOLLIN;
ev.data.fd = dnsFd;
epoll_ctl(efd, EPOLL_CTL_ADD, dnsFd, &ev);
@ -39,7 +39,7 @@ static void server_loop()
{
n = epoll_wait(efd, evs, MAX_CONNECTION + 2, -1);
while (n-- > 0)
{
{
if (evs[n].data.fd == dnsFd)
{
if (evs[n].events & EPOLLIN)
@ -60,7 +60,7 @@ static void server_loop()
static void initializate(int argc, char **argv)
{
struct sockaddr_in dnsAddr;
struct sockaddr_in dnsAddr;
char *p;
int opt, i, workers = 1;
@ -73,22 +73,22 @@ 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";
local_header = (char *)"\nLocal:";
proxy_header = (char *)"\nHost:";
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:p:s:w:L:ah")) != -1)
{
switch (opt)
{
{
case 'd':
p = strchr(optarg, ':');
if (p)
@ -101,7 +101,7 @@ static void initializate(int argc, char **argv)
dnsAddr.sin_port = htons(53);
}
dnsAddr.sin_addr.s_addr = inet_addr(optarg);
connect(dnsFd, (struct sockaddr *)&dnsAddr, sizeof(dnsAddr));
connect(dnsFd, (struct sockaddr *)&dnsAddr, sizeof(dnsAddr));
break;
case 'l':
@ -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);
@ -195,9 +195,9 @@ static void initializate(int argc, char **argv)
dns_list[i].request[11] = 0;
}
signal(SIGPIPE, SIG_IGN);
while (workers-- > 1 && fork() == 0)
//子进程中的dnsFd必须重新申请不然epoll监听可能读取到其他进程得到的数据
dns_connect(&dnsAddr);
while (workers-- > 1 && fork() == 0)
//子进程中的dnsFd必须重新申请不然epoll监听可能读取到其他进程得到的数据
dns_connect(&dnsAddr);
}
int main(int argc, char **argv)