c
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
#ifndef BAN_H
|
||||
#define BAN_H
|
||||
|
||||
#include "common.h"
|
||||
#include "ip_utils.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
/* 封禁IP */
|
||||
int ban_ip(const char *ip, bool save_to_disk);
|
||||
|
||||
/* 解封IP */
|
||||
int unban_ip(const char *ip);
|
||||
|
||||
/* 添加到持久化列表 */
|
||||
int persist_add_ip(const char *ip, const char *country_code);
|
||||
|
||||
/* 从持久化列表移除 */
|
||||
int persist_remove_ip(const char *ip);
|
||||
|
||||
/* 更新IP的国家信息 */
|
||||
int update_ip_country(const char *ip, const char *country_code);
|
||||
|
||||
/* 恢复持久化列表到nftables */
|
||||
int restore_from_persist(void);
|
||||
|
||||
/* 显示持久化列表 */
|
||||
void show_persist_list(void);
|
||||
|
||||
#endif /* BAN_H */
|
||||
@@ -0,0 +1,79 @@
|
||||
#ifndef COMMON_H
|
||||
#define COMMON_H
|
||||
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <ctype.h>
|
||||
|
||||
/* 配置常量 */
|
||||
|
||||
#define BIP_VERSION "v25.11.18"
|
||||
#define CONFIG_DIR "/etc/blockip"
|
||||
#define CONFIG_FILE CONFIG_DIR "/config"
|
||||
#define LOG_FILE "/var/log/bip.log"
|
||||
#define MAX_LOG_SIZE 10485760 // 10MB
|
||||
#define DEFAULT_MAX_RETRIES 3
|
||||
#define DEFAULT_BAN_TIME "24h"
|
||||
#define RECORD_DIR CONFIG_DIR "/counts"
|
||||
#define PERSIST_FILE CONFIG_DIR "/blacklist"
|
||||
#define WHITELIST_FILE CONFIG_DIR "/whitelist"
|
||||
#define INSTALL_PATH "/usr/local/bin/bip"
|
||||
#define NFT_TABLE "inet filter"
|
||||
#define NFT_SET "blacklist"
|
||||
#define NFT_SET_V6 "blacklist_v6"
|
||||
#define NFT_WHITELIST "whitelist"
|
||||
#define NFT_WHITELIST_V6 "whitelist_v6"
|
||||
|
||||
/* 缓冲区大小 */
|
||||
#define MAX_LINE_LEN 512
|
||||
#define MAX_IP_LEN 128
|
||||
#define MAX_COUNTRY_CODE 8
|
||||
#define MAX_COMMAND_LEN 1024
|
||||
#define MAX_PATH_LEN 256
|
||||
|
||||
/* 颜色定义 */
|
||||
#define C_RESET "\033[0m"
|
||||
#define C_GREEN "\033[32m"
|
||||
#define C_CYAN "\033[36m"
|
||||
#define C_YELLOW "\033[33m"
|
||||
#define C_RED "\033[31m"
|
||||
|
||||
/* 错误码 */
|
||||
#define SUCCESS 0
|
||||
#define ERROR_PERMISSION -1
|
||||
#define ERROR_FILE -2
|
||||
#define ERROR_NETWORK -3
|
||||
#define ERROR_INVALID_ARG -4
|
||||
|
||||
/* 工具宏 */
|
||||
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
|
||||
|
||||
/* 打印消息 */
|
||||
void msg(const char *color, const char *message);
|
||||
|
||||
/* 检查root权限 */
|
||||
int check_root(void);
|
||||
|
||||
/* 获取当前时间戳字符串 */
|
||||
void get_timestamp(char *buffer, size_t size);
|
||||
|
||||
/* 读取配置文件中的封禁时间 */
|
||||
const char* get_ban_time_from_config(void);
|
||||
|
||||
/* 保存封禁时间到配置文件 */
|
||||
int save_ban_time_to_config(const char *ban_time);
|
||||
|
||||
/* 读取配置文件中的最大重试次数 */
|
||||
int get_max_retries_from_config(void);
|
||||
|
||||
/* 保存最大重试次数到配置文件 */
|
||||
int save_max_retries_to_config(int max_retries);
|
||||
|
||||
#endif /* COMMON_H */
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef GEO_H
|
||||
#define GEO_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
/* 查询IP的国家代码 */
|
||||
int query_country_code(const char *ip, char *country_code, size_t size);
|
||||
|
||||
/* 获取国家名称 */
|
||||
const char* get_country_name(const char *country_code);
|
||||
|
||||
/* 补充持久化文件中缺失的国家信息 */
|
||||
void supplement_country_info(const char *current_ip);
|
||||
|
||||
#endif /* GEO_H */
|
||||
@@ -0,0 +1,21 @@
|
||||
#ifndef INSTALL_H
|
||||
#define INSTALL_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
/* 安装服务 */
|
||||
int install_service(void);
|
||||
|
||||
/* 卸载服务 */
|
||||
int uninstall_service(void);
|
||||
|
||||
/* 配置PAM钩子 */
|
||||
int setup_pam_hooks(void);
|
||||
|
||||
/* 移除PAM钩子 */
|
||||
int remove_pam_hooks(void);
|
||||
|
||||
/* 创建systemd服务 */
|
||||
int create_systemd_service(void);
|
||||
|
||||
#endif /* INSTALL_H */
|
||||
@@ -0,0 +1,45 @@
|
||||
#ifndef IP_UTILS_H
|
||||
#define IP_UTILS_H
|
||||
|
||||
#include "common.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
/* IP类型 */
|
||||
typedef enum {
|
||||
IP_TYPE_UNKNOWN = 0,
|
||||
IP_TYPE_V4,
|
||||
IP_TYPE_V6,
|
||||
IP_TYPE_V4_CIDR,
|
||||
IP_TYPE_V6_CIDR
|
||||
} ip_type_t;
|
||||
|
||||
/* IP信息结构 */
|
||||
typedef struct {
|
||||
char ip[MAX_IP_LEN];
|
||||
char country_code[MAX_COUNTRY_CODE];
|
||||
ip_type_t type;
|
||||
int cidr_mask;
|
||||
} ip_info_t;
|
||||
|
||||
/* 判断是否为IPv6 */
|
||||
bool is_ipv6(const char *ip);
|
||||
|
||||
/* 判断是否为CIDR格式 */
|
||||
bool is_cidr(const char *ip);
|
||||
|
||||
/* 解析IP信息 */
|
||||
int parse_ip_info(const char *input, ip_info_t *info);
|
||||
|
||||
/* 获取当前连接IP */
|
||||
char* get_remote_ip(void);
|
||||
|
||||
/* 验证IP格式 */
|
||||
bool validate_ip_format(const char *ip);
|
||||
|
||||
/* 格式化IP为nftables元素 */
|
||||
void format_nft_element(const char *ip, char *output, size_t size, const char *timeout);
|
||||
|
||||
/* 检查IP是否匹配白名单 */
|
||||
bool ip_matches_whitelist_entry(const char *ip, const char *whitelist_entry);
|
||||
|
||||
#endif /* IP_UTILS_H */
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef LOG_H
|
||||
#define LOG_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
/* 日志初始化 */
|
||||
int log_init(void);
|
||||
|
||||
/* 写入日志 */
|
||||
void log_write(const char *format, ...);
|
||||
|
||||
/* 日志轮转 */
|
||||
void log_rotate(void);
|
||||
|
||||
/* 显示最新日志 */
|
||||
void log_show_recent(int lines);
|
||||
|
||||
#endif /* LOG_H */
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef NFTABLES_H
|
||||
#define NFTABLES_H
|
||||
|
||||
#include "common.h"
|
||||
#include "ip_utils.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
/* 检查并安装nftables环境 */
|
||||
int check_and_install_nftables(void);
|
||||
|
||||
/* 初始化nftables规则 */
|
||||
int init_nftables_rules(void);
|
||||
|
||||
/* 添加IP到nftables黑名单 */
|
||||
int nft_add_to_blacklist(const ip_info_t *ip_info);
|
||||
|
||||
/* 从nftables黑名单移除IP */
|
||||
int nft_remove_from_blacklist(const char *ip);
|
||||
|
||||
/* 添加IP到nftables白名单 */
|
||||
int nft_add_to_whitelist(const char *ip);
|
||||
|
||||
/* 从nftables白名单移除IP */
|
||||
int nft_remove_from_whitelist(const char *ip);
|
||||
|
||||
/* 获取nftables集合中的元素数量 */
|
||||
int nft_get_set_count(const char *set_name);
|
||||
|
||||
/* 列出nftables集合中的元素 */
|
||||
int nft_list_set_elements(const char *set_name, char *buffer, size_t size);
|
||||
|
||||
#endif /* NFTABLES_H */
|
||||
@@ -0,0 +1,21 @@
|
||||
#ifndef PAM_H
|
||||
#define PAM_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
/* PAM检查:处理登录失败 */
|
||||
int pam_check_failed_login(void);
|
||||
|
||||
/* PAM清理:处理登录成功 */
|
||||
int pam_clean_on_success(void);
|
||||
|
||||
/* 记录失败次数 */
|
||||
int record_failure(const char *ip);
|
||||
|
||||
/* 清除失败记录 */
|
||||
int clear_failure_record(const char *ip);
|
||||
|
||||
/* 获取失败次数 */
|
||||
int get_failure_count(const char *ip);
|
||||
|
||||
#endif /* PAM_H */
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef STATS_H
|
||||
#define STATS_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
/* 显示完整统计信息 */
|
||||
void show_statistics(void);
|
||||
|
||||
/* 显示活跃封禁列表 */
|
||||
void show_active_bans(void);
|
||||
|
||||
/* 显示国家统计 */
|
||||
void show_country_stats(void);
|
||||
|
||||
/* 显示IP段聚合统计 */
|
||||
void show_subnet_aggregation(void);
|
||||
|
||||
#endif /* STATS_H */
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef WHITELIST_H
|
||||
#define WHITELIST_H
|
||||
|
||||
#include "common.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
/* 检查IP是否在白名单中 */
|
||||
bool is_in_whitelist(const char *ip);
|
||||
|
||||
/* 添加IP到白名单文件 */
|
||||
int whitelist_add_to_file(const char *ip);
|
||||
|
||||
/* 从白名单文件移除IP */
|
||||
int whitelist_remove_from_file(const char *ip);
|
||||
|
||||
/* 显示白名单列表 */
|
||||
void whitelist_show(void);
|
||||
|
||||
/* 恢复白名单到nftables */
|
||||
int whitelist_restore(void);
|
||||
|
||||
#endif /* WHITELIST_H */
|
||||
Reference in New Issue
Block a user