Support restart, status signal

This commit is contained in:
aixiao 2019-12-16 19:27:36 +08:00
parent 7049b5462b
commit 6f7a666bf3
11 changed files with 328 additions and 206 deletions

View File

@ -20,7 +20,7 @@
Options: Options:
-?,-h : help information -?,-h : help information
-d : daemon -d : daemon
-s signal : send signal to a master process: stop -s signal : send signal to a master process: stop, resatrt, reload, status
-c filename : set configuration file (default: conf/cproxy.ini) -c filename : set configuration file (default: conf/cproxy.ini)
May 7 2019 18:48:10 Compile、link. Dec 9 2019 13:36:43 Compile、link.

117
conf.c
View File

@ -1,8 +1,8 @@
#include "conf.h" #include "conf.h"
void read_conf(char *file, conf *p) void read_conf(char *file, conf * p)
{ {
if(access(file, F_OK)) { if (access(file, F_OK)) {
printf("%s DOESN'T EXISIT!\n", file); printf("%s DOESN'T EXISIT!\n", file);
exit(1); exit(1);
} }
@ -15,13 +15,16 @@ void read_conf(char *file, conf *p)
//local_port //local_port
p->server_port = iniparser_getint(ini, "server:local_port", 0); p->server_port = iniparser_getint(ini, "server:local_port", 0);
//pid_file //pid_file
p->len_server_pid_file = strlen(iniparser_getstring(ini, "server:pid_file", NULL)) + 1; p->len_server_pid_file =
strlen(iniparser_getstring(ini, "server:pid_file", NULL)) + 1;
p->server_pid_file = (char *)malloc(p->len_server_pid_file); p->server_pid_file = (char *)malloc(p->len_server_pid_file);
if (p->server_pid_file == NULL) { if (p->server_pid_file == NULL) {
goto err; goto err;
} }
memset(p->server_pid_file, 0, p->len_server_pid_file); memset(p->server_pid_file, 0, p->len_server_pid_file);
memcpy(p->server_pid_file, iniparser_getstring(ini, "server:pid_file", NULL), p->len_server_pid_file); memcpy(p->server_pid_file,
iniparser_getstring(ini, "server:pid_file", NULL),
p->len_server_pid_file);
//printf("%s\n", p->server_pid_file); //printf("%s\n", p->server_pid_file);
// http module // http module
@ -32,147 +35,170 @@ void read_conf(char *file, conf *p)
goto err; goto err;
} }
memset(p->http_ip, 0, p->len_http_ip); memset(p->http_ip, 0, p->len_http_ip);
memcpy(p->http_ip, iniparser_getstring(ini, "http:http_ip", NULL), p->len_http_ip); memcpy(p->http_ip, iniparser_getstring(ini, "http:http_ip", NULL),
p->len_http_ip);
// http port // http port
p->http_port = iniparser_getint(ini, "http:http_port", 0); p->http_port = iniparser_getint(ini, "http:http_port", 0);
// http del // http del
p->len_http_del = strlen(iniparser_getstring(ini, "http:http_del", NULL)) + 1; p->len_http_del =
strlen(iniparser_getstring(ini, "http:http_del", NULL)) + 1;
p->http_del = (char *)malloc(p->len_http_del); p->http_del = (char *)malloc(p->len_http_del);
if (p->http_del == NULL) { if (p->http_del == NULL) {
goto err; goto err;
} }
memset(p->http_del, 0, p->len_http_del); memset(p->http_del, 0, p->len_http_del);
memcpy(p->http_del, iniparser_getstring(ini, "http:http_del", NULL), p->len_http_del); memcpy(p->http_del, iniparser_getstring(ini, "http:http_del", NULL),
p->len_http_del);
// http first // http first
p->len_http_first = strlen(iniparser_getstring(ini, "http:http_first", NULL)) + 1; p->len_http_first =
strlen(iniparser_getstring(ini, "http:http_first", NULL)) + 1;
p->http_first = (char *)malloc(p->len_http_first); p->http_first = (char *)malloc(p->len_http_first);
if (p->http_first == NULL) { if (p->http_first == NULL) {
goto err; goto err;
} }
memset(p->http_first, 0, p->len_http_first); memset(p->http_first, 0, p->len_http_first);
memcpy(p->http_first, iniparser_getstring(ini, "http:http_first", NULL), p->len_http_first); memcpy(p->http_first, iniparser_getstring(ini, "http:http_first", NULL),
p->len_http_first);
// https module // https module
// https ip // https ip
p->len_https_ip = strlen(iniparser_getstring(ini, "https:https_ip", NULL)) + 1; p->len_https_ip =
strlen(iniparser_getstring(ini, "https:https_ip", NULL)) + 1;
p->https_ip = (char *)malloc(p->len_https_ip); p->https_ip = (char *)malloc(p->len_https_ip);
if (p->https_ip == NULL) { if (p->https_ip == NULL) {
goto err; goto err;
} }
memset(p->https_ip, 0, p->len_http_ip); memset(p->https_ip, 0, p->len_http_ip);
memcpy(p->https_ip, iniparser_getstring(ini, "https:https_ip", NULL), p->len_https_ip); memcpy(p->https_ip, iniparser_getstring(ini, "https:https_ip", NULL),
p->len_https_ip);
//https port //https port
p->https_port = iniparser_getint(ini, "https:https_port", 0); p->https_port = iniparser_getint(ini, "https:https_port", 0);
// https del // https del
p->len_https_del = strlen(iniparser_getstring(ini, "https:https_del", NULL)) + 1; p->len_https_del =
strlen(iniparser_getstring(ini, "https:https_del", NULL)) + 1;
p->https_del = (char *)malloc(p->len_https_del); p->https_del = (char *)malloc(p->len_https_del);
if (p->https_del == NULL) { if (p->https_del == NULL) {
goto err; goto err;
} }
memset(p->https_del, 0, p->len_https_del); memset(p->https_del, 0, p->len_https_del);
memcpy(p->https_del, iniparser_getstring(ini, "https:https_del", NULL), p->len_https_del); memcpy(p->https_del, iniparser_getstring(ini, "https:https_del", NULL),
p->len_https_del);
// https first // https first
p->len_https_first = strlen(iniparser_getstring(ini, "https:https_first", NULL)) + 1; p->len_https_first =
strlen(iniparser_getstring(ini, "https:https_first", NULL)) + 1;
p->https_first = (char *)malloc(p->len_https_first); p->https_first = (char *)malloc(p->len_https_first);
if (p->https_first == NULL) { if (p->https_first == NULL) {
goto err; goto err;
} }
memset(p->https_first, 0, p->len_https_first); memset(p->https_first, 0, p->len_https_first);
memcpy(p->https_first, iniparser_getstring(ini, "https:https_first", NULL), p->len_https_first); memcpy(p->https_first, iniparser_getstring(ini, "https:https_first", NULL),
p->len_https_first);
// http strrep // http strrep
if (iniparser_find_entry(ini, "http:strrep") == 1) { if (iniparser_find_entry(ini, "http:strrep") == 1) {
p->len_http_strrep = strlen(iniparser_getstring(ini, "http:strrep", NULL)) + 1; p->len_http_strrep =
strlen(iniparser_getstring(ini, "http:strrep", NULL)) + 1;
p->http_strrep = (char *)malloc(p->len_http_strrep); p->http_strrep = (char *)malloc(p->len_http_strrep);
if (p->http_strrep == NULL) { if (p->http_strrep == NULL) {
free(p->http_strrep); free(p->http_strrep);
} }
memset(p->http_strrep, 0, p->len_http_strrep); memset(p->http_strrep, 0, p->len_http_strrep);
memcpy(p->http_strrep, iniparser_getstring(ini, "http:strrep", NULL), p->len_http_strrep); memcpy(p->http_strrep, iniparser_getstring(ini, "http:strrep", NULL),
p->len_http_strrep);
char *p1 = strstr(p->http_strrep, "->"); char *p1 = strstr(p->http_strrep, "->");
p->http_strrep_aim = (char *)malloc(strlen(p->http_strrep) - strlen(p1+2) -2 + 1); p->http_strrep_aim =
(char *)malloc(strlen(p->http_strrep) - strlen(p1 + 2) - 2 + 1);
if (p->http_strrep_aim == NULL) { if (p->http_strrep_aim == NULL) {
free(p->http_strrep_aim); free(p->http_strrep_aim);
} }
strncpy_(p->http_strrep_aim, p->http_strrep, strlen(p->http_strrep) - strlen(p1+2) - 2); strncpy_(p->http_strrep_aim, p->http_strrep,
p->http_strrep_obj = (char *)malloc(strlen(p1+2) + 1); strlen(p->http_strrep) - strlen(p1 + 2) - 2);
p->http_strrep_obj = (char *)malloc(strlen(p1 + 2) + 1);
if (p->http_strrep_obj == NULL) { if (p->http_strrep_obj == NULL) {
free(p->http_strrep_obj); free(p->http_strrep_obj);
} }
strncpy_(p->http_strrep_obj, p1+2, strlen(p1+2)); strncpy_(p->http_strrep_obj, p1 + 2, strlen(p1 + 2));
} }
// https strrep // https strrep
if (iniparser_find_entry(ini, "https:strrep") == 1) { if (iniparser_find_entry(ini, "https:strrep") == 1) {
p->len_https_strrep = strlen(iniparser_getstring(ini, "https:strrep", NULL)) + 1; p->len_https_strrep =
strlen(iniparser_getstring(ini, "https:strrep", NULL)) + 1;
p->https_strrep = (char *)malloc(p->len_https_strrep); p->https_strrep = (char *)malloc(p->len_https_strrep);
if (p->https_strrep == NULL) { if (p->https_strrep == NULL) {
free(p->https_strrep); free(p->https_strrep);
} }
memset(p->https_strrep, 0, p->len_https_strrep); memset(p->https_strrep, 0, p->len_https_strrep);
memcpy(p->https_strrep, iniparser_getstring(ini, "https:strrep", NULL), p->len_https_strrep); memcpy(p->https_strrep, iniparser_getstring(ini, "https:strrep", NULL),
p->len_https_strrep);
char *p2 = strstr(p->https_strrep, "->"); char *p2 = strstr(p->https_strrep, "->");
p->https_strrep_aim = (char *)malloc(strlen(p->https_strrep) - strlen(p2+2) -2 + 1); p->https_strrep_aim =
(char *)malloc(strlen(p->https_strrep) - strlen(p2 + 2) - 2 + 1);
if (p->https_strrep_aim == NULL) { if (p->https_strrep_aim == NULL) {
free(p->https_strrep_aim); free(p->https_strrep_aim);
} }
strncpy_(p->https_strrep_aim, p->https_strrep, strlen(p->https_strrep) - strlen(p2+2) - 2); strncpy_(p->https_strrep_aim, p->https_strrep,
p->https_strrep_obj = (char *)malloc(strlen(p2+2) + 1); strlen(p->https_strrep) - strlen(p2 + 2) - 2);
p->https_strrep_obj = (char *)malloc(strlen(p2 + 2) + 1);
if (p->https_strrep_obj == NULL) { if (p->https_strrep_obj == NULL) {
free(p->https_strrep_obj); free(p->https_strrep_obj);
} }
strncpy_(p->https_strrep_obj, p2+2, strlen(p2+2)); strncpy_(p->https_strrep_obj, p2 + 2, strlen(p2 + 2));
} }
// http regrep // http regrep
if (iniparser_find_entry(ini, "http:regrep") == 1) { if (iniparser_find_entry(ini, "http:regrep") == 1) {
p->len_http_regrep = strlen(iniparser_getstring(ini, "http:regrep", NULL)) + 1; p->len_http_regrep =
strlen(iniparser_getstring(ini, "http:regrep", NULL)) + 1;
p->http_regrep = (char *)malloc(p->len_http_regrep); p->http_regrep = (char *)malloc(p->len_http_regrep);
if (p->http_regrep == NULL) { if (p->http_regrep == NULL) {
free(p->http_regrep); free(p->http_regrep);
} }
memset(p->http_regrep, 0, p->len_http_regrep); memset(p->http_regrep, 0, p->len_http_regrep);
memcpy(p->http_regrep, iniparser_getstring(ini, "http:regrep", NULL), p->len_http_regrep); memcpy(p->http_regrep, iniparser_getstring(ini, "http:regrep", NULL),
p->len_http_regrep);
char *p3 = strstr(p->http_regrep, "->"); char *p3 = strstr(p->http_regrep, "->");
p->http_regrep_aim = (char *)malloc(strlen(p->http_regrep) - strlen(p3+2) -2 + 1); p->http_regrep_aim =
(char *)malloc(strlen(p->http_regrep) - strlen(p3 + 2) - 2 + 1);
if (p->http_regrep_aim == NULL) { if (p->http_regrep_aim == NULL) {
free(p->http_regrep_aim); free(p->http_regrep_aim);
} }
strncpy_(p->http_regrep_aim, p->http_regrep, strlen(p->http_regrep) - strlen(p3+2) - 2); strncpy_(p->http_regrep_aim, p->http_regrep,
p->http_regrep_obj = (char *)malloc(strlen(p3+2) + 1); strlen(p->http_regrep) - strlen(p3 + 2) - 2);
p->http_regrep_obj = (char *)malloc(strlen(p3 + 2) + 1);
if (p->http_regrep_obj == NULL) { if (p->http_regrep_obj == NULL) {
free(p->http_regrep_obj); free(p->http_regrep_obj);
} }
strncpy_(p->http_regrep_obj, p3+2, strlen(p3+2)); strncpy_(p->http_regrep_obj, p3 + 2, strlen(p3 + 2));
} }
// https regrep // https regrep
if (iniparser_find_entry(ini, "https:regrep") == 1) { if (iniparser_find_entry(ini, "https:regrep") == 1) {
p->len_https_regrep = strlen(iniparser_getstring(ini, "https:regrep", NULL)) + 1; p->len_https_regrep =
strlen(iniparser_getstring(ini, "https:regrep", NULL)) + 1;
p->https_regrep = (char *)malloc(p->len_https_regrep); p->https_regrep = (char *)malloc(p->len_https_regrep);
if (p->https_regrep == NULL) { if (p->https_regrep == NULL) {
free(p->https_regrep); free(p->https_regrep);
} }
memset(p->https_regrep, 0, p->len_https_regrep); memset(p->https_regrep, 0, p->len_https_regrep);
memcpy(p->https_regrep, iniparser_getstring(ini, "https:regrep", NULL), p->len_https_regrep); memcpy(p->https_regrep, iniparser_getstring(ini, "https:regrep", NULL),
p->len_https_regrep);
char *p4 = strstr(p->https_regrep, "->"); char *p4 = strstr(p->https_regrep, "->");
p->https_regrep_aim = (char *)malloc(strlen(p->https_regrep) - strlen(p4+2) -2 + 1); p->https_regrep_aim =
(char *)malloc(strlen(p->https_regrep) - strlen(p4 + 2) - 2 + 1);
if (p->https_regrep_aim == NULL) { if (p->https_regrep_aim == NULL) {
free(p->https_regrep_aim); free(p->https_regrep_aim);
} }
strncpy_(p->https_regrep_aim, p->https_regrep, strlen(p->https_regrep) - strlen(p4+2) - 2); strncpy_(p->https_regrep_aim, p->https_regrep,
p->https_regrep_obj = (char *)malloc(strlen(p4+2) + 1); strlen(p->https_regrep) - strlen(p4 + 2) - 2);
p->https_regrep_obj = (char *)malloc(strlen(p4 + 2) + 1);
if (p->https_regrep_obj == NULL) { if (p->https_regrep_obj == NULL) {
free(p->https_regrep_obj); free(p->https_regrep_obj);
} }
strncpy_(p->https_regrep_obj, p4+2, strlen(p4+2)); strncpy_(p->https_regrep_obj, p4 + 2, strlen(p4 + 2));
} }
err: err:
@ -192,9 +218,10 @@ err:
free(p->https_first); free(p->https_first);
iniparser_freedict(ini); iniparser_freedict(ini);
return;
} }
void free_conf(conf *p) void free_conf(conf * p)
{ {
free(p->server_pid_file); free(p->server_pid_file);
free(p->http_ip); free(p->http_ip);
@ -217,5 +244,5 @@ void free_conf(conf *p)
free(p->https_regrep); free(p->https_regrep);
free(p->https_regrep_aim); free(p->https_regrep_aim);
free(p->https_regrep_obj); free(p->https_regrep_obj);
return;
} }

