优化查询IP地址

This commit is contained in:
2024-10-17 13:53:38 +08:00
parent 32adcf148e
commit 866043b976
8 changed files with 47 additions and 35 deletions

40
main.c
View File

@@ -1,33 +1,6 @@
#include "main.h"
int is_valid_ip(const char *ip)
{
struct sockaddr_in sa;
// 尝试将字符串转换为IPv4地址
int result = inet_pton(AF_INET, ip, &(sa.sin_addr));
return result != 0;
}
int nice_(int increment)
{
int oldprio = getpriority(PRIO_PROCESS, getpid());
printf("%d\n", oldprio);
return setpriority(PRIO_PROCESS, getpid(), oldprio + increment);
}
// 判断命令是否存在
int command_exists(const char *command)
{
char buffer[BUFFER];
snprintf(buffer, sizeof(buffer), "%s > /dev/null 2>&1", command);
int status = system(buffer);
return (status == 0);
}
void denyip_help()
{
puts(" DenyIp");
@@ -255,11 +228,13 @@ int main(int argc, char *argv[])
printf("qqwry 解析地域错误\n");
continue;
}
char *area = ip2region(xdb_path, line);
if (area == NULL) {
printf("ip2region 解析地域错误\n");
continue;
}
// 取环境变量
const char *REGION_ENV = getenv("REGION");
if (REGION_ENV != NULL) {
@@ -272,12 +247,12 @@ int main(int argc, char *argv[])
}
//printf("REGION_LIST : %s\n", _REGION_LIST_COPY);
split_string(QQWRY_REGION_LIST_COPY, " ", qqwry_region_list); // 分割后存储在 qqwry_region_list
if (isregion(qqwry_region, qqwry_region_list) == 1) { // 返回1表示在白名单列表
split_string(QQWRY_REGION_LIST_COPY, " ", qqwry_region_list); // 分割后存储在 qqwry_region_list
if (isregion(qqwry_region, qqwry_region_list) == 1) { // 返回1表示在白名单列表
;
} else {
split_string(_REGION_LIST_COPY, " ", _region_list);
if (isregion(area, _region_list) == 1) { // 返回1表示在白名单列表
if (isregion(area, _region_list) == 1) { // 返回1表示在白名单列表
;
} else {
char ipquery_command[BUFFER + 100] = { 0 };
@@ -322,13 +297,12 @@ int main(int argc, char *argv[])
}
// 父进程
// 父进程 主进程循环,检查子进程运行情况
int iteration = 0;
// 主进程循环,检查子进程运行情况
while (1) {
iteration++;
if (iteration >= 3600) {
if (iteration >= 3600*3) {
iteration = 0;
printf("准备重启进程...\n");
restart_process(pid1, pid2, argv);