mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
feat: http post secret
This commit is contained in:
@@ -30,6 +30,7 @@ export class ConfigUtil {
|
|||||||
let ob11Default: OB11Config = {
|
let ob11Default: OB11Config = {
|
||||||
httpPort: 3000,
|
httpPort: 3000,
|
||||||
httpHosts: [],
|
httpHosts: [],
|
||||||
|
httpSecret: "",
|
||||||
wsPort: 3001,
|
wsPort: 3001,
|
||||||
wsHosts: [],
|
wsHosts: [],
|
||||||
enableHttp: true,
|
enableHttp: true,
|
||||||
|
@@ -6,6 +6,7 @@ import {WebSocket as WebSocketClass} from "ws";
|
|||||||
import {wsReply} from "./ws/reply";
|
import {wsReply} from "./ws/reply";
|
||||||
import {log} from "../../common/utils/log";
|
import {log} from "../../common/utils/log";
|
||||||
import {getConfigUtil} from "../../common/config";
|
import {getConfigUtil} from "../../common/config";
|
||||||
|
import crypto from 'crypto';
|
||||||
|
|
||||||
export type PostEventType = OB11Message | OB11BaseMetaEvent | OB11BaseNoticeEvent
|
export type PostEventType = OB11Message | OB11BaseMetaEvent | OB11BaseNoticeEvent
|
||||||
|
|
||||||
@@ -39,18 +40,26 @@ export function postOB11Event(msg: PostEventType, reportSelf = false) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (config.ob11.enableHttpPost) {
|
if (config.ob11.enableHttpPost) {
|
||||||
|
const msgStr = JSON.stringify(msg);
|
||||||
|
const hmac = crypto.createHmac('sha1', config.ob11.httpSecret);
|
||||||
|
hmac.update(msgStr);
|
||||||
|
const sig = hmac.digest('hex');
|
||||||
|
let headers = {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"x-self-id": selfInfo.uin
|
||||||
|
}
|
||||||
|
if (config.ob11.httpSecret) {
|
||||||
|
headers["x-signature"] = "sha1=" + sig;
|
||||||
|
}
|
||||||
for (const host of config.ob11.httpHosts) {
|
for (const host of config.ob11.httpHosts) {
|
||||||
fetch(host, {
|
fetch(host, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers,
|
||||||
"Content-Type": "application/json",
|
body: msgStr
|
||||||
"x-self-id": selfInfo.uin
|
|
||||||
},
|
|
||||||
body: JSON.stringify(msg)
|
|
||||||
}).then((res: any) => {
|
}).then((res: any) => {
|
||||||
log(`新消息事件HTTP上报成功: ${host} ` + JSON.stringify(msg));
|
log(`新消息事件HTTP上报成功: ${host} ` + msgStr);
|
||||||
}, (err: any) => {
|
}, (err: any) => {
|
||||||
log(`新消息事件HTTP上报失败: ${host} ` + err + JSON.stringify(msg));
|
log(`新消息事件HTTP上报失败: ${host} `, err, msg);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -55,6 +55,14 @@ async function onSettingWindowCreated(view: Element) {
|
|||||||
SettingSwitch('ob11.enableHttpPost', config.ob11.enableHttpPost, {'control-display-id': 'config-ob11-httpHosts'}),
|
SettingSwitch('ob11.enableHttpPost', config.ob11.enableHttpPost, {'control-display-id': 'config-ob11-httpHosts'}),
|
||||||
),
|
),
|
||||||
`<div class="config-host-list" id="config-ob11-httpHosts" ${config.ob11.enableHttpPost ? '' : 'is-hidden'}>
|
`<div class="config-host-list" id="config-ob11-httpHosts" ${config.ob11.enableHttpPost ? '' : 'is-hidden'}>
|
||||||
|
<setting-item data-direction="row">
|
||||||
|
<div>
|
||||||
|
<setting-text>HTTP 事件上报密钥</setting-text>
|
||||||
|
</div>
|
||||||
|
<div class="q-input">
|
||||||
|
<input id="config-ob11-httpSecret" class="q-input__inner" data-config-key="ob11.httpSecret" type="text" value="${config.ob11.httpSecret}" placeholder="未设置" />
|
||||||
|
</div>
|
||||||
|
</setting-item>
|
||||||
<setting-item data-direction="row">
|
<setting-item data-direction="row">
|
||||||
<div>
|
<div>
|
||||||
<setting-text>HTTP 事件上报地址</setting-text>
|
<setting-text>HTTP 事件上报地址</setting-text>
|
||||||
|
Reference in New Issue
Block a user