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:
-?,-h : help information
-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)
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"
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);
exit(1);
}
@ -15,13 +15,16 @@ void read_conf(char *file, conf *p)
//local_port
p->server_port = iniparser_getint(ini, "server:local_port", 0);
//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);
if (p->server_pid_file == NULL) {
goto err;
}
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);
// http module
@ -32,147 +35,170 @@ void read_conf(char *file, conf *p)
goto err;
}
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
p->http_port = iniparser_getint(ini, "http:http_port", 0);
// 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);
if (p->http_del == NULL) {
goto err;
}
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
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);
if (p->http_first == NULL) {
goto err;
}
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 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);
if (p->https_ip == NULL) {
goto err;
}
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
p->https_port = iniparser_getint(ini, "https:https_port", 0);
// 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);
if (p->https_del == NULL) {
goto err;
}
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
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);
if (p->https_first == NULL) {
goto err;
}
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
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);
if (p->http_strrep == NULL) {
free(p->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, "->");
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) {
free(p->http_strrep_aim);
}
strncpy_(p->http_strrep_aim, p->http_strrep, strlen(p->http_strrep) - strlen(p1+2) - 2);
p->http_strrep_obj = (char *)malloc(strlen(p1+2) + 1);
strncpy_(p->http_strrep_aim, p->http_strrep,
strlen(p->http_strrep) - strlen(p1 + 2) - 2);
p->http_strrep_obj = (char *)malloc(strlen(p1 + 2) + 1);
if (p->http_strrep_obj == NULL) {
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
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);
if (p->https_strrep == NULL) {
free(p->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, "->");
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) {
free(p->https_strrep_aim);
}
strncpy_(p->https_strrep_aim, p->https_strrep, strlen(p->https_strrep) - strlen(p2+2) - 2);
p->https_strrep_obj = (char *)malloc(strlen(p2+2) + 1);
strncpy_(p->https_strrep_aim, p->https_strrep,
strlen(p->https_strrep) - strlen(p2 + 2) - 2);
p->https_strrep_obj = (char *)malloc(strlen(p2 + 2) + 1);
if (p->https_strrep_obj == NULL) {
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
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);
if (p->http_regrep == NULL) {
free(p->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, "->");
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) {
free(p->http_regrep_aim);
}
strncpy_(p->http_regrep_aim, p->http_regrep, strlen(p->http_regrep) - strlen(p3+2) - 2);
p->http_regrep_obj = (char *)malloc(strlen(p3+2) + 1);
strncpy_(p->http_regrep_aim, p->http_regrep,
strlen(p->http_regrep) - strlen(p3 + 2) - 2);
p->http_regrep_obj = (char *)malloc(strlen(p3 + 2) + 1);
if (p->http_regrep_obj == NULL) {
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
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);
if (p->https_regrep == NULL) {
free(p->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, "->");
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) {
free(p->https_regrep_aim);
}
strncpy_(p->https_regrep_aim, p->https_regrep, strlen(p->https_regrep) - strlen(p4+2) - 2);
p->https_regrep_obj = (char *)malloc(strlen(p4+2) + 1);
strncpy_(p->https_regrep_aim, p->https_regrep,
strlen(p->https_regrep) - strlen(p4 + 2) - 2);
p->https_regrep_obj = (char *)malloc(strlen(p4 + 2) + 1);
if (p->https_regrep_obj == NULL) {
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:
@ -192,9 +218,10 @@ err:
free(p->https_first);
iniparser_freedict(ini);
return;
}
void free_conf(conf *p)
void free_conf(conf * p)
{
free(p->server_pid_file);
free(p->http_ip);
@ -217,5 +244,5 @@ void free_conf(conf *p)
free(p->https_regrep);
free(p->https_regrep_aim);
free(p->https_regrep_obj);
return;
}

5
conf.h
View File

@ -6,8 +6,7 @@
#include <unistd.h>
char *strncpy_(char *dest, const char *src, size_t n);
void read_conf(char *file, conf *p);
void free_conf(conf *p);
void read_conf(char *file, conf * p);
void free_conf(conf * p);
#endif

View File

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

146
cproxy.c
View File

