mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
chore: Conflict resolution
This commit is contained in:
parent
d1eef6759c
commit
9b0f2d0983
@ -4,7 +4,7 @@
|
||||
"name": "LLOneBot",
|
||||
"slug": "LLOneBot",
|
||||
"description": "LiteLoaderQQNT的OneBotApi",
|
||||
"version": "3.3.1",
|
||||
"version": "3.4.0",
|
||||
"thumbnail": "./icon.png",
|
||||
"authors": [
|
||||
{
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Config } from "./types";
|
||||
import {Config} from "./types";
|
||||
|
||||
const fs = require("fs");
|
||||
|
||||
@ -14,7 +14,7 @@ export class ConfigUtil {
|
||||
httpPort: 3000,
|
||||
httpHosts: [],
|
||||
wsPort: 3001,
|
||||
wsHosts: []
|
||||
wsHosts: [],
|
||||
token: "",
|
||||
enableBase64: false,
|
||||
debug: false,
|
||||
@ -29,16 +29,20 @@ export class ConfigUtil {
|
||||
try {
|
||||
jsonData = JSON.parse(data)
|
||||
}
|
||||
catch (e){
|
||||
|
||||
catch (e) {}
|
||||
if (!jsonData.httpHosts) {
|
||||
jsonData.httpHosts = []
|
||||
}
|
||||
if (!jsonData.hosts) {
|
||||
jsonData.hosts = []
|
||||
if (!jsonData.wsHosts) {
|
||||
jsonData.wsHosts = []
|
||||
}
|
||||
if (!jsonData.wsPort){
|
||||
if (!jsonData.wsPort) {
|
||||
jsonData.wsPort = 3001
|
||||
}
|
||||
if (!jsonData.token){
|
||||
if (!jsonData.httpPort) {
|
||||
jsonData.httpPort = 3000
|
||||
}
|
||||
if (!jsonData.token) {
|
||||
jsonData.token = ""
|
||||
}
|
||||
return jsonData;
|
||||
|
@ -1,6 +1,5 @@
|
||||
import { NTQQApi } from '../ntqqapi/ntcall';
|
||||
import { Friend, Group, GroupMember, RawMessage, SelfInfo } from "../ntqqapi/types";
|
||||
import { log } from "./utils";
|
||||
import {NTQQApi} from '../ntqqapi/ntcall';
|
||||
import {Friend, Group, GroupMember, RawMessage, SelfInfo} from "../ntqqapi/types";
|
||||
|
||||
export let groups: Group[] = []
|
||||
export let friends: Friend[] = []
|
||||
|
@ -2,7 +2,6 @@ import * as path from "path";
|
||||
import {selfInfo} from "./data";
|
||||
import {ConfigUtil} from "./config";
|
||||
import util from "util";
|
||||
import { sendLog } from '../main/ipcsend';
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
|
2
src/global.d.ts
vendored
2
src/global.d.ts
vendored
@ -1,4 +1,4 @@
|
||||
import { Config } from "./common/types";
|
||||
import {Config} from "./common/types";
|
||||
|
||||
|
||||
declare var llonebot: {
|
||||
|
@ -1,5 +1,5 @@
|
||||
import {webContents} from 'electron';
|
||||
import { CHANNEL_LOG } from '../common/channels';
|
||||
import {CHANNEL_LOG} from '../common/channels';
|
||||
|
||||
|
||||
function sendIPCMsg(channel: string, ...data: any) {
|
||||
|
@ -1,16 +1,18 @@
|
||||
// 运行在 Electron 主进程 下的插件入口
|
||||
|
||||
import { BrowserWindow, ipcMain } from 'electron';
|
||||
import {BrowserWindow, ipcMain} from 'electron';
|
||||
|
||||
import { Config } from "../common/types";
|
||||
import {initWebsocket, postMsg, startExpress, startWebsocketServer} from "../onebot11/server";
|
||||
import { CHANNEL_GET_CONFIG, CHANNEL_LOG, CHANNEL_SET_CONFIG, } from "../common/channels";
|
||||
import { CONFIG_DIR, getConfigUtil, log } from "../common/utils";
|
||||
import { addHistoryMsg, getGroupMember, msgHistory, selfInfo, uidMaps } from "../common/data";
|
||||
import { hookNTQQApiReceive, ReceiveCmd, registerReceiveHook } from "../ntqqapi/hook";
|
||||
import { OB11Constructor } from "../onebot11/constructor";
|
||||
import { NTQQApi } from "../ntqqapi/ntcall";
|
||||
import { ChatType, RawMessage } from "../ntqqapi/types";
|
||||
import {Config} from "../common/types";
|
||||
import {postMsg, setToken, startHTTPServer, initWebsocket} from "../onebot11/server";
|
||||
import {CHANNEL_GET_CONFIG, CHANNEL_LOG, CHANNEL_SET_CONFIG,} from "../common/channels";
|
||||
import {CONFIG_DIR, getConfigUtil, log} from "../common/utils";
|
||||
import {addHistoryMsg, getGroupMember, msgHistory, selfInfo} from "../common/data";
|
||||
import {hookNTQQApiReceive, ReceiveCmd, registerReceiveHook} from "../ntqqapi/hook";
|
||||
import {OB11Constructor} from "../onebot11/constructor";
|
||||
import {NTQQApi} from "../ntqqapi/ntcall";
|
||||
import {ChatType, RawMessage} from "../ntqqapi/types";
|
||||
import {OB11FriendRecallNoticeEvent} from "../onebot11/event/notice/OB11FriendRecallNoticeEvent";
|
||||
import {OB11GroupRecallNoticeEvent} from "../onebot11/event/notice/OB11GroupRecallNoticeEvent";
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
@ -32,11 +34,11 @@ function onLoad() {
|
||||
ipcMain.on(CHANNEL_SET_CONFIG, (event: any, arg: Config) => {
|
||||
let oldConfig = getConfigUtil().getConfig();
|
||||
getConfigUtil().setConfig(arg)
|
||||
if (arg.port != oldConfig.port) {
|
||||
startHTTPServer(arg.port)
|
||||
if (arg.httpPort != oldConfig.httpPort) {
|
||||
startHTTPServer(arg.httpPort)
|
||||
}
|
||||
if (arg.wsPort != oldConfig.wsPort) {
|
||||
startWebsocketServer(arg.wsPort)
|
||||
initWebsocket(arg.wsPort)
|
||||
}
|
||||
if (arg.token != oldConfig.token) {
|
||||
setToken(arg.token);
|
||||
@ -87,8 +89,8 @@ function onLoad() {
|
||||
continue
|
||||
}
|
||||
if (message.chatType == ChatType.friend) {
|
||||
const friendRecallEvent = OB11Constructor.friendRecallEvent(message.senderUin, oriMessage.msgShortId)
|
||||
postMsg(friendRecallEvent)
|
||||
const friendRecallEvent = new OB11FriendRecallNoticeEvent(parseInt(message.senderUin), oriMessage.msgShortId);
|
||||
postMsg(friendRecallEvent);
|
||||
} else if (message.chatType == ChatType.group) {
|
||||
let operatorId = message.senderUin
|
||||
for (const element of message.elements) {
|
||||
@ -96,13 +98,14 @@ function onLoad() {
|
||||
const operator = await getGroupMember(message.peerUin, null, operatorUid)
|
||||
operatorId = operator.uin
|
||||
}
|
||||
const groupRecallEvent = OB11Constructor.groupRecallEvent(
|
||||
message.peerUin,
|
||||
message.senderUin,
|
||||
operatorId,
|
||||
const groupRecallEvent = new OB11GroupRecallNoticeEvent(
|
||||
parseInt(message.peerUin),
|
||||
parseInt(message.senderUin),
|
||||
parseInt(operatorId),
|
||||
oriMessage.msgShortId
|
||||
)
|
||||
postMsg(groupRecallEvent)
|
||||
|
||||
postMsg(groupRecallEvent);
|
||||
}
|
||||
continue
|
||||
}
|
||||
@ -125,8 +128,7 @@ function onLoad() {
|
||||
|
||||
const config = getConfigUtil().getConfig()
|
||||
startHTTPServer(config.httpPort)
|
||||
startWebsocketServer(config.wsPort);
|
||||
initWebsocket();
|
||||
initWebsocket(config.wsPort);
|
||||
setToken(config.token)
|
||||
log("LLOneBot start")
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
import {
|
||||
AtType,
|
||||
ElementType,
|
||||
SendFaceElement,
|
||||
SendPicElement,
|
||||
SendPttElement,
|
||||
SendReplyElement,
|
||||
SendTextElement,
|
||||
AtType,
|
||||
SendFaceElement
|
||||
SendTextElement
|
||||
} from "./types";
|
||||
import { NTQQApi } from "./ntcall";
|
||||
import {NTQQApi} from "./ntcall";
|
||||
|
||||
|
||||
export class SendMsgElementConstructor {
|
||||
|
@ -1,15 +1,12 @@
|
||||
import {BrowserWindow} from 'electron';
|
||||
import {log, sleep} from "../common/utils";
|
||||
import {NTQQApi, NTQQApiClass, sendMessagePool} from "./ntcall";
|
||||
import {Group, GroupMember, RawMessage, User} from "./types";
|
||||
import {Group, RawMessage, User} from "./types";
|
||||
import {addHistoryMsg, friends, groups, msgHistory} from "../common/data";
|
||||
import {v4 as uuidv4} from 'uuid';
|
||||
import {callEvent, EventType} from "../onebot11/event/manager";
|
||||
import {OB11Message} from "../onebot11/types";
|
||||
import {OB11Constructor} from "../onebot11/constructor";
|
||||
import BaseMessageEvent from "../onebot11/event/BaseMessageEvent";
|
||||
import GroupDecreaseEvent from "../onebot11/event/GroupDecreaseEvent";
|
||||
import GroupIncreaseEvent from "../onebot11/event/GroupIncreaseEvent";
|
||||
import {OB11GroupDecreaseEvent} from "../onebot11/event/notice/OB11GroupDecreaseEvent";
|
||||
import {OB11GroupIncreaseEvent} from "../onebot11/event/notice/OB11GroupIncreaseEvent";
|
||||
import {postMsg} from "../onebot11/server";
|
||||
|
||||
export let hookApiCallbacks: Record<string, (apiReturn: any) => void> = {}
|
||||
|
||||
@ -140,7 +137,7 @@ async function processGroupEvent(payload) {
|
||||
|
||||
for (const member of oldMembers) {
|
||||
if (!newMembersSet.has(member.uin)) {
|
||||
callEvent(new GroupDecreaseEvent(group.groupCode, parseInt(member.uin)));
|
||||
postMsg(new OB11GroupDecreaseEvent(group.groupCode, parseInt(member.uin)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -159,7 +156,7 @@ async function processGroupEvent(payload) {
|
||||
group.members = newMembers;
|
||||
for (const member of newMembers) {
|
||||
if (!oldMembersSet.has(member.uin)) {
|
||||
callEvent(new GroupIncreaseEvent(group.groupCode, parseInt(member.uin)));
|
||||
postMsg(new OB11GroupIncreaseEvent(group.groupCode, parseInt(member.uin)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -211,22 +208,10 @@ registerReceiveHook<{
|
||||
}
|
||||
})
|
||||
|
||||
registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.UPDATE_MSG, (payload) => {
|
||||
for (const message of payload.msgList) {
|
||||
addHistoryMsg(message)
|
||||
}
|
||||
})
|
||||
|
||||
registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.NEW_MSG, (payload) => {
|
||||
for (const message of payload.msgList) {
|
||||
// log("收到新消息,push到历史记录", message)
|
||||
addHistoryMsg(message)
|
||||
|
||||
OB11Constructor.message(message).then(
|
||||
function (message) {
|
||||
callEvent<OB11Message>(new BaseMessageEvent(), message);
|
||||
}
|
||||
);
|
||||
}
|
||||
const msgIds = Object.keys(msgHistory);
|
||||
if (msgIds.length > 30000) {
|
||||
|
@ -1,12 +1,8 @@
|
||||
import { ipcMain } from "electron";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import { ReceiveCmd, hookApiCallbacks, registerReceiveHook, removeReceiveHook } from "./hook";
|
||||
import { log } from "../common/utils";
|
||||
import { ChatType, Friend, PicElement, SelfInfo, User } from "./types";
|
||||
import { Group } from "./types";
|
||||
import { GroupMember } from "./types";
|
||||
import { RawMessage } from "./types";
|
||||
import { SendMessageElement } from "./types";
|
||||
import {ipcMain} from "electron";
|
||||
import {v4 as uuidv4} from "uuid";
|
||||
import {hookApiCallbacks, ReceiveCmd, registerReceiveHook, removeReceiveHook} from "./hook";
|
||||
import {log} from "../common/utils";
|
||||
import {ChatType, Friend, Group, GroupMember, RawMessage, SelfInfo, SendMessageElement, User} from "./types";
|
||||
import * as fs from "fs";
|
||||
|
||||
interface IPCReceiveEvent {
|
||||
|
@ -1,3 +1,5 @@
|
||||
import {log} from "../common/utils";
|
||||
|
||||
const WebSocket = require("ws");
|
||||
|
||||
class ReconnectingWebsocket {
|
||||
@ -35,12 +37,12 @@ class ReconnectingWebsocket {
|
||||
perMessageDeflate: false
|
||||
});
|
||||
|
||||
console.log("Trying to connect to the websocket server: " + this.url);
|
||||
log("Trying to connect to the websocket server: " + this.url);
|
||||
|
||||
const instance = this;
|
||||
|
||||
this.websocket.on("open", function open() {
|
||||
console.log("Connected to the websocket server: " + instance.url);
|
||||
log("Connected to the websocket server: " + instance.url);
|
||||
instance.onopen();
|
||||
});
|
||||
|
||||
@ -53,7 +55,7 @@ class ReconnectingWebsocket {
|
||||
this.websocket.on("ping", this.heartbeat);
|
||||
|
||||
this.websocket.on("close", function close() {
|
||||
console.log("The websocket connection: " + instance.url + " closed, trying reconnecting...");
|
||||
log("The websocket connection: " + instance.url + " closed, trying reconnecting...");
|
||||
instance.onclose();
|
||||
|
||||
setTimeout(instance.reconnect, 3000);
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { ActionName } from "./types";
|
||||
import {ActionName} from "./types";
|
||||
import CanSendRecord from "./CanSendRecord";
|
||||
|
||||
interface ReturnType{
|
||||
|
@ -1,5 +1,5 @@
|
||||
import BaseAction from "./BaseAction";
|
||||
import { ActionName } from "./types";
|
||||
import {ActionName} from "./types";
|
||||
|
||||
interface ReturnType{
|
||||
yes: boolean
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { ActionName } from "./types";
|
||||
import {ActionName} from "./types";
|
||||
import BaseAction from "./BaseAction";
|
||||
import { NTQQApi } from "../../ntqqapi/ntcall";
|
||||
import { getHistoryMsgByShortId, msgHistory } from "../../common/data";
|
||||
import {NTQQApi} from "../../ntqqapi/ntcall";
|
||||
import {getHistoryMsgByShortId} from "../../common/data";
|
||||
|
||||
interface Payload {
|
||||
message_id: number
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { OB11User } from '../types';
|
||||
import { OB11Constructor } from "../constructor";
|
||||
import { friends } from "../../common/data";
|
||||
import {OB11User} from '../types';
|
||||
import {OB11Constructor} from "../constructor";
|
||||
import {friends} from "../../common/data";
|
||||
import BaseAction from "./BaseAction";
|
||||
import { ActionName } from "./types";
|
||||
import {ActionName} from "./types";
|
||||
|
||||
|
||||
class GetFriendList extends BaseAction<null, OB11User[]> {
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { OB11Group } from '../types';
|
||||
import { getGroup } from "../../common/data";
|
||||
import { OB11Constructor } from "../constructor";
|
||||
import {OB11Group} from '../types';
|
||||
import {getGroup} from "../../common/data";
|
||||
import {OB11Constructor} from "../constructor";
|
||||
import BaseAction from "./BaseAction";
|
||||
import { ActionName } from "./types";
|
||||
import {ActionName} from "./types";
|
||||
|
||||
interface PayloadType {
|
||||
group_id: number
|
||||
|
@ -1,9 +1,8 @@
|
||||
import { OB11Group } from '../types';
|
||||
import { OB11Constructor } from "../constructor";
|
||||
import { groups } from "../../common/data";
|
||||
import {OB11Group} from '../types';
|
||||
import {OB11Constructor} from "../constructor";
|
||||
import {groups} from "../../common/data";
|
||||
import BaseAction from "./BaseAction";
|
||||
import { ActionName } from "./types";
|
||||
|
||||
import {ActionName} from "./types";
|
||||
|
||||
|
||||
class GetGroupList extends BaseAction<null, OB11Group[]> {
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { OB11GroupMember } from '../types';
|
||||
import { getGroupMember } from "../../common/data";
|
||||
import { OB11Constructor } from "../constructor";
|
||||
import {OB11GroupMember} from '../types';
|
||||
import {getGroupMember} from "../../common/data";
|
||||
import {OB11Constructor} from "../constructor";
|
||||
import BaseAction from "./BaseAction";
|
||||
import { ActionName } from "./types";
|
||||
import {ActionName} from "./types";
|
||||
|
||||
|
||||
export interface PayloadType {
|
||||
|
@ -1,9 +1,9 @@
|
||||
import { OB11GroupMember } from '../types';
|
||||
import { getGroup } from "../../common/data";
|
||||
import { NTQQApi } from "../../ntqqapi/ntcall";
|
||||
import { OB11Constructor } from "../constructor";
|
||||
import {OB11GroupMember} from '../types';
|
||||
import {getGroup} from "../../common/data";
|
||||
import {NTQQApi} from "../../ntqqapi/ntcall";
|
||||
import {OB11Constructor} from "../constructor";
|
||||
import BaseAction from "./BaseAction";
|
||||
import { ActionName } from "./types";
|
||||
import {ActionName} from "./types";
|
||||
|
||||
export interface PayloadType {
|
||||
group_id: number
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { OB11User } from '../types';
|
||||
import { OB11Constructor } from "../constructor";
|
||||
import { selfInfo } from "../../common/data";
|
||||
import {OB11User} from '../types';
|
||||
import {OB11Constructor} from "../constructor";
|
||||
import {selfInfo} from "../../common/data";
|
||||
import BaseAction from "./BaseAction";
|
||||
import { ActionName } from "./types";
|
||||
import {ActionName} from "./types";
|
||||
|
||||
|
||||
class GetLoginInfo extends BaseAction<null, OB11User> {
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { getHistoryMsgByShortId, msgHistory } from "../../common/data";
|
||||
import { OB11Message } from '../types';
|
||||
import { OB11Constructor } from "../constructor";
|
||||
import {getHistoryMsgByShortId} from "../../common/data";
|
||||
import {OB11Message} from '../types';
|
||||
import {OB11Constructor} from "../constructor";
|
||||
import BaseAction from "./BaseAction";
|
||||
import { ActionName } from "./types";
|
||||
import {ActionName} from "./types";
|
||||
|
||||
|
||||
export interface PayloadType {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import BaseAction from "./BaseAction";
|
||||
import {OB11Status} from "../types";
|
||||
import { ActionName } from "./types";
|
||||
import {ActionName} from "./types";
|
||||
|
||||
|
||||
export default class GetStatus extends BaseAction<any, OB11Status> {
|
||||
|
@ -1,7 +1,7 @@
|
||||
import BaseAction from "./BaseAction";
|
||||
import { OB11Version } from "../types";
|
||||
import {OB11Version} from "../types";
|
||||
import {version} from "../../common/data";
|
||||
import { ActionName } from "./types";
|
||||
import {ActionName} from "./types";
|
||||
|
||||
export default class GetVersionInfo extends BaseAction<any, OB11Version>{
|
||||
actionName = ActionName.GetVersionInfo
|
||||
|
@ -1,5 +1,5 @@
|
||||
import SendMsg from "./SendMsg";
|
||||
import { ActionName } from "./types";
|
||||
import {ActionName} from "./types";
|
||||
|
||||
|
||||
class SendGroupMsg extends SendMsg{
|
||||
|
@ -1,12 +1,12 @@
|
||||
import { AtType, ChatType, Group, SendMessageElement } from "../../ntqqapi/types";
|
||||
import { addHistoryMsg, friends, getGroup, getHistoryMsgByShortId, getStrangerByUin, } from "../../common/data";
|
||||
import { OB11MessageData, OB11MessageDataType, OB11PostSendMsg } from '../types';
|
||||
import { NTQQApi, Peer } from "../../ntqqapi/ntcall";
|
||||
import { SendMsgElementConstructor } from "../../ntqqapi/constructor";
|
||||
import { uri2local } from "../utils";
|
||||
import { v4 as uuid4 } from 'uuid';
|
||||
import {AtType, ChatType, Group, SendMessageElement} from "../../ntqqapi/types";
|
||||
import {addHistoryMsg, friends, getGroup, getHistoryMsgByShortId, getStrangerByUin,} from "../../common/data";
|
||||
import {OB11MessageData, OB11MessageDataType, OB11PostSendMsg} from '../types';
|
||||
import {NTQQApi, Peer} from "../../ntqqapi/ntcall";
|
||||
import {SendMsgElementConstructor} from "../../ntqqapi/constructor";
|
||||
import {uri2local} from "../utils";
|
||||
import {v4 as uuid4} from 'uuid';
|
||||
import BaseAction from "./BaseAction";
|
||||
import { ActionName } from "./types";
|
||||
import {ActionName} from "./types";
|
||||
import * as fs from "fs";
|
||||
|
||||
export interface ReturnDataType {
|
||||
|
@ -1,5 +1,5 @@
|
||||
import SendMsg from "./SendMsg";
|
||||
import { ActionName } from "./types";
|
||||
import {ActionName} from "./types";
|
||||
|
||||
class SendPrivateMsg extends SendMsg {
|
||||
actionName = ActionName.SendPrivateMsg
|
||||
|
@ -21,7 +21,7 @@ export const actionHandlers = [
|
||||
new GetFriendList(),
|
||||
new GetGroupList(), new GetGroupInfo(), new GetGroupMemberList(), new GetGroupMemberInfo(),
|
||||
new SendGroupMsg(), new SendPrivateMsg(), new SendMsg(),
|
||||
new DeleteMsg().
|
||||
new DeleteMsg(),
|
||||
new GetVersionInfo(),
|
||||
new CanSendRecord(),
|
||||
new CanSendImage(),
|
||||
|
@ -1,5 +1,3 @@
|
||||
import GetVersionInfo from "./GetVersionInfo";
|
||||
|
||||
export type BaseCheckResult = ValidCheckResult | InvalidCheckResult
|
||||
|
||||
export interface ValidCheckResult {
|
||||
@ -13,7 +11,7 @@ export interface InvalidCheckResult {
|
||||
[k: string | number]: any
|
||||
}
|
||||
|
||||
export enum ActionName{
|
||||
export enum ActionName {
|
||||
GetLoginInfo = "get_login_info",
|
||||
GetFriendList = "get_friend_list",
|
||||
GetGroupInfo = "get_group_info",
|
||||
|
@ -6,8 +6,7 @@ export class OB11Response {
|
||||
status: status,
|
||||
retcode: retcode,
|
||||
data: data,
|
||||
message: message,
|
||||
echo,
|
||||
message: message
|
||||
}
|
||||
}
|
||||
static ok<T>(data: T) {
|
||||
|
@ -1,19 +1,11 @@
|
||||
import {
|
||||
OB11MessageDataType,
|
||||
OB11GroupMemberRole,
|
||||
OB11Message,
|
||||
OB11Group,
|
||||
OB11GroupMember,
|
||||
OB11User
|
||||
} from "./types";
|
||||
import { AtType, ChatType, Group, GroupMember, IMAGE_HTTP_HOST, RawMessage, SelfInfo, User } from '../ntqqapi/types';
|
||||
import { getFriend, getGroupMember, getHistoryMsgBySeq, heartInterval, msgHistory, selfInfo } from '../common/data';
|
||||
import { file2base64, getConfigUtil, log } from "../common/utils";
|
||||
import { NTQQApi } from "../ntqqapi/ntcall";
|
||||
import {OB11EventConstructor} from "./events/constructor";
|
||||
import {OB11Group, OB11GroupMember, OB11GroupMemberRole, OB11Message, OB11MessageDataType, OB11User} from "./types";
|
||||
import {AtType, ChatType, Group, GroupMember, IMAGE_HTTP_HOST, RawMessage, SelfInfo, User} from '../ntqqapi/types';
|
||||
import {getFriend, getGroupMember, getHistoryMsgBySeq, selfInfo} from '../common/data';
|
||||
import {file2base64, getConfigUtil, log} from "../common/utils";
|
||||
import {NTQQApi} from "../ntqqapi/ntcall";
|
||||
|
||||
|
||||
export class OB11Constructor extends OB11EventConstructor{
|
||||
export class OB11Constructor {
|
||||
static async message(msg: RawMessage): Promise<OB11Message> {
|
||||
|
||||
const {enableBase64} = getConfigUtil().getConfig()
|
||||
|
@ -1,10 +0,0 @@
|
||||
import {selfInfo} from "../../common/data";
|
||||
import {EventType} from "./manager";
|
||||
|
||||
class BaseEvent {
|
||||
time = new Date().getTime();
|
||||
self_id = selfInfo.uin;
|
||||
post_type: EventType;
|
||||
}
|
||||
|
||||
export default BaseEvent;
|
@ -1,8 +0,0 @@
|
||||
import BaseEvent from "./BaseEvent";
|
||||
import {EventType} from "./manager";
|
||||
|
||||
class BaseMessageEvent extends BaseEvent {
|
||||
post_type = EventType.MESSAGE;
|
||||
}
|
||||
|
||||
export default BaseMessageEvent
|
15
src/onebot11/event/OB11BaseEvent.ts
Normal file
15
src/onebot11/event/OB11BaseEvent.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import {selfInfo} from "../../common/data";
|
||||
|
||||
export enum EventType {
|
||||
META = "meta_event",
|
||||
REQUEST = "request",
|
||||
NOTICE = "notice",
|
||||
MESSAGE = "message"
|
||||
}
|
||||
|
||||
|
||||
export abstract class OB11BaseEvent {
|
||||
time = new Date().getTime();
|
||||
self_id = selfInfo.uin;
|
||||
post_type: EventType;
|
||||
}
|
@ -1,32 +1,24 @@
|
||||
import {selfInfo} from "../../common/data";
|
||||
import BaseEvent from "./BaseEvent";
|
||||
import * as websocket from "ws";
|
||||
import {PostMsgType, wsReply} from "../server";
|
||||
import ReconnectingWebsocket from "../ReconnectingWebsocket";
|
||||
|
||||
const websocketList = [];
|
||||
|
||||
export enum EventType {
|
||||
META = "meta_event",
|
||||
REQUEST = "request",
|
||||
NOTICE = "notice",
|
||||
MESSAGE = "message"
|
||||
}
|
||||
|
||||
export function registerEventSender(ws) {
|
||||
export function registerEventSender(ws: websocket.WebSocket | ReconnectingWebsocket) {
|
||||
websocketList.push(ws);
|
||||
}
|
||||
|
||||
export function unregisterEventSender(ws) {
|
||||
export function unregisterEventSender(ws: websocket.WebSocket | ReconnectingWebsocket) {
|
||||
let index = websocketList.indexOf(ws);
|
||||
if (index !== -1) {
|
||||
websocketList.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
export function callEvent<DataType>(event: BaseEvent, data: DataType = null) {
|
||||
|
||||
const assignedEvent = (data == null ? event : Object.assign(event, data));
|
||||
for (const ws of websocketList) {
|
||||
ws.send(
|
||||
JSON.stringify(assignedEvent)
|
||||
);
|
||||
}
|
||||
export function callEvent(event: PostMsgType) {
|
||||
new Promise(() => {
|
||||
for (const ws of websocketList) {
|
||||
wsReply(ws, event);
|
||||
}
|
||||
}).then()
|
||||
}
|
5
src/onebot11/event/message/OB11BaseMessageEvent.ts
Normal file
5
src/onebot11/event/message/OB11BaseMessageEvent.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import {EventType, OB11BaseEvent} from "../OB11BaseEvent";
|
||||
|
||||
export abstract class OB11BaseMessageEvent extends OB11BaseEvent {
|
||||
post_type = EventType.MESSAGE;
|
||||
}
|
6
src/onebot11/event/meta/OB11BaseMetaEvent.ts
Normal file
6
src/onebot11/event/meta/OB11BaseMetaEvent.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import {EventType, OB11BaseEvent} from "../OB11BaseEvent";
|
||||
|
||||
export abstract class OB11BaseMetaEvent extends OB11BaseEvent {
|
||||
post_type = EventType.META;
|
||||
meta_event_type: string;
|
||||
}
|
21
src/onebot11/event/meta/OB11HeartbeatEvent.ts
Normal file
21
src/onebot11/event/meta/OB11HeartbeatEvent.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import {OB11BaseMetaEvent} from "./OB11BaseMetaEvent";
|
||||
|
||||
interface HeartbeatStatus {
|
||||
online: boolean | null,
|
||||
good: boolean
|
||||
}
|
||||
|
||||
export class OB11HeartbeatEvent extends OB11BaseMetaEvent {
|
||||
meta_event_type = "heartbeat";
|
||||
status: HeartbeatStatus;
|
||||
interval: number;
|
||||
|
||||
public constructor(isOnline: boolean | null, isGood: boolean, interval: number) {
|
||||
super();
|
||||
this.interval = interval;
|
||||
this.status = {
|
||||
online: isOnline,
|
||||
good: isGood
|
||||
}
|
||||
}
|
||||
}
|
17
src/onebot11/event/meta/OB11LifeCycleEvent.ts
Normal file
17
src/onebot11/event/meta/OB11LifeCycleEvent.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import {OB11BaseMetaEvent} from "./OB11BaseMetaEvent";
|
||||
|
||||
export enum LifeCycleSubType {
|
||||
ENABLE = "enable",
|
||||
DISABLE = "disable",
|
||||
CONNECT = "connect"
|
||||
}
|
||||
|
||||
export class OB11LifeCycleEvent extends OB11BaseMetaEvent {
|
||||
meta_event_type = "lifecycle";
|
||||
sub_type: LifeCycleSubType;
|
||||
|
||||
public constructor(subType: LifeCycleSubType) {
|
||||
super();
|
||||
this.sub_type = subType;
|
||||
}
|
||||
}
|
5
src/onebot11/event/notice/OB11BaseNoticeEvent.ts
Normal file
5
src/onebot11/event/notice/OB11BaseNoticeEvent.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import {EventType, OB11BaseEvent} from "../OB11BaseEvent";
|
||||
|
||||
export abstract class OB11BaseNoticeEvent extends OB11BaseEvent {
|
||||
post_type = EventType.NOTICE;
|
||||
}
|
13
src/onebot11/event/notice/OB11FriendRecallNoticeEvent.ts
Normal file
13
src/onebot11/event/notice/OB11FriendRecallNoticeEvent.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import {OB11BaseNoticeEvent} from "./OB11BaseNoticeEvent";
|
||||
|
||||
export class OB11FriendRecallNoticeEvent extends OB11BaseNoticeEvent {
|
||||
notice_type = "friend_recall"
|
||||
user_id: number
|
||||
message_id: number
|
||||
|
||||
public constructor(userId: number, messageId: number) {
|
||||
super();
|
||||
this.user_id = userId;
|
||||
this.message_id = messageId;
|
||||
}
|
||||
}
|
6
src/onebot11/event/notice/OB11GroupAdminNoticeEvent.ts
Normal file
6
src/onebot11/event/notice/OB11GroupAdminNoticeEvent.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import {OB11BaseNoticeEvent} from "./OB11BaseNoticeEvent";
|
||||
|
||||
export class OB11GroupAdminNoticeEvent extends OB11BaseNoticeEvent {
|
||||
notice_type = "group_admin"
|
||||
sub_type: string // "set" | "unset"
|
||||
}
|
@ -1,13 +1,9 @@
|
||||
import BaseEvent from "./BaseEvent";
|
||||
import {EventType} from "./manager";
|
||||
import {OB11GroupNoticeEvent} from "./OB11GroupNoticeEvent";
|
||||
|
||||
class GroupDecreaseEvent extends BaseEvent {
|
||||
post_type = EventType.NOTICE;
|
||||
export class OB11GroupDecreaseEvent extends OB11GroupNoticeEvent {
|
||||
notice_type = "group_decrease";
|
||||
subtype = "leave"; // TODO: 实现其他几种子类型的识别
|
||||
group_id: number;
|
||||
sub_type = "leave"; // TODO: 实现其他几种子类型的识别 ("leave" | "kick" | "kick_me")
|
||||
operate_id: number;
|
||||
user_id: number;
|
||||
|
||||
constructor(groupId: number, userId: number) {
|
||||
super();
|
||||
@ -16,5 +12,3 @@ class GroupDecreaseEvent extends BaseEvent {
|
||||
this.user_id = userId;
|
||||
}
|
||||
}
|
||||
|
||||
export default GroupDecreaseEvent
|
@ -1,13 +1,9 @@
|
||||
import BaseEvent from "./BaseEvent";
|
||||
import {EventType} from "./manager";
|
||||
import {OB11GroupNoticeEvent} from "./OB11GroupNoticeEvent";
|
||||
|
||||
class GroupIncreaseEvent extends BaseEvent {
|
||||
post_type = EventType.NOTICE;
|
||||
export class OB11GroupIncreaseEvent extends OB11GroupNoticeEvent {
|
||||
notice_type = "group_increase";
|
||||
subtype = "approve"; // TODO: 实现其他几种子类型的识别
|
||||
group_id: number;
|
||||
sub_type = "approve"; // TODO: 实现其他几种子类型的识别 ("approve" | "invite")
|
||||
operate_id: number;
|
||||
user_id: number;
|
||||
|
||||
constructor(groupId: number, userId: number) {
|
||||
super();
|
||||
@ -15,7 +11,4 @@ class GroupIncreaseEvent extends BaseEvent {
|
||||
this.operate_id = userId; // 实际上不应该这么实现,但是现在还没有办法识别用户是被邀请的,还是主动加入的
|
||||
this.user_id = userId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export default GroupIncreaseEvent
|
||||
}
|
6
src/onebot11/event/notice/OB11GroupNoticeEvent.ts
Normal file
6
src/onebot11/event/notice/OB11GroupNoticeEvent.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import {OB11BaseNoticeEvent} from "./OB11BaseNoticeEvent";
|
||||
|
||||
export abstract class OB11GroupNoticeEvent extends OB11BaseNoticeEvent {
|
||||
group_id: number;
|
||||
user_id: number;
|
||||
}
|
16
src/onebot11/event/notice/OB11GroupRecallNoticeEvent.ts
Normal file
16
src/onebot11/event/notice/OB11GroupRecallNoticeEvent.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import {OB11GroupNoticeEvent} from "./OB11GroupNoticeEvent";
|
||||
|
||||
export class OB11GroupRecallNoticeEvent extends OB11GroupNoticeEvent {
|
||||
notice_type = "group_recall"
|
||||
operator_id: number
|
||||
message_id: number
|
||||
|
||||
constructor(groupId: number, userId: number, operatorId: number, messageId: number) {
|
||||
super();
|
||||
|
||||
this.group_id = groupId;
|
||||
this.user_id = userId;
|
||||
this.operator_id = operatorId;
|
||||
this.message_id = messageId;
|
||||
}
|
||||
}
|
@ -1,58 +0,0 @@
|
||||
import {
|
||||
OB11EventBase,
|
||||
OB11EventPostType, OB11FriendRecallNoticeEvent,
|
||||
OB11GroupRecallNoticeEvent,
|
||||
OB11HeartEvent,
|
||||
OB11LifeCycleEvent, OB11MetaEvent, OB11NoticeEvent
|
||||
} from "./types";
|
||||
import { heartInterval, selfInfo } from "../../common/data";
|
||||
|
||||
function eventBase(post_type: OB11EventPostType): OB11EventBase {
|
||||
return {
|
||||
time: Math.floor(Date.now() / 1000),
|
||||
self_id: parseInt(selfInfo.uin),
|
||||
post_type
|
||||
}
|
||||
}
|
||||
|
||||
export class OB11EventConstructor {
|
||||
static lifeCycleEvent(): OB11LifeCycleEvent {
|
||||
return {
|
||||
...eventBase(OB11EventPostType.META) as OB11MetaEvent,
|
||||
meta_event_type: "lifecycle",
|
||||
sub_type: "connect"
|
||||
}
|
||||
}
|
||||
|
||||
static heartEvent(): OB11HeartEvent {
|
||||
return {
|
||||
...eventBase(OB11EventPostType.META) as OB11MetaEvent,
|
||||
meta_event_type: "heartbeat",
|
||||
status: {
|
||||
online: true,
|
||||
good: true
|
||||
},
|
||||
interval: heartInterval
|
||||
}
|
||||
}
|
||||
|
||||
static groupRecallEvent(group_id: string, user_id: string, operator_id: string, message_id: number): OB11GroupRecallNoticeEvent {
|
||||
return {
|
||||
...eventBase(OB11EventPostType.NOTICE) as OB11NoticeEvent,
|
||||
notice_type: "group_recall",
|
||||
group_id: parseInt(group_id),
|
||||
user_id: parseInt(user_id),
|
||||
operator_id: parseInt(operator_id),
|
||||
message_id
|
||||
}
|
||||
}
|
||||
|
||||
static friendRecallEvent(user_id: string, message_id: number): OB11FriendRecallNoticeEvent {
|
||||
return {
|
||||
...eventBase(OB11EventPostType.NOTICE) as OB11NoticeEvent,
|
||||
notice_type: "friend_recall",
|
||||
user_id: parseInt(user_id),
|
||||
message_id
|
||||
}
|
||||
}
|
||||
}
|
@ -1,67 +0,0 @@
|
||||
import { OB11Status } from "../types";
|
||||
|
||||
export enum OB11EventPostType{
|
||||
META = "meta_event",
|
||||
NOTICE = "notice"
|
||||
}
|
||||
|
||||
export interface OB11EventBase {
|
||||
time: number
|
||||
self_id: number
|
||||
post_type: OB11EventPostType
|
||||
}
|
||||
|
||||
export interface OB11MetaEvent extends OB11EventBase{
|
||||
post_type: OB11EventPostType.META
|
||||
meta_event_type: "lifecycle" | "heartbeat"
|
||||
}
|
||||
|
||||
export interface OB11NoticeEvent extends OB11EventBase{
|
||||
post_type: OB11EventPostType.NOTICE
|
||||
notice_type: "group_admin" | "group_decrease" | "group_increase" | "group_ban" | "friend_add" | "group_recall" | "friend_recall"
|
||||
}
|
||||
|
||||
interface OB11GroupNoticeBase extends OB11NoticeEvent{
|
||||
group_id: number
|
||||
user_id: number
|
||||
}
|
||||
|
||||
export interface OB11GroupAdminNoticeEvent extends OB11GroupNoticeBase{
|
||||
notice_type: "group_admin"
|
||||
sub_type: "set" | "unset"
|
||||
}
|
||||
|
||||
export interface OB11GroupMemberDecNoticeEvent extends OB11GroupNoticeBase{
|
||||
notice_type: "group_decrease"
|
||||
sub_type: "leave" | "kick" | "kick_me"
|
||||
operator_id: number
|
||||
}
|
||||
|
||||
export interface OB11GroupMemberIncNoticeEvent extends OB11GroupNoticeBase{
|
||||
notice_type: "group_increase"
|
||||
sub_type: "approve" | "invite"
|
||||
operator_id: number
|
||||
}
|
||||
|
||||
export interface OB11GroupRecallNoticeEvent extends OB11GroupNoticeBase{
|
||||
notice_type: "group_recall"
|
||||
operator_id: number
|
||||
message_id: number
|
||||
}
|
||||
|
||||
export interface OB11FriendRecallNoticeEvent extends OB11NoticeEvent{
|
||||
notice_type: "friend_recall"
|
||||
user_id: number
|
||||
message_id: number
|
||||
}
|
||||
|
||||
export interface OB11LifeCycleEvent extends OB11MetaEvent {
|
||||
meta_event_type: "lifecycle"
|
||||
sub_type: "enable" | "disable" | "connect"
|
||||
}
|
||||
|
||||
export interface OB11HeartEvent extends OB11MetaEvent {
|
||||
meta_event_type: "heartbeat"
|
||||
status: OB11Status
|
||||
interval: number
|
||||
}
|
@ -1,36 +1,25 @@
|
||||
import { getConfigUtil, log } from "../common/utils";
|
||||
|
||||
const express = require("express");
|
||||
const expressWs = require("express-ws");
|
||||
|
||||
import * as http from "http";
|
||||
import * as websocket from "ws";
|
||||
import urlParse from "url";
|
||||
import express from "express";
|
||||
import { Request } from 'express';
|
||||
import { Response } from 'express';
|
||||
import { getConfigUtil, log } from "../common/utils";
|
||||
import { heartInterval, selfInfo } from "../common/data";
|
||||
import { OB11Message, OB11Return, OB11MessageData } from './types';
|
||||
import express, {Request, Response} from "express";
|
||||
import {getConfigUtil, log} from "../common/utils";
|
||||
import {heartInterval, selfInfo} from "../common/data";
|
||||
import {OB11Message, OB11MessageData, OB11Return} from './types';
|
||||
import {actionHandlers, actionMap} from "./actions";
|
||||
import {OB11Response, OB11WebsocketResponse} from "./actions/utils";
|
||||
import {registerEventSender, unregisterEventSender} from "./event/manager";
|
||||
import {callEvent, registerEventSender, unregisterEventSender} from "./event/manager";
|
||||
import ReconnectingWebsocket from "./ReconnectingWebsocket";
|
||||
import { actionHandlers } from "./actions";
|
||||
import { OB11Response } from "./actions/utils";
|
||||
import { ActionName } from "./actions/types";
|
||||
import {ActionName} from "./actions/types";
|
||||
import {OB11BaseMetaEvent} from "./event/meta/OB11BaseMetaEvent";
|
||||
import {OB11BaseNoticeEvent} from "./event/notice/OB11BaseNoticeEvent";
|
||||
import BaseAction from "./actions/BaseAction";
|
||||
import { OB11Constructor } from "./constructor";
|
||||
import { OB11EventBase, OB11LifeCycleEvent, OB11MetaEvent, OB11NoticeEvent } from "./events/types";
|
||||
import {LifeCycleSubType, OB11LifeCycleEvent} from "./event/meta/OB11LifeCycleEvent";
|
||||
import {OB11HeartbeatEvent} from "./event/meta/OB11HeartbeatEvent";
|
||||
|
||||
let accessToken = ""
|
||||
let accessToken = "";
|
||||
let heartbeatRunning = false;
|
||||
|
||||
// @SiberianHusky 2021-08-15
|
||||
enum WebsocketType {
|
||||
API,
|
||||
EVENT,
|
||||
ALL
|
||||
}
|
||||
|
||||
function checkSendMessage(sendMsgList: OB11MessageData[]) {
|
||||
function checkUri(uri: string): boolean {
|
||||
@ -72,11 +61,11 @@ function checkSendMessage(sendMsgList: OB11MessageData[]) {
|
||||
const JSONbig = require('json-bigint')({storeAsString: true});
|
||||
|
||||
const expressAPP = express();
|
||||
let httpServer: http.Server = null;
|
||||
expressAPP.use(express.urlencoded({extended: true, limit: "500mb"}));
|
||||
|
||||
const expressWsApp = express();
|
||||
const websocketClientConnections = [];
|
||||
let httpServer: http.Server = null;
|
||||
|
||||
let websocketServer = null;
|
||||
|
||||
expressAPP.use((req, res, next) => {
|
||||
let data = '';
|
||||
@ -139,74 +128,131 @@ export function startHTTPServer(port: number) {
|
||||
}
|
||||
}
|
||||
|
||||
export function startWebsocketServer(port: number) {
|
||||
const config = getConfigUtil().getConfig();
|
||||
if (config.enableWs) {
|
||||
try {
|
||||
expressWs(expressWsApp)
|
||||
expressWsApp.listen(getConfigUtil().getConfig().wsPort, function () {
|
||||
console.log(`llonebot websocket service started 0.0.0.0:${port}`);
|
||||
});
|
||||
}
|
||||
catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
export function initWebsocket(port: number) {
|
||||
if (!heartbeatRunning) {
|
||||
setInterval(() => {
|
||||
callEvent(new OB11HeartbeatEvent(true, true, heartInterval));
|
||||
}, heartInterval); // 心跳包
|
||||
|
||||
heartbeatRunning = true;
|
||||
}
|
||||
|
||||
export function initWebsocket() {
|
||||
if (getConfigUtil().getConfig().enableWs) {
|
||||
expressWsApp.ws("/api", (ws, req) => {
|
||||
initWebsocketServer(ws, req, WebsocketType.API);
|
||||
});
|
||||
expressWsApp.ws("/event", (ws, req) => {
|
||||
initWebsocketServer(ws, req, WebsocketType.EVENT);
|
||||
});
|
||||
expressWsApp.ws("/", (ws, req) => {
|
||||
initWebsocketServer(ws, req, WebsocketType.ALL);
|
||||
});
|
||||
if (websocketServer) {
|
||||
websocketServer.close((err) => {
|
||||
log("ws server close failed!", err)
|
||||
})
|
||||
}
|
||||
|
||||
websocketServer = new websocket.Server({port});
|
||||
console.log(`llonebot websocket service started 0.0.0.0:${port}`);
|
||||
|
||||
websocketServer.on("connection", (ws, req) => {
|
||||
const url = req.url.split("?").shift();
|
||||
log("receive ws connect", url)
|
||||
let token: string = ""
|
||||
const authHeader = req.headers['authorization'];
|
||||
if (authHeader) {
|
||||
token = authHeader.split("Bearer ").pop()
|
||||
log("receive ws header token", token);
|
||||
} else {
|
||||
const parsedUrl = urlParse.parse(req.url, true);
|
||||
const urlToken = parsedUrl.query.access_token;
|
||||
if (urlToken) {
|
||||
if (Array.isArray(urlToken)) {
|
||||
token = urlToken[0]
|
||||
} else {
|
||||
token = urlToken
|
||||
}
|
||||
log("receive ws url token", token);
|
||||
}
|
||||
}
|
||||
if (accessToken) {
|
||||
if (token != accessToken) {
|
||||
ws.send(JSON.stringify(OB11WebsocketResponse.res(null, "failed", 1403, "token验证失败")))
|
||||
return ws.close()
|
||||
}
|
||||
}
|
||||
|
||||
if (url == "/api" || url == "/api/" || url == "/") {
|
||||
ws.on("message", async (msg) => {
|
||||
let receiveData: { action: ActionName, params: any, echo?: string } = {action: null, params: {}}
|
||||
let echo = ""
|
||||
log("收到正向Websocket消息", msg.toString())
|
||||
try {
|
||||
receiveData = JSON.parse(msg.toString())
|
||||
echo = receiveData.echo
|
||||
} catch (e) {
|
||||
return wsReply(ws, OB11WebsocketResponse.error("json解析失败,请检查数据格式", 1400, echo))
|
||||
}
|
||||
const action: BaseAction<any, any> = actionMap.get(receiveData.action);
|
||||
if (!action) {
|
||||
return wsReply(ws, OB11WebsocketResponse.error("不支持的api " + receiveData.action, 1404, echo))
|
||||
}
|
||||
try {
|
||||
let handleResult = await action.websocketHandle(receiveData.params, echo);
|
||||
wsReply(ws, handleResult)
|
||||
} catch (e) {
|
||||
wsReply(ws, OB11WebsocketResponse.error(`api处理出错:${e}`, 1200, echo))
|
||||
}
|
||||
})
|
||||
}
|
||||
if (url == "/event" || url == "/event/" || url == "/") {
|
||||
registerEventSender(ws);
|
||||
|
||||
log("event上报ws客户端已连接")
|
||||
|
||||
try {
|
||||
wsReply(ws, new OB11LifeCycleEvent(LifeCycleSubType.CONNECT))
|
||||
} catch (e){
|
||||
log("发送生命周期失败", e)
|
||||
}
|
||||
|
||||
ws.on("close", () => {
|
||||
log("event上报ws客户端已断开")
|
||||
unregisterEventSender(ws);
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
initReverseWebsocket();
|
||||
initReverseWebsocket().then();
|
||||
}
|
||||
|
||||
function initReverseWebsocket() {
|
||||
async function initReverseWebsocket() {
|
||||
const config = getConfigUtil().getConfig();
|
||||
if (config.enableWsReverse) {
|
||||
for (const url of config.wsHosts) {
|
||||
try {
|
||||
let wsClient = new ReconnectingWebsocket(url);
|
||||
websocketClientConnections.push(wsClient);
|
||||
registerEventSender(wsClient);
|
||||
|
||||
wsClient.onclose = function () {
|
||||
console.log("The websocket connection: " + url + " closed, trying reconnecting...");
|
||||
unregisterEventSender(wsClient);
|
||||
let index = websocketClientConnections.indexOf(wsClient);
|
||||
if (index !== -1) {
|
||||
websocketClientConnections.splice(index, 1);
|
||||
}
|
||||
wsClient.onopen = function () {
|
||||
wsReply(wsClient, new OB11LifeCycleEvent(LifeCycleSubType.CONNECT));
|
||||
}
|
||||
|
||||
wsClient.onmessage = async function (message) {
|
||||
console.log(message);
|
||||
if (typeof message === "string") {
|
||||
try {
|
||||
let recv = JSON.parse(message);
|
||||
let echo = recv.echo ?? "";
|
||||
wsClient.onclose = function () {
|
||||
unregisterEventSender(wsClient);
|
||||
}
|
||||
|
||||
if (actionMap.has(recv.action)) {
|
||||
let action = actionMap.get(recv.action);
|
||||
const result = await action.websocketHandle(recv.params, echo);
|
||||
wsClient.send(JSON.stringify(result));
|
||||
}
|
||||
else {
|
||||
wsClient.send(JSON.stringify(OB11WebsocketResponse.error("Bad Request", 1400, echo)));
|
||||
}
|
||||
} catch (e) {
|
||||
log(e.stack);
|
||||
wsClient.send(JSON.stringify(OB11WebsocketResponse.error(e.stack.toString(), 1200)));
|
||||
}
|
||||
wsClient.onmessage = async function (msg) {
|
||||
let receiveData: { action: ActionName, params: any, echo?: string } = {action: null, params: {}}
|
||||
let echo = ""
|
||||
log("收到正向Websocket消息", msg.toString())
|
||||
try {
|
||||
receiveData = JSON.parse(msg.toString())
|
||||
echo = receiveData.echo
|
||||
} catch (e) {
|
||||
return wsReply(wsClient, OB11WebsocketResponse.error("json解析失败,请检查数据格式", 1400, echo))
|
||||
}
|
||||
const action: BaseAction<any, any> = actionMap.get(receiveData.action);
|
||||
if (!action) {
|
||||
return wsReply(wsClient, OB11WebsocketResponse.error("不支持的api " + receiveData.action, 1404, echo))
|
||||
}
|
||||
try {
|
||||
let handleResult = await action.websocketHandle(receiveData.params, echo);
|
||||
wsReply(wsClient, handleResult)
|
||||
} catch (e) {
|
||||
wsReply(wsClient, OB11WebsocketResponse.error(`api处理出错:${e}`, 1200, echo))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -217,44 +263,7 @@ function initReverseWebsocket() {
|
||||
}
|
||||
}
|
||||
|
||||
function initWebsocketServer(ws, req, type: WebsocketType) {
|
||||
if (type == WebsocketType.EVENT || type == WebsocketType.ALL) {
|
||||
registerEventSender(ws);
|
||||
}
|
||||
|
||||
ws.on("message", async function (message) {
|
||||
if (type == WebsocketType.API || type == WebsocketType.ALL) {
|
||||
try {
|
||||
let recv = JSON.parse(message);
|
||||
let echo = recv.echo ?? "";
|
||||
|
||||
if (actionMap.has(recv.action)) {
|
||||
let action = actionMap.get(recv.action)
|
||||
const result = await action.websocketHandle(recv.params, echo);
|
||||
ws.send(JSON.stringify(result));
|
||||
}
|
||||
else {
|
||||
ws.send(JSON.stringify(OB11WebsocketResponse.error("Bad Request", 1400, echo)));
|
||||
}
|
||||
} catch (e) {
|
||||
log(e.stack);
|
||||
ws.send(JSON.stringify(OB11WebsocketResponse.error(e.stack.toString(), 1200)));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
ws.on("close", function (ev) {
|
||||
if (type == WebsocketType.EVENT || type == WebsocketType.ALL) {
|
||||
unregisterEventSender(ws);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
let wsEventClients: websocket.WebSocket[] = [];
|
||||
type RouterHandler = (payload: any) => Promise<OB11Return<any>>
|
||||
let routers: Record<string, RouterHandler> = {};
|
||||
|
||||
function wsReply(wsClient: websocket.WebSocket, data: OB11Return<any> | PostMsgType) {
|
||||
export function wsReply(wsClient: websocket.WebSocket | ReconnectingWebsocket, data: OB11Return<any> | PostMsgType) {
|
||||
try {
|
||||
wsClient.send(JSON.stringify(data))
|
||||
log("ws 消息上报", data)
|
||||
@ -263,104 +272,17 @@ function wsReply(wsClient: websocket.WebSocket, data: OB11Return<any> | PostMsgT
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// TODO: 统一一下逻辑
|
||||
export function startWSServer(port: number) {
|
||||
if (wsServer) {
|
||||
wsServer.close((err) => {
|
||||
log("ws server close failed!", err)
|
||||
})
|
||||
}
|
||||
wsServer = new websocket.Server({port})
|
||||
wsServer.on("connection", (ws, req) => {
|
||||
const url = req.url.split("?").shift();
|
||||
log("receive ws connect", url)
|
||||
let token: string = ""
|
||||
const authHeader = req.headers['authorization'];
|
||||
if (authHeader) {
|
||||
token = authHeader.split("Bearer ").pop()
|
||||
log("receive ws header token", token);
|
||||
} else {
|
||||
const parsedUrl = urlParse.parse(req.url, true);
|
||||
const urlToken = parsedUrl.query.access_token;
|
||||
if (urlToken) {
|
||||
if (Array.isArray(urlToken)) {
|
||||
token = urlToken[0]
|
||||
} else {
|
||||
token = urlToken
|
||||
}
|
||||
log("receive ws url token", token);
|
||||
}
|
||||
}
|
||||
if (accessToken) {
|
||||
if (token != accessToken) {
|
||||
ws.send(JSON.stringify(OB11Response.res(null, 1403, "token验证失败")))
|
||||
return ws.close()
|
||||
}
|
||||
}
|
||||
if (url == "/api" || url == "/api/" || url == "/") {
|
||||
ws.on("message", async (msg) => {
|
||||
|
||||
let receiveData: { action: ActionName, params: any, echo?: string } = {action: null, params: {}}
|
||||
let echo = ""
|
||||
log("收到ws消息", msg.toString())
|
||||
try {
|
||||
receiveData = JSON.parse(msg.toString())
|
||||
echo = receiveData.echo
|
||||
} catch (e) {
|
||||
return wsReply(ws, {...OB11Response.error("json解析失败,请检查数据格式"), echo})
|
||||
}
|
||||
const handle: RouterHandler | undefined = routers[receiveData.action]
|
||||
if (!handle) {
|
||||
let handleResult = OB11Response.error("不支持的api " + receiveData.action, 1404)
|
||||
handleResult.echo = echo
|
||||
return wsReply(ws, handleResult)
|
||||
}
|
||||
try {
|
||||
let handleResult = await handle(receiveData.params)
|
||||
if (echo){
|
||||
handleResult.echo = echo
|
||||
}
|
||||
wsReply(ws, handleResult)
|
||||
} catch (e) {
|
||||
wsReply(ws, OB11Response.error(`api处理出错:${e}`))
|
||||
}
|
||||
})
|
||||
}
|
||||
if (url == "/event" || url == "/event/" || url == "/") {
|
||||
log("event上报ws客户端已连接")
|
||||
wsEventClients.push(ws)
|
||||
try {
|
||||
wsReply(ws, OB11Constructor.lifeCycleEvent())
|
||||
}catch (e){
|
||||
log("发送生命周期失败", e)
|
||||
}
|
||||
// 心跳
|
||||
let wsHeart = setInterval(()=>{
|
||||
if (wsEventClients.find(c => c == ws)){
|
||||
wsReply(ws, OB11Constructor.heartEvent())
|
||||
}
|
||||
}, heartInterval)
|
||||
ws.on("close", () => {
|
||||
clearInterval(wsHeart);
|
||||
log("event上报ws客户端已断开")
|
||||
wsEventClients = wsEventClients.filter((c) => c != ws)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type PostMsgType = OB11Message | OB11MetaEvent | OB11NoticeEvent
|
||||
export type PostMsgType = OB11Message | OB11BaseMetaEvent | OB11BaseNoticeEvent
|
||||
|
||||
export function postMsg(msg: PostMsgType) {
|
||||
const {reportSelfMessage} = getConfigUtil().getConfig()
|
||||
const config = getConfigUtil().getConfig();
|
||||
// 判断msg是否是event
|
||||
if (!reportSelfMessage) {
|
||||
if (!config.reportSelfMessage) {
|
||||
if ((msg as OB11Message).user_id.toString() == selfInfo.uin) {
|
||||
return
|
||||
}
|
||||
}
|
||||
for (const host of getConfigUtil().getConfig().hosts) {
|
||||
for (const host of config.httpHosts) {
|
||||
fetch(host, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
@ -374,12 +296,10 @@ export function postMsg(msg: PostMsgType) {
|
||||
log(`新消息事件HTTP上报失败: ${host} ` + err + JSON.stringify(msg));
|
||||
});
|
||||
}
|
||||
for (const wsClient of wsEventClients) {
|
||||
log("新消息事件ws上报", msg)
|
||||
new Promise((resolve, reject) => {
|
||||
wsReply(wsClient, msg);
|
||||
}).then();
|
||||
}
|
||||
|
||||
log("新消息事件ws上报", msg);
|
||||
console.log("新消息事件ws上报", msg);
|
||||
callEvent(msg);
|
||||
}
|
||||
|
||||
|
||||
@ -406,7 +326,6 @@ function registerRouter(action: string, handle: (payload: any) => Promise<any>)
|
||||
expressAPP.get(url, expressAuthorize, (req: Request, res: Response) => {
|
||||
_handle(res, req.query as any || {}).then()
|
||||
});
|
||||
routers[action] = handle
|
||||
}
|
||||
|
||||
for (const action of actionHandlers) {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { AtType, RawMessage } from "../ntqqapi/types";
|
||||
import {AtType, RawMessage} from "../ntqqapi/types";
|
||||
|
||||
export interface OB11User {
|
||||
user_id: number;
|
||||
@ -89,19 +89,12 @@ export interface OB11Return<DataType> {
|
||||
retcode: number
|
||||
data: DataType
|
||||
message: string,
|
||||
echo?: string
|
||||
}
|
||||
|
||||
export interface OB11WebsocketReturn<DataType> {
|
||||
status: string
|
||||
retcode: number
|
||||
data: DataType
|
||||
export interface OB11WebsocketReturn<DataType> extends OB11Return<DataType>{
|
||||
echo: string
|
||||
message: string
|
||||
}
|
||||
|
||||
export interface OB11SendMsgReturn extends OB11Return<{ message_id: string }> {}
|
||||
|
||||
export enum OB11MessageDataType {
|
||||
text = "text",
|
||||
image = "image",
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { CONFIG_DIR, isGIF } from "../common/utils";
|
||||
import {CONFIG_DIR, isGIF} from "../common/utils";
|
||||
import * as path from 'path';
|
||||
import { NTQQApi } from '../ntqqapi/ntcall';
|
||||
import { OB11MessageData } from "./types";
|
||||
import {OB11MessageData} from "./types";
|
||||
|
||||
const fs = require("fs").promises;
|
||||
|
||||
export async function uri2local(fileName: string, uri: string){
|
||||
|
@ -1,11 +1,7 @@
|
||||
// Electron 主进程 与 渲染进程 交互的桥梁
|
||||
|
||||
import {Config} from "./common/types";
|
||||
import {
|
||||
CHANNEL_GET_CONFIG,
|
||||
CHANNEL_LOG,
|
||||
CHANNEL_SET_CONFIG,
|
||||
} from "./common/channels";
|
||||
import {CHANNEL_GET_CONFIG, CHANNEL_LOG, CHANNEL_SET_CONFIG,} from "./common/channels";
|
||||
|
||||
|
||||
const {contextBridge} = require("electron");
|
||||
|
Loading…
x
Reference in New Issue
Block a user