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

Commit

Permalink
make algo dir, mk core dir
Browse files Browse the repository at this point in the history
move fn to nessy
remove stdin
  • Loading branch information
takayama-lily committed May 18, 2021
1 parent a72d9aa commit 1f1b4f0
Show file tree
Hide file tree
Showing 30 changed files with 222 additions and 242 deletions.
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const { Client, createClient } = require("./lib/client");
const { segment, cqcode } = require("./util");
module.exports = {
setGlobalConfig: () => { },
stdin: require("./stdin"),
segment, cqcode,
Client, createClient,
};
File renamed without changes.
8 changes: 4 additions & 4 deletions lib/pb.js → lib/algo/pb.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const pb = require("./protobuf.min");
class Proto {
/**
* @param {Buffer} bytes
* @param {import("./ref").Proto} decoded
* @param {import("../ref").Proto} decoded
*/
constructor(bytes, decoded) {
if (decoded)
Expand Down Expand Up @@ -80,7 +80,7 @@ function _encode(writer, tag, value) {
}

/**
* @param {import("./ref").Proto} o
* @param {import("../ref").Proto} o
* @returns {Uint8Array}
*/
function encode(o) {
Expand Down Expand Up @@ -114,7 +114,7 @@ function long2int(long) {

/**
* @param {Buffer} buf
* @returns {import("./ref").Proto}
* @returns {import("../ref").Proto}
*/
function decode(buf) {
const data = new Proto(buf);
Expand Down Expand Up @@ -155,7 +155,7 @@ function decode(buf) {

/**
* @param {string} cmd example: OidbSvc.0x568_22
* @param {Buffer|import("./ref").Proto} body
* @param {Buffer|import("../ref").Proto} body
*/
function encodeOIDB(cmd, body) {
cmd = cmd.replace("OidbSvc.", "").replace("oidb_", "").split("_");
Expand Down
File renamed without changes.
File renamed without changes.
37 changes: 19 additions & 18 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ const os = require("os");
const { exec } = require("child_process");
const { randomBytes } = require("crypto");
const log4js = require("log4js");
const pb = require("./pb");
const pb = require("./algo/pb");
const { getApkInfo, getDeviceInfo } = require("./device");
const { checkUin, timestamp, md5, BUF0 } = require("./common");
const core = require("./core");
const frdlst = require("./friendlist");
const sysmsg = require("./sysmsg");
const oicq = require("./oicq");
const frdlst = require("./core/friendlist");
const sysmsg = require("./core/sysmsg");
const troop = require("./core/troop");
const nessy = require("./core/nessy");
const wt = require("./wtlogin/wt");
const chat = require("./message/chat");
const { Gfs } = require("./message/file");
const troop = require("./troop");
const { getErrorMessage, TimeoutError } = require("./exception");

function buildApiRet(retcode, data = null, error = null) {
Expand Down Expand Up @@ -168,7 +169,7 @@ class Client extends net.Socket {
this.data_cache = this.data_cache.slice(len);
++this.stat.recv_pkt_cnt;
try {
core.parseIncomingPacket.call(this, packet);
oicq.parseIncomingPacket.call(this, packet);
} catch (e) {
this.logger.debug(e.stack);
this.emit("internal.exception", e);
Expand All @@ -185,15 +186,15 @@ class Client extends net.Socket {
if (!this.isOnline())
return;
this.logger.mark(`Welcome, ${this.nickname} ! 初始化资源...`);
core.submitDevice.call(this);
oicq.submitDevice.call(this);
await Promise.all([
frdlst.initFL.call(this),
frdlst.initGL.call(this)
]);
this.logger.mark(`加载了${this.fl.size}个好友,${this.gl.size}个群。`);
this.sync_finished = true;
this.logger.mark("初始化完毕,开始处理消息。");
core.getMsg.call(this);
oicq.getMsg.call(this);
this.em("system.online");
});

Expand Down Expand Up @@ -271,9 +272,9 @@ class Client extends net.Socket {
return;
this.heartbeat = setInterval(async () => {
this.doCircle();
if (!await core.getMsg.call(this) && this.isOnline()) {
if (!await oicq.getMsg.call(this) && this.isOnline()) {
this.logger.warn("GetMsg timeout!");
if (!await core.getMsg.call(this) && this.isOnline())
if (!await oicq.getMsg.call(this) && this.isOnline())
this.destroy();
}
wt.heartbeat.call(this);
Expand Down Expand Up @@ -328,7 +329,7 @@ class Client extends net.Socket {
this.em("system.offline." + sub_type, { message: data.info });
});
}
await core.getMsg.call(this);
await oicq.getMsg.call(this);
}

/**
Expand Down Expand Up @@ -520,7 +521,7 @@ class Client extends net.Socket {
///////////////////////////////////////////////////

setOnlineStatus(status) {
return this.useProtocol(troop.setStatus, arguments);
return this.useProtocol(nessy.setStatus, arguments);
}

getFriendList() {
Expand All @@ -536,13 +537,13 @@ class Client extends net.Socket {
async reloadFriendList() {
const ret = await this.useProtocol(frdlst.initFL, arguments);
this.sync_finished = true;
core.getMsg.call(this);
oicq.getMsg.call(this);
return ret;
}
async reloadGroupList() {
const ret = await this.useProtocol(frdlst.initGL, arguments);
this.sync_finished = true;
core.getMsg.call(this);
oicq.getMsg.call(this);
return ret;
}

Expand Down Expand Up @@ -604,7 +605,7 @@ class Client extends net.Socket {
return this.setGroupSetting(group_id, "ingGroupMemo", String(content));
}
setGroupSetting(group_id, k, v) {
return this.useProtocol(troop.doSetting, arguments);
return this.useProtocol(troop.setting, arguments);
}
setGroupAdmin(group_id, user_id, enable = true) {
return this.useProtocol(troop.setAdmin, arguments);
Expand Down Expand Up @@ -654,7 +655,7 @@ class Client extends net.Socket {
}

sendLike(user_id, times = 1) {
return this.useProtocol(troop.sendLike, arguments);
return this.useProtocol(nessy.sendLike, arguments);
}
setNickname(nickname) {
return this.useProtocol(troop.setProfile, [0x14E22, String(nickname)]);
Expand Down Expand Up @@ -690,11 +691,11 @@ class Client extends net.Socket {
return this.useProtocol(troop.setGroupPortrait, arguments);
}
// getLevelInfo(user_id) {
// return this.useProtocol(troop.getLevelInfo, arguments);
// return this.useProtocol(nessy.getLevelInfo, arguments);
// }

getRoamingStamp(no_cache = false) {
return this.useProtocol(chat.getRoamingStamp, arguments);
return this.useProtocol(nessy.getRoamingStamp, arguments);
}

getGroupNotice(group_id) {
Expand Down
2 changes: 1 addition & 1 deletion lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
const fs = require("fs");
const crypto = require("crypto");
const util = require("util");
const pb = require("./pb");
const pb = require("./algo/pb");

function uuid() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
Expand Down
6 changes: 3 additions & 3 deletions lib/friendlist.js → lib/core/friendlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* 相关api
*/
"use strict";
const common = require("./common");
const pb = require("./pb");
const jce = require("./jce");
const common = require("../common");
const pb = require("../algo/pb");
const jce = require("../algo/jce");

const friend_sex_map = {
"0": "unknown", "1": "male", "2": "female"
Expand Down
146 changes: 146 additions & 0 deletions lib/core/nessy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
"use strict";
const https = require("https");
const pb = require("../algo/pb");
const jce = require("../algo/jce");
const { uinAutoCheck } = require("../common");

/**
* @this {import("./ref").Client}
* @param {number} user_id
* @param {number} times 1~20
* @returns {import("./ref").ProtocolResponse}
*/
async function sendLike(user_id, times = 1) {
[user_id] = uinAutoCheck(user_id);
times = parseInt(times);
if (!(times > 0 && times <= 20))
times = 1;
const ReqFavorite = jce.encodeStruct([
jce.encodeNested([
this.uin, 1, this.seq_id + 1, 1, 0, Buffer.from("0C180001060131160131", "hex")
]),
user_id, 0, 1, times
]);
const extra = {
req_id: this.seq_id + 1,
service: "VisitorSvc",
method: "ReqFavorite",
};
const body = jce.encodeWrapper({ ReqFavorite }, extra);
const blob = await this.sendUni("VisitorSvc.ReqFavorite", body);
const rsp = jce.decode(blob);
return { result: rsp[0][3], emsg: rsp[0][4] };
}

/**
* 设置在线状态
* @this {import("./ref").Client}
* @param {number} status
* @returns {import("./ref").ProtocolResponse}
*/
async function setStatus(status) {
status = parseInt(status);
if (![11, 31, 41, 50, 60, 70].includes(status))
throw new Error("bad status");
let sub = 0;
if (status > 1000) {
sub = status, status = 11;
}
const SvcReqRegister = jce.encodeStruct([
this.uin,
7, 0, "", status, 0, 0, 0, 0, 0, 248,
this.device.version.sdk, 0, "", 0, null, this.device.guid, 2052, 0, this.device.model, this.device.model,
this.device.version.release, 1, 473, 0, null, 0, 0, "", 0, "",
"", "", null, 1, null, 0, null, sub, 0
]);
const extra = {
req_id: this.seq_id + 1,
service: "PushService",
method: "SvcReqRegister",
};
const body = jce.encodeWrapper({ SvcReqRegister }, extra);
const blob = await this.sendUni("StatSvc.SetStatusFromClient", body);
const rsp = jce.decode(blob);
let result = -1;
if (rsp[9]) {
result = 0;
this.online_status = status;
}
return { result };
}

/**
* @this {import("./ref").Client}
* @param {number} user_id
* @returns {import("./ref").ProtocolResponse}
*/
async function getLevelInfo(user_id) {
[user_id] = uinAutoCheck(user_id);
const cookie = (await this.getCookies("vip.qq.com")).data.cookies;
const url = `https://club.vip.qq.com/api/vip/getQQLevelInfo?requestBody={"iUin":${user_id}}`;
try {
let data = await new Promise((resolve, reject) => {
https.get(url, { headers: { cookie } }, (res) => {
if (res.statusCode !== 200) {
return reject("statusCode: " + res.statusCode);
}
res.setEncoding("utf-8");
let data = "";
res.on("data", chunk => data += chunk);
res.on("end", () => {
try {
data = JSON.parse(data);
if (data.ret !== 0) {
return reject(data.msg);
}
resolve(data.data.mRes);
} catch {
reject("response error");
}

});
}).on("error", (e) => reject(e.message));
});
return { result: 0, data };
} catch (e) {
return { result: -1, emsg: e };
}
}

/**
* 获取漫游表情
* @this {import("../ref").Client}
* @returns {import("../ref").ProtocolResponse}
*/
async function getRoamingStamp(no_cache = false) {
if (!this.roaming_stamp.length || no_cache) {
const body = pb.encode({
1: {
1: 109,
2: this.device.version.release,
3: this.apk.ver
},
2: this.uin,
3: 1,
});
const blob = await this.sendUni("Faceroam.OpReq", body);
const rsp = pb.decode(blob);
const result = rsp[1];
if (result !== 0) {
return { result, emsg: String(rsp[2]) };
}
if (rsp[4][1]) {
const bid = String(rsp[4][3]);
const faces = Array.isArray(rsp[4][1]) ? rsp[4][1] : [rsp[4][1]];
this.roaming_stamp = faces.map(x => `https://p.qpic.cn/${bid}/${this.uin}/${x}/0`);
}
}
return {
result: 0,
data: this.roaming_stamp,
};
}

module.exports = {
setStatus, sendLike, getLevelInfo, getRoamingStamp
};
1 change: 1 addition & 0 deletions lib/core/ref.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "../ref";
6 changes: 3 additions & 3 deletions lib/sysmsg.js → lib/core/sysmsg.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* 相关api
*/
"use strict";
const pb = require("./pb");
const pb = require("../algo/pb");

/**
* @param {number} user_id
* @param {BigInt|number} seq
* @param {bigint|number} seq
*/
function _genFriendRequestFlag(user_id, seq) {
return user_id.toString(16).padStart(8, "0") + seq.toString(16);
Expand All @@ -25,7 +25,7 @@ function _parseFriendRequestFlag(flag) {
/**
* @param {number} user_id
* @param {number} group_id
* @param {BigInt|number} seq
* @param {bigint|number} seq
* @param {0|1} invite
*/
function _genGroupRequestFlag(user_id, group_id, seq, invite) {
Expand Down
Loading

0 comments on commit 1f1b4f0

Please sign in to comment.