oicq-icalingua-plus-plus 1.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/LICENSE +373 -0
  2. package/README.md +73 -0
  3. package/bin/oicq +2 -0
  4. package/http-api/README.md +187 -0
  5. package/http-api/api.js +217 -0
  6. package/http-api/config.sample.js +44 -0
  7. package/http-api/core.js +480 -0
  8. package/http-api/cq-notice.js +39 -0
  9. package/http-api/filter.js +128 -0
  10. package/http-api/index.js +39 -0
  11. package/index.d.ts +1214 -0
  12. package/index.js +34 -0
  13. package/lib/algo/jce/index.js +63 -0
  14. package/lib/algo/jce/jce.js +341 -0
  15. package/lib/algo/pb.js +159 -0
  16. package/lib/algo/protobuf.min.js +8 -0
  17. package/lib/algo/tea.js +121 -0
  18. package/lib/client-ext.js +312 -0
  19. package/lib/client-net.js +147 -0
  20. package/lib/client.js +594 -0
  21. package/lib/common.js +252 -0
  22. package/lib/core/friendlist.js +415 -0
  23. package/lib/core/nessy.js +148 -0
  24. package/lib/core/ref.d.ts +1 -0
  25. package/lib/core/sysmsg.js +388 -0
  26. package/lib/core/troop.js +586 -0
  27. package/lib/device.js +181 -0
  28. package/lib/exception.js +57 -0
  29. package/lib/message/builder.js +1093 -0
  30. package/lib/message/chat.js +276 -0
  31. package/lib/message/face.js +293 -0
  32. package/lib/message/file.js +425 -0
  33. package/lib/message/history.js +73 -0
  34. package/lib/message/image.js +533 -0
  35. package/lib/message/multi.js +242 -0
  36. package/lib/message/music.js +172 -0
  37. package/lib/message/parser.js +774 -0
  38. package/lib/message/ptt.js +389 -0
  39. package/lib/oicq.js +427 -0
  40. package/lib/onlinepush.js +616 -0
  41. package/lib/ref.d.ts +177 -0
  42. package/lib/service.js +204 -0
  43. package/lib/wtlogin/ecdh.js +15 -0
  44. package/lib/wtlogin/login-password.js +215 -0
  45. package/lib/wtlogin/login-qrcode.js +249 -0
  46. package/lib/wtlogin/tlv.js +331 -0
  47. package/lib/wtlogin/writer.js +88 -0
  48. package/lib/wtlogin/wt.js +422 -0
  49. package/package.json +50 -0
  50. package/util.js +115 -0