@ -28,27 +28,34 @@ char *read_data(int client_sock, char *data, int *data_len)
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);
}
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);
complete_data = NULL;
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); // 第一次读取客户端(浏览器)数据
SIGN = request_type(header_buffer); // 获取请求消息类型
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);
@ -65,7 +72,6 @@ void handle_client(int client_sock, struct sockaddr_in client_addr, conf *config
forward_data(client_sock, remote_sock);
}
_exit(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(remote_sock);
return;
}
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(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 hostent *server = NULL;
int sock;
int optval;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
return CLIENT_SOCKET_ERROR;
@ -139,7 +148,15 @@ int create_connection(conf *configure, int SIGN)
struct linger so_linger;
so_linger.l_onoff = 1;
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) {
return CLIENT_CONNECT_ERROR;
@ -151,21 +168,26 @@ int create_connection(conf *configure, int SIGN)
int create_server_socket(int port)
{
int server_sock;
//int optval;
int optval;
struct sockaddr_in server_addr;
if ((server_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
return SERVER_SOCKET_ERROR;
}
/*
if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
return SERVER_SETSOCKOPT_ERROR;
}
*/
struct linger so_linger;
so_linger.l_onoff = 1;
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));
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);
FILE *fp = fopen(p, "w");
if(fp == NULL) {
if (fp == NULL) {
fclose(fp);
printf("%s Open Failed\n", p);
exit(1);
@ -236,6 +258,7 @@ int init_daemon(int nochdir, int noclose, conf *configure, char *path)
void sigchld_handler(int signal)
{
while (waitpid(-1, NULL, WNOHANG) > 0) ;
return;
}
void server_loop(conf * configure)
@ -244,7 +267,8 @@ void server_loop(conf * configure)
socklen_t addrlen = sizeof(client_addr);
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) { // 创建子进程处理客户端连接请求
close(server_sock);
@ -254,9 +278,10 @@ void server_loop(conf * configure)
close(client_sock);
}
close(server_sock);
return;
}
void start_server(conf *configure)
void start_server(conf * configure)
{
signal(SIGCHLD, sigchld_handler); // 防止子进程变成僵尸进程
@ -265,16 +290,64 @@ void start_server(conf *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) {
struct passwd *pwent = NULL;
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)
@ -298,14 +371,11 @@ int _main(int argc, char *argv[])
header_buffer = (char *)malloc(BUF_SIZE);
len_header_buffer = strlen(header_buffer);
//complete_data = (char *)malloc(BUF_SIZES);
//len_complete_data = strlen(complete_data);
char *inifile = "conf/cproxy.ini";
char path[PATH_SIZE] = { 0 };
char executable_filename[PATH_SIZE] = { 0 };
(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));
read_conf(inifile, configure);
@ -324,19 +394,27 @@ int _main(int argc, char *argv[])
init_daemon(1, 1, configure, path);
break;
case 's':
if (strcasecmp(optarg, "stop") == 0) {
if (strcasecmp(optarg, "stop") == 0
|| strcasecmp(optarg, "quit") == 0) {
free_conf(configure);
free(header_buffer);
stop(1, executable_filename);
exit(stop(1, executable_filename));
}
if (strcasecmp(optarg, "restart") == 0
|| strcasecmp(optarg, "reload") == 0) {
stop(1, executable_filename);
}
if (strcasecmp(optarg, "status") == 0) {
exit(stop(2, executable_filename));
}
exit(0);
break;
case 'c':
free_conf(configure); // 如果指定-c参数就释放上次分配的内存
inifile=optarg;
free_conf(configure); // 如果指定-c参数就释放上次分配的内存
inifile = optarg;
read_conf(inifile, configure);
break;
case 'h': case '?':
case 'h':
case '?':
help_information();
exit(0);
break;
@ -345,12 +423,11 @@ int _main(int argc, char *argv[])
}
}
if (setegid(configure->uid) == -1 || seteuid(configure->uid) == -1) // 设置uid
if (setegid(configure->uid) == -1 || seteuid(configure->uid) == -1) // 设置uid
exit(1);
start_server(configure);
free_conf(configure);
//free(complete_data);
free(header_buffer);
return 0;
}
@ -359,4 +436,3 @@ int main(int argc, char *argv[])
{
return _main(argc, argv);
}

