优化公共函数

This commit is contained in:
2024-10-31 14:54:33 +08:00
parent fd186fe50a
commit 7de356fa85
6 changed files with 87 additions and 36 deletions

5
cap.c
View File

@@ -344,10 +344,11 @@ int main(int argc, char **argv)
snprintf(RULE_NAME, BUFFER, "root%d", RULE_NAME_NUMBER); snprintf(RULE_NAME, BUFFER, "root%d", RULE_NAME_NUMBER);
if (create_ipset(RULE_NAME) != 0) { if (create_ipset(RULE_NAME) != 0) {
fprintf(stderr, "创建 IPSet %s 失败\n", RULE_NAME); _printf("创建 IPSet %s 失败\n", RULE_NAME);
} }
while (1) {
while (1)
{
_printf("子进程当前 Ipset Rule 名 %s\n", RULE_NAME); _printf("子进程当前 Ipset Rule 名 %s\n", RULE_NAME);
count = get_ip_count_in_ipset(RULE_NAME); count = get_ip_count_in_ipset(RULE_NAME);

BIN
cap.o

Binary file not shown.

115
common.c
View File

@@ -1,21 +1,15 @@
#include "common.h" #include "common.h"
// 计算字符串长度 // 计算字符串长度
int _strlen(char *str) int _strlen(const char *str) {
{
char *_p = NULL;
if (str == NULL) if (str == NULL)
return 0; return 0;
_p = strchr(str, '\0'); const char *_p = strchr(str, '\0');
if (_p == NULL)
return 0;
return _p - str; return _p - str;
} }
// 自定义 printf 函数 // 自定义 printf 函数
void _printf(const char *format, ...) void _printf(const char *format, ...)
{ {
@@ -86,7 +80,7 @@ int whitelist(char *client_ip, char (*whitelist_ip)[WHITELIST_IP_NUM])
break; break;
} }
// 对比client_ip的前缀是否与白名单中的IP段匹配 // 对比 client_ip 的前缀是否与白名单中的IP段匹配
if (strncmp(client_ip, whitelist_ip[i], strlen(whitelist_ip[i])) == 0) { if (strncmp(client_ip, whitelist_ip[i], strlen(whitelist_ip[i])) == 0) {
return 1; // 匹配成功 return 1; // 匹配成功
} }
@@ -102,12 +96,12 @@ int isregion(char *str, char (*region_list)[WHITELIST_IP_NUM])
char *p; char *p;
for (i = 0; i < WHITELIST_IP_NUM; i++) { for (i = 0; i < WHITELIST_IP_NUM; i++) {
// 如果region_list[i]为空字符串,跳出循环 // 如果 region_list[i] 为空字符串,跳出循环
if (region_list[i][0] == '\0') { if (region_list[i][0] == '\0') {
break; break;
} }
// 在str中查找region_list[i] // 在str中查找 region_list[i]
p = strstr(str, region_list[i]); p = strstr(str, region_list[i]);
if (p != NULL) { if (p != NULL) {
return 1; // 匹配成功返回1 return 1; // 匹配成功返回1
@@ -128,19 +122,23 @@ int8_t _copy_new_mem(char *src, int src_len, char **dest)
return 0; return 0;
} }
char *_time() // 返回的时间字符串存储在静态缓冲区中
{ char *_time() {
char temp[BUFFER]; static char temp[BUFFER];
char *wday[] = { "0", "1", "2", "3", "4", "5", "6" }; const char *wday[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
time_t t; time_t t = time(NULL);
struct tm *p; struct tm *p = localtime(&t);
time(&t);
p = localtime(&t); // 取得当地时间
memset(temp, 0, BUFFER); if (!p) {
snprintf(temp, BUFFER, "[%d/%02d/%02d %s %02d:%02d:%02d] ", (1900 + p->tm_year), (1 + p->tm_mon), p->tm_mday, wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec); perror("localtime failed");
return NULL;
}
return strdup(temp); snprintf(temp, sizeof(temp), "[%d/%02d/%02d %s %02d:%02d:%02d]",
1900 + p->tm_year, 1 + p->tm_mon, p->tm_mday,
wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
return temp; // 返回静态缓冲区地址
} }
int is_valid_ip(const char *ip) int is_valid_ip(const char *ip)
@@ -152,22 +150,73 @@ int is_valid_ip(const char *ip)
return result != 0; return result != 0;
} }
int _nice(int increment) int _nice(int increment) {
{ // 获取当前优先级
int oldprio = getpriority(PRIO_PROCESS, getpid()); int oldprio = getpriority(PRIO_PROCESS, getpid());
printf("%d\n", oldprio); if (oldprio == -1 && errno != 0) {
perror("getpriority failed");
return -1;
}
return setpriority(PRIO_PROCESS, getpid(), oldprio + increment); printf("Current priority: %d\n", oldprio);
// 检查是否溢出
if ((increment > 0 && oldprio > INT_MAX - increment) ||
(increment < 0 && oldprio < INT_MIN - increment)) {
fprintf(stderr, "Priority overflow error\n");
return -1;
}
// 计算新的优先级
int newprio = oldprio + increment;
// 检查新的优先级是否在有效范围内
if (newprio < PRIO_MIN || newprio > PRIO_MAX) {
fprintf(stderr, "New priority out of range: %d (valid range is %d to %d)\n", newprio, PRIO_MIN, PRIO_MAX);
return -1;
}
// 设置新的优先级
if (setpriority(PRIO_PROCESS, getpid(), newprio) == -1) {
perror("setpriority failed");
return -1;
}
printf("New priority: %d\n", newprio);
return 0;
} }
// 判断命令是否存在 // 判断命令是否存在
int _command_exists(const char *command) int command_exists(const char *command) {
{ const char *path_env = getenv("PATH");
char buffer[BUFFER]; if (!path_env) {
snprintf(buffer, sizeof(buffer), "%s > /dev/null 2>&1", command); return 0; // 如果 PATH 不存在,返回不存在
int status = system(buffer); }
return (status == 0); char filepath[1024]; // 缓冲区大小
const char *dir = path_env;
while (dir && *dir) {
// 查找 PATH 中的下一个目录
const char *end = strchr(dir, ':');
size_t len = end ? (size_t)(end - dir) : strlen(dir);
// 构建路径并检查长度
if (snprintf(filepath, sizeof(filepath), "%.*s/%s", (int)len, dir, command) >= (int)sizeof(filepath)) {
return 0; // 缓冲区溢出,返回不存在
}
// 检查文件是否存在且可执行
if (access(filepath, X_OK) == 0) {
puts(filepath);
return 1; // 命令存在
}
// 更新 dir 指针
dir = end ? end + 1 : NULL;
}
return 0; // 命令不存在
} }
// 定义一个函数,执行命令并返回输出 // 定义一个函数,执行命令并返回输出

View File

@@ -17,6 +17,7 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h> #include <netdb.h>
#include <sys/utsname.h> #include <sys/utsname.h>
#include <limits.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <signal.h> #include <signal.h>
@@ -28,7 +29,7 @@
extern char *_time(); extern char *_time();
extern int _strlen(char *str); extern int _strlen(const char *str);
extern void _printf(const char *format, ...); extern void _printf(const char *format, ...);
extern int _nice(int increment); extern int _nice(int increment);

BIN
common.o

Binary file not shown.

BIN
denyip

Binary file not shown.