This commit is contained in:
+32
-33
@@ -140,61 +140,60 @@ void show_subnet_aggregation(void) {
|
|||||||
|
|
||||||
void show_country_stats(void) {
|
void show_country_stats(void) {
|
||||||
msg(C_CYAN, "=== 🌍 攻击源国家/地区统计 ===");
|
msg(C_CYAN, "=== 🌍 攻击源国家/地区统计 ===");
|
||||||
|
|
||||||
FILE *fp = fopen(PERSIST_FILE, "r");
|
FILE *fp = fopen(PERSIST_FILE, "r");
|
||||||
if (!fp) {
|
if (!fp) {
|
||||||
printf("(暂无数据)\n\n");
|
printf("(暂无数据)\n\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
struct { char code[MAX_COUNTRY_CODE]; int count; } stats[128];
|
||||||
/* 创建临时文件存储国家代码 */
|
int stat_count = 0;
|
||||||
char temp_country_file[MAX_PATH_LEN];
|
|
||||||
snprintf(temp_country_file, sizeof(temp_country_file), "/tmp/blockip_country_$$");
|
|
||||||
FILE *temp_fp = fopen(temp_country_file, "w");
|
|
||||||
|
|
||||||
char line[MAX_LINE_LEN];
|
char line[MAX_LINE_LEN];
|
||||||
bool has_data = false;
|
bool has_data = false;
|
||||||
|
|
||||||
while (fgets(line, sizeof(line), fp)) {
|
while (fgets(line, sizeof(line), fp)) {
|
||||||
line[strcspn(line, "\n")] = 0;
|
line[strcspn(line, "\n")] = 0;
|
||||||
|
|
||||||
char *pipe = strchr(line, '|');
|
char *pipe = strchr(line, '|');
|
||||||
if (pipe && strlen(pipe + 1) > 0) {
|
if (pipe && strlen(pipe + 1) > 0) {
|
||||||
fprintf(temp_fp, "%s\n", pipe + 1);
|
|
||||||
has_data = true;
|
has_data = true;
|
||||||
|
char *code = pipe + 1;
|
||||||
|
int found = 0;
|
||||||
|
for (int i = 0; i < stat_count; ++i) {
|
||||||
|
if (strcmp(stats[i].code, code) == 0) {
|
||||||
|
stats[i].count++;
|
||||||
|
found = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found && stat_count < 128) {
|
||||||
|
strncpy(stats[stat_count].code, code, MAX_COUNTRY_CODE-1);
|
||||||
|
stats[stat_count].code[MAX_COUNTRY_CODE-1] = 0;
|
||||||
|
stats[stat_count].count = 1;
|
||||||
|
stat_count++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
fclose(temp_fp);
|
|
||||||
|
|
||||||
if (!has_data) {
|
if (!has_data) {
|
||||||
printf("(暂无国家信息)\n\n");
|
printf("(暂无国家信息)\n\n");
|
||||||
remove(temp_country_file);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// 排序
|
||||||
/* 统计国家分布 */
|
for (int i = 0; i < stat_count-1; ++i) {
|
||||||
char command[MAX_COMMAND_LEN];
|
for (int j = i+1; j < stat_count; ++j) {
|
||||||
snprintf(command, sizeof(command),
|
if (stats[j].count > stats[i].count) {
|
||||||
"sort %s | uniq -c | sort -rn | head -n 9",
|
char tmp_code[MAX_COUNTRY_CODE];
|
||||||
temp_country_file);
|
int tmp_count = stats[i].count;
|
||||||
|
strcpy(tmp_code, stats[i].code);
|
||||||
fp = popen(command, "r");
|
stats[i].count = stats[j].count;
|
||||||
if (fp) {
|
strncpy(stats[i].code, stats[j].code, MAX_COUNTRY_CODE);
|
||||||
while (fgets(line, sizeof(line), fp)) {
|
stats[j].count = tmp_count;
|
||||||
int count;
|
strncpy(stats[j].code, tmp_code, MAX_COUNTRY_CODE);
|
||||||
char code[MAX_COUNTRY_CODE];
|
|
||||||
|
|
||||||
if (sscanf(line, "%d %s", &count, code) == 2) {
|
|
||||||
printf(" - %s %s(%d 个)%s\n",
|
|
||||||
get_country_name(code), C_RED, count, C_RESET);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pclose(fp);
|
|
||||||
}
|
}
|
||||||
|
int show_n = stat_count < 9 ? stat_count : 9;
|
||||||
remove(temp_country_file);
|
for (int i = 0; i < show_n; ++i) {
|
||||||
|
printf(" - %s %s(%d 个)%s\n", get_country_name(stats[i].code), C_RED, stats[i].count, C_RESET);
|
||||||
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user