Skip to content

Commit 1763fcf

Browse files
committed
refactor(webhook): 统一回调函数签名并添加缺失的头文件
修改所有webhook发送函数的回调签名,使其接收对应平台结构体指针 添加缺失的系统头文件和openssl相关头文件 更新Makefile以包含新增的webhook源文件和依赖库
1 parent ca80d06 commit 1763fcf

File tree

9 files changed

+56
-29
lines changed

9 files changed

+56
-29
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Install dependencies
1717
run: |
1818
sudo apt-get update
19-
sudo apt-get install -y libpcap-dev libncurses5-dev make gcc
19+
sudo apt-get install -y libpcap-dev libncurses5-dev libssl-dev libcurl4-openssl-dev libjson-c-dev make gcc
2020
2121
- name: Build project
2222
run: |

.vscode/c_cpp_properties.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"/opt/homebrew/Cellar/libpcap/1.10.5/include",
88
"/opt/homebrew/Cellar/libpcap/1.10.5/include/pcap",
99
"/usr/local/include",
10+
"/opt/homebrew/opt/openssl@3/",
1011
"/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1",
1112
"/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/17/include",
1213
"/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include",

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CC := gcc
22
PKG := pkg-config
33
TARGET := anyintruder
4-
SRCS := any_intruder.c src/monitor.c src/logger.c src/ui.c src/file_utilities.c src/webhook/telegram.c src/platform_webhook.c src/http_client.c
4+
SRCS := any_intruder.c src/monitor.c src/logger.c src/ui.c src/file_utilities.c src/webhook/telegram.c src/platform_webhook.c src/http_client.c src/webhook/dingding.c src/webhook/discord.c src/webhook/wechat.c
55
OBJS := $(SRCS:.c=.o)
66

