修改自定义 _printf()
This commit is contained in:
2
Makefile
2
Makefile
@@ -2,7 +2,7 @@ CROSS_COMPILE ?=
|
|||||||
CC := $(CROSS_COMPILE)gcc
|
CC := $(CROSS_COMPILE)gcc
|
||||||
STRIP := $(CROSS_COMPILE)strip
|
STRIP := $(CROSS_COMPILE)strip
|
||||||
CFLAGS += -g -Wall -Iip2region
|
CFLAGS += -g -Wall -Iip2region
|
||||||
LIBS = -static
|
LIBS =
|
||||||
BIN := denyip
|
BIN := denyip
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
28
cap.c
28
cap.c
@@ -129,7 +129,7 @@ void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char
|
|||||||
|
|
||||||
// 如果 IP 地址已在缓存中且未过期,则跳过查询
|
// 如果 IP 地址已在缓存中且未过期,则跳过查询
|
||||||
if (is_ip_in_cache(src_ip)) {
|
if (is_ip_in_cache(src_ip)) {
|
||||||
printf(RED "IP:%s 已在缓存中,跳过查询\n" REDEND, src_ip);
|
_printf(RED "IP:%s 已在缓存中,跳过查询\n" REDEND, src_ip);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,13 +137,13 @@ void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char
|
|||||||
// 执行查询并添加到缓存
|
// 执行查询并添加到缓存
|
||||||
ip2region_area = ip2region("ip2region/ip2region.xdb", src_ip);
|
ip2region_area = ip2region("ip2region/ip2region.xdb", src_ip);
|
||||||
if (ip2region_area == NULL) {
|
if (ip2region_area == NULL) {
|
||||||
printf(RED "ip2region 解析地域错误\n" REDEND);
|
_printf(RED "ip2region 解析地域错误\n" REDEND);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 取环境变量
|
// 取环境变量
|
||||||
REGION_ENV = getenv("REGION");
|
REGION_ENV = getenv("REGION");
|
||||||
if (REGION_ENV != NULL) {
|
if (REGION_ENV != NULL) {
|
||||||
printf("REGION: %s\n", REGION_ENV);
|
_printf("REGION: %s\n", REGION_ENV);
|
||||||
strcpy(_REGION_LIST, REGION_ENV);
|
strcpy(_REGION_LIST, REGION_ENV);
|
||||||
} else {
|
} else {
|
||||||
strcpy(_REGION_LIST, "局域网 内网 中国 ");
|
strcpy(_REGION_LIST, "局域网 内网 中国 ");
|
||||||
@@ -160,7 +160,7 @@ void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char
|
|||||||
if (cache_size < MAX_CACHE_SIZE) // 缓存IP数不够预备设定值
|
if (cache_size < MAX_CACHE_SIZE) // 缓存IP数不够预备设定值
|
||||||
{
|
{
|
||||||
sleep(1);
|
sleep(1);
|
||||||
printf("缓存IP数 %d\n", cache_size);
|
_printf("缓存 IP 数 %d\n", cache_size);
|
||||||
}
|
}
|
||||||
command_result = _execute_command(ip_query_command);
|
command_result = _execute_command(ip_query_command);
|
||||||
if (command_result != NULL) {
|
if (command_result != NULL) {
|
||||||
@@ -168,9 +168,9 @@ void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char
|
|||||||
|
|
||||||
char *p = strstr(command_result, "中国");
|
char *p = strstr(command_result, "中国");
|
||||||
if (p == NULL) {
|
if (p == NULL) {
|
||||||
printf(RED "%s %s\n" REDEND, src_ip, command_result);
|
_printf(RED "%s %s\n" REDEND, src_ip, command_result);
|
||||||
r = add_ip_to_ipset(RULE_NAME, src_ip);
|
r = add_ip_to_ipset(RULE_NAME, src_ip);
|
||||||
printf("add_ip_to_ipset %d\n", r);
|
_printf("add_ip_to_ipset %d\n", r);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(command_result);
|
free(command_result);
|
||||||
@@ -216,7 +216,7 @@ void usage()
|
|||||||
void cleanup_(int signum)
|
void cleanup_(int signum)
|
||||||
{
|
{
|
||||||
|
|
||||||
printf("Received signal %d, cleaning up...\n", signum);
|
_printf("Received signal %d, cleaning up...\n", signum);
|
||||||
|
|
||||||
// 释放共享内存
|
// 释放共享内存
|
||||||
if (RULE_NAME != NULL) {
|
if (RULE_NAME != NULL) {
|
||||||
@@ -347,11 +347,11 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
while (1) {
|
while (1) {
|
||||||
|
|
||||||
printf("子进程当前 Ipset Rule 名 %s\n", RULE_NAME);
|
_printf("子进程当前 Ipset Rule 名 %s\n", RULE_NAME);
|
||||||
|
|
||||||
count = get_ip_count_in_ipset(RULE_NAME);
|
count = get_ip_count_in_ipset(RULE_NAME);
|
||||||
if (count >= 0) {
|
if (count >= 0) {
|
||||||
printf("IPSet %s 中的 IP 数量: %d\n", RULE_NAME, count);
|
_printf("IPSet %s 中的 IP 数量: %d\n", RULE_NAME, count);
|
||||||
if (count >= MAXIPSET && RULE_NAME_NUMBER <= MAXIPSET_RULT_NAME_NUM) // RULE_中的IP数量不超过MAXIPSET,并且集合不能超过 MAXIPSET_RULT_NAME_NUM 个
|
if (count >= MAXIPSET && RULE_NAME_NUMBER <= MAXIPSET_RULT_NAME_NUM) // RULE_中的IP数量不超过MAXIPSET,并且集合不能超过 MAXIPSET_RULT_NAME_NUM 个
|
||||||
{
|
{
|
||||||
RULE_NAME_NUMBER++;
|
RULE_NAME_NUMBER++;
|
||||||
@@ -359,7 +359,7 @@ int main(int argc, char **argv)
|
|||||||
snprintf(RULE_NAME, BUFFER, "root%d", RULE_NAME_NUMBER); // 更新规则名称
|
snprintf(RULE_NAME, BUFFER, "root%d", RULE_NAME_NUMBER); // 更新规则名称
|
||||||
// 创建新的 IPSet
|
// 创建新的 IPSet
|
||||||
if (create_ipset(RULE_NAME) != 0) {
|
if (create_ipset(RULE_NAME) != 0) {
|
||||||
printf("创建 IPSet %s 失败\n", RULE_NAME);
|
_printf("创建 IPSet %s 失败\n", RULE_NAME);
|
||||||
} else {
|
} else {
|
||||||
char iptables_command[256];
|
char iptables_command[256];
|
||||||
sprintf(iptables_command, "iptables -I INPUT -m set --match-set %s src -j DROP", RULE_NAME);
|
sprintf(iptables_command, "iptables -I INPUT -m set --match-set %s src -j DROP", RULE_NAME);
|
||||||
@@ -369,8 +369,8 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (RULE_NAME_NUMBER == MAXIPSET_RULT_NAME_NUM) {
|
if (RULE_NAME_NUMBER == MAXIPSET_RULT_NAME_NUM) {
|
||||||
printf("已达到最大规则数限制,停止创建新规则。\n");
|
_printf("已达到最大规则数限制,停止创建新规则。\n");
|
||||||
printf("请手动清理Ipset规则\n");
|
_printf("请手动清理Ipset规则\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -386,9 +386,9 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 打印可用设备列表
|
// 打印可用设备列表
|
||||||
printf("可用的设备:\n");
|
_printf("可用的设备:\n");
|
||||||
for (device = alldevs; device != NULL; device = device->next) {
|
for (device = alldevs; device != NULL; device = device->next) {
|
||||||
printf("设备: %s\n", device->name);
|
_printf("设备: %s\n", device->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 打开设备以进行数据包捕获
|
// 打开设备以进行数据包捕获
|
||||||
|
|||||||
25
common.c
25
common.c
@@ -22,9 +22,17 @@ void _printf(const char *format, ...)
|
|||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
|
|
||||||
// 打印到控制台
|
// 获取当前时间
|
||||||
vprintf(format, args);
|
time_t now = time(NULL);
|
||||||
va_end(args); // 结束对变参列表的处理
|
struct tm local_time;
|
||||||
|
localtime_r(&now, &local_time);
|
||||||
|
char time_str[20]; // YYYY-MM-DD HH:MM:SS 格式
|
||||||
|
strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", &local_time);
|
||||||
|
|
||||||
|
// 打印时间戳到控制台
|
||||||
|
printf("[%s] ", time_str);
|
||||||
|
vprintf(format, args); // 打印内容到控制台
|
||||||
|
va_end(args); // 结束对变参列表的处理
|
||||||
|
|
||||||
// 重新启动变参列表
|
// 重新启动变参列表
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
@@ -32,13 +40,6 @@ void _printf(const char *format, ...)
|
|||||||
// 打开日志文件(追加模式)
|
// 打开日志文件(追加模式)
|
||||||
FILE *log_file = fopen(PRINT_LOG_FILE, "a");
|
FILE *log_file = fopen(PRINT_LOG_FILE, "a");
|
||||||
if (log_file != NULL) {
|
if (log_file != NULL) {
|
||||||
// 获取当前时间
|
|
||||||
time_t now = time(NULL);
|
|
||||||
struct tm local_time;
|
|
||||||
localtime_r(&now, &local_time);
|
|
||||||
char time_str[20]; // YYYY-MM-DD HH:MM:SS 格式
|
|
||||||
strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", &local_time);
|
|
||||||
|
|
||||||
// 打印时间戳到日志文件
|
// 打印时间戳到日志文件
|
||||||
fprintf(log_file, "[%s] ", time_str);
|
fprintf(log_file, "[%s] ", time_str);
|
||||||
|
|
||||||
@@ -51,10 +52,10 @@ void _printf(const char *format, ...)
|
|||||||
perror("Unable to open log file");
|
perror("Unable to open log file");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
va_end(args); // 结束对变参列表的处理
|
||||||
va_end(args); // 结束对变参列表的处理
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void split_string(char string[], char delims[], char (*whitelist_ip)[WHITELIST_IP_NUM])
|
void split_string(char string[], char delims[], char (*whitelist_ip)[WHITELIST_IP_NUM])
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user