Add leak_detector_c

This commit is contained in:
2025-04-24 10:01:18 +08:00
parent 1c28d55681
commit 6a4fb872f7
13 changed files with 337 additions and 9 deletions

21
cap.c
View File

@@ -3,6 +3,7 @@
#include "libipset.h"
#include "libcurl.h"
#include "cache.h"
#include "leak_detector_c/leak_detector_c.h"
pcap_if_t *alldevs, *device;
pcap_t *handle; // 会话句柄
@@ -37,7 +38,7 @@ void Processing_IP_addresses(char *src_ip)
// 如果ipset规则已经存在则跳过查询
snprintf(ipset_query_command, sizeof(ipset_query_command), "ipset test %s %s 2>/dev/null", RULE_NAME, src_ip);
if (system(ipset_query_command) == 0) {
// _printf(RED "Ipset 规则内已经存在 %s\n" REDEND, src_ip);
_printf(RED "Ipset 规则内已经存在 %s\n" REDEND, src_ip);
return;
}
@@ -92,10 +93,10 @@ void Processing_IP_addresses(char *src_ip)
if (parse_json_to_struct(p, &response) == 0) { // 解析 JSON 到结构体
if (NULL == strstr(response.continent_country, "中国")) { // 这时是国外IP
_printf(RED "CurlGetIpArea(): %s %s\n" REDEND, src_ip, response.continent_country);
_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);
@@ -113,11 +114,14 @@ void Processing_IP_addresses(char *src_ip)
}
}
if (ip2region_area != NULL) {
free(ip2region_area);
ip2region_area = NULL;
}
dump_mem_leak();
return;
}
@@ -199,7 +203,10 @@ void cleanup_(int signum)
int main(int argc, char **argv)
{
atexit(report_mem_leak);
// 注册 SIGTERM 信号处理函数
signal(SIGINT, cleanup_);
signal(SIGTERM, cleanup_);
int opt;
@@ -337,9 +344,12 @@ int main(int argc, char **argv)
if (create_ipset(RULE_NAME) != 0) {
_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);
}
}
@@ -349,7 +359,7 @@ int main(int argc, char **argv)
}
}
sleep(3); // 每 3 秒检查一次
sleep(7); // 每 3 秒检查一次
}
}
@@ -406,6 +416,7 @@ int main(int argc, char **argv)
pcap_freecode(&fp);
pcap_freealldevs(alldevs); // 释放设备列表
pcap_close(handle); // 关闭会话句柄
return 0;
}