fix rkeyManager

This commit is contained in:
手瓜一十雪 2024-08-09 14:05:47 +08:00
parent aaa874b099
commit bd9ee62118
3 changed files with 60 additions and 57 deletions

View File

@ -13,31 +13,34 @@ import * as fileType from 'file-type';
import imageSize from 'image-size';
import { ISizeCalculationResult } from 'image-size/dist/types/interface';
import { NodeIKernelSearchService } from '../services/NodeIKernelSearchService';
import { RkeyManager } from '../helper/rkey';
export class NTQQFileApi {
context: InstanceContext;
core: NapCatCore;
rkeyManager: RkeyManager;
constructor(context: InstanceContext, core: NapCatCore) {
this.context = context;
this.core = core;
this.rkeyManager = new RkeyManager('http://napcat-sign.wumiao.wang:2082/rkey', this.context.logger);
}
async getFileType(filePath: string) {
async getFileType(filePath: string) {
return fileType.fileTypeFromFile(filePath);
}
async copyFile(filePath: string, destPath: string) {
async copyFile(filePath: string, destPath: string) {
await this.context.wrapper.util.copyFile(filePath, destPath);
}
async getFileSize(filePath: string): Promise<number> {
async getFileSize(filePath: string): Promise<number> {
return await this.context.wrapper.util.getFileSize(filePath);
}
async getVideoUrl(peer: Peer, msgId: string, elementId: string) {
async getVideoUrl(peer: Peer, msgId: string, elementId: string) {
return (await this.context.session.getRichMediaService().getVideoPlayUrlV2(peer, msgId, elementId, 0, { downSourceType: 1, triggerType: 1 })).urlResult.domainUrl;
}
// 上传文件到QQ的文件夹
async uploadFile(filePath: string, elementType: ElementType = ElementType.PIC, elementSubType: number = 0) {
async uploadFile(filePath: string, elementType: ElementType = ElementType.PIC, elementSubType: number = 0) {
// napCatCore.wrapper.util.
const fileMd5 = await calculateFileMD5(filePath);
let ext: string = (await this.getFileType(filePath))?.ext as string || '';
@ -68,10 +71,10 @@ export class NTQQFileApi {
ext
};
}
async downloadMediaByUuid() {
async downloadMediaByUuid() {
//napCatCore.session.getRichMediaService().downloadFileForFileUuid();
}
async downloadMedia(msgId: string, chatType: ChatType, peerUid: string, elementId: string, thumbPath: string, sourcePath: string, timeout = 1000 * 60 * 2, force: boolean = false) {
async downloadMedia(msgId: string, chatType: ChatType, peerUid: string, elementId: string, thumbPath: string, sourcePath: string, timeout = 1000 * 60 * 2, force: boolean = false) {
//logDebug('receive downloadMedia task', msgId, chatType, peerUid, elementId, thumbPath, sourcePath, timeout, force);
// 用于下载收到的消息中的图片等
if (sourcePath && fs.existsSync(sourcePath)) {
@ -135,7 +138,7 @@ export class NTQQFileApi {
return filePath;
}
async getImageSize(filePath: string): Promise<ISizeCalculationResult | undefined> {
async getImageSize(filePath: string): Promise<ISizeCalculationResult | undefined> {
return new Promise((resolve, reject) => {
imageSize(filePath, (err, dimensions) => {
if (err) {
@ -146,7 +149,7 @@ export class NTQQFileApi {
});
});
}
async addFileCache(peer: Peer, msgId: string, msgSeq: string, senderUid: string, elemId: string, elemType: string, fileSize: string, fileName: string) {
async addFileCache(peer: Peer, msgId: string, msgSeq: string, senderUid: string, elemId: string, elemType: string, fileSize: string, fileName: string) {
let GroupData;
let BuddyData;
if (peer.chatType === ChatType.group) {
@ -204,7 +207,7 @@ export class NTQQFileApi {
]
});
}
async searchfile(keys: string[]) {
async searchfile(keys: string[]) {
type EventType = NodeIKernelSearchService['searchFileWithKeywords'];
interface OnListener {
searchId: string,
@ -256,7 +259,7 @@ export class NTQQFileApi {
let [ret] = (await Listener);
return ret;
}
async getImageUrl(element: PicElement) {
async getImageUrl(element: PicElement) {
if (!element) {
return '';
}
@ -274,7 +277,7 @@ export class NTQQFileApi {
if (UrlRkey) {
return IMAGE_HTTP_HOST_NT + url;
}
const rkeyData = await rkeyManager.getRkey();
const rkeyData = await this.rkeyManager.getRkey();
UrlRkey = imageAppid === '1406' ? rkeyData.private_rkey : rkeyData.group_rkey;
return IMAGE_HTTP_HOST_NT + url + `${UrlRkey}`;
} else {
@ -297,48 +300,48 @@ export class NTQQFileCacheApi {
this.context = context;
this.core = core;
}
async setCacheSilentScan(isSilent: boolean = true) {
async setCacheSilentScan(isSilent: boolean = true) {
return '';
}
getCacheSessionPathList() {
getCacheSessionPathList() {
return '';
}
clearCache(cacheKeys: Array<string> = ['tmp', 'hotUpdate']) {
clearCache(cacheKeys: Array<string> = ['tmp', 'hotUpdate']) {
// 参数未验证
return this.context.session.getStorageCleanService().clearCacheDataByKeys(cacheKeys);
}
addCacheScannedPaths(pathMap: object = {}) {
addCacheScannedPaths(pathMap: object = {}) {
return this.context.session.getStorageCleanService().addCacheScanedPaths(pathMap);
}
scanCache() {
scanCache() {
//return (await this.context.session.getStorageCleanService().scanCache()).size;
}
getHotUpdateCachePath() {
getHotUpdateCachePath() {
// 未实现
return '';
}
getDesktopTmpPath() {
getDesktopTmpPath() {
// 未实现
return '';
}
getChatCacheList(type: ChatType, pageSize: number = 1000, pageIndex: number = 0) {
getChatCacheList(type: ChatType, pageSize: number = 1000, pageIndex: number = 0) {
return this.context.session.getStorageCleanService().getChatCacheInfo(type, pageSize, 1, pageIndex);
}
getFileCacheInfo(fileType: CacheFileType, pageSize: number = 1000, lastRecord?: CacheFileListItem) {
getFileCacheInfo(fileType: CacheFileType, pageSize: number = 1000, lastRecord?: CacheFileListItem) {
const _lastRecord = lastRecord ? lastRecord : { fileType: fileType };
//需要五个参数
//return napCatCore.session.getStorageCleanService().getFileCacheInfo();
}
async clearChatCache(chats: ChatCacheListItemBasic[] = [], fileKeys: string[] = []) {
async clearChatCache(chats: ChatCacheListItemBasic[] = [], fileKeys: string[] = []) {
return this.context.session.getStorageCleanService().clearChatCacheInfo(chats, fileKeys);
}
}

View File

@ -1,5 +1,5 @@
import type { ModifyProfileParams, SelfInfo, User, UserDetailInfoByUin, UserDetailInfoByUinV2 } from '@/core/entities';
import { NodeIKernelProfileListener, ProfileListener } from '@/core/listeners';
import type { ModifyProfileParams, User, UserDetailInfoByUin, UserDetailInfoByUinV2 } from '@/core/entities';
import { NodeIKernelProfileListener } from '@/core/listeners';
import { RequestUtil } from '@/common/utils/request';
import { NodeIKernelProfileService, ProfileBizType, UserDetailSource } from '@/core/services';
import { InstanceContext, NapCatCore } from '..';
@ -11,7 +11,7 @@ export class NTQQUserApi {
this.context = context;
this.core = core;
}
async getProfileLike(uid: string) {
async getProfileLike(uid: string) {
return this.context.session.getProfileLikeService().getBuddyProfileLike({
friendUids: [
uid
@ -25,16 +25,16 @@ export class NTQQUserApi {
limit: 20
});
}
async setLongNick(longNick: string) {
async setLongNick(longNick: string) {
return this.context.session.getProfileService().setLongNick(longNick);
}
async setSelfOnlineStatus(status: number, extStatus: number, batteryStatus: number) {
async setSelfOnlineStatus(status: number, extStatus: number, batteryStatus: number) {
return this.context.session.getMsgService().setStatus({ status: status, extStatus: extStatus, batteryStatus: batteryStatus });
}
async getBuddyRecommendContactArkJson(uin: string, sencenID = '') {
async getBuddyRecommendContactArkJson(uin: string, sencenID = '') {
return this.context.session.getBuddyService().getBuddyRecommendContactArkJson(uin, sencenID);
}
async like(uid: string, count = 1): Promise<{ result: number, errMsg: string, succCounts: number }> {
async like(uid: string, count = 1): Promise<{ result: number, errMsg: string, succCounts: number }> {
return this.context.session.getProfileLikeService().setBuddyProfileLike({
friendUid: uid,
sourceId: 71,
@ -43,16 +43,16 @@ export class NTQQUserApi {
});
}
async setQQAvatar(filePath: string) {
async setQQAvatar(filePath: string) {
type setQQAvatarRet = { result: number, errMsg: string };
const ret = await this.context.session.getProfileService().setHeader(filePath) as setQQAvatarRet;
return { result: ret?.result, errMsg: ret?.errMsg };
}
async setGroupAvatar(gc: string, filePath: string) {
async setGroupAvatar(gc: string, filePath: string) {
return this.context.session.getGroupService().setHeader(gc, filePath);
}
async fetchUserDetailInfos(uids: string[]) {
async fetchUserDetailInfos(uids: string[]) {
//26702 以上使用新接口 .Dev Mlikiowa
type EventService = NodeIKernelProfileService['fetchUserDetailInfo'];
type EventListener = NodeIKernelProfileListener['onUserDetailInfoChanged'];
@ -90,7 +90,7 @@ export class NTQQUserApi {
return retData;
}
async fetchUserDetailInfo(uid: string) {
async fetchUserDetailInfo(uid: string) {
type EventService = NodeIKernelProfileService['fetchUserDetailInfo'];
type EventListener = NodeIKernelProfileListener['onUserDetailInfoChanged'];
let [_retData, profile] = await this.core.eventWrapper.CallNormalEvent
@ -126,13 +126,13 @@ export class NTQQUserApi {
};
return RetUser;
}
async getUserDetailInfo(uid: string) {
async getUserDetailInfo(uid: string) {
if (this.context.basicInfoWrapper.requireMinNTQQBuild('26702')) {
return this.fetchUserDetailInfo(uid);
}
return this.getUserDetailInfoOld(uid);
}
async getUserDetailInfoOld(uid: string) {
async getUserDetailInfoOld(uid: string) {
type EventService = NodeIKernelProfileService['getUserDetailInfoWithBizInfo'];
type EventListener = NodeIKernelProfileListener['onProfileDetailInfoChanged'];
let [_retData, profile] = await this.core.eventWrapper.CallNormalEvent
@ -153,20 +153,20 @@ export class NTQQUserApi {
);
return profile;
}
async modifySelfProfile(param: ModifyProfileParams) {
async modifySelfProfile(param: ModifyProfileParams) {
return this.context.session.getProfileService().modifyDesktopMiniProfile(param);
}
//需要异常处理
async getCookies(domain: string) {
async getCookies(domain: string) {
const ClientKeyData = await this.forceFetchClientKey();
const requestUrl = 'https://ssl.ptlogin2.qq.com/jump?ptlang=1033&clientuin=' + selfInfo.uin + '&clientkey=' + ClientKeyData.clientKey + '&u1=https%3A%2F%2F' + domain + '%2F' + selfInfo.uin + '%2Finfocenter&keyindex=19%27'
let cookies: { [key: string]: string; } = await RequestUtil.HttpsGetCookies(requestUrl);
return cookies;
}
async getPSkey(domainList: string[]) {
async getPSkey(domainList: string[]) {
return await this.context.session.getTipOffService().getPskey(domainList, true);
}
async getRobotUinRange(): Promise<Array<any>> {
async getRobotUinRange(): Promise<Array<any>> {
const robotUinRanges = await this.context.session.getRobotService().getRobotUinRange({
justFetchMsgConfig: '1',
type: 1,
@ -177,16 +177,16 @@ export class NTQQUserApi {
return robotUinRanges?.response?.robotUinRanges;
}
//需要异常处理
async getQzoneCookies() {
async getQzoneCookies() {
const ClientKeyData = await this.forceFetchClientKey();
const requestUrl = 'https://ssl.ptlogin2.qq.com/jump?ptlang=1033&clientuin=' + selfInfo.uin + '&clientkey=' + ClientKeyData.clientKey + '&u1=https%3A%2F%2Fuser.qzone.qq.com%2F' + selfInfo.uin + '%2Finfocenter&keyindex=19%27'
let cookies: { [key: string]: string; } = await RequestUtil.HttpsGetCookies(requestUrl);
return cookies;
}
//需要异常处理
async getSkey(): Promise<string | undefined> {
async getSkey(): Promise<string | undefined> {
const ClientKeyData = await this.forceFetchClientKey();
if (ClientKeyData.result !== 0) {
throw new Error('getClientKey Error');
@ -201,14 +201,14 @@ export class NTQQUserApi {
}
return skey;
}
async getUidByUin(Uin: string) {
async getUidByUin(Uin: string) {
//此代码仅临时使用,后期会被废弃
if (this.context.basicInfoWrapper.requireMinNTQQBuild('26702')) {
return await this..getUidByUinV2(Uin);
}
return await this.getUidByUinV1(Uin);
}
async getUinByUid(Uid: string) {
async getUinByUid(Uid: string) {
//此代码仅临时使用,后期会被废弃
if (this.context.basicInfoWrapper.requireMinNTQQBuild('26702')) {
return await this.getUinByUidV2(Uid);
@ -217,7 +217,7 @@ export class NTQQUserApi {
}
//后期改成流水线处理
async getUidByUinV2(Uin: string) {
async getUidByUinV2(Uin: string) {
let uid = (await this.context.session.getProfileService().getUidByUin('FriendsServiceImpl', [Uin])).get(Uin);
if (uid) return uid;
uid = (await this.context.session.getGroupService().getUidByUins([Uin])).uids.get(Uin);
@ -235,7 +235,7 @@ export class NTQQUserApi {
return uid;
}
//后期改成流水线处理
async getUinByUidV2(Uid: string) {
async getUinByUidV2(Uid: string) {
let uin = (await this.context.session.getProfileService().getUinByUid('FriendsServiceImpl', [Uid])).get(Uid);
if (uin) return uin;
uin = (await this.context.session.getGroupService().getUinByUids([Uid])).uins.get(Uid);
@ -250,7 +250,7 @@ export class NTQQUserApi {
return uin;
}
async getUidByUinV1(Uin: string) {
async getUidByUinV1(Uin: string) {
// 通用转换开始尝试
let uid = (await this.context.session.getUixConvertService().getUid([Uin])).uidInfo.get(Uin);
// Uid 好友转
@ -279,7 +279,7 @@ export class NTQQUserApi {
}
return uid;
}
async getUinByUidV1(Uid: string) {
async getUinByUidV1(Uid: string) {
let ret = await this.core.eventWrapper.callNoListenerEvent
<(Uin: string[]) => Promise<{ uinInfo: Map<string, string> }>>(
'NodeIKernelUixConvertService/getUin',
@ -307,19 +307,19 @@ export class NTQQUserApi {
// }
return uin;
}
async getRecentContactListSnapShot(count: number) {
async getRecentContactListSnapShot(count: number) {
return await this.context.session.getRecentContactService().getRecentContactListSnapShot(count);
}
async getRecentContactListSyncLimit(count: number) {
async getRecentContactListSyncLimit(count: number) {
return await this.context.session.getRecentContactService().getRecentContactListSyncLimit(count);
}
async getRecentContactListSync() {
async getRecentContactListSync() {
return await this.context.session.getRecentContactService().getRecentContactListSync();
}
async getRecentContactList() {
async getRecentContactList() {
return await this.context.session.getRecentContactService().getRecentContactList();
}
async getUserDetailInfoByUinV2(Uin: string) {
async getUserDetailInfoByUinV2(Uin: string) {
return await this.core.eventWrapper.callNoListenerEvent
<(Uin: string) => Promise<UserDetailInfoByUinV2>>(
'NodeIKernelProfileService/getUserDetailInfoByUin',
@ -327,7 +327,7 @@ export class NTQQUserApi {
Uin
);
}
async getUserDetailInfoByUin(Uin: string) {
async getUserDetailInfoByUin(Uin: string) {
return this.core.eventWrapper.callNoListenerEvent
<(Uin: string) => Promise<UserDetailInfoByUin>>(
'NodeIKernelProfileService/getUserDetailInfoByUin',
@ -335,7 +335,7 @@ export class NTQQUserApi {
Uin
);
}
async forceFetchClientKey() {
async forceFetchClientKey() {
return await this.context.session.getTicketService().forceFetchClientKey('');
}
}

View File

@ -7,7 +7,7 @@ interface ServerRkeyData {
expired_time: number;
}
class RkeyManager {
export class RkeyManager {
serverUrl: string = '';
private rkeyData: ServerRkeyData = {
group_rkey: '',