This commit is contained in:
2024-11-04 17:00:52 +08:00
parent e3509c3152
commit ad0b0d6910
3 changed files with 9 additions and 15 deletions

24
cap.c
View File

@@ -10,7 +10,7 @@ struct bpf_program fp; // 编译后的过滤器
pid_t pid = -1; // 子进程全局PID pid_t pid = -1; // 子进程全局PID
#define SHM_SIZE 1024 // 共享内存大小 #define SHM_SIZE 1024 // 共享内存大小
#define SHM_KEY 1234 // 共享内存键值 #define SHM_KEY 0124 // 共享内存键值
int shmid = -1; int shmid = -1;
int RULE_NAME_NUMBER = 0; // ipset 集合集合数 int RULE_NAME_NUMBER = 0; // ipset 集合集合数
char *RULE_NAME = NULL; // 共享内存 char *RULE_NAME = NULL; // 共享内存
@@ -219,6 +219,11 @@ void cleanup_(int signum)
{ {
_printf("Received signal %d, cleaning up...\n", signum); _printf("Received signal %d, cleaning up...\n", signum);
// 终止子进程
if (pid > 0) {
kill(pid, SIGTERM);
}
// 释放共享内存 // 释放共享内存
if (RULE_NAME != NULL) { if (RULE_NAME != NULL) {
@@ -243,10 +248,7 @@ void cleanup_(int signum)
pcap_freealldevs(alldevs); // 释放设备列表 pcap_freealldevs(alldevs); // 释放设备列表
pcap_close(handle); // 关闭会话句柄 pcap_close(handle); // 关闭会话句柄
// 终止子进程
if (pid > 0) {
kill(pid, SIGTERM);
}
// 退出主进程 // 退出主进程
exit(0); exit(0);
@@ -282,7 +284,8 @@ int main(int argc, char **argv)
{ 0, 0, 0, 0 } { 0, 0, 0, 0 }
}; };
while (-1 != (opt = getopt_long(argc, argv, optstring, longopts, &longindex))) { while (-1 != (opt = getopt_long(argc, argv, optstring, longopts, &longindex)))
{
switch (opt) { switch (opt) {
case 'd': case 'd':
if (daemon(1, 1)) { if (daemon(1, 1)) {
@@ -304,8 +307,6 @@ int main(int argc, char **argv)
"for n in $(seq 0 %d); do iptables -A INPUT -p tcp -m set --match-set root$n src -j DROP 2> /dev/null; done", "for n in $(seq 0 %d); do iptables -A INPUT -p tcp -m set --match-set root$n src -j DROP 2> /dev/null; done",
MAXIPSET_RULT_NAME_NUM); MAXIPSET_RULT_NAME_NUM);
system(Ipset_Command); system(Ipset_Command);
//system("for n in $(seq 0 MAXIPSET_RULT_NAME_NUM); do iptables -A INPUT -p tcp -m set --match-set root$n src -j DROP 2> /dev/null; done");
exit(0); exit(0);
} else if (strcmp(optarg, "stop") == 0) { } else if (strcmp(optarg, "stop") == 0) {
memset(Ipset_Command, 0, BUFFER); memset(Ipset_Command, 0, BUFFER);
@@ -314,8 +315,6 @@ int main(int argc, char **argv)
"for n in $(seq 0 %d); do iptables -D INPUT -p tcp -m set --match-set root$n src -j DROP 2> /dev/null; done", "for n in $(seq 0 %d); do iptables -D INPUT -p tcp -m set --match-set root$n src -j DROP 2> /dev/null; done",
MAXIPSET_RULT_NAME_NUM); MAXIPSET_RULT_NAME_NUM);
system(Ipset_Command); system(Ipset_Command);
//system("for n in $(seq 0 MAXIPSET_RULT_NAME_NUM); do iptables -D INPUT -p tcp -m set --match-set root$n src -j DROP 2> /dev/null; done");
exit(0); exit(0);
} else { } else {
usage(); usage();
@@ -335,11 +334,6 @@ int main(int argc, char **argv)
} }
} }
if (strcmp(interface, "eth0") != 0)
{
}
// 创建共享内存 // 创建共享内存
shmid = shmget(SHM_KEY, SHM_SIZE, IPC_CREAT | 0666); shmid = shmget(SHM_KEY, SHM_SIZE, IPC_CREAT | 0666);

BIN
cap.o

Binary file not shown.

BIN
denyip

Binary file not shown.