5
conf.h
View File

@ -6,8 +6,7 @@
#include <unistd.h> #include <unistd.h>
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

View File

@ -2,9 +2,9 @@
[http]、[https]模块关键字: [M], [U], [V], [H], [host], [port], \r, \n, \v, \f, \b, \t, \a. 如果原本请求头含有关键字也会被替换. [http]、[https]模块关键字: [M], [U], [V], [H], [host], [port], \r, \n, \v, \f, \b, \t, \a. 如果原本请求头含有关键字也会被替换.
[server]模块 [server]模块
uid 设置UID uid 设置进程UID
local_port 端口 local_port 端口
pid_file pid文件 pid_file PID文件
[http]模块 [http]模块
[M] 原请求方法 [M] 原请求方法

140
cproxy.c
View File

@ -28,27 +28,34 @@ char *read_data(int client_sock, char *data, int *data_len)
return data; return data;
} }
void servertoclient(int remote_sock, int client_sock, char *complete_data, int *len_complete_data) void servertoclient(int remote_sock, int client_sock, char *complete_data,
int *len_complete_data)
{ {
while ((*len_complete_data = read(remote_sock, complete_data, BUF_SIZE)) > 0) { while ((*len_complete_data =
read(remote_sock, complete_data, BUF_SIZE)) > 0) {
write(client_sock, complete_data, *len_complete_data); write(client_sock, complete_data, *len_complete_data);
} }
return;
} }
void clienttoserver(int remote_sock, char *complete_data, int *len_complete_data) void clienttoserver(int remote_sock, char *complete_data,
int *len_complete_data)
{ {
write(remote_sock, complete_data, *len_complete_data); write(remote_sock, complete_data, *len_complete_data);
complete_data = NULL; complete_data = NULL;
complete_data = 0; complete_data = 0;
return;
} }
// 处理客户端的连接 // 处理客户端的连接
void handle_client(int client_sock, struct sockaddr_in client_addr, conf *configure) void handle_client(int client_sock, struct sockaddr_in client_addr,
conf * configure)
{ {
read_data(client_sock, header_buffer, &len_header_buffer); // 第一次读取客户端(浏览器)数据 read_data(client_sock, header_buffer, &len_header_buffer); // 第一次读取客户端(浏览器)数据
SIGN = request_type(header_buffer); // 获取请求消息类型 SIGN = request_type(header_buffer); // 获取请求消息类型
extract_host(header_buffer); // 提取真实Host extract_host(header_buffer); // 提取真实Host
replacement_http_head(header_buffer, remote_host, &remote_port, &SIGN, configure); replacement_http_head(header_buffer, remote_host, &remote_port, &SIGN,
configure);
//printf("%s", header_buffer); //printf("%s", header_buffer);
@ -65,7 +72,6 @@ void handle_client(int client_sock, struct sockaddr_in client_addr, conf *config
forward_data(client_sock, remote_sock); forward_data(client_sock, remote_sock);
} }
_exit(0); _exit(0);
} }
if (fork() == 0) { if (fork() == 0) {
@ -80,6 +86,7 @@ void handle_client(int client_sock, struct sockaddr_in client_addr, conf *config
close(client_sock); close(client_sock);
close(remote_sock); close(remote_sock);
return;
} }
int send_data(int socket, char *buffer, int len) int send_data(int socket, char *buffer, int len)
@ -102,13 +109,15 @@ void forward_data(int source_sock, int destination_sock)
} }
shutdown(destination_sock, SHUT_RDWR); shutdown(destination_sock, SHUT_RDWR);
shutdown(source_sock, SHUT_RDWR); shutdown(source_sock, SHUT_RDWR);
return;
} }
int create_connection(conf *configure, int SIGN) int create_connection(conf * configure, int SIGN)
{ {
struct sockaddr_in server_addr; struct sockaddr_in server_addr;
struct hostent *server = NULL; struct hostent *server = NULL;
int sock; int sock;
int optval;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
return CLIENT_SOCKET_ERROR; return CLIENT_SOCKET_ERROR;
@ -139,7 +148,15 @@ int create_connection(conf *configure, int SIGN)
struct linger so_linger; struct linger so_linger;
so_linger.l_onoff = 1; so_linger.l_onoff = 1;
so_linger.l_linger = 0; so_linger.l_linger = 0;
setsockopt(sock, SOL_SOCKET, SO_LINGER, &so_linger,sizeof so_linger); if (setsockopt(sock, SOL_SOCKET, SO_LINGER, &so_linger, sizeof so_linger) <
0) {
return SERVER_SETSOCKOPT_ERROR;
}
if (setsockopt
(server_sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
return SERVER_SETSOCKOPT_ERROR;
}
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
return CLIENT_CONNECT_ERROR; return CLIENT_CONNECT_ERROR;
@ -151,21 +168,26 @@ int create_connection(conf *configure, int SIGN)
int create_server_socket(int port) int create_server_socket(int port)
{ {
int server_sock; int server_sock;
//int optval; int optval;
struct sockaddr_in server_addr; struct sockaddr_in server_addr;
if ((server_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { if ((server_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
return SERVER_SOCKET_ERROR; return SERVER_SOCKET_ERROR;
} }
/*
if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
return SERVER_SETSOCKOPT_ERROR;
}
*/
struct linger so_linger; struct linger so_linger;
so_linger.l_onoff = 1; so_linger.l_onoff = 1;
so_linger.l_linger = 0; so_linger.l_linger = 0;
setsockopt(server_sock, SOL_SOCKET, SO_LINGER, &so_linger,sizeof so_linger); if (setsockopt
(server_sock, SOL_SOCKET, SO_LINGER, &so_linger,
sizeof so_linger) < 0) {
return SERVER_SETSOCKOPT_ERROR;
}
if (setsockopt
(server_sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
return SERVER_SETSOCKOPT_ERROR;
}
memset(&server_addr, 0, sizeof(server_addr)); memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET; server_addr.sin_family = AF_INET;
@ -185,11 +207,11 @@ int create_server_socket(int port)
} }
// 守护 // 守护
int init_daemon(int nochdir, int noclose, conf *configure, char *path) int init_daemon(int nochdir, int noclose, conf * configure, char *path)
{ {
char *p = strcat(path, configure->server_pid_file); char *p = strcat(path, configure->server_pid_file);
FILE *fp = fopen(p, "w"); FILE *fp = fopen(p, "w");
if(fp == NULL) { if (fp == NULL) {
fclose(fp); fclose(fp);
printf("%s Open Failed\n", p); printf("%s Open Failed\n", p);
exit(1); exit(1);
@ -236,6 +258,7 @@ int init_daemon(int nochdir, int noclose, conf *configure, char *path)
void sigchld_handler(int signal) void sigchld_handler(int signal)
{ {
while (waitpid(-1, NULL, WNOHANG) > 0) ; while (waitpid(-1, NULL, WNOHANG) > 0) ;
return;
} }
void server_loop(conf * configure) void server_loop(conf * configure)
@ -244,7 +267,8 @@ void server_loop(conf * configure)
socklen_t addrlen = sizeof(client_addr); socklen_t addrlen = sizeof(client_addr);
while (1) { while (1) {
client_sock = accept(server_sock, (struct sockaddr *)&client_addr, &addrlen); client_sock =
accept(server_sock, (struct sockaddr *)&client_addr, &addrlen);
if (fork() == 0) { // 创建子进程处理客户端连接请求 if (fork() == 0) { // 创建子进程处理客户端连接请求
close(server_sock); close(server_sock);
@ -254,9 +278,10 @@ void server_loop(conf * configure)
close(client_sock); close(client_sock);
} }
close(server_sock); close(server_sock);
return;
} }
void start_server(conf *configure) void start_server(conf * configure)
{ {
signal(SIGCHLD, sigchld_handler); // 防止子进程变成僵尸进程 signal(SIGCHLD, sigchld_handler); // 防止子进程变成僵尸进程
@ -265,16 +290,64 @@ void start_server(conf *configure)
} }
server_loop(configure); server_loop(configure);
return;
} }
int stop(int signal, char *program_name) { void get_pid(char *proces_name)
{
char bufer[PATH_SIZE];
char comm[PATH_SIZE];
char proc_comm_name[PATH_SIZE];
int num[PATH_SIZE] = { 0 };
int n = 0;
FILE *fp;
DIR *dir;
struct dirent *ptr;
dir = opendir("/proc");
while ((ptr = readdir(dir)) != NULL) {
if (ptr->d_type == 4 && strcasecmp(ptr->d_name, ".")
&& strcasecmp(ptr->d_name, "..")) {
bzero(bufer, 0);
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(proces_name, proc_comm_name)) {
num[n] = atoi(ptr->d_name);
n += 1;
}
fclose(fp);
}
}
}
n -= 2; // 去除最后一个搜索时的本身进程
for (; n >= 0; n--) {
printf("\t%d\n", num[n]);
}
closedir(dir);
return;
}
int stop(int signal, char *program_name)
{
if (signal == 1) { if (signal == 1) {
struct passwd *pwent = NULL; struct passwd *pwent = NULL;
pwent = getpwnam("root"); pwent = getpwnam("root");
return kill_all(15,1, &program_name, pwent); return kill_all(15, 1, &program_name, pwent);
}
if (signal == 2) {
get_pid(program_name);
return 0;
} }
return 1; return 0;
} }
int get_executable_path(char *processdir, char *processname, int len) int get_executable_path(char *processdir, char *processname, int len)
@ -298,14 +371,11 @@ int _main(int argc, char *argv[])
header_buffer = (char *)malloc(BUF_SIZE); header_buffer = (char *)malloc(BUF_SIZE);
len_header_buffer = strlen(header_buffer); len_header_buffer = strlen(header_buffer);
//complete_data = (char *)malloc(BUF_SIZES);
//len_complete_data = strlen(complete_data);
char *inifile = "conf/cproxy.ini"; char *inifile = "conf/cproxy.ini";
char path[PATH_SIZE] = { 0 }; char path[PATH_SIZE] = { 0 };
char executable_filename[PATH_SIZE] = { 0 }; char executable_filename[PATH_SIZE] = { 0 };
(void)get_executable_path(path, executable_filename, sizeof(path)); (void)get_executable_path(path, executable_filename, sizeof(path));
inifile=strcat(path, inifile); inifile = strcat(path, inifile);
conf *configure = (struct CONF *)malloc(sizeof(struct CONF)); conf *configure = (struct CONF *)malloc(sizeof(struct CONF));
read_conf(inifile, configure); read_conf(inifile, configure);
@ -324,19 +394,27 @@ int _main(int argc, char *argv[])
init_daemon(1, 1, configure, path); init_daemon(1, 1, configure, path);
break; break;
case 's': case 's':
if (strcasecmp(optarg, "stop") == 0) { if (strcasecmp(optarg, "stop") == 0
|| strcasecmp(optarg, "quit") == 0) {
free_conf(configure); free_conf(configure);
free(header_buffer); free(header_buffer);
exit(stop(1, executable_filename));
}
if (strcasecmp(optarg, "restart") == 0
|| strcasecmp(optarg, "reload") == 0) {
stop(1, executable_filename); stop(1, executable_filename);
} }
exit(0); if (strcasecmp(optarg, "status") == 0) {
exit(stop(2, executable_filename));
}
break; break;
case 'c': case 'c':
free_conf(configure); // 如果指定-c参数就释放上次分配的内存 free_conf(configure); // 如果指定-c参数就释放上次分配的内存
inifile=optarg; inifile = optarg;
read_conf(inifile, configure); read_conf(inifile, configure);
break; break;
case 'h': case '?': case 'h':
case '?':
help_information(); help_information();
exit(0); exit(0);
break; break;
@ -350,7 +428,6 @@ int _main(int argc, char *argv[])
start_server(configure); start_server(configure);
free_conf(configure); free_conf(configure);
//free(complete_data);
free(header_buffer); free(header_buffer);
return 0; return 0;
} }
@ -359,4 +436,3 @@ int main(int argc, char *argv[])
{ {
return _main(argc, argv); return _main(argc, argv);
} }

View File

@ -17,7 +17,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <dirent.h> #include <dirent.h>
#define PATH_SIZE 270 #define PATH_SIZE 270
@ -42,8 +41,6 @@ int remote_sock;
char *header_buffer; char *header_buffer;
int len_header_buffer; int len_header_buffer;
//char *complete_data;
//int len_complete_data;
int SIGN; int SIGN;

View File

@ -8,22 +8,21 @@ char help_information(void)
static const struct { static const struct {
const char *a, *b, *c, *d; const char *a, *b, *c, *d;
} author = { } author = {
"aixiao@aixiao.me", "aixiao", "Author:", "Email :",}; "aixiao@aixiao.me", "aixiao", "Author:", "Email :",
static const char usage[] = };
"Usage: [-?hdsc] [-s signal] [-c filename]"; static const char usage[] = "Usage: [-?hd] [-s signal] [-c filename]";
static const char *s_help[] = { static const char *s_help[] = {
"", "",
"Options:", "Options:",
" -?,-h : help information", " -?,-h : help information",
" -d : daemon", " -d : daemon",
" -s signal : send signal to a master process: stop", " -s signal : send signal to a master process: stop, quit, restart, reload, status",
" -c filename : set configuration file (default: conf/cproxy.ini)", " -c filename : set configuration file (default: conf/cproxy.ini)",
"", "",
0 0
}; };
//fprintf(stderr, "%s %s\n", author.c, author.b); //fprintf(stderr, "%s %s\n", author.c, author.b);
fprintf(stderr, "%s %s\n", name, subject); fprintf(stderr, "%s %s\n", name, subject);
fprintf(stderr, "%s %s\n", author.c, author.a); fprintf(stderr, "%s %s\n", author.c, author.a);
@ -38,4 +37,3 @@ char help_information(void)
return 0; return 0;
} }

View File

@ -8,4 +8,3 @@
char help_information(void); char help_information(void);
#endif #endif

View File

@ -1,7 +1,8 @@
#include "cproxy_request.h" #include "cproxy_request.h"
// 字符串替换 // 字符串替换
char *replace(char *replace_memory, int *replace_memory_len, const char *src, const int src_len, const char *dest, const int dest_len) char *replace(char *replace_memory, int *replace_memory_len, const char *src,
const int src_len, const char *dest, const int dest_len)
{ {
if (!replace_memory || !src || !dest) if (!replace_memory || !src || !dest)
return replace_memory; return replace_memory;
@ -58,23 +59,22 @@ char *replace(char *replace_memory, int *replace_memory_len, const char *src, co
} }
/* 正则表达式字符串替换str为可用free释放的指针 */ /* 正则表达式字符串替换str为可用free释放的指针 */
static char *regrep(char *str, int *str_len, const char *src, char *dest, int dest_len) static char *regrep(char *str, int *str_len, const char *src, char *dest,
int dest_len)
{ {
if (!str || !src || !dest) if (!str || !src || !dest)
return NULL; return NULL;
regmatch_t pm[10]; regmatch_t pm[10];
regex_t reg; regex_t reg;
char child_num[2] = {'\\', '0'}, *p, *real_dest; char child_num[2] = { '\\', '0' }, *p, *real_dest;
int match_len, real_dest_len, i; int match_len, real_dest_len, i;
p = str; p = str;
regcomp(&reg, src, REG_NEWLINE|REG_ICASE|REG_EXTENDED); regcomp(&reg, src, REG_NEWLINE | REG_ICASE | REG_EXTENDED);
while (regexec(&reg, p, 10, pm, 0) == 0) while (regexec(&reg, p, 10, pm, 0) == 0) {
{
real_dest = (char *)malloc(dest_len); real_dest = (char *)malloc(dest_len);
if (real_dest == NULL) if (real_dest == NULL) {
{
regfree(&reg); regfree(&reg);
free(str); free(str);
return NULL; return NULL;
@ -82,15 +82,14 @@ static char *regrep(char *str, int *str_len, const char *src, char *dest, int de
memcpy(real_dest, dest, dest_len); memcpy(real_dest, dest, dest_len);
real_dest_len = dest_len; real_dest_len = dest_len;
//不进行不必要的字符串操作 //不进行不必要的字符串操作
if (pm[1].rm_so >= 0) if (pm[1].rm_so >= 0) {
{
/* 替换目标字符串中的子表达式 */ /* 替换目标字符串中的子表达式 */
for (i = 1; i < 10 && pm[i].rm_so > -1; i++) for (i = 1; i < 10 && pm[i].rm_so > -1; i++) {
{
child_num[1] = i + 48; child_num[1] = i + 48;
real_dest = replace(real_dest, &real_dest_len, child_num, 2, p + pm[i].rm_so, pm[i].rm_eo - pm[i].rm_so); real_dest =
if (real_dest == NULL) replace(real_dest, &real_dest_len, child_num, 2,
{ p + pm[i].rm_so, pm[i].rm_eo - pm[i].rm_so);
if (real_dest == NULL) {
regfree(&reg); regfree(&reg);
free(str); free(str);
return NULL; return NULL;
@ -101,25 +100,22 @@ static char *regrep(char *str, int *str_len, const char *src, char *dest, int de
match_len = pm[0].rm_eo - pm[0].rm_so; match_len = pm[0].rm_eo - pm[0].rm_so;
p += pm[0].rm_so; p += pm[0].rm_so;
//目标字符串不大于匹配字符串则不用分配新内存 //目标字符串不大于匹配字符串则不用分配新内存
if (match_len >= real_dest_len) if (match_len >= real_dest_len) {
{
memcpy(p, real_dest, real_dest_len); memcpy(p, real_dest, real_dest_len);
if (match_len > real_dest_len) if (match_len > real_dest_len)
//strcpy(p + real_dest_len, p + match_len); //strcpy(p + real_dest_len, p + match_len);
memmove(p + real_dest_len, p + match_len, *str_len - (p + match_len - str)); memmove(p + real_dest_len, p + match_len,
*str_len - (p + match_len - str));
p += real_dest_len; p += real_dest_len;
*str_len -= match_len - real_dest_len; *str_len -= match_len - real_dest_len;
} } else {
else
{
int diff; int diff;
char *before_end, *new_str; char *before_end, *new_str;
diff = real_dest_len - match_len; diff = real_dest_len - match_len;
*str_len += diff; *str_len += diff;
new_str = (char *)realloc(str, *str_len + 1); new_str = (char *)realloc(str, *str_len + 1);
if (new_str == NULL) if (new_str == NULL) {
{
free(str); free(str);
free(real_dest); free(real_dest);
regfree(&reg); regfree(&reg);
@ -246,6 +242,7 @@ void forward_header(int destination_sock)
rewrite_header(); rewrite_header();
int len = strlen(header_buffer); int len = strlen(header_buffer);
send_data(destination_sock, header_buffer, len); send_data(destination_sock, header_buffer, len);
return;
} }
// 代理中的完整URL转发前需改成 path 的形式 // 代理中的完整URL转发前需改成 path 的形式
@ -270,6 +267,7 @@ void rewrite_header()
header_buffer[l] = '\0'; header_buffer[l] = '\0';
} }
} }
return;
} }
// 判断数字有几位 // 判断数字有几位
@ -311,7 +309,8 @@ char *delete_header(char *header_buffer, const char *character, int string)
return strcat(header_buffer, p2 + 1); return strcat(header_buffer, p2 + 1);
} }
char *splice_host_port(char *tmp, char *host, char *port) { char *splice_host_port(char *tmp, char *host, char *port)
{
//memset(tmp, 0, strlen(tmp)); //memset(tmp, 0, strlen(tmp));
bzero(tmp, strlen(tmp)); bzero(tmp, strlen(tmp));
strcat(tmp, host); strcat(tmp, host);
@ -319,7 +318,8 @@ char *splice_host_port(char *tmp, char *host, char *port) {
return strcat(tmp, port); return strcat(tmp, port);
} }
int replacement_http_head(char *header_buffer, char *remote_host, int *remote_port, int *SIGN, conf *p) int replacement_http_head(char *header_buffer, char *remote_host,
int *remote_port, int *SIGN, conf * p)
{ {
char *http_firsts = (char *)malloc(strlen(p->http_first) + 1); char *http_firsts = (char *)malloc(strlen(p->http_first) + 1);
if (http_firsts) { if (http_firsts) {
@ -418,9 +418,13 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po
return 0; return 0;
} }
char *new_header_buffer = (char *)malloc(strlen(splice_head(header_buffer_backup, "\n", http_firsts)) + 1); char *new_header_buffer =
(char *)
malloc(strlen(splice_head(header_buffer_backup, "\n", http_firsts))
+ 1);
if (new_header_buffer) { if (new_header_buffer) {
strcpy(new_header_buffer, splice_head(header_buffer_backup, "\n", http_firsts)); strcpy(new_header_buffer,
splice_head(header_buffer_backup, "\n", http_firsts));
} else { } else {
free(new_header_buffer); free(new_header_buffer);
return 0; return 0;
@ -432,20 +436,28 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po
int len_v = strlen(V); int len_v = strlen(V);
int len_remote_host = strlen(remote_host); int len_remote_host = strlen(remote_host);
new_header_buffer = replace(new_header_buffer, &len, "[M]", 3, M, len_m); new_header_buffer =
new_header_buffer = replace(new_header_buffer, &len, "[U]", 3, U, len_u); replace(new_header_buffer, &len, "[M]", 3, M, len_m);
new_header_buffer = replace(new_header_buffer, &len, "[V]", 3, V, len_v); new_header_buffer =
new_header_buffer = replace(new_header_buffer, &len, "[host]", 6, remote_host, len_remote_host); replace(new_header_buffer, &len, "[U]", 3, U, len_u);
new_header_buffer =
replace(new_header_buffer, &len, "[V]", 3, V, len_v);
new_header_buffer =
replace(new_header_buffer, &len, "[host]", 6, remote_host,
len_remote_host);
char port_copy[(numbin(*remote_port) + 2)]; char port_copy[(numbin(*remote_port) + 2)];
sprintf(port_copy, "%d", *remote_port); sprintf(port_copy, "%d", *remote_port);
int len_remote_port = strlen(port_copy); int len_remote_port = strlen(port_copy);
new_header_buffer = replace(new_header_buffer, &len, "[port]", 6, port_copy, len_remote_port); new_header_buffer =
replace(new_header_buffer, &len, "[port]", 6, port_copy,
len_remote_port);
char H[(len_remote_port + len_remote_host) +1]; char H[(len_remote_port + len_remote_host) + 1];
splice_host_port(H, remote_host, port_copy); splice_host_port(H, remote_host, port_copy);
int len_h = strlen(H); int len_h = strlen(H);
new_header_buffer = replace(new_header_buffer, &len, "[H]", 3, H, len_h); new_header_buffer =
replace(new_header_buffer, &len, "[H]", 3, H, len_h);
new_header_buffer = replace(new_header_buffer, &len, "\\r", 2, "\r", 1); new_header_buffer = replace(new_header_buffer, &len, "\\r", 2, "\r", 1);
new_header_buffer = replace(new_header_buffer, &len, "\\n", 2, "\n", 1); new_header_buffer = replace(new_header_buffer, &len, "\\n", 2, "\n", 1);
@ -459,13 +471,16 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po
if (p->http_strrep) { if (p->http_strrep) {
int regrep_aim_len = strlen(p->http_strrep_aim); int regrep_aim_len = strlen(p->http_strrep_aim);
int regrep_obj_len = strlen(p->http_strrep_obj); int regrep_obj_len = strlen(p->http_strrep_obj);
new_header_buffer = replace(new_header_buffer, &len, p->http_strrep_aim, regrep_aim_len, p->http_strrep_obj, regrep_obj_len); new_header_buffer =
replace(new_header_buffer, &len, p->http_strrep_aim,
regrep_aim_len, p->http_strrep_obj, regrep_obj_len);
} }
if (p->http_regrep) { if (p->http_regrep) {
len = strlen(new_header_buffer) + 1; len = strlen(new_header_buffer) + 1;
new_header_buffer = regrep(new_header_buffer, &len, p->http_regrep_aim, p->http_regrep_obj, strlen(p->http_regrep_obj)); new_header_buffer =
regrep(new_header_buffer, &len, p->http_regrep_aim,
p->http_regrep_obj, strlen(p->http_regrep_obj));
} }
//stpcpy(p->http_ip, remote_host); //stpcpy(p->http_ip, remote_host);
//p->http_port = *remote_port; //p->http_port = *remote_port;
memset(header_buffer, 0, strlen(header_buffer)); memset(header_buffer, 0, strlen(header_buffer));
@ -533,9 +548,13 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po
return 0; return 0;
} }
char *new_header_buffer = (char *) malloc(strlen(splice_head(header_buffer_backup, "\n", https_firsts)) + 1); char *new_header_buffer =
(char *)
malloc(strlen(splice_head(header_buffer_backup, "\n", https_firsts))
+ 1);
if (new_header_buffer) { if (new_header_buffer) {
strcpy(new_header_buffer, splice_head(header_buffer_backup, "\n", https_firsts)); strcpy(new_header_buffer,
splice_head(header_buffer_backup, "\n", https_firsts));
} else { } else {
free(new_header_buffer); free(new_header_buffer);
return 0; return 0;
@ -547,21 +566,27 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po
int len_v = strlen(V); int len_v = strlen(V);
int len_remote_host = strlen(remote_host); int len_remote_host = strlen(remote_host);
new_header_buffer = replace(new_header_buffer, &len, "[M]", 3, M, len_m); new_header_buffer =
new_header_buffer = replace(new_header_buffer, &len, "[U]", 3, U, len_u); replace(new_header_buffer, &len, "[M]", 3, M, len_m);
new_header_buffer = replace(new_header_buffer, &len, "[V]", 3, V, len_v); new_header_buffer =
new_header_buffer = replace(new_header_buffer, &len, "[host]", 6, remote_host, len_remote_host); replace(new_header_buffer, &len, "[U]", 3, U, len_u);
new_header_buffer =
replace(new_header_buffer, &len, "[V]", 3, V, len_v);
new_header_buffer =
replace(new_header_buffer, &len, "[host]", 6, remote_host,
len_remote_host);
char port_copy[(numbin(*remote_port) + 2)]; char port_copy[(numbin(*remote_port) + 2)];
sprintf(port_copy, "%d", *remote_port); sprintf(port_copy, "%d", *remote_port);
int len_remote_port = strlen(port_copy); int len_remote_port = strlen(port_copy);
new_header_buffer = replace(new_header_buffer, &len, "[port]", 6, port_copy, len_remote_port); new_header_buffer =
replace(new_header_buffer, &len, "[port]", 6, port_copy,
len_remote_port);
char H[(len_remote_port + len_remote_host) +1]; char H[(len_remote_port + len_remote_host) + 1];
splice_host_port(H, remote_host, port_copy); splice_host_port(H, remote_host, port_copy);
int len_h = strlen(H); int len_h = strlen(H);
new_header_buffer = replace(new_header_buffer, &len, "[H]", 3, H, len_h); new_header_buffer =
replace(new_header_buffer, &len, "[H]", 3, H, len_h);
new_header_buffer = replace(new_header_buffer, &len, "\\r", 2, "\r", 1); new_header_buffer = replace(new_header_buffer, &len, "\\r", 2, "\r", 1);
@ -577,11 +602,15 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po
if (p->https_strrep) { if (p->https_strrep) {
int regrep_aim_len = strlen(p->https_strrep_aim); int regrep_aim_len = strlen(p->https_strrep_aim);
int regrep_obj_len = strlen(p->https_strrep_obj); int regrep_obj_len = strlen(p->https_strrep_obj);
new_header_buffer = replace(new_header_buffer, &len, p->https_strrep_aim, regrep_aim_len, p->https_strrep_obj, regrep_obj_len); new_header_buffer =
replace(new_header_buffer, &len, p->https_strrep_aim,
regrep_aim_len, p->https_strrep_obj, regrep_obj_len);
} }
if (p->https_regrep) { if (p->https_regrep) {
len = strlen(new_header_buffer) + 1; len = strlen(new_header_buffer) + 1;
new_header_buffer = regrep(new_header_buffer, &len, p->https_regrep_aim, p->https_regrep_obj, strlen(p->https_regrep_obj)); new_header_buffer =
regrep(new_header_buffer, &len, p->https_regrep_aim,
p->https_regrep_obj, strlen(p->https_regrep_obj));
} }
@ -606,4 +635,3 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po
free(header_buffer_backup); free(header_buffer_backup);
return 1; return 1;
} }

6
kill.c
View File

@ -2,7 +2,8 @@
static pid_t opt_ns_pid = 0; static pid_t opt_ns_pid = 0;
static int exact = 1, reg = 0, wait_until_dead = 1, process_group = 0, ignore_case = 0; static int exact = 1, reg = 0, wait_until_dead = 1, process_group =
0, ignore_case = 0;
static long younger_than = 0, older_than = 0; static long younger_than = 0, older_than = 0;
typedef struct NAMEINFO { typedef struct NAMEINFO {
@ -336,8 +337,7 @@ static int match_process_name(const char *proc_comm,
return (0 == strcmp2(match_name, proc_comm, ignore_case)); return (0 == strcmp2(match_name, proc_comm, ignore_case));
} }
int int kill_all(int signal, int name_count, char **namelist, struct passwd *pwent)
kill_all(int signal, int name_count, char **namelist, struct passwd *pwent)
{ {
struct stat st; struct stat st;
NAMEINFO *name_info = NULL; NAMEINFO *name_info = NULL;

2
kill.h
View File

@ -1,8 +1,6 @@
#ifndef KILL_H #ifndef KILL_H
#define KILL_H #define KILL_H
#ifndef _GNU_SOURCE #ifndef _GNU_SOURCE
#define _GNU_SOURCE #define _GNU_SOURCE
#endif #endif