This commit is contained in:
sushen339
2025-11-18 14:43:00 +08:00
parent 07f8e59945
commit 17c495b9d4
4 changed files with 44 additions and 38 deletions
BIN
View File
Binary file not shown.
+1
View File
@@ -2,6 +2,7 @@
#define INSTALL_H #define INSTALL_H
#include "common.h" #include "common.h"
#include <sys/stat.h>
/* 安装服务 */ /* 安装服务 */
int install_service(void); int install_service(void);
+19 -9
View File
@@ -116,19 +116,29 @@ int install_service(void) {
if (len != -1) { if (len != -1) {
exe_path[len] = '\0'; exe_path[len] = '\0';
char copy_cmd[MAX_COMMAND_LEN]; FILE *src = fopen(exe_path, "rb");
snprintf(copy_cmd, sizeof(copy_cmd), "cp -f %s %s && chmod +x %s", FILE *dst = fopen(INSTALL_PATH, "wb");
exe_path, INSTALL_PATH, INSTALL_PATH); if (src && dst) {
system(copy_cmd); char buf[8192];
size_t n;
while ((n = fread(buf, 1, sizeof(buf), src)) > 0) {
fwrite(buf, 1, n, dst);
}
fclose(src);
fclose(dst);
chmod(INSTALL_PATH, 0755);
} else {
if (src) fclose(src);
if (dst) fclose(dst);
}
} }
/* 创建必要的目录和文件 */ /* 创建必要的目录和文件 */
char mkdir_cmd[MAX_COMMAND_LEN]; mkdir(CONFIG_DIR, 0700);
snprintf(mkdir_cmd, sizeof(mkdir_cmd), "mkdir -p %s && chmod 700 %s", CONFIG_DIR, CONFIG_DIR); chmod(CONFIG_DIR, 0700);
system(mkdir_cmd);
snprintf(mkdir_cmd, sizeof(mkdir_cmd), "mkdir -p %s && chmod 770 %s", RECORD_DIR, RECORD_DIR); mkdir(RECORD_DIR, 0770);
system(mkdir_cmd); chmod(RECORD_DIR, 0770);
FILE *fp = fopen(PERSIST_FILE, "a"); FILE *fp = fopen(PERSIST_FILE, "a");
if (fp) { if (fp) {
+22 -27
View File
@@ -81,6 +81,20 @@ void show_active_bans(void) {
printf("\n"); printf("\n");
} }
/* 检查段idx是否会被更精确的段取代(相同count但更小mask) */
static inline bool is_agg_replaced(void *agg_array, int agg_count, int idx) {
struct agg_entry { char subnet[64]; int count; int mask; };
struct agg_entry *agg = (struct agg_entry *)agg_array;
for (int j = 0; j < agg_count; ++j) {
if (agg[j].mask > agg[idx].mask && agg[j].count == agg[idx].count &&
strncmp(agg[j].subnet, agg[idx].subnet, strlen(agg[idx].subnet)) == 0) {
return true;
}
}
return false;
}
void show_subnet_aggregation(void) { void show_subnet_aggregation(void) {
msg(C_CYAN, "=== 📊 攻击源聚合统计 (自动识别 IP 段) ==="); msg(C_CYAN, "=== 📊 攻击源聚合统计 (自动识别 IP 段) ===");
@@ -192,40 +206,22 @@ void show_subnet_aggregation(void) {
int aggregated_count = 0; int aggregated_count = 0;
for (int i = 0; i < agg_count && show_count < 10; ++i) { for (int i = 0; i < agg_count && show_count < 10; ++i) {
if (agg[i].count < 2) continue; if (agg[i].count < 2 || is_agg_replaced(agg, agg_count, i)) {
continue;
/* 检查是否被更精确的段取代(相同count但更小mask) */
bool replaced = false;
for (int j = 0; j < agg_count; ++j) {
if (agg[j].mask > agg[i].mask && agg[j].count == agg[i].count &&
strncmp(agg[j].subnet, agg[i].subnet, strlen(agg[i].subnet)) == 0) {
replaced = true;
break;
} }
}
if (replaced) continue;
has_output = true; has_output = true;
/* 检查是否被已显示的更大段覆盖,避免重复计数 */ /* 检查是否被已显示的更大段覆盖,避免重复计数 */
bool covered = false; bool covered = false;
for (int k = 0; k < i; ++k) { for (int k = 0; k < i; ++k) {
if (agg[k].count >= 2 && agg[k].mask < agg[i].mask) { if (agg[k].count >= 2 && agg[k].mask < agg[i].mask &&
/* 检查k是否也被取代 */ !is_agg_replaced(agg, agg_count, k) &&
bool k_replaced = false; strncmp(agg[i].subnet, agg[k].subnet, strlen(agg[k].subnet)) == 0) {
for (int m = 0; m < agg_count; ++m) {
if (agg[m].mask > agg[k].mask && agg[m].count == agg[k].count &&
strncmp(agg[m].subnet, agg[k].subnet, strlen(agg[k].subnet)) == 0) {
k_replaced = true;
break;
}
}
if (!k_replaced && strncmp(agg[i].subnet, agg[k].subnet, strlen(agg[k].subnet)) == 0) {
covered = true; covered = true;
break; break;
} }
} }
}
if (!covered) { if (!covered) {
aggregated_count += agg[i].count; aggregated_count += agg[i].count;
@@ -256,14 +252,13 @@ void show_subnet_aggregation(void) {
} }
int scattered_count = total_ipv4 - aggregated_count; int scattered_count = total_ipv4 - aggregated_count;
if (!has_output) { /* 显示散乱IP */
if (scattered_count > 0 || (!has_output && total_ipv4 > 0)) {
if (scattered_count > 0) { if (scattered_count > 0) {
printf(" - (散乱 IPv4) (%d 个)\n", scattered_count); printf(" - (散乱 IPv4) (%d 个)\n", scattered_count);
} else if (total_ipv4 > 0) { } else {
printf(" - (散乱 IPv4)\n"); printf(" - (散乱 IPv4)\n");
} }
} else if (scattered_count > 0) {
printf(" - (散乱 IPv4) (%d 个)\n", scattered_count);
} }
if (v6_count > 0) { if (v6_count > 0) {