feat & refactor: packet highway (in almost right impl)
This commit is contained in:
1 parent
083b9ce22b
commit
29d08c2987
5 files changed
+308
-290
No files matched your search
@@ -2,16 +2,17 @@ import * as fs from "node:fs";
|
||||
import {LogWrapper} from "@/common/log";
|
||||
import {PacketClient} from "@/core/packet/client";
|
||||
import {PacketPacker} from "@/core/packet/packer";
|
||||
import {NapProtoEncodeStructType, NapProtoMsg} from "@/core/packet/proto/NapProto";
|
||||
import {NapProtoMsg} from "@/core/packet/proto/NapProto";
|
||||
import {HttpConn0x6ff_501Response} from "@/core/packet/proto/action/action";
|
||||
import {PacketHighwayClient} from "@/core/packet/highway/client";
|
||||
import {ChatType, Peer} from "@/core";
|
||||
import {IPv4, NTV2RichMediaResp} from "@/core/packet/proto/oidb/common/Ntv2.RichMediaResp";
|
||||
import {NTV2RichMediaResp} from "@/core/packet/proto/oidb/common/Ntv2.RichMediaResp";
|
||||
import {OidbSvcTrpcTcpBaseRsp} from "@/core/packet/proto/oidb/OidbBase";
|
||||
import {PacketMsgPicElement} from "@/core/packet/msg/element";
|
||||
import {NTHighwayIPv4, NTV2RichMediaHighwayExt} from "@/core/packet/proto/highway/highway";
|
||||
import {NTV2RichMediaHighwayExt} from "@/core/packet/proto/highway/highway";
|
||||
import {int32ip2str, oidbIpv4s2HighwayIpv4s} from "@/core/packet/highway/utils";
|
||||
|
||||
export const BlockSize = 1024 * 1024;
|
||||
export const BlockSize = 512 * 1024;
|
||||
|
||||
interface HighwayServerAddr {
|
||||
ip: string
|
||||
@@ -25,12 +26,14 @@ export interface PacketHighwaySig {
|
||||
serverAddr: HighwayServerAddr[]
|
||||
}
|
||||
|
||||
|
||||
|
||||
export class PacketHighwaySession {
|
||||
protected packetClient: PacketClient;
|
||||
protected packetHighwayClient: PacketHighwayClient;
|
||||
protected sig: PacketHighwaySig;
|
||||
protected logger: LogWrapper;
|
||||
protected packer: PacketPacker;
|
||||
protected sig: PacketHighwaySig;
|
||||
protected packetHighwayClient: PacketHighwayClient;
|
||||
|
||||
constructor(logger: LogWrapper, client: PacketClient) {
|
||||
this.packetClient = client;
|
||||
@@ -45,56 +48,50 @@ export class PacketHighwaySession {
|
||||
this.packetHighwayClient = new PacketHighwayClient(this.sig, this.logger);
|
||||
}
|
||||
|
||||
get available(): boolean {
|
||||
return this.packetClient.available && this.sig.sigSession !== null &&
|
||||
this.sig.sessionKey !== null && this.sig.serverAddr.length > 0;
|
||||
}
|
||||
|
||||
private int32ip2str(ip: number) {
|
||||
ip = ip & 0xffffffff;
|
||||
return [ip & 0xff, (ip & 0xff00) >> 8, (ip & 0xff0000) >> 16, ((ip & 0xff000000) >> 24) & 0xff].join('.');
|
||||
}
|
||||
|
||||
private oidbIpv4s2HighwayIpv4s(ipv4s: NapProtoEncodeStructType<typeof IPv4>[]): NapProtoEncodeStructType<typeof NTHighwayIPv4>[] {
|
||||
return ipv4s.map((ipv4) => {
|
||||
return {
|
||||
domain: {
|
||||
isEnable: true,
|
||||
ip: this.int32ip2str(ipv4.outIP!),
|
||||
}
|
||||
} as NapProtoEncodeStructType<typeof NTHighwayIPv4>
|
||||
})
|
||||
private async checkAvailable() {
|
||||
if (!this.packetClient.available) {
|
||||
this.logger.logError('[Highway] packetClient not available!');
|
||||
throw new Error('packetClient not available!');
|
||||
}
|
||||
if (this.sig.sigSession === null || this.sig.sessionKey === null) {
|
||||
this.logger.logError('[Highway] sigSession or sessionKey not available!');
|
||||
await this.prepareUpload();
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: add signal to auto prepare when ready
|
||||
// TODO: refactor
|
||||
async prepareUpload(): Promise<void> {
|
||||
this.logger.log('[Highway] prepare tcpUpload!');
|
||||
private async prepareUpload(): Promise<void> {
|
||||
this.logger.logDebug('[Highway] prepareUpload!');
|
||||
const packet = this.packer.packHttp0x6ff_501();
|
||||
const req = await this.packetClient.sendPacket('HttpConn.0x6ff_501', packet, true);
|
||||
const u8RspData = Buffer.from(req.hex_data, 'hex');
|
||||
const rsp = new NapProtoMsg(HttpConn0x6ff_501Response).decode(u8RspData);
|
||||
const rsp = new NapProtoMsg(HttpConn0x6ff_501Response).decode(
|
||||
Buffer.from(req.hex_data, 'hex')
|
||||
);
|
||||
this.sig.sigSession = rsp.httpConn.sigSession
|
||||
this.sig.sessionKey = rsp.httpConn.sessionKey
|
||||
// this.logger.log(`[Highway] sigSession ${Buffer.from(this.sigSession).toString('hex')},
|
||||
// sessionKey ${Buffer.from(this.sessionKey).toString('hex')}`)
|
||||
this.logger.logDebug(`[Highway] sigSession ${Buffer.from(this.sig.sigSession).toString('hex')}, sessionKey ${Buffer.from(this.sig.sessionKey).toString('hex')}`)
|
||||
for (const info of rsp.httpConn.serverInfos) {
|
||||
if (info.serviceType !== 1) continue;
|
||||
for (const addr of info.serverAddrs) {
|
||||
this.logger.log(`[Highway PrepareUpload] server addr add: ${this.int32ip2str(addr.ip)}:${addr.port}`);
|
||||
this.logger.logDebug(`[Highway PrepareUpload] server addr add: ${int32ip2str(addr.ip)}:${addr.port}`);
|
||||
this.sig.serverAddr.push({
|
||||
ip: this.int32ip2str(addr.ip),
|
||||
ip: int32ip2str(addr.ip),
|
||||
port: addr.port
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async uploadGroupImageReq(groupUin: number, img: PacketMsgPicElement): Promise<void> {
|
||||
if (!this.available) {
|
||||
this.logger.logError('[Highway] not ready to Upload image!');
|
||||
return;
|
||||
async uploadImage(peer: Peer, img: PacketMsgPicElement): Promise<void> {
|
||||
await this.checkAvailable();
|
||||
if (peer.chatType === ChatType.KCHATTYPEGROUP) {
|
||||
await this.uploadGroupImageReq(Number(peer.peerUid), img);
|
||||
}
|
||||
// TODO: handle c2c pic upload
|
||||
}
|
||||
|
||||
private async uploadGroupImageReq(groupUin: number, img: PacketMsgPicElement): Promise<void> {
|
||||
const preReq = await this.packer.packUploadGroupImgReq(groupUin, img);
|
||||
const preRespRaw = await this.packetClient.sendPacket('OidbSvcTrpcTcp.0x11c4_100', preReq, true);
|
||||
const preResp = new NapProtoMsg(OidbSvcTrpcTcpBaseRsp).decode(
|
||||
@@ -103,8 +100,7 @@ export class PacketHighwaySession {
|
||||
const preRespData = new NapProtoMsg(NTV2RichMediaResp).decode(preResp.body);
|
||||
const ukey = preRespData.upload.uKey;
|
||||
if (ukey && ukey != "") {
|
||||
this.logger.log(`[Highway] get upload ukey: ${ukey}, need upload!`);
|
||||
this.logger.log(preRespData.upload.msgInfo)
|
||||
this.logger.logDebug(`[Highway] get upload ukey: ${ukey}, need upload!`);
|
||||
const index = preRespData.upload.msgInfo.msgInfoBody[0].index;
|
||||
const sha1 = Buffer.from(index.info.fileSha1, 'hex');
|
||||
const md5 = Buffer.from(index.info.fileHash, 'hex');
|
||||
@@ -112,7 +108,7 @@ export class PacketHighwaySession {
|
||||
fileUuid: index.fileUuid,
|
||||
uKey: ukey,
|
||||
network: {
|
||||
ipv4S: this.oidbIpv4s2HighwayIpv4s(preRespData.upload.ipv4S)
|
||||
ipv4S: oidbIpv4s2HighwayIpv4s(preRespData.upload.ipv4S)
|
||||
},
|
||||
msgInfoBody: preRespData.upload.msgInfo.msgInfoBody,
|
||||
blockSize: BlockSize,
|
||||
@@ -120,24 +116,17 @@ export class PacketHighwaySession {
|
||||
fileSha1: [sha1]
|
||||
}
|
||||
})
|
||||
console.log('extend', Buffer.from(extend).toString('hex'))
|
||||
await this.packetHighwayClient.httpUpload(1004, fs.createReadStream(img.path, { highWaterMark: BlockSize }), img.size, md5, extend);
|
||||
await this.packetHighwayClient.upload(
|
||||
1004,
|
||||
fs.createReadStream(img.path, {highWaterMark: BlockSize}),
|
||||
img.size,
|
||||
md5,
|
||||
extend
|
||||
);
|
||||
} else {
|
||||
this.logger.logError(`[Highway] get upload invalid ukey ${ukey}, don't need upload!`);
|
||||
}
|
||||
img.msgInfo = preRespData.upload.msgInfo;
|
||||
// img.groupPicExt = new NapProtoMsg(CustomFace).decode(preRespData.tcpUpload.compatQMsg)
|
||||
}
|
||||
|
||||
async uploadImage(peer: Peer, img: PacketMsgPicElement): Promise<void> {
|
||||
await this.prepareUpload();
|
||||
if (!this.available) {
|
||||
this.logger.logError('[Highway] not ready to tcpUpload image!');
|
||||
return;
|
||||
}
|
||||
if (peer.chatType === ChatType.KCHATTYPEGROUP) {
|
||||
await this.uploadGroupImageReq(Number(peer.peerUid), img);
|
||||
}
|
||||
// const uploadReq
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user