This commit is contained in:
2025-07-23 15:00:23 +08:00
parent 1440451f8a
commit 1361b15f79
3 changed files with 13 additions and 18 deletions

View File

@@ -88,9 +88,6 @@ void free_ip_cache()
cache_size = 0;
}
char cn_ip[MAXIPSET_][MAXIPLEN] = { 0 };
// 添加一个 IP 到集合(如果已存在则不添加)

26
cap.c
View File

@@ -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;
}

View File

@@ -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) {