From 1361b15f797ba6ede3480cebc81f54f44f6e8898 Mon Sep 17 00:00:00 2001 From: aixiao Date: Wed, 23 Jul 2025 15:00:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache.c | 3 --- cap.c | 26 +++++++++++++------------- libipset.c | 2 -- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/cache.c b/cache.c index 6bf9439..d617d5e 100644 --- a/cache.c +++ b/cache.c @@ -88,9 +88,6 @@ void free_ip_cache() cache_size = 0; } - - - char cn_ip[MAXIPSET_][MAXIPLEN] = { 0 }; // 添加一个 IP 到集合(如果已存在则不添加) diff --git a/cap.c b/cap.c index 81ad72f..02c071a 100644 --- a/cap.c +++ b/cap.c @@ -43,7 +43,7 @@ void Processing_IP_addresses(char *src_ip) } if (1 == is_ip_in_set(cn_ip, src_ip)) { - _printf(RED "IP:%s 已经标记为国内,跳过!!!\n" REDEND, src_ip); + _printf(RED "IP:%s 已经标记为国内,跳过!\n" REDEND, src_ip); for (int i = 0; i < MAXIPSET_; i++) { if (cn_ip[i][0] != '\0') { @@ -96,11 +96,11 @@ void Processing_IP_addresses(char *src_ip) if (NULL == strstr(response.continent_country, "中国")) { // 这时是国外IP _printf(RED "CurlGetIpArea(): %s %s\r\n" REDEND, src_ip, response.continent_country); add_ip_to_ipset(RULE_NAME, src_ip); - } else { // 这时是国内IP - if (-1 == add_cn_ip(cn_ip, src_ip)) { // 添加国内IP到缓存 + } else { // 这时是国内IP + if (-1 == add_cn_ip(cn_ip, src_ip)) { // 添加国内IP到缓存 _printf(RED "add_cn_ip() Error!!! 错误:集合已满\n" REDEND); } - _printf("IP: %s 离线库为国外, API 判断为国内, 标记为已处理!!!\n", src_ip); + _printf("IP: %s 离线库为国外, API 判断为国内, 标记为已处理!\n", src_ip); if (append_string_to_file("cn.txt", src_ip) != 0) { _printf("append_string_to_file() Error!!!\n"); @@ -115,13 +115,11 @@ void Processing_IP_addresses(char *src_ip) } } - if (ip2region_area != NULL) { free(ip2region_area); ip2region_area = NULL; } - // 实时检测内存泄漏 dump_mem_leak(); return; @@ -151,7 +149,7 @@ void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char // src_ip: 存储转换后字符串的缓冲区 // INET_ADDRSTRLEN: 缓冲区的长度 inet_ntop(AF_INET, &(ip_header->ip_src), src_ip, INET_ADDRSTRLEN); - + Processing_IP_addresses(src_ip); return; @@ -327,6 +325,9 @@ int main(int argc, char **argv) remove_char(local_addr, '\n'); printf("Local Address: %s\n", local_addr); + strcpy(cn_ip[line_count], local_addr); + line_count++; + printf("Read %d lines from file:\n", line_count); for (int i = 0; i < line_count; i++) { printf("Line %d: %s\n", i + 1, cn_ip[i]); @@ -360,11 +361,11 @@ int main(int argc, char **argv) _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); - system(iptables_command); - */ - add_iptables_rule(RULE_NAME); + char iptables_command[256]; + sprintf(iptables_command, "iptables -I INPUT -m set --match-set %s src -j DROP", RULE_NAME); + system(iptables_command); + */ + add_iptables_rule(RULE_NAME); } } @@ -431,7 +432,6 @@ int main(int argc, char **argv) pcap_freecode(&fp); pcap_freealldevs(alldevs); // 释放设备列表 pcap_close(handle); // 关闭会话句柄 - return 0; } diff --git a/libipset.c b/libipset.c index 35d24bd..529785b 100644 --- a/libipset.c +++ b/libipset.c @@ -208,7 +208,6 @@ int get_ip_count_in_ipset(char *set_name) return ip_count; } - void add_iptables_rule(const char *rule_name) { char iptables_command[256]; @@ -221,7 +220,6 @@ void add_iptables_rule(const char *rule_name) fprintf(stderr, "Error: iptables command is too long.\n"); return; } - // 执行命令并检查返回值 int ret = system(iptables_command); if (ret != 0) {