@yume-chan/adb 0.0.10 → 0.0.13
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.
- package/CHANGELOG.json +50 -0
- package/CHANGELOG.md +29 -1
- package/LICENSE +1 -1
- package/README.md +71 -51
- package/esm/adb.d.ts +48 -0
- package/esm/adb.d.ts.map +1 -0
- package/esm/adb.js +149 -136
- package/esm/adb.js.map +1 -1
- package/{dts → esm}/auth.d.ts +11 -11
- package/esm/auth.d.ts.map +1 -0
- package/esm/auth.js +72 -110
- package/esm/auth.js.map +1 -1
- package/esm/backend.d.ts +9 -0
- package/esm/backend.d.ts.map +1 -0
- package/{dts → esm}/commands/base.d.ts +1 -1
- package/esm/commands/base.d.ts.map +1 -0
- package/esm/commands/base.js +1 -0
- package/esm/commands/base.js.map +1 -1
- package/{dts → esm}/commands/framebuffer.d.ts +3 -3
- package/esm/commands/framebuffer.d.ts.map +1 -0
- package/esm/commands/framebuffer.js +15 -18
- package/esm/commands/framebuffer.js.map +1 -1
- package/esm/commands/index.d.ts +9 -0
- package/esm/commands/index.d.ts.map +1 -0
- package/esm/commands/index.js +8 -8
- package/esm/commands/index.js.map +1 -1
- package/esm/commands/install.d.ts +4 -0
- package/esm/commands/install.d.ts.map +1 -0
- package/esm/commands/install.js +19 -13
- package/esm/commands/install.js.map +1 -1
- package/esm/commands/power.d.ts +23 -0
- package/esm/commands/power.d.ts.map +1 -0
- package/esm/commands/power.js +45 -0
- package/esm/commands/power.js.map +1 -0
- package/{dts → esm}/commands/reverse.d.ts +3 -3
- package/esm/commands/reverse.d.ts.map +1 -0
- package/esm/commands/reverse.js +49 -60
- package/esm/commands/reverse.js.map +1 -1
- package/esm/commands/subprocess/index.d.ts +56 -0
- package/esm/commands/subprocess/index.d.ts.map +1 -0
- package/esm/commands/subprocess/index.js +88 -0
- package/esm/commands/subprocess/index.js.map +1 -0
- package/esm/commands/subprocess/legacy.d.ts +35 -0
- package/esm/commands/subprocess/legacy.d.ts.map +1 -0
- package/esm/commands/subprocess/legacy.js +53 -0
- package/esm/commands/subprocess/legacy.js.map +1 -0
- package/esm/commands/subprocess/protocol.d.ts +39 -0
- package/esm/commands/subprocess/protocol.d.ts.map +1 -0
- package/esm/commands/subprocess/protocol.js +157 -0
- package/esm/commands/subprocess/protocol.js.map +1 -0
- package/esm/commands/subprocess/protocols/index.d.ts +4 -0
- package/esm/commands/subprocess/protocols/index.d.ts.map +1 -0
- package/esm/commands/subprocess/protocols/index.js +4 -0
- package/esm/commands/subprocess/protocols/index.js.map +1 -0
- package/esm/commands/subprocess/protocols/none.d.ts +35 -0
- package/esm/commands/subprocess/protocols/none.d.ts.map +1 -0
- package/esm/commands/subprocess/protocols/none.js +53 -0
- package/esm/commands/subprocess/protocols/none.js.map +1 -0
- package/esm/commands/subprocess/protocols/shell.d.ts +39 -0
- package/esm/commands/subprocess/protocols/shell.d.ts.map +1 -0
- package/esm/commands/subprocess/protocols/shell.js +155 -0
- package/esm/commands/subprocess/protocols/shell.js.map +1 -0
- package/esm/commands/subprocess/protocols/types.d.ts +50 -0
- package/esm/commands/subprocess/protocols/types.d.ts.map +1 -0
- package/esm/commands/{shell → subprocess/protocols}/types.js +0 -0
- package/esm/commands/subprocess/protocols/types.js.map +1 -0
- package/esm/commands/subprocess/types.d.ts +50 -0
- package/esm/commands/subprocess/types.d.ts.map +1 -0
- package/esm/commands/subprocess/types.js +2 -0
- package/esm/commands/{shell → subprocess}/types.js.map +1 -1
- package/{dts/commands/shell → esm/commands/subprocess}/utils.d.ts +0 -0
- package/esm/commands/subprocess/utils.d.ts.map +1 -0
- package/esm/commands/{shell → subprocess}/utils.js +0 -0
- package/esm/commands/subprocess/utils.js.map +1 -0
- package/esm/commands/sync/index.d.ts +8 -0
- package/esm/commands/sync/index.d.ts.map +1 -0
- package/esm/commands/sync/index.js +7 -7
- package/esm/commands/sync/index.js.map +1 -1
- package/esm/commands/sync/list.d.ts +41 -0
- package/esm/commands/sync/list.d.ts.map +1 -0
- package/esm/commands/sync/list.js +52 -20
- package/esm/commands/sync/list.js.map +1 -1
- package/esm/commands/sync/pull.d.ts +11 -0
- package/esm/commands/sync/pull.d.ts.map +1 -0
- package/esm/commands/sync/pull.js +17 -11
- package/esm/commands/sync/pull.js.map +1 -1
- package/esm/commands/sync/push.d.ts +8 -0
- package/esm/commands/sync/push.d.ts.map +1 -0
- package/esm/commands/sync/push.js +19 -37
- package/esm/commands/sync/push.js.map +1 -1
- package/{dts → esm}/commands/sync/request.d.ts +4 -3
- package/esm/commands/sync/request.d.ts.map +1 -0
- package/esm/commands/sync/request.js +25 -27
- package/esm/commands/sync/request.js.map +1 -1
- package/{dts → esm}/commands/sync/response.d.ts +4 -3
- package/esm/commands/sync/response.d.ts.map +1 -0
- package/esm/commands/sync/response.js +17 -20
- package/esm/commands/sync/response.js.map +1 -1
- package/{dts → esm}/commands/sync/stat.d.ts +16 -4
- package/esm/commands/sync/stat.d.ts.map +1 -0
- package/esm/commands/sync/stat.js +38 -30
- package/esm/commands/sync/stat.js.map +1 -1
- package/esm/commands/sync/sync.d.ts +61 -0
- package/esm/commands/sync/sync.d.ts.map +1 -0
- package/esm/commands/sync/sync.js +122 -89
- package/esm/commands/sync/sync.js.map +1 -1
- package/{dts → esm}/commands/tcpip.d.ts +1 -1
- package/{dts → esm}/commands/tcpip.d.ts.map +1 -1
- package/esm/commands/tcpip.js +14 -19
- package/esm/commands/tcpip.js.map +1 -1
- package/esm/crypto.d.ts +37 -0
- package/esm/crypto.d.ts.map +1 -0
- package/esm/crypto.js +77 -62
- package/esm/crypto.js.map +1 -1
- package/esm/features.d.ts +8 -0
- package/esm/features.d.ts.map +1 -0
- package/esm/features.js +7 -3
- package/esm/features.js.map +1 -1
- package/esm/index.d.ts +11 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +10 -10
- package/esm/index.js.map +1 -1
- package/esm/packet.d.ts +47 -0
- package/esm/packet.d.ts.map +1 -0
- package/esm/packet.js +27 -52
- package/esm/packet.js.map +1 -1
- package/esm/socket/controller.d.ts +37 -0
- package/esm/socket/controller.d.ts.map +1 -0
- package/esm/socket/controller.js +52 -66
- package/esm/socket/controller.js.map +1 -1
- package/esm/socket/dispatcher.d.ts +44 -0
- package/esm/socket/dispatcher.d.ts.map +1 -0
- package/esm/socket/dispatcher.js +146 -165
- package/esm/socket/dispatcher.js.map +1 -1
- package/esm/socket/index.d.ts +3 -0
- package/esm/socket/index.d.ts.map +1 -0
- package/esm/socket/index.js +2 -6
- package/esm/socket/index.js.map +1 -1
- package/esm/socket/socket.d.ts +46 -0
- package/esm/socket/socket.d.ts.map +1 -0
- package/esm/socket/socket.js +77 -13
- package/esm/socket/socket.js.map +1 -1
- package/esm/stream/buffered.d.ts +35 -0
- package/esm/stream/buffered.d.ts.map +1 -0
- package/esm/stream/buffered.js +123 -0
- package/esm/stream/buffered.js.map +1 -0
- package/esm/stream/detect.bak.d.ts +240 -0
- package/esm/stream/detect.bak.d.ts.map +1 -0
- package/esm/stream/detect.bak.js +60 -0
- package/esm/stream/detect.bak.js.map +1 -0
- package/esm/stream/detect.d.ts +2 -0
- package/esm/stream/detect.d.ts.map +1 -0
- package/esm/stream/detect.js +5 -0
- package/esm/stream/detect.js.map +1 -0
- package/esm/stream/detect.native.d.ts +240 -0
- package/esm/stream/detect.native.d.ts.map +1 -0
- package/esm/stream/detect.native.js +60 -0
- package/esm/stream/detect.native.js.map +1 -0
- package/esm/stream/detect.polyfill.d.ts +18 -0
- package/esm/stream/detect.polyfill.d.ts.map +1 -0
- package/esm/stream/detect.polyfill.js +9 -0
- package/esm/stream/detect.polyfill.js.map +1 -0
- package/esm/stream/index.d.ts +4 -0
- package/esm/stream/index.d.ts.map +1 -0
- package/esm/stream/index.js +3 -2
- package/esm/stream/index.js.map +1 -1
- package/esm/stream/transform.d.ts +98 -0
- package/esm/stream/transform.d.ts.map +1 -0
- package/esm/stream/transform.js +402 -0
- package/esm/stream/transform.js.map +1 -0
- package/{dts → esm}/utils/auto-reset-event.d.ts +1 -1
- package/esm/utils/auto-reset-event.d.ts.map +1 -0
- package/esm/utils/auto-reset-event.js +2 -1
- package/esm/utils/auto-reset-event.js.map +1 -1
- package/esm/utils/base64.d.ts +5 -0
- package/esm/utils/base64.d.ts.map +1 -0
- package/esm/utils/base64.js +116 -80
- package/esm/utils/base64.js.map +1 -1
- package/esm/utils/encoding.d.ts +3 -0
- package/esm/utils/encoding.d.ts.map +1 -0
- package/esm/utils/encoding.js +1 -1
- package/esm/utils/encoding.js.map +1 -1
- package/esm/utils/index.d.ts +4 -0
- package/esm/utils/index.d.ts.map +1 -0
- package/esm/utils/index.js +3 -5
- package/esm/utils/index.js.map +1 -1
- package/package.json +50 -44
- package/src/adb.ts +177 -150
- package/src/auth.ts +37 -30
- package/src/backend.ts +3 -12
- package/src/commands/base.ts +1 -1
- package/src/commands/framebuffer.ts +4 -4
- package/src/commands/index.ts +8 -8
- package/src/commands/install.ts +24 -15
- package/src/commands/power.ts +54 -0
- package/src/commands/reverse.ts +10 -8
- package/src/commands/subprocess/index.ts +139 -0
- package/src/commands/subprocess/protocols/index.ts +3 -0
- package/src/commands/subprocess/protocols/none.ts +68 -0
- package/src/commands/subprocess/protocols/shell.ts +190 -0
- package/src/commands/subprocess/protocols/types.ts +59 -0
- package/src/commands/{shell → subprocess}/utils.ts +0 -0
- package/src/commands/sync/index.ts +7 -7
- package/src/commands/sync/list.ts +56 -9
- package/src/commands/sync/pull.ts +27 -18
- package/src/commands/sync/push.ts +25 -30
- package/src/commands/sync/request.ts +8 -7
- package/src/commands/sync/response.ts +12 -6
- package/src/commands/sync/stat.ts +43 -14
- package/src/commands/sync/sync.ts +102 -34
- package/src/commands/tcpip.ts +3 -3
- package/src/crypto.ts +95 -84
- package/src/features.ts +6 -2
- package/src/index.ts +11 -10
- package/src/packet.ts +49 -53
- package/src/socket/dispatcher.ts +110 -109
- package/src/socket/index.ts +2 -6
- package/src/socket/socket.ts +125 -17
- package/src/stream/buffered.ts +147 -0
- package/src/stream/detect.native.ts +362 -0
- package/src/stream/detect.polyfill.ts +28 -0
- package/src/stream/detect.ts +5 -0
- package/src/stream/index.ts +3 -2
- package/src/stream/transform.ts +483 -0
- package/src/utils/auto-reset-event.ts +1 -1
- package/src/utils/base64.ts +161 -129
- package/src/utils/index.ts +3 -5
- package/tsconfig.build.json +3 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.test.json +10 -0
- package/dts/adb.d.ts +0 -46
- package/dts/adb.d.ts.map +0 -1
- package/dts/auth.d.ts.map +0 -1
- package/dts/backend.d.ts +0 -13
- package/dts/backend.d.ts.map +0 -1
- package/dts/commands/base.d.ts.map +0 -1
- package/dts/commands/demo-mode.d.ts +0 -41
- package/dts/commands/demo-mode.d.ts.map +0 -1
- package/dts/commands/framebuffer.d.ts.map +0 -1
- package/dts/commands/index.d.ts +0 -9
- package/dts/commands/index.d.ts.map +0 -1
- package/dts/commands/install.d.ts +0 -3
- package/dts/commands/install.d.ts.map +0 -1
- package/dts/commands/reverse.d.ts.map +0 -1
- package/dts/commands/shell/index.d.ts +0 -46
- package/dts/commands/shell/index.d.ts.map +0 -1
- package/dts/commands/shell/legacy.d.ts +0 -38
- package/dts/commands/shell/legacy.d.ts.map +0 -1
- package/dts/commands/shell/protocol.d.ts +0 -45
- package/dts/commands/shell/protocol.d.ts.map +0 -1
- package/dts/commands/shell/types.d.ts +0 -47
- package/dts/commands/shell/types.d.ts.map +0 -1
- package/dts/commands/shell/utils.d.ts.map +0 -1
- package/dts/commands/sync/index.d.ts +0 -8
- package/dts/commands/sync/index.d.ts.map +0 -1
- package/dts/commands/sync/list.d.ts +0 -17
- package/dts/commands/sync/list.d.ts.map +0 -1
- package/dts/commands/sync/pull.d.ts +0 -11
- package/dts/commands/sync/pull.d.ts.map +0 -1
- package/dts/commands/sync/push.d.ts +0 -8
- package/dts/commands/sync/push.d.ts.map +0 -1
- package/dts/commands/sync/request.d.ts.map +0 -1
- package/dts/commands/sync/response.d.ts.map +0 -1
- package/dts/commands/sync/stat.d.ts.map +0 -1
- package/dts/commands/sync/sync.d.ts +0 -68
- package/dts/commands/sync/sync.d.ts.map +0 -1
- package/dts/crypto.d.ts +0 -24
- package/dts/crypto.d.ts.map +0 -1
- package/dts/features.d.ts +0 -6
- package/dts/features.d.ts.map +0 -1
- package/dts/index.d.ts +0 -11
- package/dts/index.d.ts.map +0 -1
- package/dts/packet.d.ts +0 -27
- package/dts/packet.d.ts.map +0 -1
- package/dts/socket/close-event-emitter.d.ts +0 -8
- package/dts/socket/close-event-emitter.d.ts.map +0 -1
- package/dts/socket/controller.d.ts +0 -43
- package/dts/socket/controller.d.ts.map +0 -1
- package/dts/socket/data-event-emitter.d.ts +0 -10
- package/dts/socket/data-event-emitter.d.ts.map +0 -1
- package/dts/socket/dispatcher.d.ts +0 -53
- package/dts/socket/dispatcher.d.ts.map +0 -1
- package/dts/socket/index.d.ts +0 -7
- package/dts/socket/index.d.ts.map +0 -1
- package/dts/socket/logger.d.ts +0 -9
- package/dts/socket/logger.d.ts.map +0 -1
- package/dts/socket/socket.d.ts +0 -22
- package/dts/socket/socket.d.ts.map +0 -1
- package/dts/stream/buffered-stream.d.ts +0 -28
- package/dts/stream/buffered-stream.d.ts.map +0 -1
- package/dts/stream/index.d.ts +0 -3
- package/dts/stream/index.d.ts.map +0 -1
- package/dts/stream/stream.d.ts +0 -15
- package/dts/stream/stream.d.ts.map +0 -1
- package/dts/utils/auto-reset-event.d.ts.map +0 -1
- package/dts/utils/base64.d.ts +0 -7
- package/dts/utils/base64.d.ts.map +0 -1
- package/dts/utils/chunk.d.ts +0 -3
- package/dts/utils/chunk.d.ts.map +0 -1
- package/dts/utils/encoding.d.ts +0 -3
- package/dts/utils/encoding.d.ts.map +0 -1
- package/dts/utils/event-queue.d.ts +0 -35
- package/dts/utils/event-queue.d.ts.map +0 -1
- package/dts/utils/index.d.ts +0 -6
- package/dts/utils/index.d.ts.map +0 -1
- package/esm/commands/demo-mode.js +0 -170
- package/esm/commands/demo-mode.js.map +0 -1
- package/esm/commands/shell/index.js +0 -62
- package/esm/commands/shell/index.js.map +0 -1
- package/esm/commands/shell/legacy.js +0 -49
- package/esm/commands/shell/legacy.js.map +0 -1
- package/esm/commands/shell/protocol.js +0 -106
- package/esm/commands/shell/protocol.js.map +0 -1
- package/esm/commands/shell/utils.js.map +0 -1
- package/esm/socket/close-event-emitter.js +0 -28
- package/esm/socket/close-event-emitter.js.map +0 -1
- package/esm/socket/data-event-emitter.js +0 -25
- package/esm/socket/data-event-emitter.js.map +0 -1
- package/esm/socket/logger.js +0 -2
- package/esm/socket/logger.js.map +0 -1
- package/esm/stream/buffered-stream.js +0 -67
- package/esm/stream/buffered-stream.js.map +0 -1
- package/esm/stream/stream.js +0 -41
- package/esm/stream/stream.js.map +0 -1
- package/esm/utils/chunk.js +0 -70
- package/esm/utils/chunk.js.map +0 -1
- package/esm/utils/event-queue.js +0 -104
- package/esm/utils/event-queue.js.map +0 -1
- package/src/commands/demo-mode.ts +0 -160
- package/src/commands/shell/index.ts +0 -87
- package/src/commands/shell/legacy.ts +0 -59
- package/src/commands/shell/protocol.ts +0 -121
- package/src/commands/shell/types.ts +0 -55
- package/src/socket/close-event-emitter.ts +0 -28
- package/src/socket/controller.ts +0 -105
- package/src/socket/data-event-emitter.ts +0 -27
- package/src/socket/logger.ts +0 -12
- package/src/stream/buffered-stream.ts +0 -92
- package/src/stream/stream.ts +0 -50
- package/src/utils/chunk.ts +0 -70
- package/src/utils/encoding.ts +0 -13
- package/src/utils/event-queue.ts +0 -145
package/src/crypto.ts
CHANGED
|
@@ -1,51 +1,64 @@
|
|
|
1
|
+
import { getBigUint64, setBigUint64 } from '@yume-chan/dataview-bigint-polyfill/esm/fallback.js';
|
|
2
|
+
|
|
1
3
|
const BigInt0 = BigInt(0);
|
|
2
4
|
const BigInt1 = BigInt(1);
|
|
3
5
|
const BigInt2 = BigInt(2);
|
|
6
|
+
const BigInt64 = BigInt(64);
|
|
4
7
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Gets the `BigInt` value at the specified byte offset and length from the start of the view. There is
|
|
10
|
+
* no alignment constraint; multi-byte values may be fetched from any offset.
|
|
11
|
+
*
|
|
12
|
+
* Only supports Big-Endian, because that's what ADB uses.
|
|
13
|
+
* @param byteOffset The place in the buffer at which the value should be retrieved.
|
|
14
|
+
*/
|
|
15
|
+
export function getBigUint(dataView: DataView, byteOffset: number, length: number): bigint {
|
|
14
16
|
let result = BigInt0;
|
|
15
17
|
|
|
16
|
-
//
|
|
18
|
+
// Currently `length` must be a multiplication of 8
|
|
17
19
|
// Support for arbitrary length can be easily added
|
|
18
20
|
|
|
19
|
-
for (let i =
|
|
20
|
-
result
|
|
21
|
-
const value =
|
|
21
|
+
for (let i = byteOffset; i < byteOffset + length; i += 8) {
|
|
22
|
+
result <<= BigInt64;
|
|
23
|
+
const value = getBigUint64(dataView, i, false);
|
|
22
24
|
result += value;
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
return result;
|
|
26
28
|
}
|
|
27
29
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Stores an arbitrary-precision positive `BigInt` value at the specified byte offset from the start of the view.
|
|
32
|
+
* @param byteOffset The place in the buffer at which the value should be set.
|
|
33
|
+
* @param value The value to set.
|
|
34
|
+
* @param littleEndian If `false` or `undefined`, a big-endian value should be written,
|
|
35
|
+
* otherwise a little-endian value should be written.
|
|
36
|
+
*/
|
|
37
|
+
export function setBigUint(dataView: DataView, byteOffset: number, value: bigint, littleEndian?: boolean) {
|
|
38
|
+
const start = byteOffset;
|
|
39
|
+
|
|
40
|
+
if (littleEndian) {
|
|
41
|
+
while (value > BigInt0) {
|
|
42
|
+
setBigUint64(dataView, byteOffset, value, true);
|
|
43
|
+
byteOffset += 8;
|
|
44
|
+
value >>= BigInt64;
|
|
45
|
+
}
|
|
46
|
+
} else {
|
|
47
|
+
// Because we don't know how long (in bits) the `value` is,
|
|
48
|
+
// Convert it to an array of `uint64` first.
|
|
49
|
+
const uint64Array: bigint[] = [];
|
|
50
|
+
while (value > BigInt0) {
|
|
51
|
+
uint64Array.push(BigInt.asUintN(64, value));
|
|
52
|
+
value >>= BigInt64;
|
|
53
|
+
}
|
|
34
54
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
55
|
+
for (let i = uint64Array.length - 1; i >= 0; i -= 1) {
|
|
56
|
+
setBigUint64(dataView, byteOffset, uint64Array[i]!, false);
|
|
57
|
+
byteOffset += 8;
|
|
58
|
+
}
|
|
39
59
|
}
|
|
40
|
-
}
|
|
41
60
|
|
|
42
|
-
|
|
43
|
-
const view = new DataView(buffer);
|
|
44
|
-
while (value > BigInt0) {
|
|
45
|
-
view.setBigUint64(offset, value, true);
|
|
46
|
-
offset += 8;
|
|
47
|
-
value /= BigInt2To64;
|
|
48
|
-
}
|
|
61
|
+
return byteOffset - start;
|
|
49
62
|
}
|
|
50
63
|
|
|
51
64
|
// These values are correct only if
|
|
@@ -74,10 +87,10 @@ const RsaPrivateKeyNLength = 2048 / 8;
|
|
|
74
87
|
const RsaPrivateKeyDOffset = 303;
|
|
75
88
|
const RsaPrivateKeyDLength = 2048 / 8;
|
|
76
89
|
|
|
77
|
-
export function parsePrivateKey(key:
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
90
|
+
export function parsePrivateKey(key: Uint8Array): [n: bigint, d: bigint] {
|
|
91
|
+
const view = new DataView(key.buffer, key.byteOffset, key.byteLength);
|
|
92
|
+
const n = getBigUint(view, RsaPrivateKeyNOffset, RsaPrivateKeyNLength);
|
|
93
|
+
const d = getBigUint(view, RsaPrivateKeyDOffset, RsaPrivateKeyDLength);
|
|
81
94
|
return [n, d];
|
|
82
95
|
}
|
|
83
96
|
|
|
@@ -85,11 +98,6 @@ export function parsePrivateKey(key: ArrayBuffer): [n: bigint, d: bigint] {
|
|
|
85
98
|
// I can't understand, but it does work
|
|
86
99
|
// Only used with numbers less than 2^32 so doesn't need BigInt
|
|
87
100
|
export function modInverse(a: number, m: number) {
|
|
88
|
-
// validate inputs
|
|
89
|
-
[a, m] = [Number(a), Number(m)];
|
|
90
|
-
if (Number.isNaN(a) || Number.isNaN(m)) {
|
|
91
|
-
return NaN; // invalid input
|
|
92
|
-
}
|
|
93
101
|
a = (a % m + m) % m;
|
|
94
102
|
if (!a || m < 2) {
|
|
95
103
|
return NaN; // invalid input
|
|
@@ -108,7 +116,7 @@ export function modInverse(a: number, m: number) {
|
|
|
108
116
|
let x = 1;
|
|
109
117
|
let y = 0;
|
|
110
118
|
for (let i = s.length - 2; i >= 0; --i) {
|
|
111
|
-
[x, y] = [y, x - y * Math.floor(s[i]
|
|
119
|
+
[x, y] = [y, x - y * Math.floor(s[i]!.a / s[i]!.b)];
|
|
112
120
|
}
|
|
113
121
|
return (y % m + m) % m;
|
|
114
122
|
}
|
|
@@ -118,18 +126,16 @@ export function calculatePublicKeyLength() {
|
|
|
118
126
|
}
|
|
119
127
|
|
|
120
128
|
export function calculatePublicKey(
|
|
121
|
-
privateKey:
|
|
122
|
-
):
|
|
129
|
+
privateKey: Uint8Array
|
|
130
|
+
): Uint8Array;
|
|
123
131
|
export function calculatePublicKey(
|
|
124
|
-
privateKey:
|
|
125
|
-
output:
|
|
126
|
-
outputOffset?: number
|
|
132
|
+
privateKey: Uint8Array,
|
|
133
|
+
output: Uint8Array,
|
|
127
134
|
): number;
|
|
128
135
|
export function calculatePublicKey(
|
|
129
|
-
privateKey:
|
|
130
|
-
output?:
|
|
131
|
-
|
|
132
|
-
): ArrayBuffer | number {
|
|
136
|
+
privateKey: Uint8Array,
|
|
137
|
+
output?: Uint8Array,
|
|
138
|
+
): Uint8Array | number {
|
|
133
139
|
// Android has its own public key generation algorithm
|
|
134
140
|
// See https://android.googlesource.com/platform/system/core.git/+/91784040db2b9273687f88d8b95f729d4a61ecc2/libcrypto_utils/android_pubkey.cpp#111
|
|
135
141
|
|
|
@@ -138,7 +144,7 @@ export function calculatePublicKey(
|
|
|
138
144
|
// [
|
|
139
145
|
// modulusLengthInWords, // 32-bit integer, a "word" is 32-bit so it must be 2048 / 8 / 4
|
|
140
146
|
// // Actually the comment in Android source code was wrong
|
|
141
|
-
// n0inv, // 32-bit integer, the modular inverse of (
|
|
147
|
+
// n0inv, // 32-bit integer, the modular inverse of (low 32 bits of n)
|
|
142
148
|
// modulus, // n
|
|
143
149
|
// rr, // Montgomery parameter R^2
|
|
144
150
|
// exponent, // 32-bit integer, must be 65537
|
|
@@ -150,20 +156,21 @@ export function calculatePublicKey(
|
|
|
150
156
|
// extract `n` from private key
|
|
151
157
|
const [n] = parsePrivateKey(privateKey);
|
|
152
158
|
|
|
153
|
-
let outputType: '
|
|
159
|
+
let outputType: 'Uint8Array' | 'number';
|
|
154
160
|
const outputLength = calculatePublicKeyLength();
|
|
155
161
|
if (!output) {
|
|
156
|
-
output = new
|
|
157
|
-
outputType = '
|
|
162
|
+
output = new Uint8Array(outputLength);
|
|
163
|
+
outputType = 'Uint8Array';
|
|
158
164
|
} else {
|
|
159
|
-
if (output.byteLength
|
|
165
|
+
if (output.byteLength < outputLength) {
|
|
160
166
|
throw new Error('output buffer is too small');
|
|
161
167
|
}
|
|
162
168
|
|
|
163
169
|
outputType = 'number';
|
|
164
170
|
}
|
|
165
171
|
|
|
166
|
-
const outputView = new DataView(output);
|
|
172
|
+
const outputView = new DataView(output.buffer, output.byteOffset, output.byteLength);
|
|
173
|
+
let outputOffset = 0;
|
|
167
174
|
|
|
168
175
|
// modulusLengthInWords
|
|
169
176
|
outputView.setUint32(outputOffset, 2048 / 8 / 4, true);
|
|
@@ -177,19 +184,18 @@ export function calculatePublicKey(
|
|
|
177
184
|
outputOffset += 4;
|
|
178
185
|
|
|
179
186
|
// Write n
|
|
180
|
-
|
|
187
|
+
setBigUint(outputView, outputOffset, n, true);
|
|
181
188
|
outputOffset += 256;
|
|
182
189
|
|
|
183
190
|
// Calculate rr = (2^(rsa_size)) ^ 2 mod n
|
|
184
191
|
let rr = BigInt(2) ** BigInt(4096) % n;
|
|
185
|
-
|
|
186
|
-
outputOffset += 256;
|
|
192
|
+
outputOffset += setBigUint(outputView, outputOffset, rr, true);
|
|
187
193
|
|
|
188
194
|
// exponent
|
|
189
195
|
outputView.setUint32(outputOffset, 65537, true);
|
|
190
196
|
outputOffset += 4;
|
|
191
197
|
|
|
192
|
-
if (outputType === '
|
|
198
|
+
if (outputType === 'Uint8Array') {
|
|
193
199
|
return output;
|
|
194
200
|
} else {
|
|
195
201
|
return outputLength;
|
|
@@ -223,22 +229,22 @@ export function powMod(base: bigint, exponent: bigint, modulus: bigint): bigint
|
|
|
223
229
|
return r;
|
|
224
230
|
}
|
|
225
231
|
|
|
226
|
-
export const
|
|
232
|
+
export const SHA1_DIGEST_LENGTH = 20;
|
|
227
233
|
|
|
228
|
-
export const
|
|
229
|
-
export const
|
|
230
|
-
export const
|
|
231
|
-
export const
|
|
234
|
+
export const ASN1_SEQUENCE = 0x30;
|
|
235
|
+
export const ASN1_OCTET_STRING = 0x04;
|
|
236
|
+
export const ASN1_NULL = 0x05;
|
|
237
|
+
export const ASN1_OID = 0x06;
|
|
232
238
|
|
|
233
239
|
// PKCS#1 SHA-1 hash digest info
|
|
234
|
-
export const
|
|
235
|
-
|
|
236
|
-
|
|
240
|
+
export const SHA1_DIGEST_INFO = new Uint8Array([
|
|
241
|
+
ASN1_SEQUENCE, 0x0d + SHA1_DIGEST_LENGTH,
|
|
242
|
+
ASN1_SEQUENCE, 0x09,
|
|
237
243
|
// SHA-1 (1 3 14 3 2 26)
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
];
|
|
244
|
+
ASN1_OID, 0x05, 1 * 40 + 3, 14, 3, 2, 26,
|
|
245
|
+
ASN1_NULL, 0x00,
|
|
246
|
+
ASN1_OCTET_STRING, SHA1_DIGEST_LENGTH
|
|
247
|
+
]);
|
|
242
248
|
|
|
243
249
|
// SubtleCrypto.sign() will hash the given data and sign the hash
|
|
244
250
|
// But we don't need the hashing step
|
|
@@ -246,7 +252,7 @@ export const Sha1DigestInfo = [
|
|
|
246
252
|
// encrypt the given data with its private key)
|
|
247
253
|
// However SubtileCrypto.encrypt() doesn't accept 'RSASSA-PKCS1-v1_5' algorithm
|
|
248
254
|
// So we need to implement the encryption by ourself
|
|
249
|
-
export function sign(privateKey:
|
|
255
|
+
export function sign(privateKey: Uint8Array, data: Uint8Array): Uint8Array {
|
|
250
256
|
const [n, d] = parsePrivateKey(privateKey);
|
|
251
257
|
|
|
252
258
|
// PKCS#1 padding
|
|
@@ -259,7 +265,7 @@ export function sign(privateKey: ArrayBuffer, data: ArrayBuffer): ArrayBuffer {
|
|
|
259
265
|
padded[index] = 1;
|
|
260
266
|
index += 1;
|
|
261
267
|
|
|
262
|
-
const fillLength = padded.length -
|
|
268
|
+
const fillLength = padded.length - SHA1_DIGEST_INFO.length - data.length - 1;
|
|
263
269
|
while (index < fillLength) {
|
|
264
270
|
padded[index] = 0xff;
|
|
265
271
|
index += 1;
|
|
@@ -268,18 +274,23 @@ export function sign(privateKey: ArrayBuffer, data: ArrayBuffer): ArrayBuffer {
|
|
|
268
274
|
padded[index] = 0;
|
|
269
275
|
index += 1;
|
|
270
276
|
|
|
271
|
-
padded.set(
|
|
272
|
-
index +=
|
|
277
|
+
padded.set(SHA1_DIGEST_INFO, index);
|
|
278
|
+
index += SHA1_DIGEST_INFO.length;
|
|
273
279
|
|
|
274
|
-
padded.set(
|
|
280
|
+
padded.set(data, index);
|
|
275
281
|
|
|
276
282
|
// Encryption
|
|
277
283
|
// signature = padded ** d % n
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
284
|
+
const view = new DataView(padded.buffer);
|
|
285
|
+
const signature = powMod(
|
|
286
|
+
getBigUint(view, 0, view.byteLength),
|
|
287
|
+
d,
|
|
288
|
+
n
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
// `padded` is not used anymore,
|
|
292
|
+
// re-use the buffer to store the result
|
|
293
|
+
setBigUint(view, 0, signature, false);
|
|
294
|
+
|
|
295
|
+
return padded;
|
|
285
296
|
}
|
package/src/features.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
// The order follows
|
|
2
|
+
// https://android.googlesource.com/platform/packages/modules/adb/+/79010dc6d5ca7490c493df800d4421730f5466ca/transport.cpp#1252
|
|
1
3
|
export enum AdbFeatures {
|
|
2
|
-
|
|
4
|
+
ShellV2 = "shell_v2",
|
|
3
5
|
Cmd = 'cmd',
|
|
4
|
-
|
|
6
|
+
StatV2 = 'stat_v2',
|
|
7
|
+
ListV2 = 'ls_v2',
|
|
8
|
+
FixedPushMkdir = 'fixed_push_mkdir',
|
|
5
9
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './
|
|
4
|
-
export * from './
|
|
5
|
-
export * from './
|
|
6
|
-
export * from './
|
|
7
|
-
export * from './
|
|
8
|
-
export * from './
|
|
9
|
-
export * from './
|
|
10
|
-
export * from './
|
|
1
|
+
|
|
2
|
+
export * from './adb.js';
|
|
3
|
+
export * from './auth.js';
|
|
4
|
+
export * from './backend.js';
|
|
5
|
+
export * from './commands/index.js';
|
|
6
|
+
export * from './crypto.js';
|
|
7
|
+
export * from './features.js';
|
|
8
|
+
export * from './packet.js';
|
|
9
|
+
export * from './socket/index.js';
|
|
10
|
+
export * from './stream/index.js';
|
|
11
|
+
export * from './utils/index.js';
|
package/src/packet.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import Struct from '@yume-chan/struct';
|
|
2
|
-
import {
|
|
3
|
-
import { BufferedStream } from './stream';
|
|
2
|
+
import { TransformStream } from "./stream/index.js";
|
|
4
3
|
|
|
5
4
|
export enum AdbCommand {
|
|
6
5
|
Auth = 0x48545541, // 'AUTH'
|
|
@@ -11,7 +10,7 @@ export enum AdbCommand {
|
|
|
11
10
|
Write = 0x45545257, // 'WRTE'
|
|
12
11
|
}
|
|
13
12
|
|
|
14
|
-
const AdbPacketHeader =
|
|
13
|
+
export const AdbPacketHeader =
|
|
15
14
|
new Struct({ littleEndian: true })
|
|
16
15
|
.uint32('command')
|
|
17
16
|
.uint32('arg0')
|
|
@@ -20,65 +19,62 @@ const AdbPacketHeader =
|
|
|
20
19
|
.uint32('checksum')
|
|
21
20
|
.int32('magic');
|
|
22
21
|
|
|
23
|
-
|
|
22
|
+
export type AdbPacketHeader = typeof AdbPacketHeader['TDeserializeResult'];
|
|
23
|
+
|
|
24
|
+
type AdbPacketHeaderInit = typeof AdbPacketHeader['TInit'];
|
|
25
|
+
|
|
26
|
+
export const AdbPacket =
|
|
24
27
|
new Struct({ littleEndian: true })
|
|
25
28
|
.fields(AdbPacketHeader)
|
|
26
|
-
.
|
|
29
|
+
.uint8Array('payload', { lengthField: 'payloadLength' });
|
|
27
30
|
|
|
28
|
-
export type AdbPacket = typeof
|
|
31
|
+
export type AdbPacket = typeof AdbPacket['TDeserializeResult'];
|
|
29
32
|
|
|
30
|
-
|
|
33
|
+
/**
|
|
34
|
+
* `AdbPacketData` contains all the useful fields of `AdbPacket`.
|
|
35
|
+
*
|
|
36
|
+
* `AdbBackend#connect` will return a `ReadableStream<AdbPacketData>`,
|
|
37
|
+
* so each backend can encode `AdbPacket` in different ways.
|
|
38
|
+
*
|
|
39
|
+
* `AdbBackend#connect` will return a `WritableStream<AdbPacketInit>`,
|
|
40
|
+
* however, `AdbPacketDispatcher` will transform `AdbPacketData` to `AdbPacketInit` for you,
|
|
41
|
+
* so `AdbSocket#writable#write` only needs `AdbPacketData`.
|
|
42
|
+
*/
|
|
43
|
+
export type AdbPacketData = Omit<typeof AdbPacket['TInit'], 'checksum' | 'magic'>;
|
|
31
44
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
// Detect boundary
|
|
35
|
-
// Note that it relies on the backend to only return data from one write operation
|
|
36
|
-
let buffer: ArrayBuffer;
|
|
37
|
-
do {
|
|
38
|
-
// Maybe it's a payload from last connection.
|
|
39
|
-
// Ignore and try again
|
|
40
|
-
buffer = await backend.read(24);
|
|
41
|
-
} while (buffer.byteLength !== 24);
|
|
42
|
-
|
|
43
|
-
let bufferUsed = false;
|
|
44
|
-
const stream = new BufferedStream({
|
|
45
|
-
async read(length: number) {
|
|
46
|
-
if (!bufferUsed) {
|
|
47
|
-
bufferUsed = true;
|
|
48
|
-
return buffer;
|
|
49
|
-
}
|
|
50
|
-
return backend.read(length);
|
|
51
|
-
}
|
|
52
|
-
});
|
|
45
|
+
// All fields except `magic`, which can be calculated in `AdbPacketSerializeStream`
|
|
46
|
+
export type AdbPacketInit = Omit<typeof AdbPacket['TInit'], 'magic'>;
|
|
53
47
|
|
|
54
|
-
|
|
48
|
+
export function calculateChecksum(payload: Uint8Array): number;
|
|
49
|
+
export function calculateChecksum(init: AdbPacketData): AdbPacketInit;
|
|
50
|
+
export function calculateChecksum(payload: Uint8Array | AdbPacketData): number | AdbPacketInit {
|
|
51
|
+
if (payload instanceof Uint8Array) {
|
|
52
|
+
return payload.reduce((result, item) => result + item, 0);
|
|
53
|
+
} else {
|
|
54
|
+
(payload as AdbPacketInit).checksum = calculateChecksum(payload.payload);
|
|
55
|
+
return payload as AdbPacketInit;
|
|
55
56
|
}
|
|
57
|
+
}
|
|
56
58
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const array = new Uint8Array(init.payload);
|
|
65
|
-
checksum = array.reduce((result, item) => result + item, 0);
|
|
66
|
-
} else {
|
|
67
|
-
checksum = 0;
|
|
68
|
-
}
|
|
59
|
+
export class AdbPacketSerializeStream extends TransformStream<AdbPacketInit, Uint8Array>{
|
|
60
|
+
public constructor() {
|
|
61
|
+
super({
|
|
62
|
+
transform: async (init, controller) => {
|
|
63
|
+
// This syntax is ugly, but I don't want to create a new object.
|
|
64
|
+
(init as unknown as AdbPacketHeaderInit).magic = init.command ^ 0xFFFFFFFF;
|
|
65
|
+
(init as unknown as AdbPacketHeaderInit).payloadLength = init.payload.byteLength;
|
|
69
66
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
};
|
|
67
|
+
controller.enqueue(
|
|
68
|
+
AdbPacketHeader.serialize(
|
|
69
|
+
init as unknown as AdbPacketHeaderInit
|
|
70
|
+
)
|
|
71
|
+
);
|
|
76
72
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
73
|
+
if (init.payload.byteLength) {
|
|
74
|
+
// Enqueue payload separately to avoid copying
|
|
75
|
+
controller.enqueue(init.payload);
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
});
|
|
83
79
|
}
|
|
84
80
|
}
|