2020-07-30 18:10:31 +08:00
|
|
|
#include "main.h"
|
|
|
|
#include "http_proxy.h"
|
|
|
|
#include "http_request.h"
|
2020-01-21 19:48:05 +08:00
|
|
|
#include "timeout.h"
|
|
|
|
#include "conf.h"
|
2020-02-13 15:33:38 +08:00
|
|
|
#include "kill.h"
|
2020-01-21 19:48:05 +08:00
|
|
|
#include "help.h"
|
2020-06-20 16:59:51 +08:00
|
|
|
#include "httpdns.h"
|
2020-01-21 19:48:05 +08:00
|
|
|
|
|
|
|
#define SERVER_STOP 1
|
|
|
|
#define SERVER_RELOAD 2
|
|
|
|
#define SERVER_STATUS 3
|
|
|
|
|
|
|
|
struct epoll_event ev, events[MAX_CONNECTION + 1];
|
|
|
|
int epollfd, server_sock;
|
|
|
|
conn cts[MAX_CONNECTION];
|
|
|
|
|
2020-06-08 20:28:15 +08:00
|
|
|
int create_connection(char *remote_host, int remote_port)
|
|
|
|
{
|
2020-01-21 19:48:05 +08:00
|
|
|
struct sockaddr_in server_addr;
|
|
|
|
struct hostent *server;
|
2020-08-16 07:58:53 +08:00
|
|
|
int sock = -1;
|
|
|
|
server = NULL;
|
2020-01-21 19:48:05 +08:00
|
|
|
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
|
|
|
perror("socket");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-06-08 20:28:15 +08:00
|
|
|
|
2020-01-21 19:48:05 +08:00
|
|
|
if ((server = gethostbyname(remote_host)) == NULL) {
|
|
|
|
perror("gethostbyname");
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
2020-06-08 20:28:15 +08:00
|
|
|
|
2020-01-21 19:48:05 +08:00
|
|
|
memset(&server_addr, 0, sizeof(server_addr));
|
|
|
|
server_addr.sin_family = AF_INET;
|
2020-08-16 07:58:53 +08:00
|
|
|
memmove(&server_addr.sin_addr.s_addr, server->h_addr, server->h_length);
|
2020-01-21 19:48:05 +08:00
|
|
|
server_addr.sin_port = htons(remote_port);
|
2020-06-08 20:28:15 +08:00
|
|
|
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
2020-01-21 19:48:05 +08:00
|
|
|
perror("connect");
|
2020-06-08 20:28:15 +08:00
|
|
|
close(sock);
|
2020-01-21 19:48:05 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2020-06-08 20:28:15 +08:00
|
|
|
|
2020-01-21 19:48:05 +08:00
|
|
|
fcntl(sock, F_SETFL, O_NONBLOCK);
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:28:15 +08:00
|
|
|
int create_server_socket(int port)
|
|
|
|
{
|
2020-01-21 19:48:05 +08:00
|
|
|
int server_sock;
|
|
|
|
int optval = 1;
|
|
|
|
struct sockaddr_in server_addr;
|
|
|
|
if ((server_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
|
|
|
perror("socket");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
|
|
|
|
perror("setsockopt");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memset(&server_addr, 0, sizeof(server_addr));
|
|
|
|
server_addr.sin_family = AF_INET;
|
|
|
|
server_addr.sin_port = htons(port);
|
|
|
|
server_addr.sin_addr.s_addr = INADDR_ANY;
|
2020-06-08 20:28:15 +08:00
|
|
|
if (bind(server_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) != 0) {
|
2020-01-21 19:48:05 +08:00
|
|
|
perror("bind");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (listen(server_sock, 50) < 0) {
|
|
|
|
perror("listen");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return server_sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
void accept_client()
|
|
|
|
{
|
|
|
|
struct epoll_event epollEvent;
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
conn *client;
|
|
|
|
socklen_t addr_len = sizeof(addr);
|
|
|
|
|
|
|
|
// 偶数为客户端,奇数为服务端
|
|
|
|
for (client = cts; client - cts < MAX_CONNECTION; client += 2)
|
|
|
|
if (client->fd < 0)
|
|
|
|
break;
|
|
|
|
if (client - cts >= MAX_CONNECTION)
|
|
|
|
return;
|
2020-06-08 20:28:15 +08:00
|
|
|
client->timer = (client + 1)->timer = 0;
|
2020-01-21 19:48:05 +08:00
|
|
|
client->fd = accept(server_sock, (struct sockaddr *)&addr, &addr_len);
|
|
|
|
if (client->fd < 0)
|
|
|
|
return;
|
|
|
|
fcntl(client->fd, F_SETFL, O_NONBLOCK);
|
2020-06-08 20:28:15 +08:00
|
|
|
epollEvent.events = EPOLLIN | EPOLLET;
|
2020-01-21 19:48:05 +08:00
|
|
|
epollEvent.data.ptr = client;
|
|
|
|
epoll_ctl(epollfd, EPOLL_CTL_ADD, client->fd, &epollEvent);
|
|
|
|
}
|
|
|
|
|
2020-06-20 16:59:51 +08:00
|
|
|
void *http_proxy_loop(void *p)
|
2020-01-21 19:48:05 +08:00
|
|
|
{
|
2020-06-20 16:59:51 +08:00
|
|
|
conf *configure = (conf *) p;
|
2020-01-21 19:48:05 +08:00
|
|
|
int n;
|
2020-06-20 16:59:51 +08:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
n = epoll_wait(epollfd, events, MAX_CONNECTION, -1);
|
|
|
|
while (n-- > 0) {
|
|
|
|
if (events[n].data.fd == server_sock) {
|
|
|
|
accept_client();
|
|
|
|
} else {
|
|
|
|
if (events[n].events & EPOLLIN) {
|
|
|
|
tcp_in((conn *) events[n].data.ptr, configure);
|
|
|
|
}
|
|
|
|
if (events[n].events & EPOLLOUT) {
|
|
|
|
tcp_out((conn *) events[n].data.ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(epollfd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *start_server(conf * configure)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
pthread_t thread_id;
|
2020-01-21 19:48:05 +08:00
|
|
|
if (timeout_minute)
|
2020-08-16 07:58:53 +08:00
|
|
|
pthread_create(&thread_id, NULL, &tcp_timeout_check, NULL);
|
2020-01-21 19:48:05 +08:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
n = epoll_wait(epollfd, events, MAX_CONNECTION, -1);
|
|
|
|
while (n-- > 0) {
|
|
|
|
if (events[n].data.fd == server_sock) {
|
|
|
|
accept_client();
|
|
|
|
} else {
|
2020-06-08 20:28:15 +08:00
|
|
|
if (events[n].events & EPOLLIN) {
|
2020-01-21 19:48:05 +08:00
|
|
|
tcp_in((conn *) events[n].data.ptr, configure);
|
|
|
|
}
|
|
|
|
if (events[n].events & EPOLLOUT) {
|
|
|
|
tcp_out((conn *) events[n].data.ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(epollfd);
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:28:15 +08:00
|
|
|
int process_signal(int signal, char *process_name)
|
2020-01-21 19:48:05 +08:00
|
|
|
{
|
|
|
|
char bufer[PATH_SIZE];
|
|
|
|
char comm[PATH_SIZE];
|
|
|
|
char proc_comm_name[PATH_SIZE];
|
2020-08-16 07:58:53 +08:00
|
|
|
int number[PATH_SIZE] = { 0 };
|
2020-01-21 19:48:05 +08:00
|
|
|
int n = 0;
|
|
|
|
FILE *fp;
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *ptr;
|
|
|
|
dir = opendir("/proc");
|
2020-08-16 07:58:53 +08:00
|
|
|
bzero(bufer, 0);
|
|
|
|
bzero(comm, 0);
|
|
|
|
bzero(proc_comm_name, 0);
|
2020-01-21 19:48:05 +08:00
|
|
|
while ((ptr = readdir(dir)) != NULL) {
|
2020-08-16 07:58:53 +08:00
|
|
|
if (ptr->d_type == DT_DIR && strcasecmp(ptr->d_name, ".") && strcasecmp(ptr->d_name, "..")) {
|
2020-01-21 19:48:05 +08:00
|
|
|
sprintf(comm, "/proc/%s/comm", ptr->d_name);
|
|
|
|
if (access(comm, F_OK) == 0) {
|
|
|
|
fp = fopen(comm, "r");
|
|
|
|
if (fgets(bufer, PATH_SIZE - 1, fp) == NULL) {
|
|
|
|
fclose(fp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
sscanf(bufer, "%s", proc_comm_name);
|
|
|
|
if (!strcmp(process_name, proc_comm_name)) {
|
2020-08-16 07:58:53 +08:00
|
|
|
number[n] = atoi(ptr->d_name);
|
2020-01-21 19:48:05 +08:00
|
|
|
n += 1;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-06-20 16:59:51 +08:00
|
|
|
closedir(dir);
|
|
|
|
|
2020-08-21 17:51:19 +08:00
|
|
|
if (signal == SERVER_STATUS) { // 状态
|
|
|
|
n -= 2; // 去除最后一个搜索时的本身进程和最后加一后未使用的
|
|
|
|
for (; n >= 0; n--) { // 依据数组从大到小的下标打印PID
|
2020-08-16 07:58:53 +08:00
|
|
|
printf("\t%d\n", number[n]);
|
2020-06-20 16:59:51 +08:00
|
|
|
}
|
2020-02-13 15:33:38 +08:00
|
|
|
}
|
2020-07-30 18:10:31 +08:00
|
|
|
if (signal == SERVER_STOP || signal == SERVER_RELOAD) { // 关闭
|
2020-02-13 15:33:38 +08:00
|
|
|
struct passwd *pwent = NULL;
|
|
|
|
pwent = getpwnam("root");
|
|
|
|
return kill_all(15, 1, &process_name, pwent);
|
2020-01-21 19:48:05 +08:00
|
|
|
}
|
2020-06-20 16:59:51 +08:00
|
|
|
|
2020-01-21 19:48:05 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_executable_path(char *processdir, char *processname, int len)
|
|
|
|
{
|
|
|
|
char *filename;
|
|
|
|
if (readlink("/proc/self/exe", processdir, len) <= 0)
|
|
|
|
return -1;
|
|
|
|
filename = strrchr(processdir, '/');
|
|
|
|
if (filename == NULL)
|
|
|
|
return -1;
|
|
|
|
++filename;
|
|
|
|
strcpy(processname, filename);
|
|
|
|
*filename = '\0';
|
|
|
|
return (int)(filename - processdir);
|
|
|
|
}
|
|
|
|
|
2020-06-20 16:59:51 +08:00
|
|
|
void server_ini()
|
|
|
|
{
|
|
|
|
signal(SIGPIPE, SIG_IGN); // 忽略PIPE信号
|
|
|
|
if (daemon(1, 1)) {
|
|
|
|
perror("daemon");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//while (process-- > 1 && fork() == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _main(int argc, char *argv[])
|
2020-01-21 19:48:05 +08:00
|
|
|
{
|
2020-06-20 16:59:51 +08:00
|
|
|
int opt, i;
|
2020-01-21 19:48:05 +08:00
|
|
|
char path[PATH_SIZE] = { 0 };
|
|
|
|
char executable_filename[PATH_SIZE] = { 0 };
|
|
|
|
(void)get_executable_path(path, executable_filename, sizeof(path));
|
2020-03-24 11:46:32 +08:00
|
|
|
char *inifile = "/CProxy.conf";
|
2020-01-21 19:48:05 +08:00
|
|
|
inifile = strcat(path, inifile);
|
|
|
|
conf *configure = (struct CONF *)malloc(sizeof(struct CONF));
|
2020-08-21 17:51:19 +08:00
|
|
|
memset(configure, 0, sizeof(struct CONF));
|
2020-01-21 19:48:05 +08:00
|
|
|
read_conf(inifile, configure);
|
|
|
|
|
2020-06-20 16:59:51 +08:00
|
|
|
sslEncodeCode = 0; // 默认SSL不转码
|
2020-06-08 20:28:15 +08:00
|
|
|
if (configure->sslencoding > 0) // 如果配置文件有sslencoding值,优先使用配置文件读取的值
|
|
|
|
sslEncodeCode = configure->sslencoding;
|
2020-06-20 16:59:51 +08:00
|
|
|
timeout_minute = 0; // 默认不超时
|
2020-07-30 18:10:31 +08:00
|
|
|
if (configure->timeout > 0) // 如果配置文件有值,优先使用配置文件读取的值
|
|
|
|
timeout_minute = configure->timeout;
|
2020-06-20 16:59:51 +08:00
|
|
|
process = 2; // 默认开启2个进程
|
|
|
|
if (configure->process > 0) // 如果配置文件有值,优先使用配置文件读取的值
|
2020-01-21 19:48:05 +08:00
|
|
|
process = configure->process;
|
|
|
|
|
2020-03-24 11:46:32 +08:00
|
|
|
int longindex = 0;
|
|
|
|
char optstring[] = ":l:f:t:p:c:e:s:h?";
|
|
|
|
static struct option longopts[] = {
|
|
|
|
{ "local_address", required_argument, 0, 'l' },
|
|
|
|
{ "remote_address", required_argument, 0, 'f' },
|
|
|
|
{ "timeout", required_argument, 0, 't' },
|
|
|
|
{ "process", required_argument, 0, 'p' },
|
|
|
|
{ "config", required_argument, 0, 'c' },
|
|
|
|
{ "coding", required_argument, 0, 'e' },
|
|
|
|
{ "signal", required_argument, 0, 's' },
|
|
|
|
{ "help", no_argument, 0, 'h' },
|
|
|
|
{ "?", no_argument, 0, '?' },
|
|
|
|
{ 0, 0, 0, 0 }
|
|
|
|
};
|
2020-01-21 19:48:05 +08:00
|
|
|
char *p = NULL;
|
2020-08-21 17:51:19 +08:00
|
|
|
//char optstring[] = ":l:f:t:p:c:e:s:h?";
|
2020-03-24 11:46:32 +08:00
|
|
|
//while (-1 != (opt = getopt(argc, argv, optstring))) {
|
|
|
|
while (-1 != (opt = getopt_long(argc, argv, optstring, longopts, &longindex))) {
|
2020-01-21 19:48:05 +08:00
|
|
|
switch (opt) {
|
|
|
|
case 'l':
|
|
|
|
p = strchr(optarg, ':');
|
|
|
|
if (p) {
|
|
|
|
strncpy(local_host, optarg, p - optarg);
|
|
|
|
local_port = atoi(p + 1);
|
|
|
|
} else {
|
|
|
|
strncpy(local_host, optarg, strlen(local_host));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
p = strchr(optarg, ':');
|
|
|
|
if (p) {
|
|
|
|
strncpy(remote_host, optarg, p - optarg);
|
|
|
|
remote_port = atoi(p + 1);
|
|
|
|
} else {
|
|
|
|
strncpy(remote_host, optarg, strlen(remote_host));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 't':
|
2020-06-08 20:28:15 +08:00
|
|
|
timeout_minute = (time_t) atoi(optarg); // 如果指定-t,优先使用参数提供的值(输入值 > 配置文件读取的值)
|
2020-01-21 19:48:05 +08:00
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
process = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
free_conf(configure);
|
|
|
|
inifile = optarg;
|
|
|
|
read_conf(inifile, configure);
|
|
|
|
break;
|
2020-03-24 11:46:32 +08:00
|
|
|
case 'e':
|
|
|
|
sslEncodeCode = atoi(optarg);
|
|
|
|
break;
|
2020-01-21 19:48:05 +08:00
|
|
|
case 's':
|
|
|
|
if (strcasecmp(optarg, "stop") == 0 || strcasecmp(optarg, "quit") == 0) {
|
|
|
|
free_conf(configure);
|
|
|
|
exit(process_signal(SERVER_STOP, executable_filename));
|
|
|
|
}
|
2020-06-20 16:59:51 +08:00
|
|
|
if (strcasecmp(optarg, "restart") == 0 || strcasecmp(optarg, "reload") == 0) {
|
2020-01-21 19:48:05 +08:00
|
|
|
process_signal(SERVER_RELOAD, executable_filename);
|
2020-06-20 16:59:51 +08:00
|
|
|
}
|
2020-01-21 19:48:05 +08:00
|
|
|
if (strcasecmp(optarg, "status") == 0)
|
|
|
|
exit(process_signal(SERVER_STATUS, executable_filename));
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
|
|
|
help_information();
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
2020-06-20 16:59:51 +08:00
|
|
|
}
|
2020-06-08 20:28:15 +08:00
|
|
|
|
2020-08-21 17:51:19 +08:00
|
|
|
server_ini(); // 守护进程
|
|
|
|
httpdns_initialize(configure); // 初始化http_dns
|
2020-01-21 19:48:05 +08:00
|
|
|
memset(cts, 0, sizeof(cts));
|
2020-06-08 20:28:15 +08:00
|
|
|
for (i = MAX_CONNECTION; i--;)
|
2020-01-21 19:48:05 +08:00
|
|
|
cts[i].fd = -1;
|
2020-06-08 20:28:15 +08:00
|
|
|
// 为服务端的结构体分配内存
|
|
|
|
for (i = 1; i < MAX_CONNECTION; i += 2) {
|
2020-01-21 19:48:05 +08:00
|
|
|
cts[i].header_buffer = (char *)malloc(BUFFER_SIZE);
|
2020-06-08 20:28:15 +08:00
|
|
|
if (cts[i].header_buffer == NULL) {
|
2020-01-21 19:48:05 +08:00
|
|
|
fputs("out of memory.", stderr);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2020-07-30 18:10:31 +08:00
|
|
|
server_sock = create_server_socket(configure->tcp_listen);
|
2020-06-20 16:59:51 +08:00
|
|
|
epollfd = epoll_create(MAX_CONNECTION);
|
2020-01-21 19:48:05 +08:00
|
|
|
if (epollfd == -1) {
|
|
|
|
perror("epoll_create");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
static struct epoll_event event;
|
|
|
|
event.events = EPOLLIN;
|
|
|
|
event.data.fd = server_sock;
|
|
|
|
if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, server_sock, &event)) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (setegid(configure->uid) == -1 || seteuid(configure->uid) == -1) // 设置uid
|
|
|
|
exit(1);
|
2020-06-08 20:28:15 +08:00
|
|
|
|
2020-08-21 17:51:19 +08:00
|
|
|
//start_server(configure); // 单线程
|
2020-08-16 07:58:53 +08:00
|
|
|
//httpdns_loop(configure);
|
2020-08-21 17:51:19 +08:00
|
|
|
|
2020-07-30 18:10:31 +08:00
|
|
|
pthread_t thread_id = 0;
|
2020-06-20 16:59:51 +08:00
|
|
|
sigset_t signal_mask;
|
|
|
|
sigemptyset(&signal_mask);
|
|
|
|
sigaddset(&signal_mask, SIGPIPE); // 忽略PIPE信号
|
|
|
|
if (pthread_sigmask(SIG_BLOCK, &signal_mask, NULL) != 0) {
|
|
|
|
printf("block sigpipe error\n");
|
|
|
|
}
|
|
|
|
if (timeout_minute)
|
2020-08-16 07:58:53 +08:00
|
|
|
pthread_create(&thread_id, NULL, &tcp_timeout_check, NULL);
|
2020-06-20 16:59:51 +08:00
|
|
|
if (pthread_create(&thread_id, NULL, &http_proxy_loop, (void *)configure) != 0)
|
|
|
|
perror("pthread_create");
|
2020-07-30 18:10:31 +08:00
|
|
|
if (pthread_create(&thread_id, NULL, &httpdns_loop, (void *)configure) != 0)
|
2020-06-20 16:59:51 +08:00
|
|
|
perror("pthread_create");
|
2020-08-21 17:51:19 +08:00
|
|
|
|
2020-06-20 16:59:51 +08:00
|
|
|
pthread_join(thread_id, NULL);
|
|
|
|
pthread_exit(NULL);
|
|
|
|
|
2020-08-21 17:51:19 +08:00
|
|
|
/* 线程分离未使用
|
|
|
|
pthread_attr_t attr;
|
|
|
|
pthread_attr_init(&attr);
|
|
|
|
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
|
|
|
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
|
|
|
pthread_create(&thread_id, &attr, &tcp_timeout_check, NULL);
|
|
|
|
pthread_create(&thread_id, &attr, &http_proxy_loop, (void *)configure);
|
|
|
|
pthread_create(&thread_id, &attr, &httpdns_loop, (void *)configure);
|
|
|
|
pthread_exit(NULL);
|
|
|
|
*/
|
|
|
|
return;
|
2020-01-21 19:48:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2020-06-20 16:59:51 +08:00
|
|
|
_main(argc, argv);
|
2020-01-21 19:48:05 +08:00
|
|
|
}
|