diff --git a/Makefile b/Makefile index 9094130..b89b615 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CROSS_COMPILE ?= CC := $(CROSS_COMPILE)gcc STRIP := $(CROSS_COMPILE)strip CFLAGS += -g -Wall -Iip2region -LIBS = -static +LIBS = BIN := denyip diff --git a/cap.c b/cap.c index 5e8897e..48f83ba 100644 --- a/cap.c +++ b/cap.c @@ -129,7 +129,7 @@ void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char // 如果 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; } @@ -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); if (ip2region_area == NULL) { - printf(RED "ip2region 解析地域错误\n" REDEND); + _printf(RED "ip2region 解析地域错误\n" REDEND); return; } // 取环境变量 REGION_ENV = getenv("REGION"); if (REGION_ENV != NULL) { - printf("REGION: %s\n", REGION_ENV); + _printf("REGION: %s\n", REGION_ENV); strcpy(_REGION_LIST, REGION_ENV); } else { 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数不够预备设定值 { sleep(1); - printf("缓存IP数 %d\n", cache_size); + _printf("缓存 IP 数 %d\n", cache_size); } command_result = _execute_command(ip_query_command); 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, "中国"); 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); - printf("add_ip_to_ipset %d\n", r); + _printf("add_ip_to_ipset %d\n", r); } free(command_result); @@ -216,7 +216,7 @@ void usage() void cleanup_(int signum) { - printf("Received signal %d, cleaning up...\n", signum); + _printf("Received signal %d, cleaning up...\n", signum); // 释放共享内存 if (RULE_NAME != NULL) { @@ -347,11 +347,11 @@ int main(int argc, char **argv) } while (1) { - printf("子进程当前 Ipset Rule 名 %s\n", RULE_NAME); + _printf("子进程当前 Ipset Rule 名 %s\n", RULE_NAME); count = get_ip_count_in_ipset(RULE_NAME); 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 个 { RULE_NAME_NUMBER++; @@ -359,7 +359,7 @@ int main(int argc, char **argv) snprintf(RULE_NAME, BUFFER, "root%d", RULE_NAME_NUMBER); // 更新规则名称 // 创建新的 IPSet if (create_ipset(RULE_NAME) != 0) { - printf("创建 IPSet %s 失败\n", RULE_NAME); + _printf("创建 IPSet %s 失败\n", RULE_NAME); } else { char iptables_command[256]; 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) { - printf("已达到最大规则数限制,停止创建新规则。\n"); - printf("请手动清理Ipset规则\n"); + _printf("已达到最大规则数限制,停止创建新规则。\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) { - printf("设备: %s\n", device->name); + _printf("设备: %s\n", device->name); } // 打开设备以进行数据包捕获 diff --git a/cap.o b/cap.o index 9c3cab1..8fdfa89 100644 Binary files a/cap.o and b/cap.o differ diff --git a/common.c b/common.c index 725bdf8..125c259 100644 --- a/common.c +++ b/common.c @@ -22,9 +22,17 @@ void _printf(const char *format, ...) va_list args; va_start(args, format); - // 打印到控制台 - vprintf(format, args); - va_end(args); // 结束对变参列表的处理 + // 获取当前时间 + 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); + + // 打印时间戳到控制台 + printf("[%s] ", time_str); + vprintf(format, args); // 打印内容到控制台 + va_end(args); // 结束对变参列表的处理 // 重新启动变参列表 va_start(args, format); @@ -32,13 +40,6 @@ void _printf(const char *format, ...) // 打开日志文件(追加模式) FILE *log_file = fopen(PRINT_LOG_FILE, "a"); 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); @@ -51,10 +52,10 @@ void _printf(const char *format, ...) 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]) { int i = 0; diff --git a/common.o b/common.o index 3035047..1ae8024 100644 Binary files a/common.o and b/common.o differ diff --git a/denyip b/denyip index 438710a..1a57f99 100644 Binary files a/denyip and b/denyip differ