格式化风格

This commit is contained in:
aixiao 2022-09-29 14:45:29 +08:00
parent 526aac6765
commit 78ffb8d017
6 changed files with 90 additions and 118 deletions

2
conf.c
View File

@ -216,7 +216,7 @@ void free_conf(conf * conf)
return;
}
void ptintf_conf(conf *conf)
void ptintf_conf(conf * conf)
{
printf("%s\n", conf->DAEMON);
printf("%d\n", conf->TIME);

4
conf.h
View File

@ -25,8 +25,8 @@ typedef struct CONF {
char *RECV_MAIL;
} conf;
void read_conf(char *filename, conf *configure);
void read_conf(char *filename, conf * configure);
void free_conf(conf * conf);
void ptintf_conf(conf *conf);
void ptintf_conf(conf * conf);
#endif

View File

@ -2,7 +2,7 @@
int x_get_rule(const char *chain, struct xtc_handle *handle, char *ipv4)
{
int r=1;
int r = 1;
const struct ipt_entry *entry;
struct ipt_entry_match *entry_match;
@ -15,9 +15,8 @@ int x_get_rule(const char *chain, struct xtc_handle *handle, char *ipv4)
inet_ntop(AF_INET, &(entry->ip.dst), addr, sizeof(addr));
//printf("%s\n", addr);
//printf("%s\n", t);
if (0 == strcmp(ipv4, addr) && 0 == strcmp(t, "DROP") && 0 == strcmp(entry_match->u.user.name, "tcp"))
{
r=0;
if (0 == strcmp(ipv4, addr) && 0 == strcmp(t, "DROP") && 0 == strcmp(entry_match->u.user.name, "tcp")) {
r = 0;
break;
}
}
@ -28,21 +27,20 @@ int x_get_rule(const char *chain, struct xtc_handle *handle, char *ipv4)
int show_all_rule(char *ipv4)
{
int r=0;
int r = 0;
struct xtc_handle *handle;
const char *chain = NULL;
struct ipt_counters counter;
handle = iptc_init("filter");
for (chain = iptc_first_chain(handle); chain; chain = iptc_next_chain(handle))
{
for (chain = iptc_first_chain(handle); chain; chain = iptc_next_chain(handle)) {
if (chain != NULL && handle != NULL) {
iptc_get_policy(chain, &counter, handle);
}
r = x_get_rule(chain, handle, ipv4);
if (r==0){
if (r == 0) {
break;
}
}
@ -82,7 +80,7 @@ struct ipt_entry_match *get_match(const char *sports, const char *dports, unsign
size = IPT_ALIGN(sizeof(*match)) + IPT_ALIGN(sizeof(*udpinfo));
match = (struct ipt_entry_match *)calloc(1, size);
match->u.match_size = size;
strncpy(match->u.user.name, protocol, IPT_FUNCTION_MAXNAMELEN-2);
strncpy(match->u.user.name, protocol, IPT_FUNCTION_MAXNAMELEN - 2);
udpinfo = (struct ipt_udp *)match->data;
udpinfo->spts[1] = udpinfo->dpts[1] = 0xFFFF;
@ -99,8 +97,7 @@ struct ipt_entry_match *get_match(const char *sports, const char *dports, unsign
return match;
}
int iptc_add_rule(const char *table, const char *chain, int protocol, const char *iniface, const char *outiface, unsigned int src,
unsigned int dest, const char *srcports, const char *destports, const char *target, const char *dnat_to, const int append)
int iptc_add_rule(const char *table, const char *chain, int protocol, const char *iniface, const char *outiface, unsigned int src, unsigned int dest, const char *srcports, const char *destports, const char *target, const char *dnat_to, const int append)
{
struct xtc_handle *handle;
struct ipt_entry *chain_entry;
@ -200,7 +197,6 @@ int iptc_add_rule(const char *table, const char *chain, int protocol, const char
return 1;
}
if (entry_match)
free(entry_match);
free(entry_target);

View File

@ -18,7 +18,6 @@
/* Dest port. */
#define NFC_IP_DST_PT 0x0400
#ifndef IPT_MIN_ALIGN
#define IPT_MIN_ALIGN (__alignof__(struct ipt_entry))
#endif
@ -30,9 +29,7 @@
#define IPTC_TARGET_SIZE XT_ALIGN(sizeof(struct ipt_entry_target))
#define IPTC_FULL_SIZE IPTC_ENTRY_SIZE + IPTC_MATCH_SIZE + IPTC_TARGET_SIZE
int show_all_rule(char *ipv4);
int iptc_add_rule(const char *table, const char *chain, int protocol, const char *iniface, const char *outiface, unsigned int src,
unsigned int dest, const char *srcports, const char *destports, const char *target, const char *dnat_to, const int append);
int iptc_add_rule(const char *table, const char *chain, int protocol, const char *iniface, const char *outiface, unsigned int src, unsigned int dest, const char *srcports, const char *destports, const char *target, const char *dnat_to, const int append);
#endif

50
rhost.c
View File

@ -58,7 +58,6 @@ static char *GET_PUBLIC_IP(char *URL)
//对于同一次阻塞的curl_easy_perform而言在写完获取的数据之前会多次调用 WriteMemoryCallback
res = curl_easy_perform(curl_handle);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
} else {
@ -85,20 +84,18 @@ int check_system()
}
// 钉钉告警
int dingding_warning(char *illegal_ip, char *public_ip, conf *conf)
int dingding_warning(char *illegal_ip, char *public_ip, conf * conf)
{
FILE *fp;
char temp[64];
char jsonObj[BUFFER];
memset(jsonObj, 0, BUFFER);
memset(temp, 0, 64);
strcpy(temp, public_ip);
temp[strlen(public_ip)-1] = '\0';
temp[strlen(public_ip) - 1] = '\0';
if ((fp = fopen("libcurl_ding.log", "wt+")) == NULL)
{
if ((fp = fopen("libcurl_ding.log", "wt+")) == NULL) {
return 1;
}
@ -107,8 +104,7 @@ int dingding_warning(char *illegal_ip, char *public_ip, conf *conf)
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl == NULL)
{
if (curl == NULL) {
return 1;
}
@ -150,29 +146,26 @@ int dingding_warning(char *illegal_ip, char *public_ip, conf *conf)
}
// 邮件告警
int mail_warning(char *illegal_ip, char *public_ip, conf *conf)
int mail_warning(char *illegal_ip, char *public_ip, conf * conf)
{
FILE *fp = NULL;
char buff[BUFFER];
char text[BUFFER];
char temp[64];
memset(buff, 0, BUFFER);
memset(text, 0, BUFFER);
memset(temp, 0, 64);
strcpy(temp, public_ip);
temp[strlen(public_ip)-1] = '\0';
temp[strlen(public_ip) - 1] = '\0';
sprintf(text, "echo \"主机:%s, 禁止%s访问\" | mail -s \"System ban IP\" %s", temp, illegal_ip, conf->RECV_MAIL);
if (NULL == (fp = popen(text, "r")))
{
if (NULL == (fp = popen(text, "r"))) {
perror("popen text");
}
while (fgets(buff, BUFFER, fp) != NULL)
{
while (fgets(buff, BUFFER, fp) != NULL) {
buff[strlen(buff) - 1] = '\0';
}
@ -183,29 +176,27 @@ int mail_warning(char *illegal_ip, char *public_ip, conf *conf)
}
// 第三方邮箱告警
int QQ_mail_warning(char *illegal_ip, char *public_ip, conf *conf)
int QQ_mail_warning(char *illegal_ip, char *public_ip, conf * conf)
{
char string[BUFFER+(sizeof(QQMAIL))];
char string[BUFFER + (sizeof(QQMAIL))];
char text[BUFFER];
char temp[32];
memset(string, 0, BUFFER+(sizeof(QQMAIL)));
memset(string, 0, BUFFER + (sizeof(QQMAIL)));
memset(text, 0, BUFFER);
memset(temp, 0, 32);
strcpy(temp, public_ip);
temp[strlen(public_ip)-1] = '\0';
temp[strlen(public_ip) - 1] = '\0';
sprintf(text, "主机:%s, 禁止%s访问", temp, illegal_ip);
sprintf(string, QQMAIL, conf->RECV_MAIL, text);
return system(string);
}
// 封禁非法IP
int rule(conf *conf)
int rule(conf * conf)
{
FILE *fp, *fc;
@ -273,7 +264,6 @@ int rule(conf *conf)
strcat(command, "\"");
strcat(command, awk);
if ((fp = popen(command, "r")) == NULL) // 执行命令
{
perror("popen command");
@ -350,22 +340,18 @@ int main(int argc, char *argv[], char **env)
read_conf("rhost.conf", conf);
//ptintf_conf(conf);
// 新版本获取公网IP
public_ip = GET_PUBLIC_IP("http://ip.sb");
//printf("%s", public_ip);
signal(SIGCHLD, sig_child); // 创建捕捉子进程退出信号
if (0 == strcmp(conf->DAEMON, "on"))
{
if (0 == strcmp(conf->DAEMON, "on")) {
goto goto_daemon;
}
if (argv[1] != NULL && 0 == strcmp(argv[1], "-d"))
{
if (argv[1] != NULL && 0 == strcmp(argv[1], "-d")) {
goto_daemon:
if (daemon(1, 1)) // 守护进程
{
@ -378,17 +364,13 @@ goto_daemon:
sleep(conf->TIME);
}
}
else
{
} else {
rule(conf);
}
free_conf(conf);
free(conf);
free(public_ip);
return 0;
}

View File

@ -13,7 +13,6 @@
#include <sys/types.h>
#include <arpa/inet.h>
#define CENTOS_SYSTEM 1
#define DEBISN_SYSTEM 2
#define UNKNOWN_SYSTEM 3
@ -33,10 +32,8 @@
#define QQMAIL "email -r %s -s \"System ban IP\" -t \"%s\""
extern void read_conf(char *filename, conf *configure);
extern void read_conf(char *filename, conf * configure);
extern void free_conf(conf * conf);
extern void ptintf_conf(conf *conf);
extern void ptintf_conf(conf * conf);
#endif