优化查询IP地址
This commit is contained in:
Binary file not shown.
@@ -46,6 +46,10 @@ func main() {
|
|||||||
log.Fatalf("解析 JSON 时出错: %v", err)
|
log.Fatalf("解析 JSON 时出错: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 提取并打印 continent 和 country 字段
|
if ipInfo.Msg == "查询成功" {
|
||||||
fmt.Printf("%s%s\n", ipInfo.Data.Continent, ipInfo.Data.Country)
|
// 提取并打印 continent 和 country 字段
|
||||||
|
fmt.Printf("%s%s\n", ipInfo.Data.Continent, ipInfo.Data.Country)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("查询失败\n")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
26
common.c
26
common.c
@@ -143,3 +143,29 @@ char *_time()
|
|||||||
return strdup(temp);
|
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);
|
||||||
|
}
|
||||||
8
common.h
8
common.h
@@ -18,6 +18,10 @@
|
|||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <sys/utsname.h>
|
#include <sys/utsname.h>
|
||||||
|
|
||||||
|
#include <sys/resource.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <sys/prctl.h>
|
||||||
|
|
||||||
#define PRINT_LOG_FILE "Gateway.log"
|
#define PRINT_LOG_FILE "Gateway.log"
|
||||||
#define BUFFER 1024
|
#define BUFFER 1024
|
||||||
#define WHITELIST_IP_NUM 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 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
|
#endif
|
||||||
|
|||||||
40
main.c
40
main.c
@@ -1,33 +1,6 @@
|
|||||||
#include "main.h"
|
#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()
|
void denyip_help()
|
||||||
{
|
{
|
||||||
puts(" DenyIp");
|
puts(" DenyIp");
|
||||||
@@ -255,11 +228,13 @@ int main(int argc, char *argv[])
|
|||||||
printf("qqwry 解析地域错误\n");
|
printf("qqwry 解析地域错误\n");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *area = ip2region(xdb_path, line);
|
char *area = ip2region(xdb_path, line);
|
||||||
if (area == NULL) {
|
if (area == NULL) {
|
||||||
printf("ip2region 解析地域错误\n");
|
printf("ip2region 解析地域错误\n");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 取环境变量
|
// 取环境变量
|
||||||
const char *REGION_ENV = getenv("REGION");
|
const char *REGION_ENV = getenv("REGION");
|
||||||
if (REGION_ENV != NULL) {
|
if (REGION_ENV != NULL) {
|
||||||
@@ -272,12 +247,12 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
//printf("REGION_LIST : %s\n", _REGION_LIST_COPY);
|
//printf("REGION_LIST : %s\n", _REGION_LIST_COPY);
|
||||||
|
|
||||||
split_string(QQWRY_REGION_LIST_COPY, " ", qqwry_region_list); // 分割后存储在 qqwry_region_list
|
split_string(QQWRY_REGION_LIST_COPY, " ", qqwry_region_list); // 分割后存储在 qqwry_region_list
|
||||||
if (isregion(qqwry_region, qqwry_region_list) == 1) { // 返回1表示在白名单列表
|
if (isregion(qqwry_region, qqwry_region_list) == 1) { // 返回1表示在白名单列表
|
||||||
;
|
;
|
||||||
} else {
|
} else {
|
||||||
split_string(_REGION_LIST_COPY, " ", _region_list);
|
split_string(_REGION_LIST_COPY, " ", _region_list);
|
||||||
if (isregion(area, _region_list) == 1) { // 返回1表示在白名单列表
|
if (isregion(area, _region_list) == 1) { // 返回1表示在白名单列表
|
||||||
;
|
;
|
||||||
} else {
|
} else {
|
||||||
char ipquery_command[BUFFER + 100] = { 0 };
|
char ipquery_command[BUFFER + 100] = { 0 };
|
||||||
@@ -322,13 +297,12 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 父进程
|
// 父进程 主进程循环,检查子进程运行情况
|
||||||
int iteration = 0;
|
int iteration = 0;
|
||||||
// 主进程循环,检查子进程运行情况
|
|
||||||
while (1) {
|
while (1) {
|
||||||
iteration++;
|
iteration++;
|
||||||
|
|
||||||
if (iteration >= 3600) {
|
if (iteration >= 3600*3) {
|
||||||
iteration = 0;
|
iteration = 0;
|
||||||
printf("准备重启进程...\n");
|
printf("准备重启进程...\n");
|
||||||
restart_process(pid1, pid2, argv);
|
restart_process(pid1, pid2, argv);
|
||||||
|
|||||||
Reference in New Issue
Block a user