126 lines
2.9 KiB
C
126 lines
2.9 KiB
C
|
#include "common.h"
|
||
|
|
||
|
|
||
|
// 计算字符串长度
|
||
|
int _strlen(char *str)
|
||
|
{
|
||
|
char *_p = NULL;
|
||
|
|
||
|
if (str == NULL)
|
||
|
return 0;
|
||
|
|
||
|
_p = strchr(str, '\0');
|
||
|
|
||
|
if (_p == NULL)
|
||
|
return 0;
|
||
|
|
||
|
return _p - str;
|
||
|
}
|
||
|
|
||
|
// 自定义 printf 函数
|
||
|
void my_printf(const char *format, ...) {
|
||
|
va_list args;
|
||
|
va_start(args, format);
|
||
|
|
||
|
// 打印到控制台
|
||
|
vprintf(format, args);
|
||
|
va_end(args); // 结束对变参列表的处理
|
||
|
|
||
|
// 重新启动变参列表
|
||
|
va_start(args, format);
|
||
|
|
||
|
// 打开日志文件(追加模式)
|
||
|
FILE *log_file = fopen(PRINT_LOG_FILE, "a");
|
||
|
if (log_file != NULL) {
|
||
|
// 获取当前时间
|
||
|
time_t now = time(NULL);
|
||
|
struct tm local_time;
|
||
|
localtime_r(&now, &local_time);
|
||
|
char time_str[20]; // YYYY-MM-DD HH:MM:SS 格式
|
||
|
strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", &local_time);
|
||
|
|
||
|
// 打印时间戳到日志文件
|
||
|
fprintf(log_file, "[%s] ", time_str);
|
||
|
|
||
|
// 打印内容到日志文件
|
||
|
vfprintf(log_file, format, args);
|
||
|
|
||
|
// 关闭日志文件
|
||
|
fclose(log_file);
|
||
|
} else {
|
||
|
perror("Unable to open log file");
|
||
|
}
|
||
|
|
||
|
va_end(args); // 结束对变参列表的处理
|
||
|
}
|
||
|
|
||
|
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)
|
||
|
{
|
||
|
i++;
|
||
|
strcpy(whitelist_ip[i], result);
|
||
|
result = strtok(NULL, delims);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// IP段白名单对比
|
||
|
int whitelist(char *client_ip, char (*whitelist_ip)[WHITELIST_IP_NUM])
|
||
|
{
|
||
|
int i;
|
||
|
|
||
|
for (i = 1; i < WHITELIST_IP_NUM - 1; i++) {
|
||
|
if (strcmp(whitelist_ip[i], "\0") == 0) // 如果字符串为空就跳出循环
|
||
|
{
|
||
|
break;
|
||
|
}
|
||
|
if ((strncmp(client_ip, whitelist_ip[i], _strlen(whitelist_ip[i]))) == 0) // 对比client_ip长度,
|
||
|
{
|
||
|
return 1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
// 地域段白名单对比
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
char *_time()
|
||
|
{
|
||
|
char temp[BUFFER];
|
||
|
char *wday[] = { "0", "1", "2", "3", "4", "5", "6" };
|
||
|
time_t t;
|
||
|
struct tm *p;
|
||
|
time(&t);
|
||
|
p = localtime(&t); // 取得当地时间
|
||
|
|
||
|
memset(temp, 0, BUFFER);
|
||
|
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);
|
||
|
|
||
|
return strdup(temp);
|
||
|
}
|