Optimize daemon().

This commit is contained in:
aixiao 2022-04-23 21:55:24 +08:00
parent f6800178f4
commit 0efa5309a6
10 changed files with 178 additions and 158 deletions

View File

@ -1,15 +1,14 @@
global {
uid=3004;
timeout=60;
encode=129;
tcp_listen=0124;
dns_listen=0126;
udp_listen = 10010;
}
http {
http_ip="172.22.86.139";
http_port=129;
http_ip="git.aixiao.me";
http_port=128;
http_del="Host,";
http_first="[M] [U] [V]\r\nHost: [H]\r\n";
strrep="Windows NT 10.0" -> "Linux";
@ -17,15 +16,15 @@ http {
strrep="aixiao.me" -> "AIXIAO.ME";
regrep="Accept-Encoding*.+?" -> "Accept-Encoding: GZIP, deflate";
regrep="Connection*.+?" -> "Connection: KEEP-alive";
encode=129;
encode=128;
}
https {
https_ip="172.22.86.139";
https_port=129;
https_ip="git.aixiao.me";
https_port=128;
https_del="Host,host,x-online-host";
https_first="[M] [U] [V]\r\nHost: [host]:[port]\r\n";
encode=129;
encode=128;
}
httpdns {

View File

@ -1,88 +1,90 @@
//global模块
global {
//进程UID
uid=3004;
timeout=7;
encode=128;
//超时时间(秒)
timeout=60;
//TCP, DNS, UDP监听地址
tcp_listen=0124;
dns_listen=0126;
udp_listen = 10010;
}
//http模块
http {
http_ip="2001:19f0:4401:2f:5400:3ff:fec4:e376";
//http模块关键字: [M], [method], [uri], [U], [V], [version], [H], [host], [port], \r, \n, \v, \f, \b, \t, \a. 如果原本请求头含有关键字也会被替换.
//[M]、[method] 原请求方法
//[U] 原请求url
//[uri] 原请求uri(http 模块)
//[V]、[version] 原请求协议版本
//[host] 原请求host
//[port] 原请求端口
//[H] 原请求[host]:[port]
//指定HTTP服务器IP
http_ip="aixiao.me";
//指定HTTP服务器端口
http_port=129;
//删除HTTP头字段,逗号分开
http_del="Host,";
//自定义HTTP请求头
http_first="[M] [U] [V]\r\nHost: [H]\r\n";
//字符串替换, 以"->"为分界符,"Windows NT 10.0"字符串替换为"Linux"字符串.(可以有多个)
strrep="Windows NT 10.0" -> "Linux";
strrep="Linux" -> "aixiao.me";
strrep="aixiao.me" -> "AIXIAO.ME";
//正则表达式替换, 以"->"为分界符,匹配到的内容"Accept-Encoding*.+?"替换为"Accept-Encoding: GZIP, deflate"字符串.(可以有多个)
regrep="Accept-Encoding*.+?" -> "Accept-Encoding: GZIP, deflate";
regrep="Connection*.+?" -> "Connection: KEEP-alive";
// 编码, 代码128-255要与服务端一致默认为0不编码
encode=129;
}
//https模块
https {
https_ip="2001:19f0:4401:2f:5400:3ff:fec4:e376";
https_ip="aixiao.me";
https_port=129;
https_del="Host,host,x-online-host";
https_first="[M] [U] [V]\r\nHost: [host]\r\n";
strrep="Windows NT 10.0" -> "Linux";
strrep="Linux" -> "aixiao.me";
strrep="aixiao.me" -> "AIXIAO.ME";
regrep="Host*.+?" -> "host: [host]:443";
regrep="host*.+?" -> "Host: [H]";
regrep="host*.+?" -> "Host: [host]";
https_first="[M] [U] [V]\r\nHost: [host]:[port]\r\n";
encode=129;
}
//httpdns模块
httpdns {
//httpdns 模块关键字: [M], [D], [V], \r, \n, \v, \f, \b, \t, \a.
//默认 [M] 为 GET
//默认 [V] 为 HTTP/1.0
//HTTPDNS服务器IP
addr = 119.29.29.29:80;
//自定义HTTPDNS请求头
http_req = "[M] [U] [V]\r\nHost: [H]\r\n\r\n";
}
//httpudp模块
httpudp {
//httpudp 模块关键字: [M], [U], [V], [H].
//默认 [M] 为 CONNECT
//默认 [V] 为 HTTP/1.1
//HTTPUDP服务器IP
addr = 47.240.75.93:10010;
//自定义HTTPUDP请求头
http_req = "[M] [U] [V]\r\nHost: [H]\r\n";
}
global模块
uid=3004; //进程UID
process=2; //进程数量(已经弃用)
timeout=60; //超时时间(秒)
sslencoding=128; //编码(非零生效)
tcp_listen=0124; //TCP监听端口
tcp6_listen=0124; //TCP6监听端口 (TCP和TCP6可以使用同一个端口, IPV4的数据走内部IPV4程序结构, IPV6数据走内部IPV6程序结构.)(已经弃用)
dns_listen=0125; //UDP监听端口
http、https模块
http、https 模块关键字: [M], [method], [uri], [U], [V], [version], [H], [host], [port], \r, \n, \v, \f, \b, \t, \a. 如果原本请求头含有关键字也会被替换.
[M]、[method] 原请求方法
[U] 原请求url
[uri] 原请求uri(http 模块)
[V]、[version] 原请求协议版本
[host] 原请求host
[port] 原请求端口
[H] 原请求[host]:[port]
http_ip=47.240.75.93; //指定HTTP服务器IP
http_port=127; //指定HTTP服务器端口
http_del="x-online-host,X-Online-Host,host,Host"; //删除HTTP头字段,逗号分开
http_first="[M] [U] [V]\r\nHost: [H]\r\n"; //自定义HTTP请求头
关键字strrep替换字符串指令.
strrep = "Mi MIX 2->Linux"; //以"->"为分界符,"Mi MIX 2"字符串替换为"Linux"字符串.
关键字regrep正则匹配替换字符串.
regrep = "Host*.+?->Host: iread.wo.cn:443"; //以"->"为分界符,匹配到的内容"Host*.+?"替换为"Host: iread.wo.cn:443"字符串.
httpdns模块
httpdns 模块关键字: [M], [D], [V], \r, \n, \v, \f, \b, \t, \a.
默认 [M] 为 GET
默认 [V] 为 HTTP/1.0
addr=119.29.29.29:80; //HTTPDNS服务器IP
http_req = "[M] [U] [V]\r\nHost: [H]\r\n\r\n"; //自定义HTTPDNS请求头
httpudp模块
httpudp 模块关键字: [M], [U], [V], [H].
默认 [M] 为 CONNECT
默认 [V] 为 HTTP/1.1
addr=47.240.75.93:10010; //HTTPUDP服务器IP
http_req = "[M] [U] [V]\r\nHost: [H]\r\n\r\n"; //自定义HTTPUDP请求头

View File

@ -5,7 +5,7 @@ CFLAGS += -g -O2 -Wall -pthread
LIBS =
OBJ := CProxy
all: main.o http_proxy.o http_request.o httpdns.o httpudp.o conf.o help.o
all: main.o http_proxy.o http_request.o httpdns.o httpudp.o conf.o
$(CC) $(CFLAGS) -o $(OBJ) $^ $(LIBS)
.c.o:
$(CC) $(CFLAGS) -c $<

38
conf.c
View File

@ -113,7 +113,7 @@ static char *read_module(char *buff, const char *module_name)
return strndup(p, p0 - p);
}
static void parse_global_module(char *content, conf * p)
static void parse_global_module(char *content, conf *p)
{
char *var, *val_begin, *val_end, *lineEnd;
@ -155,7 +155,7 @@ static void parse_global_module(char *content, conf * p)
}
}
static void parse_http_module(char *content, conf * p)
static void parse_http_module(char *content, conf *p)
{
char *var, *val_begin, *val_end, *lineEnd;
tcp *http_node = NULL;
@ -264,7 +264,7 @@ static void parse_http_module(char *content, conf * p)
}
}
static void parse_https_module(char *content, conf * p)
static void parse_https_module(char *content, conf *p)
{
char *var, *val_begin, *val_end, *lineEnd;
tcp *https_node = NULL;
@ -380,7 +380,7 @@ static void parse_https_module(char *content, conf * p)
}
// 打印tcp链表
void print_tcp(tcp * p)
void print_tcp(tcp *p)
{
tcp *temp = p;
while (temp) {
@ -451,21 +451,21 @@ void free_tcp(tcp **conf_head)
}
}
static void parse_httpdns_module(char *content, conf * p)
static void parse_httpdns_module(char *content, conf *p)
{
char *var, *val_begin, *val_end, *lineEnd;
while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) {
if (strcasecmp(var, "addr") == 0)
{
p->addr_len = val_end - val_begin;
if (copy_new_mem(val_begin, p->addr_len, &p->addr) != 0)
p->httpdns_addr_len = val_end - val_begin;
if (copy_new_mem(val_begin, p->httpdns_addr_len, &p->httpdns_addr) != 0)
return ;
}
else if (strcasecmp(var, "http_req") == 0)
{
p->http_req_len = val_end - val_begin;
if (copy_new_mem(val_begin, p->http_req_len, &p->http_req) != 0)
p->httpdns_http_req_len = val_end - val_begin;
if (copy_new_mem(val_begin, p->httpdns_http_req_len, &p->httpdns_http_req) != 0)
return ;
}
else if (strcasecmp(var, "encode") == 0)
@ -477,7 +477,7 @@ static void parse_httpdns_module(char *content, conf * p)
}
}
static void parse_httpudp_module(char *content, conf * p)
static void parse_httpudp_module(char *content, conf *p)
{
char *var, *val_begin, *val_end, *lineEnd;
@ -503,7 +503,7 @@ static void parse_httpudp_module(char *content, conf * p)
}
}
void free_conf(conf * p)
void free_conf(conf *p)
{
// http module
if (p->http_ip)
@ -522,25 +522,21 @@ void free_conf(conf * p)
free(p->https_first);
// httpdns module
if (p->addr)
free(p->addr);
if (p->http_req) {
p->http_req_len = 0;
free(p->http_req);
}
if (p->httpdns_addr)
free(p->httpdns_addr);
if (p->httpdns_http_req)
free(p->httpdns_http_req);
// httpudp module
if(p->httpudp_addr)
free(p->httpudp_addr);
if (p->httpudp_http_req) {
p->httpudp_http_req_len = 0;
if (p->httpudp_http_req)
free(p->httpudp_http_req);
}
return;
}
void read_conf(char *filename, conf * configure)
void read_conf(char *filename, conf *configure)
{
char *buff, *global_content, *http_content, *https_content, *httpdns_content, *httpudp_content;
FILE *file;

8
conf.h
View File

@ -33,10 +33,10 @@ typedef struct CONF {
int https_encode;
// httpdns module
char *addr;
char *http_req;
int addr_len;
int http_req_len;
char *httpdns_addr;
char *httpdns_http_req;
int httpdns_addr_len;
int httpdns_http_req_len;
int encode;
// httpudp module

36
help.c
View File

@ -1,36 +0,0 @@
#include "help.h"
char help_information(void)
{
static const char name[] = "C";
static const char subject[] = "Proxy Server";
static const struct {
const char *email;
} author = {
"AIXIAO@AIXIAO.ME",
};
static const char usage[] = "Usage: [-?h] [-s signal] [-c filename]";
static const char *s_help[] = {
"",
"Options:",
" -s --signal : send signal to a master process: stop, quit, restart, reload, status",
" -c --config : set configuration file, default: CProxy.conf",
" -? -h --? --help : help information",
"",
0
};
fprintf(stderr, "%s %s\n", name, subject);
fprintf(stderr, "Author: %s\n", author.email);
fprintf(stderr, "%s\n", usage);
int l;
for (l = 0; s_help[l]; l++) {
fprintf(stderr, "%s\n", s_help[l]);
}
BUILD("Compile、link.\n");
return 0;
}

9
help.h
View File

@ -1,9 +0,0 @@
#ifndef HELP_H
#define HELP_H
#include <stdio.h>
#define BUILD(fmt...) do { fprintf(stderr,"%s %s ",__DATE__,__TIME__); fprintf(stderr, ##fmt); } while(0)
char help_information(void);
#endif

View File

@ -639,7 +639,7 @@ char *request_head(conn_t * in, conf * configure)
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[port]", 6, http_request->port, http_request->port_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[H]", 3, http_request->H, http_request->H_len);
//printf("%s", incomplete_head); // 打印HTTPS HEADER
printf("%s", incomplete_head); // 打印HTTPS HEADER
char *new_incomplete_data;
new_incomplete_data = (char *)realloc(in->incomplete_data, incomplete_head_len + 1);
@ -702,7 +702,7 @@ char *request_head(conn_t * in, conf * configure)
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[port]", 6, http_request->port, http_request->port_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[H]", 3, http_request->H, http_request->H_len);
//printf("%s", incomplete_head); // 打印HTTP HEADER
printf("%s", incomplete_head); // 打印HTTP HEADER
char *new_incomplete_data;
new_incomplete_data = (char *)realloc(in->incomplete_data, incomplete_head_len + 1);

