20250423
This commit is contained in:
34
cap.c
34
cap.c
@@ -4,26 +4,22 @@
|
||||
#include "libcurl.h"
|
||||
#include "cache.h"
|
||||
|
||||
|
||||
pcap_if_t *alldevs, *device;
|
||||
pcap_t *handle; // 会话句柄
|
||||
pcap_if_t *alldevs, *device;
|
||||
pcap_t *handle; // 会话句柄
|
||||
struct bpf_program fp; // 编译后的过滤器
|
||||
|
||||
pid_t pid = -1; // 子进程全局PID
|
||||
#define SHM_SIZE 1024 // 共享内存大小
|
||||
#define SHM_KEY 0124 // 共享内存键值
|
||||
int shmid = -1;
|
||||
int RULE_NAME_NUMBER = 0; // ipset 集合集合数
|
||||
char *RULE_NAME = NULL; // 共享内存
|
||||
|
||||
|
||||
char *ip2region_area = NULL; // ip2region 解析结果
|
||||
char *command_result = NULL; // 执行命令的结果
|
||||
int shmid = -1;
|
||||
int RULE_NAME_NUMBER = 0; // ipset 集合集合数
|
||||
char *RULE_NAME = NULL; // 共享内存
|
||||
|
||||
char *ip2region_area = NULL; // ip2region 解析结果
|
||||
char *command_result = NULL; // 执行命令的结果
|
||||
|
||||
void Processing_IP_addresses(char *src_ip)
|
||||
{
|
||||
|
||||
// 地域白名单
|
||||
char _region_list[WHITELIST_IP_NUM][WHITELIST_IP_NUM] = { { 0 }, { 0 } };
|
||||
char _REGION_LIST[BUFFER] = { 0 };
|
||||
@@ -53,8 +49,9 @@ void Processing_IP_addresses(char *src_ip)
|
||||
printf("%s ", cn_ip[i]);
|
||||
}
|
||||
}
|
||||
if (cn_ip_len(cn_ip) >= 1024) { // 清理集合
|
||||
if (cn_ip_len(cn_ip) >= 10240) { // 清理集合
|
||||
clear_ip_set(cn_ip);
|
||||
truncate_file("cn.txt"); // 清空文件
|
||||
}
|
||||
|
||||
printf("cn_ip_len(cn_ip): %d\n", cn_ip_len(cn_ip));
|
||||
@@ -97,8 +94,10 @@ void Processing_IP_addresses(char *src_ip)
|
||||
if (NULL == strstr(response.continent_country, "中国")) { // 这时是国外IP
|
||||
_printf(RED "CurlGetIpArea(): %s %s\n" REDEND, src_ip, response.continent_country);
|
||||
add_ip_to_ipset(RULE_NAME, src_ip);
|
||||
} else { // 这时是国内IP
|
||||
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);
|
||||
|
||||
if (append_string_to_file("cn.txt", src_ip) != 0) {
|
||||
@@ -119,7 +118,7 @@ void Processing_IP_addresses(char *src_ip)
|
||||
ip2region_area = NULL;
|
||||
}
|
||||
|
||||
return ;
|
||||
return;
|
||||
}
|
||||
|
||||
// 回调函数,在捕获到每个数据包时调用
|
||||
@@ -128,12 +127,11 @@ void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char
|
||||
int ethernet_header_len = 14;
|
||||
//struct ip *ip_header = (struct ip *)(packet + ethernet_header_len);
|
||||
struct ip *ip_header = (struct ip *)(packet + ethernet_header_len);
|
||||
if (ip_header->ip_v != 4) return; // 只处理 IPv4
|
||||
if (ip_header->ip_v != 4)
|
||||
return; // 只处理 IPv4
|
||||
|
||||
char src_ip[INET_ADDRSTRLEN] = { 0 };
|
||||
|
||||
|
||||
|
||||
inet_ntop(AF_INET, &(ip_header->ip_src), src_ip, INET_ADDRSTRLEN);
|
||||
Processing_IP_addresses(src_ip);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user