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

Commit

Permalink
Merge pull request #89 from takayama-lily/dev
Browse files Browse the repository at this point in the history
-
  • Loading branch information
takayama-lily authored Nov 12, 2020
2 parents c4a9719 + d3fdfcd commit b2fabd3
Show file tree
Hide file tree
Showing 18 changed files with 417 additions and 216 deletions.
85 changes: 27 additions & 58 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const {getErrorMessage} = require("./exception");
const BUF0 = Buffer.alloc(0);

const server_list = [
{ip:"msfwifi.3g.qq.com",port:8080,ping:null},
{ip:"msfwifi.3g.qq.com", port:8080},
];

function buildApiRet(retcode, data = null, error = null) {
Expand All @@ -41,28 +41,8 @@ class Client extends net.Socket {
}
class AndroidClient extends Client {
reconn_flag = true;
logger;
config;
status = Client.OFFLINE;
kickoff_reconn = false;
ignore_self = true;

// default phone
apkid = "com.tencent.mobileqq";
apkver = "8.4.1.2703";
apkname = "A8.4.1.2703aac4";
apksign = Buffer.from([166, 183, 69, 191, 36, 162, 194, 119, 82, 119, 22, 246, 243, 110, 182, 141]);
buildtime = 1591690260;
appid = 16;
sub_appid = 537064989;
bitmap = 184024956;
sigmap = 34869472;
sdkver = "6.0.0.2433";
ksid;
device;

uin = 0;
password_md5;

nickname = "";
age = 0;
sex = "unknown";
Expand All @@ -81,8 +61,6 @@ class AndroidClient extends Client {

session_id = crypto.randomBytes(4);
random_key = crypto.randomBytes(16);
captcha_sign;
t104;

sig = {
srm_token: BUF0,
Expand All @@ -96,6 +74,7 @@ class AndroidClient extends Client {
sig_key: BUF0,
ticket_key: BUF0,
device_token: BUF0,
emp_time: timestamp(),
};
cookies = {};

Expand All @@ -105,9 +84,7 @@ class AndroidClient extends Client {
const2 = crypto.randomBytes(4).readUInt32BE();
const3 = crypto.randomBytes(1)[0];

dir;

constructor(uin, config = {}) {
constructor(uin, config) {
super();
this.uin = uin;

Expand All @@ -120,32 +97,20 @@ class AndroidClient extends Client {
...config
};
this.config = config;

this.dir = createCacheDir(config.data_dir, uin);

this.logger = log4js.getLogger(`[BOT:${uin}]`);
this.logger.level = config.log_level;
this.ignore_self = config.ignore_self;
this.kickoff_reconn = config.kickoff;

if (config.platform == 3)
this.sub_appid = 537061176;
else if (config.platform == 2) {
this.sub_appid = 537065549;
this.apkid = "com.tencent.minihd.qq";
this.apkver = "5.8.9.3460";
this.apkname = "A5.8.9.3460";
this.apksign = Buffer.from([170, 57, 120, 244, 31, 217, 111, 249, 145, 74, 102, 158, 24, 100, 116, 199]);
this.buildtime = 1595836208;
this.bitmap = 150470524;
this.sigmap = 1970400;
}
this.ignore_self = !!config.ignore_self;
this.kickoff_reconn = !!config.kickoff;

const filepath = path.join(this.dir, `device-${uin}.json`);
if (!fs.existsSync(filepath))
this.logger.info("创建了新的设备文件:" + filepath);
this.device = device(filepath);
this.ksid = Buffer.from(`|${this.device.imei}|` + this.apkname);
this.device = device.getDeviceInfo(filepath);
this.apk = device.getApkInfo(config.platform);
if (config.platform == 3)
this.apk.subid = 537061176;
this.ksid = Buffer.from(`|${this.device.imei}|` + this.apk.name);

this.on("error", (err)=>{
this.logger.error(err.message);
Expand All @@ -165,7 +130,7 @@ class AndroidClient extends Client {
this.reconn_flag = false;
setTimeout(()=>{
this._connect(this.register.bind(this));
}, 1000);
}, 500);
}
});

Expand Down Expand Up @@ -259,14 +224,16 @@ class AndroidClient extends Client {
return;
this.heartbeat = setInterval(async()=>{
this.doCircle();
if (Date.now() - this.send_timestamp > 240000)
core.getMsg.call(this);
try {
await wt.heartbeat.call(this);
} catch (e) {
this.logger.warn("Heartbeat timeout!");
if (Date.now() - this.recv_timestamp > 10000)
this.destroy();
} catch {
try {
await wt.heartbeat.call(this);
} catch {
this.logger.warn("Heartbeat timeout!");
if (Date.now() - this.recv_timestamp > 15000)
this.destroy();
}
}
}, 60000);
}
Expand Down Expand Up @@ -301,7 +268,6 @@ class AndroidClient extends Client {
sub_type = "kickoff";
if (this.kickoff_reconn) {
this.logger.info("3秒后重新连接..");
this.ksid = Buffer.from(`|${this.device.imei}|` + this.apkname);
setTimeout(this.login.bind(this), 3000);
} else {
this.terminate();
Expand Down Expand Up @@ -387,6 +353,9 @@ class AndroidClient extends Client {
}
}
doCircle() {
wt.exchangeEMP.call(this);
if (Date.now() - this.send_timestamp > 120000)
core.getMsg.call(this);
for (let time of this.seq_cache.keys()) {
if (timestamp() - time >= 60)
this.seq_cache.delete(time);
Expand Down Expand Up @@ -604,7 +573,7 @@ class AndroidClient extends Client {
///////////////////////////////////////////////////

async getCookies(domain) {
// await wt.exchangeEMP();
await wt.exchangeEMP.call(this);
if (domain && !this.cookies[domain])
return buildApiRet(100, null, {code: -1, message: "unknown domain"});
let cookies = `uin=o${this.uin}; skey=${this.sig.skey};`;
Expand All @@ -614,7 +583,7 @@ class AndroidClient extends Client {
}

async getCsrfToken() {
// await wt.exchangeEMP();
await wt.exchangeEMP.call(this);
let token = 5381;
for (let v of this.sig.skey)
token = token + (token << 5) + v;
Expand Down Expand Up @@ -674,8 +643,6 @@ class AndroidClient extends Client {
}
}

//----------------------------------------------------------------------------------------------------

/**
* @deprecated
*/
Expand Down Expand Up @@ -707,6 +674,8 @@ function createCacheDir(dir, uin) {
*/
function setGlobalConfig() {}

//----------------------------------------------------------------------------------------------------

/**
* @param {Number} uin
* @param {JSON} config
Expand Down
43 changes: 42 additions & 1 deletion device.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function genDevice(filepath) {
* @param {String} filepath
* @returns {import("./lib/ref").Device}
*/
module.exports = function(filepath) {
function getDeviceInfo(filepath) {
var d;
if (fs.existsSync(filepath)) {
d = JSON.parse(fs.readFileSync(filepath));
Expand Down Expand Up @@ -110,3 +110,44 @@ module.exports = function(filepath) {
device.guid = md5(Buffer.concat([Buffer.from(device.imei), Buffer.from(device.mac_address)]));
return device;
};

const apk = {
1: {
id: "com.tencent.mobileqq",
name: "A8.4.1.2703aac4",
version: "8.4.1.2703",
ver: "8.4.1",
sign: Buffer.from([166, 183, 69, 191, 36, 162, 194, 119, 82, 119, 22, 246, 243, 110, 182, 141]),
buildtime: 1591690260,
appid: 16,
subid: 537064989,
bitmap: 184024956,
sigmap: 34869472,
sdkver: "6.0.0.2428",
},
2: {
id: "com.tencent.minihd.qq",
name: "A5.8.9.3460",
version: "5.8.9.3460",
ver: "5.8.9",
sign: Buffer.from([170, 57, 120, 244, 31, 217, 111, 249, 145, 74, 102, 158, 24, 100, 116, 199]),
buildtime: 1595836208,
appid: 16,
subid: 537065549,
bitmap: 150470524,
sigmap: 1970400,
sdkver: "6.0.0.2433",
}
}

/**
* @param {Number} platform
* @returns {import("./lib/ref").ApkInfo}
*/
function getApkInfo(platform) {
return apk[platform] ? apk[platform] : apk[2];
}

module.exports = {
getDeviceInfo, getApkInfo
}
31 changes: 15 additions & 16 deletions docs/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@

----

|[消息]|文字和表情|长消息|图片|语音|合并转发|xml/json|匿名|
|[消息]|文字和表情|长消息|图片|语音|合并转发|分享|匿名|
|-|-|-|-|-|-|-|-|
|好友|||||<s>◯</s>|||
|群聊|||||<s>◯</s>|||
|讨论组|||||<s>◯</s>|||
|临时会话||||||||
|好友|||||<s>◯</s>|||
|群聊|||||<s>◯</s>|||
|讨论组|||||<s>◯</s>|||
|临时会话||||||||

----

|[好友功能]|API|事件|
|-|-|-|
|好友列表||◯(好友增加)|
|好友列表||◯(好友增减)|
|陌生人列表|||
|处理申请|||
|撤回消息|||
Expand All @@ -29,12 +29,12 @@

|[群功能]|API|事件|
|-|-|-|
|群列表||◯(群增加)|
|成员列表||◯(成员增加)|
|群列表||◯(群增减)|
|成员列表||◯(成员增减)|
|踢人|||
|禁言|||
|撤回|||
|修改名片|||
|修改名片|||
|修改群名|||
|群公告|||
|其他设置|||
Expand All @@ -45,9 +45,9 @@
|转让|||
|解散|||
|退群|||
|同意邀请|||
|处理申请|||
|邀请好友入群|||
|群邀请|||
|群申请|||
|邀请好友|||
|添加群|||

----
Expand All @@ -61,7 +61,7 @@
|修改个人说明||
|修改签名||
|修改头像||
|获取cookies||
|获取cookies||

----

Expand All @@ -77,8 +77,7 @@ CQ码是指字符串格式下用于表示多媒体内容的方式,形如:
|[CQ码]|||说明|
|-|-|-|-|
|at|||[CQ:at,qq=123456,text=@ABC,dummy=0]<br>text用来定义@不到时的输出<br>dummy设为1可以假@|
|face|◯|◯|[CQ:face,id=104]
|sface|||此表情HD协议不支持,[CQ:sface,id=271,text=/吃瓜]|
|face|||[CQ:face,id=104]|
|bface|||原创表情,[CQ:bface,file=xxxxxxxx,text=摸头]|
|dice&rps|||骰子和猜拳:<br>[CQ:dice,id=1]<br>[CQ:rps,id=1]|
|image|||参考 [图片](https://github.com/howmanybots/onebot/blob/master/v11/specs/message/segment.md#%E5%9B%BE%E7%89%87)|
Expand All @@ -87,7 +86,7 @@ CQ码是指字符串格式下用于表示多媒体内容的方式,形如:
|anonymous|||发匿名,[CQ:anonymous,ignore=1]<br>ignore可省略,为0时匿名失败不发送|
|notice|||群公告|
|file|||群文件|
|music|||
|music|||[CQ:music,type=qq,id=xxxxxx]<br>[CQ:music,type=163,id=xxxxxx]|
|video|||
|location|||[CQ:location,address=江西省九江市修水县,lat=29.063940,lng=114.339610]|
|contact|◯|✕|联系人或群推荐
Expand Down
17 changes: 9 additions & 8 deletions lib/individual.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use strict";
const fs = require("fs");
const {uinAutoCheck, md5} = require("./common");
const {downloadWebImage, highwayUpload} = require("./service");
const {downloadWebImage, highwayUpload, readFile} = require("./service");
const pb = require("./pb");
const jce = require("./jce");

Expand All @@ -19,17 +18,19 @@ async function setPortrait(file) {
if (file.startsWith("base64://")) {
buf = Buffer.from(file.replace("base64://", ""), "base64");
} else if (file.startsWith("http")) {
buf = await downloadWebImage(file);
try {
buf = await downloadWebImage(file);
} catch (e) {
throw new Error(e);
}
} else {
file = file.replace(/^file:\/{2,3}/, "");
try {
buf = await fs.promises.readFile(file)
buf = await readFile(file);
} catch (e) {
throw new Error("Local file not exists: " + file);
throw new Error(e);
}
}
if (!buf || !buf.length)
throw new Error("Fail to get file: " + file);
}
const body = pb.encode({
1281: {
Expand Down Expand Up @@ -193,7 +194,7 @@ async function setSign(sign = "") {
3: {
1: 109,
2: {6: 825110830},
3: this.apkver.substr(0, 5)
3: this.apk.ver
},
5: {
1: this.uin,
Expand Down
Loading

0 comments on commit b2fabd3

Please sign in to comment.