增加libcurl库调用百度API,与原来的的go命令执行调用API两种。libcurl涉及静态链接,考虑使用哪种?
This commit is contained in:
55
cap.c
55
cap.c
@@ -1,6 +1,7 @@
|
||||
#include "cap.h"
|
||||
#include "common.h"
|
||||
#include "libipset.h"
|
||||
#include "libcurl.h"
|
||||
|
||||
|
||||
pcap_if_t *alldevs, *device;
|
||||
@@ -17,7 +18,7 @@ char *RULE_NAME = NULL; // 共享内存
|
||||
char *ip2region_area = NULL; // ip2region 解析结果
|
||||
char *command_result = NULL; // 执行命令的结果
|
||||
|
||||
#define CACHE_TTL 600 // 设定缓存的存活时间为 600 秒 (10 分钟)
|
||||
#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 数量
|
||||
@@ -125,6 +126,12 @@ void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char
|
||||
char _REGION_LIST[BUFFER] = { 0 };
|
||||
const char *REGION_ENV = NULL;
|
||||
|
||||
char ipset_query_command[256] = { 0 };
|
||||
char ip_query_command[256] = { 0 };
|
||||
|
||||
// 定义 Response 结构体
|
||||
Response response;
|
||||
|
||||
int r = 0;
|
||||
//char *t = _time();
|
||||
|
||||
@@ -138,6 +145,12 @@ void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果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);
|
||||
return;
|
||||
}
|
||||
|
||||
// 执行查询并添加到缓存
|
||||
ip2region_area = ip2region("ip2region/ip2region.xdb", src_ip);
|
||||
@@ -145,6 +158,7 @@ void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char
|
||||
_printf(RED "ip2region 解析地域错误\n" REDEND);
|
||||
return;
|
||||
}
|
||||
|
||||
// 取环境变量
|
||||
REGION_ENV = getenv("REGION");
|
||||
if (REGION_ENV != NULL) {
|
||||
@@ -158,15 +172,43 @@ void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char
|
||||
if (isregion(ip2region_area, _region_list) == 1) { // 返回1表示在白名单列表
|
||||
;
|
||||
} else {
|
||||
//puts(ip2region_area);
|
||||
|
||||
char ip_query_command[256] = { 0 };
|
||||
snprintf(ip_query_command, sizeof(ip_query_command), "./IP_region_query/ipquery %s", src_ip);
|
||||
|
||||
if (cache_size < MAX_CACHE_SIZE) // 缓存IP数不够预备设定值
|
||||
{
|
||||
sleep(1);
|
||||
_printf("缓存 IP 数 %d\n", cache_size);
|
||||
}
|
||||
|
||||
|
||||
char *p = curl_get_area(src_ip);
|
||||
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);
|
||||
}
|
||||
|
||||
} else {
|
||||
fprintf(stderr, "Failed to parse JSON.\n");
|
||||
}
|
||||
|
||||
free(p);
|
||||
|
||||
|
||||
/*
|
||||
//go 执行命令方法
|
||||
snprintf(ip_query_command, sizeof(ip_query_command), "./IP_region_query/ipquery %s", src_ip);
|
||||
command_result = _execute_command(ip_query_command);
|
||||
if (command_result != NULL) {
|
||||
add_ip_to_cache(src_ip); // 添加 IP 到缓存
|
||||
@@ -186,6 +228,7 @@ void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char
|
||||
|
||||
if (command_result != NULL)
|
||||
free(command_result);
|
||||
*/
|
||||
}
|
||||
|
||||
if (ip2region_area != NULL)
|
||||
@@ -389,7 +432,7 @@ int main(int argc, char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
sleep(3); // 每 3 秒检查一次
|
||||
sleep(9); // 每 3 秒检查一次
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user