优化
This commit is contained in:
46
main.c
46
main.c
@@ -10,10 +10,10 @@
|
||||
|
||||
#include "ip2region.h"
|
||||
#include "qqwry.h"
|
||||
#include "common.h"
|
||||
|
||||
#define RED "\033[31m"
|
||||
#define RESET "\033[0m"
|
||||
#define BUFFER 512
|
||||
#define WHITELIST_IP_NUM 1024
|
||||
#define MAXIPSET 65534
|
||||
|
||||
@@ -21,42 +21,6 @@ char *xdb_path = "ip2region.xdb";
|
||||
pid_t pid1, pid2; // 保存子进程的 PID
|
||||
|
||||
|
||||
void split_string(char string[], char delims[], char (*whitelist_ip)[WHITELIST_IP_NUM])
|
||||
{
|
||||
int i = 0;
|
||||
char *result = NULL;
|
||||
|
||||
result = strtok(string, delims);
|
||||
while (result != NULL) {
|
||||
|
||||
strcpy(whitelist_ip[i], result);
|
||||
result = strtok(NULL, delims);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
// 地域段白名单对比
|
||||
int isregion(char *str, char (*region_list)[WHITELIST_IP_NUM])
|
||||
{
|
||||
int i;
|
||||
char *p;
|
||||
|
||||
for (i = 1; i < WHITELIST_IP_NUM - 1; i++) {
|
||||
if (strcmp(region_list[i], "\0") == 0) // 如果字符串为空就跳出循环
|
||||
{
|
||||
break;
|
||||
}
|
||||
//printf("%s %s\n", str, region_list[i]);
|
||||
// 在str中查找region_list[i]
|
||||
p = strstr(str, region_list[i]);
|
||||
if (p != NULL) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int is_valid_ip(const char *ip)
|
||||
{
|
||||
struct sockaddr_in sa;
|
||||
@@ -231,6 +195,7 @@ int main(int argc, char *argv[])
|
||||
perror("daemon");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 进程优先级
|
||||
if (-1 == (nice_(-20)))
|
||||
perror("nice_");
|
||||
@@ -239,6 +204,7 @@ int main(int argc, char *argv[])
|
||||
if ((r = system("ipset create root hash:ip > /dev/null 2>&1")) != -1) {
|
||||
;
|
||||
}
|
||||
|
||||
// 判断必要命令是否存在
|
||||
if (command_exists("which tcpdump")) {
|
||||
;
|
||||
@@ -263,7 +229,6 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
printf("%s\n", line);
|
||||
|
||||
pclose(fp);
|
||||
sleep(3);
|
||||
}
|
||||
@@ -291,12 +256,14 @@ int main(int argc, char *argv[])
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// 打开管道来执行命令
|
||||
FILE *fp = popen(command_tcpdump, "r");
|
||||
if (fp == NULL) {
|
||||
perror("popen failed");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 逐行读取命令输出
|
||||
while (fgets(line, sizeof(line), fp) != NULL) {
|
||||
line[strcspn(line, "\n")] = '\0';
|
||||
@@ -334,12 +301,13 @@ int main(int argc, char *argv[])
|
||||
;
|
||||
} else {
|
||||
char ipquery_command[BUFFER + 100] = { 0 };
|
||||
snprintf(ipquery_command, BUFFER + 100, "./ipquery %s", line);
|
||||
snprintf(ipquery_command, BUFFER + 100, "./IP_region_query/ipquery %s", line);
|
||||
FILE *fp = popen(ipquery_command, "r");
|
||||
if (fp == NULL) {
|
||||
perror("popen failed");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 创建足够大的缓冲区来存储命令输出
|
||||
char buffer[1024 * 2]; // 2KB 缓冲区
|
||||
size_t bytesRead = fread(buffer, 1, sizeof(buffer) - 1, fp);
|
||||
|
||||
Reference in New Issue
Block a user