优化
This commit is contained in:
@@ -20,7 +20,7 @@
|
||||
|
||||
```bash
|
||||
sudo apt update
|
||||
sudo apt install build-essential golang libpcap-dev libcap-dev libsystemd-dev
|
||||
sudo apt install build-essential golang libpcap-dev libcap-dev libipset-dev libsystemd-dev
|
||||
```
|
||||
|
||||
在 Centos 7 系统上安装所需的开发工具和库:
|
||||
|
||||
30
cap.c
30
cap.c
@@ -4,29 +4,29 @@
|
||||
|
||||
|
||||
pcap_if_t *alldevs, *device;
|
||||
pcap_t *handle; // 会话句柄
|
||||
struct bpf_program fp; // 编译后的过滤器
|
||||
pcap_t *handle; // 会话句柄
|
||||
struct bpf_program fp; // 编译后的过滤器
|
||||
|
||||
pid_t pid = -1; // 子进程全局PID
|
||||
pid_t pid = -1; // 子进程全局PID
|
||||
|
||||
#define SHM_SIZE 1024 // 共享内存大小
|
||||
#define SHM_KEY 1234 // 共享内存键值
|
||||
#define SHM_SIZE 1024 // 共享内存大小
|
||||
#define SHM_KEY 1234 // 共享内存键值
|
||||
int shmid = -1;
|
||||
int RULE_NAME_NUMBER = 0; // ipset 集合集合数
|
||||
char *RULE_NAME = NULL; // 共享内存
|
||||
char *ip2region_area = NULL; // ip2region 解析结果
|
||||
char *command_result = NULL; // 执行命令的结果
|
||||
int RULE_NAME_NUMBER = 0; // ipset 集合集合数
|
||||
char *RULE_NAME = NULL; // 共享内存
|
||||
char *ip2region_area = NULL; // ip2region 解析结果
|
||||
char *command_result = NULL; // 执行命令的结果
|
||||
|
||||
#define CACHE_TTL 600 // 设定缓存的存活时间为 600 秒 (10 分钟)
|
||||
#define MAX_CACHE_SIZE 100 // 缓存最多存储 100 个 IP 地址
|
||||
#define CACHE_TTL 600 // 设定缓存的存活时间为 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; // 指向下一个节点
|
||||
char ip[INET_ADDRSTRLEN]; // 存储 IP 地址
|
||||
time_t timestamp; // 记录缓存时间
|
||||
struct ip_cache_node *next; // 指向下一个节点
|
||||
};
|
||||
|
||||
|
||||
@@ -175,7 +175,7 @@ void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char
|
||||
if (p == NULL) {
|
||||
_printf(RED "%s %s\n" REDEND, src_ip, command_result);
|
||||
r = add_ip_to_ipset(RULE_NAME, src_ip);
|
||||
_printf("add_ip_to_ipset %d\n", r);
|
||||
_printf("add_ip_to_ipset() return %d\n", r);
|
||||
}
|
||||
|
||||
free(command_result);
|
||||
|
||||
Reference in New Issue
Block a user