112
main.c
View File

@ -4,7 +4,6 @@
#include "httpdns.h"
#include "httpudp.h"
#include "conf.h"
#include "help.h"
#define SERVER_TYPE_STOP 1
#define SERVER_TYPE_RELOAD 2
@ -29,6 +28,41 @@ int epollfd, server_sock, server_sock6, local_port, process;
conn_t cts[MAX_CONNECTION];
char local_host[CACHE_SIZE];
static char help_information(void)
{
static const char name[] = "C";
static const char subject[] = "Proxy Server";
static const struct {
const char *email;
} author = {
"AIXIAO@AIXIAO.ME",
};
static const char usage[] = "Usage: [-?h] [-s signal] [-c filename]";
static const char *s_help[] = {
"",
"Options:",
" -s --signal : send signal to a master process: stop, quit, restart, reload, status",
" -c --config : set configuration file, default: CProxy.conf",
" -? -h --? --help : help information",
"",
0
};
fprintf(stderr, "%s %s\n", name, subject);
fprintf(stderr, "Author: %s\n", author.email);
fprintf(stderr, "%s\n", usage);
int l;
for (l = 0; s_help[l]; l++) {
fprintf(stderr, "%s\n", s_help[l]);
}
BUILD("Compile、link.\n");
return 0;
}
int create_server_socket(int port)
{
int server_sock;
@ -143,7 +177,7 @@ void accept_client6()
void *http_proxy_loop(void *p)
{
conf *configure = (conf *) p;
conf *configure = (conf *)p;
int n;
ev.events = EPOLLIN;
@ -184,8 +218,6 @@ int process_signal(int signal, char *process_name)
char bufer[CACHE_SIZE];
char comm[CACHE_SIZE];
char proc_comm_name[CACHE_SIZE];
//int number[CACHE_SIZE] = { 0 };
//int n = 0;
FILE *fp;
DIR *dir;
struct dirent *ptr;
@ -296,7 +328,7 @@ void *timeout_check(void *nullPtr)
return NULL;
}
void initialize(conf * configure)
void initialize(conf *configure)
{
int i;
char *p;
@ -324,15 +356,15 @@ void initialize(conf * configure)
// httpdns module
global.dns_listen_fd = udp_listen((char *)"127.0.0.1", configure->dns_listen);
if ((p = strchr(configure->addr, ':')) != NULL) {
if ((p = strchr(configure->httpdns_addr, ':')) != NULL) {
*p = '\0';
httpdns.dst.sin_port = htons(atoi(p + 1));
} else {
httpdns.dst.sin_port = htons(80);
}
httpdns.dst.sin_addr.s_addr = inet_addr(configure->addr);
httpdns.http_req_len = configure->http_req_len;
copy_new_mem(configure->http_req, httpdns.http_req_len, &httpdns.http_req);
httpdns.dst.sin_addr.s_addr = inet_addr(configure->httpdns_addr);
httpdns.http_req_len = configure->httpdns_http_req_len;
copy_new_mem(configure->httpdns_http_req, httpdns.http_req_len, &httpdns.http_req);
// httpudp module
global.udp_listen_fd = udp_listen((char *)"0.0.0.0", configure->udp_listen);
@ -357,7 +389,7 @@ void initialize(conf * configure)
}
void thread_loop(conf * configure)
void thread_loop(conf *configure)
{
pthread_t thread_id = 0;
sigset_t signal_mask;
@ -400,6 +432,8 @@ void thread_loop(conf * configure)
void _main(int argc, char *argv[])
{
char *p = NULL;
int longindex = 0;
int opt;
char path[CACHE_SIZE] = { 0 };
char executable_filename[CACHE_SIZE] = { 0 };
@ -411,18 +445,9 @@ void _main(int argc, char *argv[])
memset(configure, 0, sizeof(struct CONF));
read_conf(inifile, configure);
sslEncodeCode = 0; // 默认SSL不转码
if (configure->sslencoding > 0) // 如果配置文件有sslencoding值,优先使用配置文件读取的值
sslEncodeCode = configure->sslencoding;
timeout_minute = 0; // 默认不超时
if (configure->timeout > 0) // 如果配置文件有值,优先使用配置文件读取的值
timeout_minute = configure->timeout;
process = 2; // 默认开启2个进程
if (configure->process > 0) // 如果配置文件有值,优先使用配置文件读取的值
process = configure->process;
int longindex = 0;
char optstring[] = ":l:f:t:p:c:e:s:h?";
static struct option longopts[] = {
{ "local_address", required_argument, 0, 'l' },
@ -436,7 +461,6 @@ void _main(int argc, char *argv[])
{ "?", no_argument, 0, '?' },
{ 0, 0, 0, 0 }
};
char *p = NULL;
while (-1 != (opt = getopt_long(argc, argv, optstring, longopts, &longindex))) {
switch (opt) {
case 'l':
@ -496,9 +520,51 @@ void _main(int argc, char *argv[])
;
}
}
#if DAEMON
// 守护进程
int nochdir = 0;
int noclose = 0;
int pid;
if ((pid = fork()) < 0) {
return ;
} else if (0 != pid) {
free_tcp(&http_head_strrep);
free_tcp(&http_head_regrep);
free_tcp(&https_head_strrep);
free_tcp(&https_head_regrep);
free_conf(configure);
free(configure);
exit(0);
}
if (setsid() < 0) {
return ;
}
signal(SIGHUP, SIG_IGN);
if ((pid = fork()) < 0) {
return ;
} else if (0 != pid) {
free_tcp(&http_head_strrep);
free_tcp(&http_head_regrep);
free_tcp(&https_head_strrep);
free_tcp(&https_head_regrep);
free_conf(configure);
free(configure);
exit(0);
}
if (0 == nochdir) {
chdir("/");
}
if (0 == noclose) {
freopen("/dev/null", "r", stdin);
freopen("/dev/null", "w", stdout);
freopen("/dev/null", "w", stderr);
}
#endif
server_ini(); // 守护进程
// 反转链表,使读取的配置正序
http_head_strrep = local_reverse(http_head_strrep);
http_head_regrep = local_reverse(http_head_regrep);

2
main.h
View File

@ -26,8 +26,10 @@
#define MAX_CONNECTION 1020
#define BUFFER_SIZE 8192
#define CACHE_SIZE 270
#define BUILD(fmt...) do { fprintf(stderr,"%s %s ",__DATE__,__TIME__); fprintf(stderr, ##fmt); } while(0)
#define ERRDEBUG fprintf(stderr,"Error Occured at File: %s, Function: %s, Line: %d, Date: %s, Time: %s.\n", __FILE__, __FUNCTION__, __LINE__, __DATE__, __TIME__);
#define DAEMON 1
struct global {
int tcp_listen_fd, dns_listen_fd, udp_listen_fd, uid, procs, timeout_m;