Support the same configuration name in the configuration file (still under test).

This commit is contained in:
aixiao 2022-01-02 18:36:09 +08:00
parent 40df4da8b8
commit cb68da2b3c
14 changed files with 252 additions and 224 deletions

BIN
CProxy Executable file

Binary file not shown.

View File

@ -9,21 +9,25 @@ global {
} }
http { http {
http_ip="47.240.75.93"; http_ip="2001:19f0:4401:2f:5400:3ff:fec4:e376";
http_port=127; http_port=127;
http_del="Host"; http_del="Host";
http_first="[M] http://[host][U] [V]\r\nHost: [H]\r\n"; http_first="[M] http://[host][U] [V]\r\nHost: [H]\r\n";
//strrep="Windows NT 10.0->Linux"; strrep="Windows NT 10.0" -> "Linux";
//regrep="Host:*.+?->Host: [host]:80"; strrep="XiaoMi MIX 2S" -> "Linux";
regrep="Host:*.+?" -> "Host: [host]:80";
regrep="Host:*.+?" -> "host: [host]:80";
} }
https { https {
https_ip="47.240.75.93"; https_ip="2001:19f0:4401:2f:5400:3ff:fec4:e376";
https_port=127; https_port=127;
https_del="Host,host,x-online-host"; https_del="Host,host,x-online-host";
https_first="[M] [U] [V]\r\nHost: [host]\r\n"; https_first="[M] [U] [V]\r\nHost: [host]\r\n";
//strrep="Windows NT 10.0->Linux"; strrep = "Windows NT 10.0" -> "Linux1";
//regrep="Host*.+?->host: [host]:443"; strrep = "XiaoMi MIX 2S" -> "Linux2";
regrep = "Host*.+?" -> "host: [host]:443";
regrep = "Host*.+?" -> "Host: [host]:443";
} }
httpdns { httpdns {

View File

@ -4,8 +4,8 @@
可以修改HTTP协议消息头(request). 可以修改HTTP协议消息头(request).
可以修改HTTP协议CONNECT方法消息头. 可以修改HTTP协议CONNECT方法消息头.
可以修改HTTP协议GET方法消息头. 可以修改HTTP协议GET方法消息头.
httptcp支持IPV4/IPV6. HttpTCP支持IPV4/IPV6.
支持httpdns、httpudp代理 支持HttpDNS、HttpUDP代理
## Build ## Build

249
conf.c
View File

@ -219,6 +219,8 @@ static void parse_http_module(char *content, conf * p)
{ {
char *var, *val_begin, *val_end, *lineEnd; char *var, *val_begin, *val_end, *lineEnd;
int val_begin_len; int val_begin_len;
tcp *http_node = NULL;
char *p1 = NULL, *s = NULL, *t = NULL;
while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) { while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) {
if (strcasecmp(var, "http_ip") == 0) { if (strcasecmp(var, "http_ip") == 0) {
@ -241,53 +243,59 @@ static void parse_http_module(char *content, conf * p)
memcpy(p->http_first, val_begin, val_begin_len); memcpy(p->http_first, val_begin, val_begin_len);
p->http_first_len = val_begin_len; p->http_first_len = val_begin_len;
} else if (strcasecmp(var, "strrep") == 0) { } else if (strcasecmp(var, "strrep") == 0) {
val_begin_len = strlen(val_begin) + 1; http_node = (tcp *)malloc(sizeof(struct tcp));
if (http_node == NULL)
return ;
memset(http_node, 0, sizeof(struct tcp));
http_node->strrep = strdup(val_begin);
p->http_strrep = (char *)malloc(val_begin_len); p1 = strstr(val_begin, "->");
if (p->http_strrep == NULL) for (t = p1; *t != '"'; ++t) ;
free(p->http_strrep); http_node->strrep_t = strdup(t + 1);
memset(p->http_strrep, 0, val_begin_len);
memcpy(p->http_strrep, val_begin, val_begin_len);
char *p1 = strstr(val_begin, "->"); for (s = p1 - 1; *s == ' '; s--) {
p->http_strrep_aim = (char *)malloc(val_begin_len - strlen(p1 + 2) - 2 + 1); if (s == val_begin)
if (p->http_strrep_aim == NULL) { return;
free(p->http_strrep_aim);
} }
memset(p->http_strrep_aim, 0, val_begin_len - strlen(p1 + 2) - 2 + 1); if (*s == '"')
strncpy_(p->http_strrep_aim, val_begin, val_begin_len - strlen(p1 + 2) - 3); // 实际 val_begin_len 多1 s--;
p->http_strrep_obj = (char *)malloc(strlen(p1 + 2) + 1);
if (p->http_strrep_obj == NULL) { http_node->strrep_s = strndup(val_begin, s - val_begin + 1);
free(p->http_strrep_obj);
http_node->next = NULL;
if (http_head == NULL) {
http_head = http_node;
} else {
http_node->next = http_head;
http_head = http_node;
} }
memset(p->http_strrep_obj, 0, strlen(p1 + 2) + 1);
strncpy_(p->http_strrep_obj, p1 + 2, strlen(p1 + 2));
p->http_strrep_aim_len = strlen(p->http_strrep_aim);
p->http_strrep_obj_len = strlen(p->http_strrep_obj);
} else if (strcasecmp(var, "regrep") == 0) { } else if (strcasecmp(var, "regrep") == 0) {
val_begin_len = strlen(val_begin) + 1; http_node = (tcp *) malloc(sizeof(struct tcp));
if (http_node == NULL)
return ;
memset(http_node, 0, sizeof(struct tcp));
http_node->regrep = strdup(val_begin);
p->http_regrep = (char *)malloc(val_begin_len); p1 = strstr(val_begin, "->");
if (p->http_regrep == NULL) for (t = p1; *t != '"'; ++t) ;
free(p->http_regrep); http_node->regrep_t = strdup(t + 1);
memset(p->http_regrep, 0, val_begin_len);
memcpy(p->http_regrep, val_begin, val_begin_len);
char *p1 = strstr(val_begin, "->"); for (s = p1 - 1; *s == ' '; s--) {
p->http_regrep_aim = (char *)malloc(val_begin_len - strlen(p1 + 2) - 2 + 1); if (s == val_begin)
if (p->http_regrep_aim == NULL) { return ;
free(p->http_regrep_aim);
} }
memset(p->http_regrep_aim, 0, val_begin_len - strlen(p1 + 2) - 2 + 1); if (*s == '"')
strncpy_(p->http_regrep_aim, val_begin, val_begin_len - strlen(p1 + 2) - 3); s--;
p->http_regrep_obj = (char *)malloc(strlen(p1 + 2) + 1);
if (p->http_regrep_obj == NULL) { http_node->regrep_s = strndup(val_begin, s - val_begin + 1);
free(p->http_regrep_obj);
http_node->next = NULL;
if (http_head == NULL) {
http_head = http_node;
} else {
http_node->next = http_head;
http_head = http_node;
} }
memset(p->http_regrep_obj, 0, strlen(p1 + 2) + 1);
strncpy_(p->http_regrep_obj, p1 + 2, strlen(p1 + 2));
p->http_regrep_aim_len = strlen(p->http_regrep_aim);
p->http_regrep_obj_len = strlen(p->http_regrep_obj);
} }
content = strchr(lineEnd + 1, '\n'); content = strchr(lineEnd + 1, '\n');
@ -298,6 +306,8 @@ static void parse_https_module(char *content, conf * p)
{ {
char *var, *val_begin, *val_end, *lineEnd; char *var, *val_begin, *val_end, *lineEnd;
int val_begin_len; int val_begin_len;
tcp *https_node = NULL;
char *p1 = NULL, *s = NULL, *t = NULL;
while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) { while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) {
if (strcasecmp(var, "https_ip") == 0) { if (strcasecmp(var, "https_ip") == 0) {
@ -320,57 +330,118 @@ static void parse_https_module(char *content, conf * p)
memcpy(p->https_first, val_begin, val_begin_len); memcpy(p->https_first, val_begin, val_begin_len);
p->https_first_len = val_begin_len; p->https_first_len = val_begin_len;
} else if (strcasecmp(var, "strrep") == 0) { } else if (strcasecmp(var, "strrep") == 0) {
val_begin_len = strlen(val_begin) + 1; // 链表操作,支持多个相同配置KEY
https_node = (tcp *)malloc(sizeof(struct tcp));
if (https_node == NULL)
return ;
memset(https_node, 0, sizeof(struct tcp));
https_node->strrep = strdup(val_begin);
p->https_strrep = (char *)malloc(val_begin_len); p1 = strstr(val_begin, "->");
if (p->https_strrep == NULL) for (t = p1; *t != '"'; ++t) ;
free(p->https_strrep); https_node->strrep_t = strdup(t + 1);
memset(p->https_strrep, 0, val_begin_len);
memcpy(p->https_strrep, val_begin, val_begin_len);
char *p1 = strstr(val_begin, "->"); for (s = p1 - 1; *s == ' '; s--) {
p->https_strrep_aim = (char *)malloc(val_begin_len - strlen(p1 + 2) - 2 + 1); if (s == val_begin)
if (p->https_strrep_aim == NULL) { return;
free(p->https_strrep_aim);
} }
memset(p->https_strrep_aim, 0, val_begin_len - strlen(p1 + 2) - 2 + 1); if (*s == '"')
strncpy_(p->https_strrep_aim, val_begin, val_begin_len - strlen(p1 + 2) - 3); s--;
p->https_strrep_obj = (char *)malloc(strlen(p1 + 2) + 1);
if (p->https_strrep_obj == NULL) { https_node->strrep_s = strndup(val_begin, s - val_begin + 1);
free(p->https_strrep_obj);
https_node->next = NULL;
if (https_head == NULL) {
https_head = https_node;
} else {
https_node->next = https_head;
https_head = https_node;
} }
memset(p->https_strrep_obj, 0, strlen(p1 + 2) + 1);
strncpy_(p->https_strrep_obj, p1 + 2, strlen(p1 + 2));
p->https_strrep_aim_len = strlen(p->https_strrep_aim);
p->https_strrep_obj_len = strlen(p->https_strrep_obj);
} else if (strcasecmp(var, "regrep") == 0) { } else if (strcasecmp(var, "regrep") == 0) {
val_begin_len = strlen(val_begin) + 1; https_node = (tcp *) malloc(sizeof(struct tcp));
if (https_node == NULL)
return ;
memset(https_node, 0, sizeof(struct tcp));
https_node->regrep = strdup(val_begin);
p->https_regrep = (char *)malloc(val_begin_len); p1 = strstr(val_begin, "->");
if (p->https_regrep == NULL) for (t = p1; *t != '"'; ++t) ;
free(p->https_regrep); https_node->regrep_t = strdup(t + 1);
memset(p->https_regrep, 0, val_begin_len);
memcpy(p->https_regrep, val_begin, val_begin_len);
char *p1 = strstr(val_begin, "->"); for (s = p1 - 1; *s == ' '; s--) {
p->https_regrep_aim = (char *)malloc(val_begin_len - strlen(p1 + 2) - 2 + 1); if (s == val_begin)
if (p->https_regrep_aim == NULL) return ;
free(p->https_regrep_aim); }
memset(p->https_regrep_aim, 0, val_begin_len - strlen(p1 + 2) - 2 + 1); if (*s == '"')
strncpy_(p->https_regrep_aim, val_begin, val_begin_len - strlen(p1 + 2) - 3); s--;
p->https_regrep_obj = (char *)malloc(strlen(p1 + 2) + 1);
if (p->https_regrep_obj == NULL) https_node->regrep_s = strndup(val_begin, s - val_begin + 1);
free(p->https_regrep_obj);
memset(p->https_regrep_obj, 0, strlen(p1 + 2) + 1); https_node->next = NULL;
strncpy_(p->https_regrep_obj, p1 + 2, strlen(p1 + 2)); if (https_head == NULL) {
p->https_regrep_aim_len = strlen(p->https_regrep_aim); https_head = https_node;
p->https_regrep_obj_len = strlen(p->https_regrep_obj); } else {
https_node->next = https_head;
https_head = https_node;
}
} }
content = strchr(lineEnd + 1, '\n'); content = strchr(lineEnd + 1, '\n');
} }
} }
// 打印链表
void print_tcp(tcp * p)
{
tcp *temp = p;
while (temp) {
if (temp->strrep)
;//printf("%s\n", temp->strrep);
if (temp->strrep_s)
printf("%s\n", temp->strrep_s);
if (temp->strrep_t)
printf("%s\n", temp->strrep_t);
if (temp->regrep)
;//printf("%s\n", temp->regrep);
if (temp->regrep_s)
printf("%s\n", temp->regrep_s);
if (temp->regrep_t)
printf("%s\n", temp->regrep_t);
temp = temp->next;
}
}
// free链表
void free_tcp(tcp * p)
{
while (p) {
tcp *temp = p;
if (temp->strrep)
free(temp->strrep);
if (temp->strrep_s)
free(temp->strrep_s);
if (temp->strrep_t)
free(temp->strrep_t);
if (temp->regrep)
free(temp->regrep);
if (temp->regrep_s)
free(temp->regrep_s);
if (temp->regrep_t)
free(temp->regrep_t);
if (temp)
free(temp);
p = p->next;
}
free(p);
}
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; char *var, *val_begin, *val_end, *lineEnd;
@ -383,7 +454,6 @@ static void parse_httpdns_module(char *content, conf * p)
memset(p->addr, 0, val_begin_len); memset(p->addr, 0, val_begin_len);
memcpy(p->addr, val_begin, val_begin_len); memcpy(p->addr, val_begin, val_begin_len);
} else if (strcasecmp(var, "http_req") == 0) { } else if (strcasecmp(var, "http_req") == 0) {
//val_begin_len = strlen(val_begin) + 1;
val_begin_len = val_end - val_begin; val_begin_len = val_end - val_begin;
p->http_req = (char *)malloc(val_begin_len + 1); p->http_req = (char *)malloc(val_begin_len + 1);
memset(p->http_req, 0, val_begin_len); memset(p->http_req, 0, val_begin_len);
@ -409,7 +479,6 @@ static void parse_httpudp_module(char *content, conf * p)
memset(p->httpudp_addr, 0, val_begin_len); memset(p->httpudp_addr, 0, val_begin_len);
memcpy(p->httpudp_addr, val_begin, val_begin_len); memcpy(p->httpudp_addr, val_begin, val_begin_len);
} else if (strcasecmp(var, "http_req") == 0) { } else if (strcasecmp(var, "http_req") == 0) {
//val_begin_len = strlen(val_begin) + 1;
val_begin_len = val_end - val_begin; val_begin_len = val_end - val_begin;
p->httpudp_http_req = (char *)malloc(val_begin_len + 1); p->httpudp_http_req = (char *)malloc(val_begin_len + 1);
memset(p->httpudp_http_req, 0, val_begin_len); memset(p->httpudp_http_req, 0, val_begin_len);
@ -432,18 +501,6 @@ void free_conf(conf * p)
free(p->http_del); free(p->http_del);
if (p->http_first) if (p->http_first)
free(p->http_first); free(p->http_first);
if (p->http_strrep)
free(p->http_strrep);
if (p->http_strrep_aim)
free(p->http_strrep_aim);
if (p->http_strrep_obj)
free(p->http_strrep_obj);
if (p->http_regrep)
free(p->http_regrep);
if (p->http_regrep_aim)
free(p->http_regrep_aim);
if (p->http_regrep_obj)
free(p->http_regrep_obj);
// https module // https module
if (p->https_ip) if (p->https_ip)
@ -452,18 +509,6 @@ void free_conf(conf * p)
free(p->https_del); free(p->https_del);
if (p->https_first) if (p->https_first)
free(p->https_first); free(p->https_first);
if (p->https_strrep)
free(p->https_strrep);
if (p->https_strrep_aim)
free(p->https_strrep_aim);
if (p->https_strrep_obj)
free(p->https_strrep_obj);
if (p->https_regrep)
free(p->https_regrep);
if (p->https_regrep_aim)
free(p->https_regrep_aim);
if (p->https_regrep_obj)
free(p->https_regrep_obj);
// httpdns module // httpdns module
if (p->addr) if (p->addr)

38
conf.h
View File

@ -24,27 +24,11 @@ typedef struct CONF {
int http_port; int http_port;
char *http_ip, *http_del, *http_first; char *http_ip, *http_del, *http_first;
int http_ip_len, http_del_len, http_first_len; int http_ip_len, http_del_len, http_first_len;
char *http_strrep, *http_regrep;
int http_strrep_len, http_regrep_len;
char *http_strrep_aim, *http_strrep_obj;
int http_strrep_aim_len, http_strrep_obj_len;
char *http_regrep_aim, *http_regrep_obj;
int http_regrep_aim_len, http_regrep_obj_len;
// https module // https module
int https_port; int https_port;
char *https_ip, *https_del, *https_first; char *https_ip, *https_del, *https_first;
int https_ip_len, https_del_len, https_first_len; int https_ip_len, https_del_len, https_first_len;
char *https_strrep, *https_regrep;
int https_strrep_len, https_regrep_len;
char *https_strrep_aim, *https_strrep_obj;
int https_strrep_aim_len, https_strrep_obj_len;
char *https_regrep_aim, *https_regrep_obj;
int https_regrep_aim_len, https_regrep_obj_len;
// httpdns module // httpdns module
char *addr; char *addr;
@ -59,8 +43,26 @@ typedef struct CONF {
int httpudp_encode; int httpudp_encode;
} conf; } conf;
typedef struct tcp {
char *strrep;
char *strrep_s, *strrep_t;
char *regrep;
char *regrep_s, *regrep_t;
struct tcp *next;
} tcp;
extern tcp *http_head;
extern tcp *http_node;
extern tcp *https_head;
extern tcp *https_node;
extern void print_tcp(tcp *p);
extern void free_tcp(tcp *p);
char *strncpy_(char *dest, const char *src, size_t n); char *strncpy_(char *dest, const char *src, size_t n);
void read_conf(char *file, conf * p); void read_conf(char *file, conf *p);
void free_conf(conf * p); void free_conf(conf *p);
#endif #endif

BIN
conf.o Executable file

Binary file not shown.

BIN
help.o Executable file

Binary file not shown.

BIN
http_proxy.o Executable file

Binary file not shown.

View File

@ -544,10 +544,12 @@ char *request_head(conn_t * in, conf * configure)
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[host]", 6, http_request->host, http_request->host_len); incomplete_head = replace(incomplete_head, &incomplete_head_len, "[host]", 6, http_request->host, http_request->host_len);
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, "[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); incomplete_head = replace(incomplete_head, &incomplete_head_len, "[H]", 3, http_request->H, http_request->H_len);
/*
if (configure->https_strrep) if (configure->https_strrep)
incomplete_head = replace(incomplete_head, &incomplete_head_len, configure->https_strrep_aim, configure->https_strrep_aim_len, configure->https_strrep_obj, configure->https_strrep_obj_len); incomplete_head = replace(incomplete_head, &incomplete_head_len, configure->https_strrep_aim, configure->https_strrep_aim_len, configure->https_strrep_obj, configure->https_strrep_obj_len);
if (configure->https_regrep) if (configure->https_regrep)
incomplete_head = regrep(incomplete_head, &incomplete_head_len, configure->https_regrep_aim, configure->https_regrep_obj, configure->https_regrep_obj_len); incomplete_head = regrep(incomplete_head, &incomplete_head_len, configure->https_regrep_aim, configure->https_regrep_obj, configure->https_regrep_obj_len);
*/
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[H]", 3, http_request->H, http_request->H_len); incomplete_head = replace(incomplete_head, &incomplete_head_len, "[H]", 3, http_request->H, http_request->H_len);
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[host]", 6, http_request->host, http_request->host_len); incomplete_head = replace(incomplete_head, &incomplete_head_len, "[host]", 6, http_request->host, http_request->host_len);
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, "[port]", 6, http_request->port, http_request->port_len);
@ -607,10 +609,12 @@ char *request_head(conn_t * in, conf * configure)
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[host]", 6, http_request->host, http_request->host_len); incomplete_head = replace(incomplete_head, &incomplete_head_len, "[host]", 6, http_request->host, http_request->host_len);
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, "[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); incomplete_head = replace(incomplete_head, &incomplete_head_len, "[H]", 3, http_request->H, http_request->H_len);
/*
if (configure->http_strrep) if (configure->http_strrep)
incomplete_head = replace(incomplete_head, &incomplete_head_len, configure->http_strrep_aim, configure->http_strrep_aim_len, configure->http_strrep_obj, configure->http_strrep_obj_len); incomplete_head = replace(incomplete_head, &incomplete_head_len, configure->http_strrep_aim, configure->http_strrep_aim_len, configure->http_strrep_obj, configure->http_strrep_obj_len);
if (configure->http_regrep) if (configure->http_regrep)
incomplete_head = regrep(incomplete_head, &incomplete_head_len, configure->http_regrep_aim, configure->http_regrep_obj, configure->http_regrep_obj_len); incomplete_head = regrep(incomplete_head, &incomplete_head_len, configure->http_regrep_aim, configure->http_regrep_obj, configure->http_regrep_obj_len);
*/
incomplete_head = replace(incomplete_head, &incomplete_head_len, "[host]", 6, http_request->host, http_request->host_len); incomplete_head = replace(incomplete_head, &incomplete_head_len, "[host]", 6, http_request->host, http_request->host_len);
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, "[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); incomplete_head = replace(incomplete_head, &incomplete_head_len, "[H]", 3, http_request->H, http_request->H_len);

BIN
http_request.o Executable file

Binary file not shown.

BIN
httpdns.o Executable file

Binary file not shown.

BIN
httpudp.o Executable file

Binary file not shown.

117
main.c
View File

@ -6,11 +6,10 @@
#include "conf.h" #include "conf.h"
#include "help.h" #include "help.h"
#define SERVER_STOP 1 #define SERVER_TYPE_STOP 1
#define SERVER_RELOAD 2 #define SERVER_TYPE_RELOAD 2
#define SERVER_STATUS 3 #define SERVER_TYPE_STATUS 3
#define SERVICE_TYPE_STATUS_NOT_PRINT 4
struct global global; struct global global;
struct tcp_mode http, https; struct tcp_mode http, https;
@ -19,44 +18,14 @@ char *default_ssl_request;
int default_ssl_request_len; int default_ssl_request_len;
uint16_t tcp_listen_port; uint16_t tcp_listen_port;
tcp *http_head = NULL;
tcp *https_head = NULL;
struct epoll_event ev, events[MAX_CONNECTION + 1]; struct epoll_event ev, events[MAX_CONNECTION + 1];
int epollfd, server_sock, server_sock6, local_port, process; int epollfd, server_sock, server_sock6, local_port, process;
conn_t cts[MAX_CONNECTION]; conn_t cts[MAX_CONNECTION];
char local_host[CACHE_SIZE]; char local_host[CACHE_SIZE];
/*
int create_connection(char *remote_host, int remote_port)
{
struct sockaddr_in server_addr;
struct hostent *server;
int sock = -1;
server = NULL;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
return -1;
}
if ((server = gethostbyname(remote_host)) == NULL) {
perror("gethostbyname");
errno = EFAULT;
return -1;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
memmove(&server_addr.sin_addr.s_addr, server->h_addr, server->h_length);
server_addr.sin_port = htons(remote_port);
//server_addr.sin_addr.s_addr = inet_addr(remote_host);
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("connect");
close(sock);
return -1;
}
fcntl(sock, F_SETFL, O_NONBLOCK);
return sock;
}
*/
int create_server_socket(int port) int create_server_socket(int port)
{ {
@ -257,17 +226,24 @@ int process_signal(int signal, char *process_name)
char bufer[PATH_SIZE]; char bufer[PATH_SIZE];
char comm[PATH_SIZE]; char comm[PATH_SIZE];
char proc_comm_name[PATH_SIZE]; char proc_comm_name[PATH_SIZE];
int number[PATH_SIZE] = { 0 }; //int number[PATH_SIZE] = { 0 };
int n = 0; //int n = 0;
FILE *fp; FILE *fp;
DIR *dir; DIR *dir;
struct dirent *ptr; struct dirent *ptr;
pid_t self_pid;
self_pid = getpid();
dir = opendir("/proc"); dir = opendir("/proc");
bzero(bufer, 0); bzero(bufer, 0);
bzero(comm, 0); bzero(comm, 0);
bzero(proc_comm_name, 0); bzero(proc_comm_name, 0);
while ((ptr = readdir(dir)) != NULL) { while ((ptr = readdir(dir)) != NULL) {
if (ptr->d_type == DT_DIR && strcasecmp(ptr->d_name, ".") && strcasecmp(ptr->d_name, "..")) { if (ptr->d_type != DT_DIR)
continue;
if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0 || atoi(ptr->d_name) == self_pid)
continue;
sprintf(comm, "/proc/%s/comm", ptr->d_name); sprintf(comm, "/proc/%s/comm", ptr->d_name);
if (access(comm, F_OK) == 0) { if (access(comm, F_OK) == 0) {
fp = fopen(comm, "r"); fp = fopen(comm, "r");
@ -277,30 +253,24 @@ int process_signal(int signal, char *process_name)
} }
sscanf(bufer, "%s", proc_comm_name); sscanf(bufer, "%s", proc_comm_name);
if (!strcmp(process_name, proc_comm_name)) { if (!strcmp(process_name, proc_comm_name)) {
number[n] = atoi(ptr->d_name); if (signal == SERVER_TYPE_STOP)
n += 1; kill(atoi(ptr->d_name), SIGTERM);
else {
closedir(dir);
if (signal != SERVICE_TYPE_STATUS_NOT_PRINT)
printf("\t%d\n", atoi(ptr->d_name));;
return 0;
}
} }
fclose(fp); fclose(fp);
} }
} }
}
closedir(dir); closedir(dir);
if (signal == SERVER_STATUS) { // 状态 if (signal == SERVER_TYPE_STATUS)
n -= 2; // 去除最后一个搜索时的本身进程和最后加一后未使用的 ;
for (; n >= 0; n--) { // 依据数组从大到小的下标打印PID else if (signal == SERVICE_TYPE_STATUS_NOT_PRINT)
printf("\t%d\n", number[n]); return 1;
}
}
if (signal == SERVER_STOP || signal == SERVER_RELOAD) { // 关闭
n -= 2;
for (; n >= 0; n--) {
//kill(number[n], SIGTERM);
kill(number[n], SIGKILL);
}
}
return 0; return 0;
} }
@ -439,8 +409,6 @@ void thread_loop(conf * configure)
if (pthread_sigmask(SIG_BLOCK, &signal_mask, NULL) != 0) { if (pthread_sigmask(SIG_BLOCK, &signal_mask, NULL) != 0) {
printf("block sigpipe error\n"); printf("block sigpipe error\n");
} }
//if (timeout_minute)
// pthread_create(&thread_id, NULL, &tcp_timeout_check, NULL);
if (global.timeout_m) if (global.timeout_m)
pthread_create(&thread_id, NULL, &timeout_check, NULL); pthread_create(&thread_id, NULL, &timeout_check, NULL);
@ -462,7 +430,6 @@ void thread_loop(conf * configure)
pthread_join(thread_id, NULL); pthread_join(thread_id, NULL);
pthread_exit(NULL); pthread_exit(NULL);
/* 线程分离 /* 线程分离
pthread_attr_t attr; pthread_attr_t attr;
pthread_attr_init(&attr); pthread_attr_init(&attr);
@ -474,6 +441,8 @@ void thread_loop(conf * configure)
*/ */
} }
void _main(int argc, char *argv[]) void _main(int argc, char *argv[])
{ {
int opt; int opt;
@ -487,6 +456,12 @@ void _main(int argc, char *argv[])
memset(configure, 0, sizeof(struct CONF)); memset(configure, 0, sizeof(struct CONF));
read_conf(inifile, configure); read_conf(inifile, configure);
print_tcp(https_head);
free_tcp(https_head);
print_tcp(http_head);
free_tcp(http_head);
//exit(0);
sslEncodeCode = 0; // 默认SSL不转码 sslEncodeCode = 0; // 默认SSL不转码
if (configure->sslencoding > 0) // 如果配置文件有sslencoding值,优先使用配置文件读取的值 if (configure->sslencoding > 0) // 如果配置文件有sslencoding值,优先使用配置文件读取的值
sslEncodeCode = configure->sslencoding; sslEncodeCode = configure->sslencoding;
@ -512,8 +487,6 @@ void _main(int argc, char *argv[])
{ 0, 0, 0, 0 } { 0, 0, 0, 0 }
}; };
char *p = NULL; char *p = NULL;
//char optstring[] = ":l:f:t:p:c:e:s:h?";
//while (-1 != (opt = getopt(argc, argv, optstring))) {
while (-1 != (opt = getopt_long(argc, argv, optstring, longopts, &longindex))) { while (-1 != (opt = getopt_long(argc, argv, optstring, longopts, &longindex))) {
switch (opt) { switch (opt) {
case 'l': case 'l':
@ -551,13 +524,14 @@ void _main(int argc, char *argv[])
case 's': case 's':
if (strcasecmp(optarg, "stop") == 0 || strcasecmp(optarg, "quit") == 0) { if (strcasecmp(optarg, "stop") == 0 || strcasecmp(optarg, "quit") == 0) {
free_conf(configure); free_conf(configure);
exit(process_signal(SERVER_STOP, executable_filename)); exit(process_signal(SERVER_TYPE_STOP, executable_filename));
} }
if (strcasecmp(optarg, "restart") == 0 || strcasecmp(optarg, "reload") == 0) { if (strcasecmp(optarg, "restart") == 0 || strcasecmp(optarg, "reload") == 0) {
process_signal(SERVER_RELOAD, executable_filename); process_signal(SERVER_TYPE_STOP, executable_filename);
while (process_signal(SERVICE_TYPE_STATUS_NOT_PRINT, executable_filename) == 0);
} }
if (strcasecmp(optarg, "status") == 0) if (strcasecmp(optarg, "status") == 0)
exit(process_signal(SERVER_STATUS, executable_filename)); exit(process_signal(SERVER_TYPE_STATUS, executable_filename));
break; break;
case 'h': case 'h':
case '?': case '?':
@ -570,14 +544,13 @@ void _main(int argc, char *argv[])
} }
server_ini(); // 守护进程 server_ini(); // 守护进程
// 设置每个进程允许打开的最大文件数 rt.rlim_max = rt.rlim_cur = MAX_CONNECTION * 2; // 设置每个进程允许打开的最大文件数
rt.rlim_max = rt.rlim_cur = MAX_CONNECTION * 2; if (setrlimit(RLIMIT_NOFILE, &rt) == -1)
if (setrlimit(RLIMIT_NOFILE, &rt) == -1) {
perror("setrlimit"); perror("setrlimit");
}
initialize(configure); initialize(configure);
if (setegid(configure->uid) == -1 || seteuid(configure->uid) == -1) // 设置uid if (setegid(configure->uid) == -1 || seteuid(configure->uid) == -1) // 设置uid
exit(1); exit(1);

BIN
main.o Executable file

Binary file not shown.