77
BREW_PCAP := $(shell command -v brew >/dev/null 2>&1 && brew --prefix libpcap 2>/dev/null || echo)
@@ -17,11 +17,11 @@ PCAP_LIBS := $(shell PKG_CONFIG_PATH="$(PKG_CONFIG_PATH)" $(PKG) --libs libpca
1717
NCURSES_CFLAGS := $(shell $(PKG) --cflags ncurses 2>/dev/null || echo)
1818
NCURSES_LIBS := $(shell $(PKG) --libs ncurses 2>/dev/null || echo)
1919

20-
CFLAGS := -Wall -O2 -Iinclude -pthread $(PCAP_CFLAGS) $(NCURSES_CFLAGS)
21-
LIBS := $(PCAP_LIBS) $(NCURSES_LIBS)
20+
CFLAGS := -Wall -O2 -Iinclude $(shell pkg-config --cflags openssl) $(shell pkg-config --cflags libcurl) $(shell pkg-config --cflags json-c) -pthread $(PCAP_CFLAGS) $(NCURSES_CFLAGS)
21+
LIBS := $(shell pkg-config --libs openssl) $(shell pkg-config --libs libcurl) $(shell pkg-config --libs json-c) $(PCAP_LIBS) $(NCURSES_LIBS)
2222

2323
ifeq ($(strip $(LIBS)),)
24-
LIBS := -lpcap -lncurses
24+
LIBS := -L/usr/local/opt/openssl/lib -lpcap -lncurses -lssl -lcrypto
2525
endif
2626

2727
.PHONY: all run clean

any_intruder.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,42 +78,51 @@ void wx_cleanup(WeChat *wechat) {
7878
* @return int 发送状态码(成功为 0x0,失败为非零值)
7979
*/
8080
int send_to(Platform Platform, const char *log_content) {
81+
DING_DING dingding;
82+
Discord discord;
83+
WeChat bot;
84+
char *webhook_url;
85+
int result = -0x1;
86+
8187
switch (Platform) {
8288
case PLATFORM_TELEGRAM:
8389
return telegram_send_message(log_content);
84-
case PLATFORM_DINGDING:
85-
DING_DING dingding;
8690

91+
case PLATFORM_DINGDING:
8792
if(dingding_init(&dingding) != 0x0) {
8893
fprintf(stderr, "Failed to initialize dingding webhook\n");
8994
return -0x1;
9095
}
96+
result = dingding_send(&dingding, log_content, dingding_cleanup);
97+
break;
9198

92-
return dingding_send(&dingding, log_content, dingding_cleanup);
9399
case PLATFORM_DISCORD:
94-
Discord discord;
95-
96-
char *webhook_url = yaml_get_value("discord_webhook_url");
97-
100+
webhook_url = yaml_get_value("discord_webhook_url");
98101
if(discord_bot_init(&discord, webhook_url) != 0x0) {
99102
fprintf(stderr, "Failed to initialize discord webhook\n");
100103
return -0x1;
101104
}
102-
103-
return discord_bot_send(&discord, "user", log_content, NULL);
105+
result = discord_bot_send(&discord, "user", log_content, NULL);
106+
break;
104107

105108
case PLATFORM_WECHAT:
106-
WeChat bot;
107-
108-
if (wechat_bot_init(&bot, "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=XXXXXX") != 0x0) {
109+
if (wechat_bot_init(&bot, "https://qyapi.d?key=XXXXXX") != 0x0) {
109110
fprintf(stderr, "初始化失败\n");
110111
return 0x1;
111112
}
112-
113-
return wechat_bot_send(&bot, log_content, wx_cleanup);
114-
default:
115-
return -0x1;
113+
result = wechat_bot_send(&bot, log_content, wx_cleanup);
114+
break;
115+
116+
case PLATFORM_SLACK:
117+
case PLATFORM_MSTEAMS:
118+
case PLATFORM_NONE:
119+
case PLATFORM_FEISHU:
120+
fprintf(stderr, "Platform not implemented yet\n");
121+
result = -0x1;
122+
break;
116123
}
124+
125+
return result;
117126
}
118127

119128
/**

includes/webhook/dingding.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ typedef struct {
1414

1515
int dingding_init(DING_DING *dingding);
1616

17-
int dingding_send(DING_DING *dingding, char *message, void *(then)(void));
17+
int dingding_send(DING_DING *dingding, const char *message, void (*then)(DING_DING *));
1818

1919
char *dingding_get_access_token(DING_DING *dingding);
2020
char *dingding_get_secret(DING_DING *dingding);

includes/webhook/wechat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ int wechat_bot_init(WeChat *wechat, const char *webhook_url);
2222
* @param message - 文本内容
2323
* @param then - 发送完成回调函数(可为空)
2424
*/
25-
int wechat_bot_send(WeChat *wechat, const char *message, void (*then)(void));
25+
int wechat_bot_send(WeChat *wechat, const char *message, void (*then)(WeChat *));
2626

2727
#endif

src/webhook/dingding.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
#include <unistd.h>
2+
#include <openssl/bio.h>
3+
#include <openssl/evp.h>
4+
#include <openssl/buffer.h>
5+
#include <openssl/hmac.h>
6+
#include <curl/curl.h>
7+
#include <openssl/bio.h>
8+
#include <openssl/evp.h>
9+
#include <openssl/buffer.h>
10+
#include <openssl/hmac.h>
11+
#include <openssl/bio.h>
12+
#include <openssl/evp.h>
13+
#include <openssl/buffer.h>
14+
#include <openssl/hmac.h>
115
#include <stdio.h>
216
#include <stdlib.h>
317
#include <string.h>
@@ -86,7 +100,7 @@ int dingding_init(DING_DING *dingding) {
86100
* @param then 发送完成后的回调函数
87101
* @return int 发送状态码(成功为 0x0,失败为非零值)
88102
*/
89-
int dingding_send(DING_DING *dingding, char *message, void (*then) (void)) {
103+
int dingding_send(DING_DING *dingding, const char *message, void (*then)(DING_DING *)) {
90104
assert(dingding != NULL);
91105
assert(message != NULL);
92106

@@ -116,8 +130,8 @@ int dingding_send(DING_DING *dingding, char *message, void (*then) (void)) {
116130

117131
if (!curl) {
118132
fprintf(stderr, "Failed to init CURL\n");
119-
free(base64_sig);
120-
free(url_sig);
133+
free(base64_signature);
134+
free(url_signature);
121135
return -2;
122136
}
123137

@@ -158,7 +172,7 @@ int dingding_send(DING_DING *dingding, char *message, void (*then) (void)) {
158172
free(url_signature);
159173
free(base64_signature);
160174

161-
if (then) then();
175+
if (then) then(dingding);
162176
sleep(0x5);
163177

164178
return (response == CURLE_OK) ? 0x0 : -0x3;

src/webhook/discord.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#include <unistd.h>
2+
#include <json-c/json.h>
3+
#include <stdio.h>
14
#include <stdlib.h>
25
#include <string.h>
36
#include <assert.h>

src/webhook/wechat.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ int wechat_bot_init(WeChat *wechat, const char *webhook_url) {
2929
* 企业微信发送文本消息(使用官方 webhook REST 接口)
3030
* 文档参考:https://developer.work.weixin.qq.com/document/path/91770
3131
*/
32-
int wechat_bot_send(WeChat *wechat, const char *message, void (*then)(void)) {
32+
int wechat_bot_send(WeChat *wechat, const char *message, void (*then)(WeChat *)) {
3333
char payload[0x1000];
3434

3535
if (!wechat || !wechat->webhook_url || !message) {
@@ -77,7 +77,7 @@ int wechat_bot_send(WeChat *wechat, const char *message, void (*then)(void)) {
7777
curl_slist_free_all(headers);
7878
curl_easy_cleanup(curl);
7979

80-
if (then) then();
80+
if (then) then(wechat);
8181

8282
return 0x0;
8383
}

0 commit comments

Comments
 (0)