优化查询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

Binary file not shown.

View File

@@ -46,6 +46,10 @@ func main() {
log.Fatalf("解析 JSON 时出错: %v", err)
}
// 提取并打印 continent 和 country 字段
fmt.Printf("%s%s\n", ipInfo.Data.Continent, ipInfo.Data.Country)
if ipInfo.Msg == "查询成功" {
// 提取并打印 continent 和 country 字段
fmt.Printf("%s%s\n", ipInfo.Data.Continent, ipInfo.Data.Country)
} else {
fmt.Printf("查询失败\n")
}
}

View File

@@ -143,3 +143,29 @@ char *_time()
return strdup(temp);
}
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);
}

View File

@@ -18,6 +18,10 @@
#include <netdb.h>
#include <sys/utsname.h>
#include <sys/resource.h>
#include <signal.h>
#include <sys/prctl.h>
#define PRINT_LOG_FILE "Gateway.log"
#define BUFFER 1024
#define WHITELIST_IP_NUM 1024
@@ -31,4 +35,8 @@ extern int isregion(char *str, char (*region_list)[WHITELIST_IP_NUM]);
extern int8_t copy_new_mem(char *src, int src_len, char **dest);
extern int is_valid_ip(const char *ip);
extern int nice_(int increment);
extern int command_exists(const char *command);
#endif

BIN
common.o

Binary file not shown.

BIN
denyip

Binary file not shown.

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);

BIN
main.o

Binary file not shown.