80 lines
1.8 KiB
C
80 lines
1.8 KiB
C
#include <dirent.h>
|
||
#include <pthread.h>
|
||
#include "main.h"
|
||
|
||
struct global global;
|
||
|
||
struct tcp_mode http, https;
|
||
struct save_header *saveHdrs;
|
||
char *default_ssl_request;
|
||
int default_ssl_request_len;
|
||
|
||
void *timeout_check(void *nullPtr)
|
||
{
|
||
while (1) {
|
||
sleep(60);
|
||
if (global.udp_listen_fd >= 0)
|
||
udp_timeout_check();
|
||
}
|
||
|
||
return NULL;
|
||
}
|
||
|
||
/* 初始化变量 */
|
||
static void initVariable()
|
||
{
|
||
memset(&global, 0, sizeof(global));
|
||
memset(&https, 0, sizeof(https));
|
||
memset(&udp, 0, sizeof(udp));
|
||
saveHdrs = NULL;
|
||
http.dst.sin_family = https.dst.sin_family = udp.dst.sin_family = AF_INET;
|
||
global.tcp_listen_fd = global.udp_listen_fd = global.uid = -1;
|
||
}
|
||
|
||
static void server_init()
|
||
{
|
||
/* 忽略PIPE信号 */
|
||
signal(SIGPIPE, SIG_IGN);
|
||
//不能用setgid和setuid,这两个函数不能切换回root,可能导致HTTPUDP代理失败
|
||
if (global.uid > -1 && (setegid(global.uid) == -1 || seteuid(global.uid) == -1)) {
|
||
perror("setegid(or seteuid)");
|
||
exit(1);
|
||
}
|
||
#ifndef DEBUG
|
||
if (daemon(1, 1) == -1) {
|
||
perror("daemon");
|
||
exit(1);
|
||
}
|
||
#endif
|
||
/*
|
||
一个进程只开一个子进程,
|
||
程序结束时子进程先写入dns缓存,
|
||
之后主进程再写入,
|
||
否则可能导致缓存文件格式错误
|
||
*/
|
||
//while (global.procs-- > 1 && (child_pid = fork()) == 0);
|
||
}
|
||
|
||
static void start_server_loop()
|
||
{
|
||
//printf("%s", udp.http_request);
|
||
//printf("%d\n", udp.http_request_len);
|
||
|
||
pthread_t thread_id;
|
||
|
||
if (global.timeout_m)
|
||
pthread_create(&thread_id, NULL, &timeout_check, NULL);
|
||
udp_init();
|
||
udp_loop(NULL);
|
||
}
|
||
|
||
int main(int argc, char *argv[])
|
||
{
|
||
initVariable();
|
||
read_conf(argv[1]);
|
||
server_init();
|
||
start_server_loop();
|
||
|
||
return 0;
|
||
}
|