Add files via upload
This commit is contained in:
parent
705cabbd8c
commit
3a7d93668a
12
dns.c
12
dns.c
@ -1,7 +1,7 @@
|
|||||||
#include "dns.h"
|
#include "dns.h"
|
||||||
#include "http.h"
|
#include "http.h"
|
||||||
|
|
||||||
struct dns dns_list[MAX_CONNECTION / 2]; //一个客户端 + 一个服务端 占用一个dns结构体
|
struct dns dns_list[MAX_CONNECTION / 2]; //一个客户端 + 一个服务端 占用一个dns结构体
|
||||||
int dnsFd;
|
int dnsFd;
|
||||||
|
|
||||||
void read_dns_rsp()
|
void read_dns_rsp()
|
||||||
@ -17,10 +17,10 @@ void read_dns_rsp()
|
|||||||
memcpy(&dns_flag, rsp_data, 2);
|
memcpy(&dns_flag, rsp_data, 2);
|
||||||
dns = dns_list + dns_flag;
|
dns = dns_list + dns_flag;
|
||||||
client = cts + (dns_flag << 1);
|
client = cts + (dns_flag << 1);
|
||||||
//判断是否是正常DNS回应,是否已关闭连接
|
//判断是否是正常DNS回应,是否已关闭连接
|
||||||
if (dns_flag > MAX_CONNECTION >> 1 || client->fd < 0)
|
if (dns_flag > MAX_CONNECTION >> 1 || client->fd < 0)
|
||||||
continue;
|
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);
|
close_connection(client);
|
||||||
continue;
|
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 int8_t send_dns_req(struct dns *dns)
|
||||||
{
|
{
|
||||||
static int write_len;
|
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)
|
void dns_connect(struct sockaddr_in *dnsAddr)
|
||||||
{
|
{
|
||||||
dnsFd = socket(AF_INET, SOCK_DGRAM, 0);
|
dnsFd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
if (dnsFd < 0)
|
if (dnsFd < 0)
|
||||||
{
|
{
|
||||||
perror("socket");
|
perror("socket");
|
||||||
exit(1);
|
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);
|
fcntl(dnsFd, F_SETFL, O_NONBLOCK);
|
||||||
}
|
}
|
||||||
|
2
dns.h
2
dns.h
@ -4,7 +4,7 @@
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
|
||||||
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
46
http.c
46
http.c
@ -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 char *uri, *url, *p, *lf, *header, *new_data, *proxy_host;
|
||||||
static int len;
|
static int len;
|
||||||
|
|
||||||
del_hdr(client_data, data_len);
|
|
||||||
header = client_data;
|
header = client_data;
|
||||||
proxy_host = host;
|
proxy_host = host;
|
||||||
do {
|
do {
|
||||||
|
del_hdr(client_data, data_len);
|
||||||
/* 将完整url转换为uri */
|
/* 将完整url转换为uri */
|
||||||
url = strchr(header, ' ');
|
url = strchr(header, ' ');
|
||||||
lf = strchr(header, '\n');
|
lf = strchr(header, '\n');
|
||||||
@ -435,7 +435,7 @@ void tcp_in(conn_t *in)
|
|||||||
if (in->fd < 0)
|
if (in->fd < 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
//如果in - cts是奇数,那么是服务端触发事件
|
//如果in - cts是奇数,那么是服务端触发事件
|
||||||
if ((in - cts) & 1)
|
if ((in - cts) & 1)
|
||||||
{
|
{
|
||||||
if (in->ready_data_len == 0)
|
if (in->ready_data_len == 0)
|
||||||
@ -537,28 +537,28 @@ void *accept_loop(void *ptr)
|
|||||||
{
|
{
|
||||||
conn_t *client;
|
conn_t *client;
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
/* 偶数为客户端,奇数为服务端 */
|
/* 偶数为客户端,奇数为服务端 */
|
||||||
for (client = cts; client - cts < MAX_CONNECTION; client += 2)
|
for (client = cts; client - cts < MAX_CONNECTION; client += 2)
|
||||||
if (client->fd < 0)
|
if (client->fd < 0)
|
||||||
break;
|
break;
|
||||||
if (client - cts >= MAX_CONNECTION)
|
if (client - cts >= MAX_CONNECTION)
|
||||||
{
|
{
|
||||||
sleep(3);
|
sleep(3);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
client->fd = accept(lisFd, (struct sockaddr *)&addr, &addr_len);
|
client->fd = accept(lisFd, (struct sockaddr *)&addr, &addr_len);
|
||||||
if (client->fd >= 0)
|
if (client->fd >= 0)
|
||||||
{
|
{
|
||||||
fcntl(client->fd, F_SETFL, fcntl(client->fd, F_GETFL)|O_NONBLOCK);
|
fcntl(client->fd, F_SETFL, fcntl(client->fd, F_GETFL)|O_NONBLOCK);
|
||||||
ev.data.ptr = client;
|
ev.data.ptr = client;
|
||||||
ev.events = EPOLLIN|EPOLLET;
|
ev.events = EPOLLIN|EPOLLET;
|
||||||
epoll_ctl(efd, EPOLL_CTL_ADD, client->fd, &ev);
|
epoll_ctl(efd, EPOLL_CTL_ADD, client->fd, &ev);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void create_listen(char *ip, int port)
|
void create_listen(char *ip, int port)
|
||||||
|
32
main.c
32
main.c
@ -27,11 +27,11 @@ static void usage()
|
|||||||
|
|
||||||
static void server_loop()
|
static void server_loop()
|
||||||
{
|
{
|
||||||
pthread_t thread_id;
|
pthread_t thread_id;
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
//单独进程accept多进程并发环境下不会惊群
|
//单独进程accept多进程并发环境下不会惊群
|
||||||
pthread_create(&thread_id, NULL, accept_loop, NULL);
|
pthread_create(&thread_id, NULL, accept_loop, NULL);
|
||||||
ev.events = EPOLLIN;
|
ev.events = EPOLLIN;
|
||||||
ev.data.fd = dnsFd;
|
ev.data.fd = dnsFd;
|
||||||
epoll_ctl(efd, EPOLL_CTL_ADD, dnsFd, &ev);
|
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);
|
n = epoll_wait(efd, evs, MAX_CONNECTION + 2, -1);
|
||||||
while (n-- > 0)
|
while (n-- > 0)
|
||||||
{
|
{
|
||||||
if (evs[n].data.fd == dnsFd)
|
if (evs[n].data.fd == dnsFd)
|
||||||
{
|
{
|
||||||
if (evs[n].events & EPOLLIN)
|
if (evs[n].events & EPOLLIN)
|
||||||
@ -60,7 +60,7 @@ static void server_loop()
|
|||||||
|
|
||||||
static void initializate(int argc, char **argv)
|
static void initializate(int argc, char **argv)
|
||||||
{
|
{
|
||||||
struct sockaddr_in dnsAddr;
|
struct sockaddr_in dnsAddr;
|
||||||
char *p;
|
char *p;
|
||||||
int opt, i, workers = 1;
|
int opt, i, workers = 1;
|
||||||
|
|
||||||
@ -73,22 +73,22 @@ static void initializate(int argc, char **argv)
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
dnsAddr.sin_family = addr.sin_family = AF_INET;
|
dnsAddr.sin_family = addr.sin_family = AF_INET;
|
||||||
//默认dns地址
|
//默认dns地址
|
||||||
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 = 0;
|
strict_spilce = 0;
|
||||||
local_header = NULL;
|
local_header = NULL;
|
||||||
ssl_proxy = (char *)"CONNECT";
|
ssl_proxy = (char *)"CONNECT";
|
||||||
local_header = (char *)"\nLocal:";
|
local_header = (char *)"\nLocal:";
|
||||||
proxy_header = (char *)"\nHost:";
|
proxy_header = (char *)"\nHost:";
|
||||||
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:p:s:w:L:ah")) != -1)
|
while ((opt = getopt(argc, argv, "d:l:p:s:w:L:ah")) != -1)
|
||||||
{
|
{
|
||||||
switch (opt)
|
switch (opt)
|
||||||
{
|
{
|
||||||
case 'd':
|
case 'd':
|
||||||
p = strchr(optarg, ':');
|
p = strchr(optarg, ':');
|
||||||
if (p)
|
if (p)
|
||||||
@ -101,7 +101,7 @@ static void initializate(int argc, char **argv)
|
|||||||
dnsAddr.sin_port = htons(53);
|
dnsAddr.sin_port = htons(53);
|
||||||
}
|
}
|
||||||
dnsAddr.sin_addr.s_addr = inet_addr(optarg);
|
dnsAddr.sin_addr.s_addr = inet_addr(optarg);
|
||||||
connect(dnsFd, (struct sockaddr *)&dnsAddr, sizeof(dnsAddr));
|
connect(dnsFd, (struct sockaddr *)&dnsAddr, sizeof(dnsAddr));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'l':
|
case 'l':
|
||||||
@ -118,7 +118,7 @@ static void initializate(int argc, char **argv)
|
|||||||
break;
|
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;
|
||||||
if (optarg[proxy_header_len] == ':')
|
if (optarg[proxy_header_len] == ':')
|
||||||
optarg[proxy_header_len--] = '\0';
|
optarg[proxy_header_len--] = '\0';
|
||||||
@ -169,7 +169,7 @@ static void initializate(int argc, char **argv)
|
|||||||
memset(cts, 0, sizeof(cts));
|
memset(cts, 0, sizeof(cts));
|
||||||
for (i = MAX_CONNECTION; i--; )
|
for (i = MAX_CONNECTION; i--; )
|
||||||
cts[i].fd = -1;
|
cts[i].fd = -1;
|
||||||
//为服务端的结构体分配内存
|
//为服务端的结构体分配内存
|
||||||
for (i = 1; i < MAX_CONNECTION; i += 2)
|
for (i = 1; i < MAX_CONNECTION; i += 2)
|
||||||
{
|
{
|
||||||
cts[i].ready_data = (char *)malloc(BUFFER_SIZE);
|
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;
|
dns_list[i].request[11] = 0;
|
||||||
}
|
}
|
||||||
signal(SIGPIPE, SIG_IGN);
|
signal(SIGPIPE, SIG_IGN);
|
||||||
while (workers-- > 1 && fork() == 0)
|
while (workers-- > 1 && fork() == 0)
|
||||||
//子进程中的dnsFd必须重新申请,不然epoll监听可能读取到其他进程得到的数据
|
//子进程中的dnsFd必须重新申请,不然epoll监听可能读取到其他进程得到的数据
|
||||||
dns_connect(&dnsAddr);
|
dns_connect(&dnsAddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
|
Loading…
Reference in New Issue
Block a user