View File

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

View File

@ -8,22 +8,21 @@ char help_information(void)
static const struct {
const char *a, *b, *c, *d;
} author = {
"aixiao@aixiao.me", "aixiao", "Author:", "Email :",};
static const char usage[] =
"Usage: [-?hdsc] [-s signal] [-c filename]";
"aixiao@aixiao.me", "aixiao", "Author:", "Email :",
};
static const char usage[] = "Usage: [-?hd] [-s signal] [-c filename]";
static const char *s_help[] = {
"",
"Options:",
" -?,-h : help information",
" -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)",
"",
0
};
//fprintf(stderr, "%s %s\n", author.c, author.b);
fprintf(stderr, "%s %s\n", name, subject);
fprintf(stderr, "%s %s\n", author.c, author.a);
@ -38,4 +37,3 @@ char help_information(void)
return 0;
}

View File

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

View File

@ -1,7 +1,8 @@
#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)
return replace_memory;
@ -58,23 +59,22 @@ char *replace(char *replace_memory, int *replace_memory_len, const char *src, co
}
/* 正则表达式字符串替换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)
return NULL;
regmatch_t pm[10];
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;
p = str;
regcomp(&reg, src, REG_NEWLINE|REG_ICASE|REG_EXTENDED);
while (regexec(&reg, p, 10, pm, 0) == 0)
{
regcomp(&reg, src, REG_NEWLINE | REG_ICASE | REG_EXTENDED);
while (regexec(&reg, p, 10, pm, 0) == 0) {
real_dest = (char *)malloc(dest_len);
if (real_dest == NULL)
{
if (real_dest == NULL) {
regfree(&reg);
free(str);
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);
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;
real_dest = 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)
{
real_dest =
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);
free(str);
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;
p += pm[0].rm_so;
//目标字符串不大于匹配字符串则不用分配新内存
if (match_len >= real_dest_len)
{
if (match_len >= real_dest_len) {
memcpy(p, real_dest, real_dest_len);
if (match_len > real_dest_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;
*str_len -= match_len - real_dest_len;
}
else
{
} else {
int diff;
char *before_end, *new_str;
diff = real_dest_len - match_len;
*str_len += diff;
new_str = (char *)realloc(str, *str_len + 1);
if (new_str == NULL)
{
if (new_str == NULL) {
free(str);
free(real_dest);
regfree(&reg);
@ -174,15 +170,15 @@ uint8_t request_type(char *req)
else if (strncmp(req, "CONNECT", 7) == 0)
return HTTP_CONNECT;
else if (strncmp(req, "HEAD", 4) == 0 ||
strncmp(req, "PUT", 3) == 0 ||
strncmp(req, "OPTIONS", 7) == 0 ||
strncmp(req, "MOVE", 4) == 0 ||
strncmp(req, "COPY", 4) == 0 ||
strncmp(req, "TRACE", 5) == 0 ||
strncmp(req, "DELETE", 6) == 0 ||
strncmp(req, "LINK", 4) == 0 ||
strncmp(req, "UNLINK", 6) == 0 ||
strncmp(req, "PATCH", 5) == 0 || strncmp(req, "WRAPPED", 7) == 0)
strncmp(req, "PUT", 3) == 0 ||
strncmp(req, "OPTIONS", 7) == 0 ||
strncmp(req, "MOVE", 4) == 0 ||
strncmp(req, "COPY", 4) == 0 ||
strncmp(req, "TRACE", 5) == 0 ||
strncmp(req, "DELETE", 6) == 0 ||
strncmp(req, "LINK", 4) == 0 ||
strncmp(req, "UNLINK", 6) == 0 ||
strncmp(req, "PATCH", 5) == 0 || strncmp(req, "WRAPPED", 7) == 0)
return HTTP_OTHERS;
else
return OTHER;
@ -246,6 +242,7 @@ void forward_header(int destination_sock)
rewrite_header();
int len = strlen(header_buffer);
send_data(destination_sock, header_buffer, len);
return;
}
// 代理中的完整URL转发前需改成 path 的形式
@ -270,6 +267,7 @@ void rewrite_header()
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);
}
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));
bzero(tmp, strlen(tmp));
strcat(tmp, host);
@ -319,7 +318,8 @@ char *splice_host_port(char *tmp, char *host, char *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);
if (http_firsts) {
@ -418,9 +418,13 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po
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) {
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 {
free(new_header_buffer);
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_remote_host = strlen(remote_host);
new_header_buffer = replace(new_header_buffer, &len, "[M]", 3, M, len_m);
new_header_buffer = 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);
new_header_buffer =
replace(new_header_buffer, &len, "[M]", 3, M, len_m);
new_header_buffer =
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)];
sprintf(port_copy, "%d", *remote_port);
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);
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, "\\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) {
int regrep_aim_len = strlen(p->http_strrep_aim);
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) {
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);
//p->http_port = *remote_port;
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;
}
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) {
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 {
free(new_header_buffer);
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_remote_host = strlen(remote_host);
new_header_buffer = replace(new_header_buffer, &len, "[M]", 3, M, len_m);
new_header_buffer = 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);
new_header_buffer =
replace(new_header_buffer, &len, "[M]", 3, M, len_m);
new_header_buffer =
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)];
sprintf(port_copy, "%d", *remote_port);
int len_remote_port = strlen(port_copy);
new_header_buffer = replace(new_header_buffer, &len, "[port]", 6, port_copy, len_remote_port);
char H[(len_remote_port + len_remote_host) +1];
new_header_buffer =
replace(new_header_buffer, &len, "[port]", 6, port_copy,
len_remote_port);
char H[(len_remote_port + len_remote_host) + 1];
splice_host_port(H, remote_host, port_copy);
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);
@ -577,11 +602,15 @@ int replacement_http_head(char *header_buffer, char *remote_host, int *remote_po
if (p->https_strrep) {
int regrep_aim_len = strlen(p->https_strrep_aim);
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) {
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);
return 1;
}

52
kill.c
View File

@ -2,7 +2,8 @@
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;
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));
}
int
kill_all(int signal, int name_count, char **namelist, struct passwd *pwent)
int kill_all(int signal, int name_count, char **namelist, struct passwd *pwent)
{
struct stat st;
NAMEINFO *name_info = NULL;
@ -407,32 +407,32 @@ kill_all(int signal, int name_count, char **namelist, struct passwd *pwent)
!= 0)
continue;
} else {
if (!name_info[j].st.st_dev) {
if (!match_process_name(comm, length, command, namelist[j],
name_info[j].name_length, got_long))
continue;
if (!name_info[j].st.st_dev) {
if (!match_process_name(comm, length, command, namelist[j],
name_info[j].name_length, got_long))
continue;
} else {
int ok = 1;
if (asprintf(&path, PROC_BASE "/%d/exe", pid_table[i]) < 0)
continue;
if (stat(path, &st) < 0)
ok = 0;
else if (name_info[j].st.st_dev != st.st_dev ||
name_info[j].st.st_ino != st.st_ino) {
size_t len = strlen(namelist[j]);
char *linkbuf = malloc(len + 1);
if (!linkbuf ||
readlink(path, linkbuf, len + 1) != (ssize_t) len ||
memcmp(namelist[j], linkbuf, len))
} else {
int ok = 1;
if (asprintf(&path, PROC_BASE "/%d/exe", pid_table[i]) < 0)
continue;
if (stat(path, &st) < 0)
ok = 0;
free(linkbuf);
else if (name_info[j].st.st_dev != st.st_dev ||
name_info[j].st.st_ino != st.st_ino) {
size_t len = strlen(namelist[j]);
char *linkbuf = malloc(len + 1);
if (!linkbuf ||
readlink(path, linkbuf, len + 1) != (ssize_t) len ||
memcmp(namelist[j], linkbuf, len))
ok = 0;
free(linkbuf);
}
free(path);
if (!ok)
continue;
}
free(path);
if (!ok)
continue;
}
}
found_name = j;
break;

2
kill.h
View File

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