Skip to content
This repository has been archived by the owner on Nov 2, 2023. It is now read-only.

Commit

Permalink
use https for image url
Browse files Browse the repository at this point in the history
improve pb decode performance
improve pkt send performance
  • Loading branch information
takayama-lily committed Apr 26, 2021
1 parent b834359 commit b383c4a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 28 deletions.
15 changes: 10 additions & 5 deletions lib/message/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,15 @@ class Parser {
if (!this.gid) { //私图
data.file = buildImageFileParam(elem[7].raw, elem[2], elem[9], elem[8], elem[5]);
if (elem[15])
data.url = "http://c2cpicdw.qpic.cn" + elem[15].raw;
data.url = "https://c2cpicdw.qpic.cn" + elem[15].raw;
else if (elem[10])
data.url = `http://c2cpicdw.qpic.cn/offpic_new/${this.uid}/${elem[10].raw}/0?term=2`;
data.url = `https://c2cpicdw.qpic.cn/offpic_new/${this.uid}/${elem[10].raw}/0?term=2`;
} else { //群图
data.file = buildImageFileParam(elem[13].raw, elem[25], elem[22], elem[23], elem[20]);
if (elem[16])
data.url = "http://gchat.qpic.cn" + elem[16].raw;
data.url = "https://gchat.qpic.cn" + elem[16].raw;
else
data.url = `http://gchat.qpic.cn/gchatpic_new/${this.uid}/${code2uin(this.gid)}-${elem[7]}-${elem[13].raw.toString("hex").toUpperCase()}/0?term=2`;
data.url = `https://gchat.qpic.cn/gchatpic_new/${this.uid}/${code2uin(this.gid)}-${elem[7]}-${elem[13].raw.toString("hex").toUpperCase()}/0?term=2`;
}
return data;
}
Expand Down Expand Up @@ -613,7 +613,12 @@ async function parseGroupMsg(msg, realtime = false) {
let user;
if (!parser.anonymous) {
try {
user = (await this.getGroupMemberInfo(group_id, user_id)).data;
try {
user = this.gml.get(group_id).get(user_id);
this.getGroupMemberInfo(group_id, user_id);
} catch {
user = (await this.getGroupMemberInfo(group_id, user_id)).data;
}
if (user && realtime) {
const extra = parser.extra;
if (extra[7])
Expand Down
3 changes: 3 additions & 0 deletions lib/pb.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ function encode(o) {
* @param {pb.Long} long
*/
function long2int(long) {
if (long.high === 0) {
return long.low >= 0 ? long.low : 4294967296 + long.low;
}
const bigint = (BigInt(long.high) << 32n) | (BigInt(long.low) & 0xffffffffn);
const int = parseInt(long);
return Number.isSafeInteger(int) ? int : bigint;
Expand Down
15 changes: 9 additions & 6 deletions lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function _buildHighwayUploadRequestPackets(o, cmd, seq = randomBytes(2).readUInt
*/
function highwayUpload(ip, port, o, cmd) {
ip = int32ip2str(ip);
this.logger.trace(`highway ip:${ip} port:${port}`);
this.logger.debug(`highway ip:${ip} port:${port}`);
return new Promise((resolve) => {
const client = net.connect(port, ip, () => {
const pkt = packets.shift();
Expand Down Expand Up @@ -147,16 +147,19 @@ function _downloadFromWeb(url, headers, timeout, proxy, mime_type, maxsize = MAX
if (res.headers["content-length"] && res.headers["content-length"] > maxsize) {
return reject(`文件体积太大(maxsize=${maxsize})。`);
}
let data = Buffer.alloc(0);
let data = [], size = 0;
res.on("data", (chunk) => {
data = Buffer.concat([data, chunk]);
if (data.length >= maxsize) {
size += chunk.length;
if (size > maxsize) {
res.destroy();
reject(`文件体积太大(maxsize=${maxsize})。`);
} else {
data.push(chunk);
}
});
res.on("end", () => {
resolve(data);
resolve(Buffer.concat(data));
data = null;
});
});
req.on("error", (e) => {
Expand All @@ -181,7 +184,7 @@ function downloadWebImage(url, proxy, timeout, headers = null) {
return _downloadFromWeb(url, headers, timeout, proxy, "image");
}
function downloadWebRecord(url, proxy, timeout, headers = null) {
return _downloadFromWeb(url, headers, timeout, proxy, "", 0xfffffff);
return _downloadFromWeb(url, headers, timeout, proxy, "");
}

function readFile(path, maxsize = MAX_UPLOAD_SIZE) {
Expand Down
44 changes: 28 additions & 16 deletions lib/wtlogin/wt.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,32 @@ function build0x0BPacket(cmd, body, seq = 0) {
seq = seq ? seq : this.nextSeq();
this.logger.trace(`send:${cmd} seq:${seq}`);
const type = cmd === "wtlogin.exchange_emp" ? 2 : 1;
let sso = new Writer()
.writeWithLength(cmd)
.writeWithLength(this.session_id)
.writeU32(4)
.read();
sso = new Writer().writeWithLength(sso).writeWithLength(body).read();
body = new Writer()
.writeU32(0x0B)
.writeU8(type)
.write32(seq)
.writeU8(0)
.writeWithLength(this.uin.toString())
.writeBytes(tea.encrypt(sso, type === 1 ? this.sig.d2key : BUF16))
.read();
return new Writer().writeWithLength(body).read();

let len = cmd.length + 20;
const sso = Buffer.allocUnsafe(len + body.length + 4);
sso.writeUInt32BE(len, 0);
sso.writeUInt32BE(cmd.length + 4, 4);
sso.fill(cmd, 8);
let offset = cmd.length + 8;
sso.writeUInt32BE(8, offset);
sso.fill(this.session_id, offset + 4);
sso.writeUInt32BE(4, offset + 8);
sso.writeUInt32BE(body.length + 4, offset + 12);
sso.fill(body, offset + 16);

const encrypted = tea.encrypt(sso, type === 1 ? this.sig.d2key : BUF16);
const uin = String(this.uin);
len = encrypted.length + uin.length + 18;
const pkt = Buffer.allocUnsafe(len);
pkt.writeUInt32BE(len, 0);
pkt.writeUInt32BE(0x0B, 4);
pkt.writeUInt8(type, 8);
pkt.writeInt32BE(seq, 9);
pkt.writeUInt8(0, 13);
pkt.writeUInt32BE(uin.length + 4, 14);
pkt.fill(uin, 18);
pkt.fill(encrypted, uin.length + 18);
return pkt;
}

//login req----------------------------------------------------------------------------------------
Expand Down Expand Up @@ -382,7 +393,8 @@ async function exchangeEMP() {
decodeT119.call(this, t[0x119]);
} else {
fs.unlink(path.join(this.dir, "token"), NOOP);
this.logger.warn("刷新cookies失败,可能是由于你切换过登录协议所导致。如果你需要使用依赖cookies的功能建议立即重启程序。");
this.sig.emp_time = 0xffffffff;
this.logger.warn("刷新cookies失败,可能是由于你切换过登录协议所导致。如果你需要使用依赖cookies的功能建议立即重新登录。");
}
} catch { }
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"dependencies": {
"https-proxy-agent": "^5.0.0",
"image-size": "^0.9.7",
"jce": "^0.2.1",
"jce": "^0.2.3",
"log4js": "^6.3.0",
"long": "^4.0.0"
},
Expand Down

0 comments on commit b383c4a

Please sign in to comment.