优化:
添加主要处理函数Processing_IP_addresses(src_ip); 数据处理放到cache.c 暂时未发现Bug
This commit is contained in:
163
cap.c
163
cap.c
@@ -10,131 +10,27 @@ 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; // 执行命令的结果
|
||||
|
||||
#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 数量
|
||||
|
||||
// 定义链表结构,用于缓存 IP 地址
|
||||
struct ip_cache_node {
|
||||
char ip[INET_ADDRSTRLEN]; // 存储 IP 地址
|
||||
time_t timestamp; // 记录缓存时间
|
||||
struct ip_cache_node *next; // 指向下一个节点
|
||||
};
|
||||
|
||||
|
||||
// 将新 IP 添加到缓存,若缓存过大则移除最早的 IP
|
||||
void add_ip_to_cache(const char *ip)
|
||||
void Processing_IP_addresses(char *src_ip)
|
||||
{
|
||||
// 如果缓存大小超过限制,移除最早的 IP
|
||||
if (cache_size >= MAX_CACHE_SIZE) {
|
||||
struct ip_cache_node *current = ip_cache_head;
|
||||
struct ip_cache_node *prev = NULL;
|
||||
|
||||
// 找到链表的最后一个节点
|
||||
while (current->next != NULL) {
|
||||
prev = current;
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
// 移除最后一个节点(最早的 IP)
|
||||
if (prev != NULL) {
|
||||
prev->next = NULL;
|
||||
} else {
|
||||
ip_cache_head = NULL;
|
||||
}
|
||||
free(current);
|
||||
cache_size--;
|
||||
}
|
||||
|
||||
// 创建新的缓存节点并添加到链表头部
|
||||
struct ip_cache_node *new_node = (struct ip_cache_node *)malloc(sizeof(struct ip_cache_node));
|
||||
if (new_node == NULL) {
|
||||
perror("malloc");
|
||||
return;
|
||||
}
|
||||
strncpy(new_node->ip, ip, INET_ADDRSTRLEN);
|
||||
new_node->timestamp = time(NULL); // 记录当前时间
|
||||
new_node->next = ip_cache_head;
|
||||
ip_cache_head = new_node;
|
||||
cache_size++;
|
||||
}
|
||||
|
||||
// 检查 IP 是否已在缓存中并是否过期
|
||||
int is_ip_in_cache(const char *ip)
|
||||
{
|
||||
time_t now = time(NULL); // 获取当前时间
|
||||
struct ip_cache_node *current = ip_cache_head;
|
||||
struct ip_cache_node *prev = NULL;
|
||||
|
||||
while (current != NULL) {
|
||||
// 如果 IP 匹配并且未过期
|
||||
if (strcmp(current->ip, ip) == 0) {
|
||||
if (now - current->timestamp <= CACHE_TTL) {
|
||||
return 1; // IP 在缓存中,且未过期
|
||||
} else {
|
||||
// 如果过期,从链表中移除这个节点
|
||||
if (prev == NULL) {
|
||||
ip_cache_head = current->next;
|
||||
} else {
|
||||
prev->next = current->next;
|
||||
}
|
||||
|
||||
free(current);
|
||||
cache_size--;
|
||||
return 0; // IP 过期,不再缓存
|
||||
}
|
||||
}
|
||||
|
||||
prev = current;
|
||||
current = current->next;
|
||||
}
|
||||
return 0; // IP 不在缓存中
|
||||
}
|
||||
|
||||
// 清理缓存链表,释放所有节点的内存
|
||||
void free_ip_cache()
|
||||
{
|
||||
struct ip_cache_node *current = ip_cache_head;
|
||||
while (current != NULL) {
|
||||
struct ip_cache_node *next = current->next;
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
|
||||
ip_cache_head = NULL;
|
||||
cache_size = 0;
|
||||
}
|
||||
|
||||
|
||||
// 回调函数,在捕获到每个数据包时调用
|
||||
void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
|
||||
{
|
||||
int ethernet_header_len = 14;
|
||||
struct ip *ip_header = (struct ip *)(packet + ethernet_header_len);
|
||||
char src_ip[INET_ADDRSTRLEN] = { 0 };
|
||||
|
||||
// 地域白名单
|
||||
char _region_list[WHITELIST_IP_NUM][WHITELIST_IP_NUM] = { { 0 }, { 0 } };
|
||||
char _REGION_LIST[BUFFER] = { 0 };
|
||||
const char *REGION_ENV = NULL;
|
||||
|
||||
char ipset_query_command[256] = { 0 };
|
||||
|
||||
// 定义 Response 结构体
|
||||
Response response;
|
||||
|
||||
inet_ntop(AF_INET, &(ip_header->ip_src), src_ip, INET_ADDRSTRLEN);
|
||||
|
||||
// 如果 IP 地址已在缓存中且未过期,则跳过查询
|
||||
if (is_ip_in_cache(src_ip)) {
|
||||
_printf(RED "IP:%s 已在缓存中,跳过查询\n" REDEND, src_ip);
|
||||
@@ -156,14 +52,12 @@ void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char
|
||||
printf("%s ", cn_ip[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (cn_ip_len(cn_ip) >= 100) { // 清理集合
|
||||
if (cn_ip_len(cn_ip) >= 1024) { // 清理集合
|
||||
clear_ip_set(cn_ip);
|
||||
}
|
||||
|
||||
printf("cn_ip_len(cn_ip): %d\n", cn_ip_len(cn_ip));
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -196,34 +90,47 @@ void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char
|
||||
|
||||
add_ip_to_cache(src_ip); // 添加到缓存
|
||||
|
||||
char *p = curl_get_area(src_ip);
|
||||
char *p = CurlGetIpArea(src_ip);
|
||||
if (parse_json_to_struct(p, &response) == 0) { // 解析 JSON 到结构体
|
||||
|
||||
if (NULL == strstr(response.continent_country, "中国")) { // 这时是国外IP
|
||||
_printf(RED "%s %s\n" REDEND, src_ip, response.continent_country);
|
||||
_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
|
||||
add_cn_ip(cn_ip, src_ip); // 添加国内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");
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
fprintf(stderr, "Failed to parse JSON.\n");
|
||||
}
|
||||
|
||||
if (p != NULL)
|
||||
free(p);
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (ip2region_area != NULL) {
|
||||
free(ip2region_area);
|
||||
ip2region_area = NULL;
|
||||
}
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
// 回调函数,在捕获到每个数据包时调用
|
||||
void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
|
||||
{
|
||||
int ethernet_header_len = 14;
|
||||
struct ip *ip_header = (struct ip *)(packet + ethernet_header_len);
|
||||
char src_ip[INET_ADDRSTRLEN] = { 0 };
|
||||
|
||||
inet_ntop(AF_INET, &(ip_header->ip_src), src_ip, INET_ADDRSTRLEN);
|
||||
Processing_IP_addresses(src_ip);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -236,6 +143,7 @@ void usage()
|
||||
puts(" Usage: denyip [-d] [-i <interface>] [-s <start|stop>] [-h|-?]");
|
||||
puts(" -d --daemon Daemon mode");
|
||||
puts(" -i --interface interface (default eth0)");
|
||||
puts(" -f --protocol 过滤器 [\"tcp\" | \"udp\" | \"tcp or udp\"] (default \"tcp\")");
|
||||
puts(" -l print iptables rule");
|
||||
puts(" -s --signal regular signal (default start|stop) ");
|
||||
puts(" start Enable Iptables rule");
|
||||
@@ -292,7 +200,7 @@ int main(int argc, char **argv)
|
||||
|
||||
int opt;
|
||||
char errbuf[PCAP_ERRBUF_SIZE] = { 0 }; // 错误缓冲区
|
||||
char protocol[] = "tcp";
|
||||
char protocol[256] = "tcp";
|
||||
char interface[256] = "{ 0 }";
|
||||
char Ipset_Command[BUFFER] = { 0 };
|
||||
|
||||
@@ -302,9 +210,10 @@ int main(int argc, char **argv)
|
||||
memset(errbuf, 0, PCAP_ERRBUF_SIZE);
|
||||
|
||||
int longindex = 0;
|
||||
char optstring[] = "di:s:lh?";
|
||||
char optstring[] = "di:f:s:lh?";
|
||||
static struct option longopts[] = {
|
||||
{ "interface", required_argument, 0, 'i' },
|
||||
{ "protocol", required_argument, 0, 'f' },
|
||||
{ "signal", required_argument, 0, 's' },
|
||||
{ "daemon", no_argument, 0, 'd' },
|
||||
{ "l", no_argument, 0, 'l' },
|
||||
@@ -323,6 +232,9 @@ int main(int argc, char **argv)
|
||||
case 'i':
|
||||
strcpy(interface, optarg);
|
||||
break;
|
||||
case 'f':
|
||||
strcpy(protocol, optarg);
|
||||
break;
|
||||
case 'l':
|
||||
system("iptables -L -v -n --line-numbers");
|
||||
exit(0);
|
||||
@@ -381,16 +293,25 @@ int main(int argc, char **argv)
|
||||
return 1;
|
||||
}
|
||||
|
||||
char *local_addr = GetLocalAddr("https://inet-ip.aixiao.me/");
|
||||
if (local_addr == NULL) {
|
||||
perror("GetLocalAddr()");
|
||||
return 1;
|
||||
}
|
||||
remove_char(local_addr, '\n');
|
||||
printf("Local Address: %s\n", local_addr);
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
free(local_addr);
|
||||
}
|
||||
|
||||
pid = fork(); // 创建子进程
|
||||
if (pid == 0) // 子进程
|
||||
{
|
||||
|
||||
int count = 0;
|
||||
snprintf(RULE_NAME, BUFFER, "root%d", RULE_NAME_NUMBER);
|
||||
|
||||
@@ -400,7 +321,6 @@ int main(int argc, char **argv)
|
||||
|
||||
while (1) {
|
||||
//_printf("子进程当前 Ipset Rule 名 %s\n", RULE_NAME);
|
||||
|
||||
count = get_ip_count_in_ipset(RULE_NAME);
|
||||
if (count >= 0) {
|
||||
_printf("子进程当前 Ipset Rule 名 %s, 数量: %d \n", RULE_NAME, count);
|
||||
@@ -417,7 +337,6 @@ int main(int argc, char **argv)
|
||||
sprintf(iptables_command, "iptables -I INPUT -m set --match-set %s src -j DROP", RULE_NAME);
|
||||
system(iptables_command);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (RULE_NAME_NUMBER == MAXIPSET_RULT_NAME_NUM) {
|
||||
@@ -426,7 +345,7 @@ int main(int argc, char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
sleep(9); // 每 3 秒检查一次
|
||||
sleep(3); // 每 3 秒检查一次
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user