@@ -0,0 +1,249 @@
1
+ /**
2
+ * 二维码登录流程
3
+ *
4
+ * token -> ok(二次)
5
+ * token not exists ↘
6
+ * fetch -> qrcodeLogin -> ok
7
+ * token (expired) ↗
8
+ */
9
+ "use strict";
10
+ const fs = require("fs");
11
+ const path = require("path");
12
+ const Readable = require("stream").Readable;
13
+ const jsqr = require("jsqr");
14
+ const { PNG } = require("pngjs");
15
+ const qrt = require("qrcode-terminal");
16
+ const { WtLogin, readTlv, BUF_UNKNOWN} = require("./wt");
17
+ const tea = require("../algo/tea");
18
+ const Writer = require("./writer");
19
+ const { timestamp, BUF16, BUF0 } = require("../common");
20
+
21
+ const ERROR_FETCH = new Error("获取二维码失败,请重试。");
22
+ const ERROR_SCAN = new Error("扫描二维码失败,请重新获取。");
23
+ const ERROR_UNKNOWN = new Error("扫码遇到未知错误。");
24
+
25
+ WtLogin.prototype._buildTransEmpPacket = function (cmdid, head, body) {
26
+ this.c.nextSeq();
27
+ body = new Writer()
28
+ .writeBytes(head)
29
+ .writeU32(timestamp())
30
+ .writeU8(2)
31
+ .writeU16(44 + body.length)
32
+ .writeU16(cmdid)
33
+ .writeBytes(Buffer.alloc(21))
34
+ .writeU8(3)
35
+ .writeU16(0)
36
+ .writeU16(50)
37
+ .writeU32(this.seq_id)
38
+ .writeU64(0)
39
+ .writeBytes(body)
40
+ .writeU8(3)
41
+ .read();
42
+
43
+ body = new Writer()
44
+ .writeU8(0x02)
45
+ .writeU8(0x01)
46
+ .writeBytes(this.random_key)
47
+ .writeU16(0x131)
48
+ .writeU16(0x01)
49
+ .writeTlv(this.ecdh.public_key)
50
+ .writeBytes(tea.encrypt(body, this.ecdh.share_key))
51
+ .read();
52
+
53
+ return new Writer()
54
+ .writeWithLength(
55
+ new Writer()
56
+ .writeU32(0x0A)
57
+ .writeU8(2)
58
+ .writeWithLength(BUF0)
59
+ .writeU8(0)
60
+ .writeWithLength("0")
61
+ .writeBytes(tea.encrypt(
62
+ new Writer()
63
+ .writeWithLength(
64
+ new Writer().writeU32(this.seq_id)
65
+ .writeU32(537067759)
66
+ .writeU32(537067759)
67
+ .writeBytes(BUF_UNKNOWN)
68
+ .writeWithLength(BUF0)
69
+ .writeWithLength("wtlogin.trans_emp")
70
+ .writeWithLength(this.session_id)
71
+ .writeWithLength(this.device.imei)
72
+ .writeU32(4)
73
+ .writeU16(this.ksid.length + 2)
74
+ .writeBytes(this.ksid)
75
+ .writeU32(4)
76
+ .read()
77
+ )
78
+ .writeWithLength(
79
+ new Writer()
80
+ .writeU8(0x02)
81
+ .writeU16(29 + body.length)
82
+ .writeU16(8001)
83
+ .writeU16(0x812)
84
+ .writeU16(1)
85
+ .writeU32(0)
86
+ .writeU8(3)
87
+ .writeU8(0x87)
88
+ .writeU8(0)
89
+ .writeU32(2)
90
+ .writeU32(0)
91
+ .writeU32(0)
92
+ .writeBytes(body)
93
+ .writeU8(0x03)
94
+ .read()
95
+ )
96
+ .read(), BUF16)
97
+ )
98
+ .read()
99
+ )
100
+ .read();
101
+ }
102
+
103
+ WtLogin.prototype.fetchQrcode = async function () {
104
+ if (this.qrcode_sig)
105
+ return this.qrcodeLogin();
106
+ const t = this.tlvPacker;
107
+ const body = new Writer()
108
+ .writeU16(0)
109
+ .writeU32(16)
110
+ .writeU64(0)
111
+ .writeU8(8)
112
+ .writeTlv(BUF0)
113
+ .writeU16(6)
114
+ .writeBytes(t(0x16))
115
+ .writeBytes(t(0x1B))
116
+ .writeBytes(t(0x1D))
117
+ .writeBytes(t(0x1F))
118
+ .writeBytes(t(0x33))
119
+ .writeBytes(t(0x35))
120
+ .read()
121
+ const pkt = this._buildTransEmpPacket(0x31, Buffer.from("0001110000001000000072000000", "hex"), body);
122
+ try {
123
+ let payload = await this.c.send(pkt);
124
+ payload = tea.decrypt(payload.slice(16, -1), this.ecdh.share_key);
125
+ const stream = Readable.from(payload, { objectMode: false });
126
+ stream.read(55);
127
+ const sig = stream.read(stream.read(2).readUInt16BE());
128
+ stream.read(2);
129
+ const t = readTlv(stream);
130
+ if (t[0x17]) {
131
+ this.qrcode_sig = sig;
132
+ const filepath = path.join(this.dir, "qrcode.png");
133
+ await fs.promises.writeFile(filepath, t[0x17]);
134
+ this.logger.mark("请用手机QQ扫描二维码,若打印出错请打开:" + filepath);
135
+ try {
136
+ const qrdata = PNG.sync.read(t[0x17]);
137
+ const qr = jsqr(new Uint8ClampedArray(qrdata.data), qrdata.width, qrdata.height);
138
+ qrt.generate(qr.data, console.log);
139
+ } catch { }
140
+ return this.c.em("system.login.qrcode", {
141
+ image: t[0x17]
142
+ });
143
+ }
144
+ throw ERROR_FETCH;
145
+ } catch (e) {
146
+ this.logger.error(e.message);
147
+ this.c.em("system.login.error", {
148
+ message: "获取二维码失败,请重试。",
149
+ code: -1
150
+ })
151
+ }
152
+ };
153
+
154
+ WtLogin.prototype.qrcodeLogin = async function () {
155
+ const body = new Writer()
156
+ .writeU16(5)
157
+ .writeU8(1)
158
+ .writeU32(8)
159
+ .writeU32(16)
160
+ .writeTlv(this.qrcode_sig)
161
+ .writeU64(0)
162
+ .writeU8(8)
163
+ .writeTlv(BUF0)
164
+ .writeU16(0)
165
+ .read()
166
+ this.qrcode_sig = undefined;
167
+ const pkt = this._buildTransEmpPacket(0x12, Buffer.from("0000620000001000000072000000", "hex"), body);
168
+ try {
169
+ let payload = await this.c.send(pkt);
170
+ payload = tea.decrypt(payload.slice(16, -1), this.ecdh.share_key);
171
+ const stream = Readable.from(payload, { objectMode: false });
172
+ stream.read(48);
173
+ let len = stream.read(2).readUInt16BE();
174
+ if (len > 0) {
175
+ len--;
176
+ if (stream.read(1)[0] === 2) {
177
+ stream.read(8);
178
+ len -= 8;
179
+ }
180
+ if (len > 0) {
181
+ stream.read(len);
182
+ }
183
+ }
184
+ stream.read(4);
185
+ const retcode = stream.read(1)[0];
186
+ this.logger.debug("二维码扫码结果:" + retcode);
187
+ if (retcode != 0)
188
+ throw ERROR_SCAN;
189
+ stream.read(4);
190
+ const uin = stream.read(4).readUInt32BE();
191
+ if (uin !== this.uin)
192
+ throw new Error(`扫码账号(${uin})与登录账号(${this.uin})不符`);
193
+ stream.read(6);
194
+ const t = readTlv(stream);
195
+ const t106 = t[0x18];
196
+ const t16a = t[0x19];
197
+ const t318 = t[0x65];
198
+ const tgtgt = t[0x1e];
199
+ if (t106 && t16a && t318 && tgtgt) {
200
+ this.t106 = t106;
201
+ this.device.tgtgt = tgtgt;
202
+ this._qrcodeLogin(t16a, t318);
203
+ } else {
204
+ throw ERROR_UNKNOWN;
205
+ }
206
+ } catch (e) {
207
+ this.logger.error(e.message);
208
+ this.c.em("system.login.error", {
209
+ message: e.message,
210
+ code: -2
211
+ })
212
+ }
213
+ };
214
+
215
+ WtLogin.prototype._qrcodeLogin = async function (t16a, t318) {
216
+ const t = this.tlvPacker;
217
+ let body = new Writer()
218
+ .writeU16(9)
219
+ .writeU16(24)
220
+ .writeBytes(t(0x18))
221
+ .writeBytes(t(0x1))
222
+ .writeU16(0x106)
223
+ .writeTlv(this.t106)
224
+ .writeBytes(t(0x116))
225
+ .writeBytes(t(0x100))
226
+ .writeBytes(t(0x107))
227
+ .writeBytes(t(0x142))
228
+ .writeBytes(t(0x144))
229
+ .writeBytes(t(0x145))
230
+ .writeBytes(t(0x147))
231
+ .writeU16(0x16a)
232
+ .writeTlv(t16a)
233
+ .writeBytes(t(0x154))
234
+ .writeBytes(t(0x141))
235
+ .writeBytes(t(0x8))
236
+ .writeBytes(t(0x511))
237
+ .writeBytes(t(0x187))
238
+ .writeBytes(t(0x188))
239
+ .writeBytes(t(0x194))
240
+ .writeBytes(t(0x191))
241
+ .writeBytes(t(0x202))
242
+ .writeBytes(t(0x177))
243
+ .writeBytes(t(0x516))
244
+ .writeBytes(t(0x521))
245
+ .writeU16(0x318)
246
+ .writeTlv(t318)
247
+ .read();
248
+ this.sendLogin("wtlogin.login", body);
249
+ };
@@ -0,0 +1,331 @@
1
+ "use strict";
2
+ const crypto = require("crypto");
3
+ const tea = require("../algo/tea");
4
+ const pb = require("../algo/pb");
5
+ const Writer = require("./writer");
6
+ const { md5 } = require("../common");
7
+
8
+ /**
9
+ * @this {import("./wt").WtLogin}
10
+ * @param {number} tag
11
+ * @returns {Buffer}
12
+ */
13
+ function packTlv(tag, ...args) {
14
+ /**
15
+ * @type {Writer}
16
+ */
17
+ const stream = tlv_map[tag].apply(this, args);
18
+ const len = Buffer.allocUnsafe(2);
19
+ len.writeUInt16BE(stream.readableLength);
20
+ stream.unshift(len);
21
+ const tag_buf = Buffer.allocUnsafe(2);
22
+ tag_buf.writeUInt16BE(tag);
23
+ stream.unshift(tag_buf);
24
+ return stream.read();
25
+ }
26
+
27
+ /**
28
+ * @type {({[k: number]: (this: import("./wt").WtLogin, ...args: any[]) => Writer})}
29
+ */
30
+ const tlv_map = {
31
+ 0x01: function () {
32
+ return new Writer()
33
+ .writeU16(1) // ip ver
34
+ .writeBytes(crypto.randomBytes(4))
35
+ .writeU32(this.uin)
36
+ .write32(Date.now() & 0xffffffff)
37
+ .writeBytes(Buffer.alloc(4)) //ip
38
+ .writeU16(0);
39
+ },
40
+ 0x08: function () {
41
+ return new Writer()
42
+ .writeU16(0)
43
+ .writeU32(2052)
44
+ .writeU16(0);
45
+ },
46
+ 0x16: function () {
47
+ return new Writer()
48
+ .writeU32(7)
49
+ .writeU32(16)
50
+ .writeU32(537067759)
51
+ .writeBytes(this.device.guid)
52
+ .writeTlv("com.tencent.qqlite")
53
+ .writeTlv("4.0.2")
54
+ .writeTlv(Buffer.from([0xA6, 0xB7, 0x45, 0xBF, 0x24, 0xA2, 0xC2, 0x77, 0x52, 0x77, 0x16, 0xF6, 0xF3, 0x6E, 0xB6, 0x8D]));
55
+ },
56
+ 0x18: function () {
57
+ return new Writer()
58
+ .writeU16(1) // ping ver
59
+ .writeU32(1536)
60
+ .writeU32(this.apk.appid)
61
+ .writeU32(0) // app client ver
62
+ .writeU32(this.uin)
63
+ .writeU16(0)
64
+ .writeU16(0);
65
+ },
66
+ 0x1B: function () {
67
+ return new Writer()
68
+ .writeU32(0)
69
+ .writeU32(0)
70
+ .writeU32(3)
71
+ .writeU32(4)
72
+ .writeU32(72)
73
+ .writeU32(2)
74
+ .writeU32(2)
75
+ .writeU16(0);
76
+ },
77
+ 0x1D: function () {
78
+ return new Writer()
79
+ .writeU8(1)
80
+ .writeU32(184024956)
81
+ .writeU32(0)
82
+ .writeU8(0)
83
+ .writeU32(0);
84
+ },
85
+ 0x1F: function () {
86
+ return new Writer()
87
+ .writeU8(0)
88
+ .writeTlv("android")
89
+ .writeTlv("7.1.2")
90
+ .writeU16(2)
91
+ .writeTlv("China Mobile GSM")
92
+ .writeTlv(Buffer.alloc(0))
93
+ .writeTlv("wifi");
94
+ },
95
+ 0x33: function () {
96
+ return new Writer().writeBytes(this.device.guid);
97
+ },
98
+ 0x35: function () {
99
+ return new Writer().writeU32(8);
100
+ },
101
+ 0x100: function (emp = 0) {
102
+ return new Writer()
103
+ .writeU16(1) // db buf ver
104
+ .writeU32(7) // sso ver, dont over 7
105
+ .writeU32(this.apk.appid)
106
+ .writeU32(emp ? 2 : this.apk.subid)
107
+ .writeU32(0) // app client ver
108
+ .writeU32(this.apk.sigmap);
109
+ },
110
+ 0x104: function () {
111
+ return new Writer().writeBytes(this.t104);
112
+ },
113
+ 0x106: function () {
114
+ const body = new Writer()
115
+ .writeU16(4) // tgtgt ver
116
+ .writeBytes(crypto.randomBytes(4))
117
+ .writeU32(7) // sso ver
118
+ .writeU32(this.apk.appid)
119
+ .writeU32(0) // app client ver
120
+ .writeU64(this.uin)
121
+ .write32(Date.now() & 0xffffffff)
122
+ .writeBytes(Buffer.alloc(4)) // dummy ip
123
+ .writeU8(1) // save password
124
+ .writeBytes(this.password_md5)
125
+ .writeBytes(this.device.tgtgt)
126
+ .writeU32(0)
127
+ .writeU8(1) // guid available
128
+ .writeBytes(this.device.guid)
129
+ .writeU32(this.apk.subid)
130
+ .writeU32(1) // login type password
131
+ .writeTlv(String(this.uin))
132
+ .writeU16(0)
133
+ .read();
134
+ const buf = Buffer.alloc(4);
135
+ buf.writeUInt32BE(this.uin);
136
+ const key = md5(Buffer.concat([
137
+ this.password_md5, Buffer.alloc(4), buf
138
+ ]));
139
+ return new Writer().writeBytes(tea.encrypt(body, key));
140
+ },
141
+ 0x107: function () {
142
+ return new Writer()
143
+ .writeU16(0) // pic type
144
+ .writeU8(0) // captcha type
145
+ .writeU16(0) // pic size
146
+ .writeU8(1); // ret type
147
+ },
148
+ 0x108: function () {
149
+ return new Writer().writeBytes(this.ksid);
150
+ },
151
+ 0x109: function () {
152
+ return new Writer().writeBytes(md5(this.device.imei));
153
+ },
154
+ 0x10a: function () {
155
+ return new Writer().writeBytes(this.sig.tgt);
156
+ },
157
+ 0x116: function () {
158
+ return new Writer()
159
+ .writeU8(0)
160
+ .writeU32(this.apk.bitmap)
161
+ .writeU32(0x10400) // sub sigmap
162
+ .writeU8(1) // size of app id list
163
+ .writeU32(1600000226); // app id list[0]
164
+ },
165
+ 0x124: function () {
166
+ return new Writer()
167
+ .writeTlv(this.device.os_type.slice(0, 16))
168
+ .writeTlv(this.device.version.release.slice(0, 16))
169
+ .writeU16(2) // network type
170
+ .writeTlv(this.device.sim.slice(0, 16))
171
+ .writeU16(0)
172
+ .writeTlv(this.device.apn.slice(0, 16));
173
+ },
174
+ 0x128: function () {
175
+ return new Writer()
176
+ .writeU16(0)
177
+ .writeU8(0) // guid new
178
+ .writeU8(1) // guid available
179
+ .writeU8(0) // guid changed
180
+ .writeU32(16777216) // guid flag
181
+ .writeTlv(this.device.model.slice(0, 32))
182
+ .writeTlv(this.device.guid.slice(0, 16))
183
+ .writeTlv(this.device.brand.slice(0, 16));
184
+ },
185
+ 0x141: function () {
186
+ return new Writer()
187
+ .writeU16(1) // ver
188
+ .writeTlv(this.device.sim)
189
+ .writeU16(2) // network type
190
+ .writeTlv(this.device.apn);
191
+ },
192
+ 0x142: function () {
193
+ return new Writer()
194
+ .writeU16(0)
195
+ .writeTlv(this.apk.id.slice(0, 32));
196
+ },
197
+ 0x143: function (d2) {
198
+ return new Writer().writeBytes(d2);
199
+ },
200
+ 0x144: function () {
201
+ const body = new Writer()
202
+ .writeU16(5) // tlv cnt
203
+ .writeBytes(packTlv.call(this, 0x109))
204
+ .writeBytes(packTlv.call(this, 0x52d))
205
+ .writeBytes(packTlv.call(this, 0x124))
206
+ .writeBytes(packTlv.call(this, 0x128))
207
+ .writeBytes(packTlv.call(this, 0x16e));
208
+ return new Writer().writeBytes(tea.encrypt(body.read(), this.device.tgtgt));
209
+ },
210
+ 0x145: function () {
211
+ return new Writer().writeBytes(this.device.guid);
212
+ },
213
+ 0x147: function () {
214
+ return new Writer()
215
+ .writeU32(this.apk.appid)
216
+ .writeTlv(this.apk.ver.slice(0, 5))
217
+ .writeTlv(this.apk.sign);
218
+ },
219
+ 0x154: function () {
220
+ return new Writer().writeU32(this.seq_id + 1);
221
+ },
222
+ 0x16a: function () {
223
+ return new Writer().writeBytes(this.sig.srm_token);
224
+ },
225
+ 0x16e: function () {
226
+ return new Writer().writeBytes(this.device.model);
227
+ },
228
+ 0x174: function () {
229
+ return new Writer().writeBytes(this.t174);
230
+ },
231
+ 0x177: function () {
232
+ return new Writer()
233
+ .writeU8(0x01)
234
+ .writeU32(this.apk.buildtime)
235
+ .writeTlv(this.apk.sdkver);
236
+ },
237
+ 0x17a: function () {
238
+ return new Writer().writeU32(9);
239
+ },
240
+ 0x17c: function (code) {
241
+ return new Writer().writeTlv(code);
242
+ },
243
+ 0x187: function () {
244
+ return new Writer().writeBytes(md5(this.device.mac_address));
245
+ },
246
+ 0x188: function () {
247
+ return new Writer().writeBytes(md5(this.device.android_id));
248
+ },
249
+ 0x191: function () {
250
+ return new Writer().writeU8(0x82);
251
+ },
252
+ 0x193: function (ticket) {
253
+ return new Writer().writeBytes(ticket);
254
+ },
255
+ 0x194: function () {
256
+ return new Writer().writeBytes(this.device.imsi);
257
+ },
258
+ 0x197: function () {
259
+ return new Writer().writeTlv(Buffer.alloc(1));
260
+ },
261
+ 0x198: function () {
262
+ return new Writer().writeTlv(Buffer.alloc(1));
263
+ },
264
+ 0x202: function () {
265
+ return new Writer()
266
+ .writeTlv(this.device.wifi_bssid.slice(0, 16))
267
+ .writeTlv(this.device.wifi_ssid.slice(0, 32));
268
+ },
269
+ 0x400: function () {
270
+ return new Writer()
271
+ .writeU16(1)
272
+ .writeU64(this.uin)
273
+ .writeBytes(this.device.guid)
274
+ .writeBytes(crypto.randomBytes(16))
275
+ .write32(1)
276
+ .write32(16)
277
+ .write32(Date.now() & 0xffffffff)
278
+ .writeBytes(Buffer.alloc(0));
279
+ },
280
+ 0x401: function () {
281
+ return new Writer().writeBytes(crypto.randomBytes(16));
282
+ },
283
+ 0x511: function () {
284
+ const domains = [
285
+ "tenpay.com", "openmobile.qq.com", "docs.qq.com", "connect.qq.com",
286
+ "qzone.qq.com", "vip.qq.com", "qun.qq.com", "game.qq.com", "qqweb.qq.com",
287
+ "office.qq.com", "ti.qq.com", "mail.qq.com", "gamecenter.qq.com", "mma.qq.com",
288
+ ];
289
+ const stream = new Writer().writeU16(domains.length);
290
+ for (let v of domains)
291
+ stream.writeU8(0x01).writeTlv(v);
292
+ return stream;
293
+ },
294
+ 0x516: function () {
295
+ return new Writer().writeU32(0);
296
+ },
297
+ 0x521: function () {
298
+ return new Writer()
299
+ .writeU32(0) // product type
300
+ .writeU16(0); // const
301
+ },
302
+ 0x525: function () {
303
+ return new Writer()
304
+ .writeU16(1) // tlv cnt
305
+ .writeU16(0x536) // tag
306
+ .writeTlv(Buffer.from([0x1, 0x0])); // zero
307
+ },
308
+ 0x52d: function () {
309
+ const d = this.device;
310
+ const buf = pb.encode({
311
+ 1: d.bootloader,
312
+ 2: d.proc_version,
313
+ 3: d.version.codename,
314
+ 4: d.version.incremental,
315
+ 5: d.fingerprint,
316
+ 6: d.boot_id,
317
+ 7: d.android_id,
318
+ 8: d.baseband,
319
+ 9: d.version.incremental,
320
+ });
321
+ return new Writer().writeBytes(buf);
322
+ },
323
+ };
324
+
325
+ /**
326
+ * @param {import("./wt").WtLogin} wt
327
+ * @returns {(this: import("./wt").WtLogin, tag: number, ...args: any[]) => Buffer}
328
+ */
329
+ module.exports.getPacker = function (wt) {
330
+ return packTlv.bind(wt);
331
+ };
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ const PassThrough = require("stream").PassThrough;
3
+
4
+ class Writer extends PassThrough {
5
+
6
+ /**
7
+ * @param {Number} v 0x0~0xff
8
+ * @returns {this}
9
+ */
10
+ writeU8(v) {
11
+ const buf = Buffer.allocUnsafe(1);
12
+ buf.writeUInt8(v);
13
+ this.write(buf);
14
+ return this;
15
+ }
16
+
17
+ /**
18
+ * @param {Number} v 0x0~0xffff
19
+ * @returns {this}
20
+ */
21
+ writeU16(v) {
22
+ const buf = Buffer.allocUnsafe(2);
23
+ buf.writeUInt16BE(v);
24
+ this.write(buf);
25
+ return this;
26
+ }
27
+
28
+ /**
29
+ * @param {Number} v
30
+ * @returns {this}
31
+ */
32
+ write32(v) {
33
+ const buf = Buffer.allocUnsafe(4);
34
+ buf.writeInt32BE(v);
35
+ this.write(buf);
36
+ return this;
37
+ }
38
+
39
+ /**
40
+ * @param {Number} v 0x0~0xffffffff
41
+ * @returns {this}
42
+ */
43
+ writeU32(v) {
44
+ const buf = Buffer.allocUnsafe(4);
45
+ buf.writeUInt32BE(v);
46
+ this.write(buf);
47
+ return this;
48
+ }
49
+
50
+ /**
51
+ * @param {Number} v 0x0~0xffffffffffffffff JS有精度问题,大于52位的整数传BigInt
52
+ * @returns {this}
53
+ */
54
+ writeU64(v) {
55
+ const buf = Buffer.allocUnsafe(8);
56
+ buf.writeBigUInt64BE(BigInt(v));
57
+ this.write(buf);
58
+ return this;
59
+ }
60
+
61
+ /**
62
+ * @param {String|Buffer} v 写入原始数据
63
+ * @returns {this}
64
+ */
65
+ writeBytes(v) {
66
+ if (typeof v === "string")
67
+ v = Buffer.from(v);
68
+ this.write(v);
69
+ return this;
70
+ }
71
+
72
+ /**
73
+ * @param {String|Buffer} v 前面会追加一个UInt32保存v的长度+4
74
+ * @returns {this}
75
+ */
76
+ writeWithLength(v) {
77
+ return this.writeU32(Buffer.byteLength(v) + 4).writeBytes(v);
78
+ }
79
+
80
+ /**
81
+ * @param {String|Buffer} v 前面会追加一个UInt16保存v的长度
82
+ * @returns {this}
83
+ */
84
+ writeTlv(v) {
85
+ return this.writeU16(Buffer.byteLength(v)).writeBytes(v);
86
+ }
87
+ }
88
+ module.exports = Writer;