微小优化
This commit is contained in:
2
Makefile
2
Makefile
@@ -1,7 +1,7 @@
|
|||||||
CROSS_COMPILE ?=
|
CROSS_COMPILE ?=
|
||||||
CC := $(CROSS_COMPILE)gcc
|
CC := $(CROSS_COMPILE)gcc
|
||||||
STRIP := $(CROSS_COMPILE)strip
|
STRIP := $(CROSS_COMPILE)strip
|
||||||
CFLAGS += -g -Wall -fsanitize=address -Iip2region
|
CFLAGS += -g -Wall -Iip2region
|
||||||
LIBS =
|
LIBS =
|
||||||
BIN := denyip
|
BIN := denyip
|
||||||
|
|
||||||
|
|||||||
25
cap.c
25
cap.c
@@ -17,10 +17,10 @@ char *ip2region_area = NULL; // ip2region 解析结果
|
|||||||
#define SHM_SIZE (MAXIPSET_ * MAXIPLEN)
|
#define SHM_SIZE (MAXIPSET_ * MAXIPLEN)
|
||||||
int shmid[3] = {-1};
|
int shmid[3] = {-1};
|
||||||
|
|
||||||
char (*cn_ip)[MAXIPLEN] = NULL; // 用于存储国内 IP 地址
|
char (*cn_ip)[MAXIPLEN] = NULL; // 用于存储国内 IP 地址
|
||||||
char (*whitelist_ip)[MAXIPLEN] = NULL; // 最终要这样定义
|
char (*whitelist_ip)[MAXIPLEN] = NULL; // 用于存储白名单 IP 地址
|
||||||
char *RULE_NAME = NULL; // 共享内存
|
char *RULE_NAME = NULL; // 共享内存存储 ipset 规则名称
|
||||||
int RULE_NAME_NUMBER = 0; // ipset 集合集合数
|
int RULE_NAME_NUMBER = 0; // ipset 集合集合数
|
||||||
|
|
||||||
|
|
||||||
void Processing_IP_addresses(char *src_ip)
|
void Processing_IP_addresses(char *src_ip)
|
||||||
@@ -38,6 +38,8 @@ void Processing_IP_addresses(char *src_ip)
|
|||||||
_printf(RED "IP:%s 白名单IP, 跳过!\n" REDEND, src_ip);
|
_printf(RED "IP:%s 白名单IP, 跳过!\n" REDEND, src_ip);
|
||||||
//printf("%s %d\n", whitelist_ip[0], cn_ip_len(whitelist_ip));
|
//printf("%s %d\n", whitelist_ip[0], cn_ip_len(whitelist_ip));
|
||||||
|
|
||||||
|
//del_ip_to_ipset("root0", src_ip); // 删除 ipset 规则
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -386,8 +388,9 @@ int main(int argc, char **argv)
|
|||||||
pids[0] = fork();
|
pids[0] = fork();
|
||||||
if ( 0 == pids[0] ) // 子进程
|
if ( 0 == pids[0] ) // 子进程
|
||||||
{
|
{
|
||||||
while(1) {
|
while(1)
|
||||||
|
{
|
||||||
|
// 重新加载白名单Ip
|
||||||
if (1 == file_exists_access("whitelist.txt")) {
|
if (1 == file_exists_access("whitelist.txt")) {
|
||||||
clear_ip_set(whitelist_ip); // 清空白名单IP集合
|
clear_ip_set(whitelist_ip); // 清空白名单IP集合
|
||||||
int line_count = 0;
|
int line_count = 0;
|
||||||
@@ -397,11 +400,14 @@ int main(int argc, char **argv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("White list IPs: \n ");
|
||||||
for (int i = 0; i < line_count; i++) {
|
for (int i = 0; i < line_count; i++) {
|
||||||
printf("Line %d: %s\n", i + 1, whitelist_ip[i]);
|
printf("%s ", whitelist_ip[i]);
|
||||||
}
|
}
|
||||||
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 打印国内IP集合
|
||||||
for (int i = 0; i < MAXIPSET_; i++) {
|
for (int i = 0; i < MAXIPSET_; i++) {
|
||||||
if (cn_ip[i][0] != '\0') {
|
if (cn_ip[i][0] != '\0') {
|
||||||
printf("%s ", cn_ip[i]);
|
printf("%s ", cn_ip[i]);
|
||||||
@@ -409,12 +415,13 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
printf("cn_ip_len(cn_ip): %d\n", cn_ip_len(cn_ip));
|
printf("cn_ip_len(cn_ip): %d\n", cn_ip_len(cn_ip));
|
||||||
|
|
||||||
if (cn_ip_len(cn_ip) >= 1024) { // 清理集合
|
// 如果国内IP数量超过100,清理集合
|
||||||
|
if (cn_ip_len(cn_ip) >= 100) { // 清理集合
|
||||||
clear_ip_set(cn_ip);
|
clear_ip_set(cn_ip);
|
||||||
truncate_file("cn.txt"); // 清空文件
|
truncate_file("cn.txt"); // 清空文件
|
||||||
}
|
}
|
||||||
|
|
||||||
sleep(10); // 每 60 秒检查一次
|
sleep(30); // 每 60 秒检查一次
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,5 +14,6 @@ extern int create_ipset(char *set_name);
|
|||||||
extern int add_ip_to_ipset(char *set_name, char *ip);
|
extern int add_ip_to_ipset(char *set_name, char *ip);
|
||||||
extern int get_ip_count_in_ipset(char *set_name);
|
extern int get_ip_count_in_ipset(char *set_name);
|
||||||
extern void add_iptables_rule(const char *rule_name);
|
extern void add_iptables_rule(const char *rule_name);
|
||||||
|
extern int del_ip_to_ipset(char *set_name, char *ip);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user