稍微优化

This commit is contained in:
2025-01-15 15:30:12 +08:00
parent 3b902877b5
commit a79a59bd97
3 changed files with 26 additions and 8 deletions

17
cap.c
View File

@@ -20,8 +20,8 @@ char *command_result = NULL; // 执行命令的结果
#define CACHE_TTL 180 // 设定缓存的存活时间为 600 秒 (10 分钟)
#define MAX_CACHE_SIZE 100 // 缓存最多存储 100 个 IP 地址
struct ip_cache_node *ip_cache_head = NULL; // 缓存链表的头节点
int cache_size = 0; // 当前缓存中的 IP 数量
struct ip_cache_node *ip_cache_head = NULL; // 缓存链表的头节点
int cache_size = 0; // 当前缓存中的 IP 数量
// 定义链表结构,用于缓存 IP 地址
struct ip_cache_node {
@@ -127,7 +127,7 @@ void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char
const char *REGION_ENV = NULL;
char ipset_query_command[256] = { 0 };
char ip_query_command[256] = { 0 };
//char ip_query_command[256] = { 0 };
// 定义 Response 结构体
Response response;
@@ -181,22 +181,23 @@ void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char
char *p = curl_get_area(src_ip);
puts(p);
//puts(p);
if (parse_json_to_struct(p, &response) == 0) { // 解析 JSON 到结构体
// 输出解析结果
/*
printf("Code: %s\n", response.code);
printf("IP: %s\n", response.ip);
printf("Continent: %s\n", response.data.continent);
printf("Country: %s\n", response.data.country);
printf("ISP: %s\n", response.data.isp);
printf("Region: %s\n", response.data.region);
*/
char *p1 = strstr(response.continent_country, "中国");
if (p1 == NULL) {
_printf(RED "%s %s\n" REDEND, src_ip, response.continent_country);
r = add_ip_to_ipset(RULE_NAME, src_ip);
_printf("add_ip_to_ipset() return %d\n", r);
//_printf("add_ip_to_ipset() return %d\n", r);
}
} else {
@@ -405,11 +406,11 @@ int main(int argc, char **argv)
while (1)
{
_printf("子进程当前 Ipset Rule 名 %s\n", RULE_NAME);
//_printf("子进程当前 Ipset Rule 名 %s\n", RULE_NAME);
count = get_ip_count_in_ipset(RULE_NAME);
if (count >= 0) {
_printf("IPSet %s 中的 IP 数量: %d\n", RULE_NAME, count);
_printf("子进程当前 Ipset Rule 名 %s, 数量: %d\n", RULE_NAME, count);
if (count >= MAXIPSET && RULE_NAME_NUMBER <= MAXIPSET_RULT_NAME_NUM) // RULE_中的IP数量不超过MAXIPSET并且集合不能超过 MAXIPSET_RULT_NAME_NUM 个
{
RULE_NAME_NUMBER++;

BIN
ip2region/a.out Normal file

Binary file not shown.

17
ip2region/a.out.valgrind Normal file
View File

@@ -0,0 +1,17 @@
root@NIUYULING:/mnt/c/Users/root/Desktop/git.aixiao.me/DenyIP/ip2region# /usr/bin/valgrind --tool=memcheck --leak-check=full --show-reachable=yes --main-stacksize=8388608 --track-origins=yes ./a.out
==239839== Memcheck, a memory error detector
==239839== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==239839== Using Valgrind-3.20.0 and LibVEX; rerun with -h for copyright info
==239839== Command: ./a.out
==239839==
澳大利亚|0|0|0|0
==239839==
==239839== HEAP SUMMARY:
==239839== in use at exit: 0 bytes in 0 blocks
==239839== total heap usage: 7 allocs, 7 frees, 534,473 bytes allocated
==239839==
==239839== All heap blocks were freed -- no leaks are possible
==239839==
==239839== For lists of detected and suppressed errors, rerun with: -s
==239839== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
root@NIUYULING:/mnt/c/Users/root/Desktop/git.aixiao.me/DenyIP/ip2region#