@yume-chan/adb 0.0.17 → 0.0.18
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 +6 -0
- package/CHANGELOG.md +6 -1
- package/LICENSE +1 -1
- package/esm/adb.d.ts +6 -6
- package/esm/adb.d.ts.map +1 -1
- package/esm/adb.js +55 -35
- package/esm/adb.js.map +1 -1
- package/esm/auth.d.ts +4 -4
- package/esm/auth.d.ts.map +1 -1
- package/esm/auth.js +6 -6
- package/esm/auth.js.map +1 -1
- package/esm/backend.d.ts +3 -3
- package/esm/backend.d.ts.map +1 -1
- package/esm/commands/base.d.ts +2 -2
- package/esm/commands/base.d.ts.map +1 -1
- package/esm/commands/base.js +1 -1
- package/esm/commands/framebuffer.d.ts +7 -7
- package/esm/commands/framebuffer.d.ts.map +1 -1
- package/esm/commands/framebuffer.js +32 -33
- package/esm/commands/framebuffer.js.map +1 -1
- package/esm/commands/install.d.ts +2 -2
- package/esm/commands/install.d.ts.map +1 -1
- package/esm/commands/install.js +12 -6
- package/esm/commands/install.js.map +1 -1
- package/esm/commands/power.d.ts +1 -1
- package/esm/commands/power.d.ts.map +1 -1
- package/esm/commands/power.js +14 -10
- package/esm/commands/power.js.map +1 -1
- package/esm/commands/reverse.d.ts +3 -3
- package/esm/commands/reverse.d.ts.map +1 -1
- package/esm/commands/reverse.js +14 -12
- package/esm/commands/reverse.js.map +1 -1
- package/esm/commands/subprocess/command.d.ts +2 -2
- package/esm/commands/subprocess/command.d.ts.map +1 -1
- package/esm/commands/subprocess/command.js +14 -16
- package/esm/commands/subprocess/command.js.map +1 -1
- package/esm/commands/subprocess/protocols/none.d.ts +4 -4
- package/esm/commands/subprocess/protocols/none.d.ts.map +1 -1
- package/esm/commands/subprocess/protocols/none.js.map +1 -1
- package/esm/commands/subprocess/protocols/shell.d.ts +4 -4
- package/esm/commands/subprocess/protocols/shell.d.ts.map +1 -1
- package/esm/commands/subprocess/protocols/shell.js +23 -15
- package/esm/commands/subprocess/protocols/shell.js.map +1 -1
- package/esm/commands/subprocess/protocols/types.d.ts +4 -4
- package/esm/commands/subprocess/protocols/types.d.ts.map +1 -1
- package/esm/commands/sync/list.d.ts +6 -6
- package/esm/commands/sync/list.d.ts.map +1 -1
- package/esm/commands/sync/list.js +23 -11
- package/esm/commands/sync/list.js.map +1 -1
- package/esm/commands/sync/pull.d.ts +4 -4
- package/esm/commands/sync/pull.d.ts.map +1 -1
- package/esm/commands/sync/pull.js +12 -11
- package/esm/commands/sync/pull.js.map +1 -1
- package/esm/commands/sync/push.d.ts +3 -3
- package/esm/commands/sync/push.d.ts.map +1 -1
- package/esm/commands/sync/push.js +6 -7
- package/esm/commands/sync/push.js.map +1 -1
- package/esm/commands/sync/request.d.ts +4 -4
- package/esm/commands/sync/request.d.ts.map +1 -1
- package/esm/commands/sync/request.js +7 -7
- package/esm/commands/sync/request.js.map +1 -1
- package/esm/commands/sync/response.d.ts +4 -4
- package/esm/commands/sync/response.d.ts.map +1 -1
- package/esm/commands/sync/response.js +7 -7
- package/esm/commands/sync/response.js.map +1 -1
- package/esm/commands/sync/stat.d.ts +5 -5
- package/esm/commands/sync/stat.d.ts.map +1 -1
- package/esm/commands/sync/stat.js +37 -27
- package/esm/commands/sync/stat.js.map +1 -1
- package/esm/commands/sync/sync.d.ts +6 -6
- package/esm/commands/sync/sync.d.ts.map +1 -1
- package/esm/commands/sync/sync.js +20 -19
- package/esm/commands/sync/sync.js.map +1 -1
- package/esm/crypto.d.ts.map +1 -1
- package/esm/crypto.js +25 -15
- package/esm/crypto.js.map +1 -1
- package/esm/index.d.ts +9 -9
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +9 -9
- package/esm/index.js.map +1 -1
- package/esm/packet.d.ts +8 -8
- package/esm/packet.d.ts.map +1 -1
- package/esm/packet.js +14 -12
- package/esm/packet.js.map +1 -1
- package/esm/socket/dispatcher.d.ts +6 -6
- package/esm/socket/dispatcher.d.ts.map +1 -1
- package/esm/socket/dispatcher.js +22 -19
- package/esm/socket/dispatcher.js.map +1 -1
- package/esm/socket/socket.d.ts +4 -4
- package/esm/socket/socket.d.ts.map +1 -1
- package/esm/socket/socket.js +37 -17
- package/esm/socket/socket.js.map +1 -1
- package/esm/utils/auto-reset-event.d.ts +1 -1
- package/esm/utils/auto-reset-event.d.ts.map +1 -1
- package/esm/utils/auto-reset-event.js +2 -2
- package/esm/utils/base64.d.ts.map +1 -1
- package/esm/utils/base64.js +30 -29
- package/esm/utils/base64.js.map +1 -1
- package/package.json +16 -13
- package/src/adb.ts +133 -84
- package/src/auth.ts +27 -19
- package/src/backend.ts +6 -4
- package/src/commands/base.ts +2 -2
- package/src/commands/framebuffer.ts +37 -41
- package/src/commands/install.ts +19 -11
- package/src/commands/power.ts +15 -11
- package/src/commands/reverse.ts +48 -29
- package/src/commands/subprocess/command.ts +24 -21
- package/src/commands/subprocess/protocols/none.ts +4 -3
- package/src/commands/subprocess/protocols/shell.ts +65 -33
- package/src/commands/subprocess/protocols/types.ts +8 -5
- package/src/commands/sync/list.ts +51 -24
- package/src/commands/sync/pull.ts +51 -34
- package/src/commands/sync/push.ts +25 -11
- package/src/commands/sync/request.ts +20 -22
- package/src/commands/sync/response.ts +31 -25
- package/src/commands/sync/stat.ts +78 -55
- package/src/commands/sync/sync.ts +47 -26
- package/src/crypto.ts +60 -32
- package/src/index.ts +9 -10
- package/src/packet.ts +40 -30
- package/src/socket/dispatcher.ts +115 -78
- package/src/socket/socket.ts +68 -27
- package/src/utils/auto-reset-event.ts +3 -3
- package/src/utils/base64.ts +51 -41
- package/tsconfig.build.json +1 -1
- package/tsconfig.build.tsbuildinfo +1 -1
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
type BufferedReadableStream,
|
|
3
|
+
type WritableStreamDefaultWriter,
|
|
4
|
+
} from "@yume-chan/stream-extra";
|
|
5
|
+
import Struct, { placeholder } from "@yume-chan/struct";
|
|
3
6
|
|
|
4
|
-
import { AdbSyncRequestId, adbSyncWriteRequest } from
|
|
5
|
-
import {
|
|
7
|
+
import { AdbSyncRequestId, adbSyncWriteRequest } from "./request.js";
|
|
8
|
+
import { AdbSyncResponseId, adbSyncReadResponse } from "./response.js";
|
|
6
9
|
|
|
7
10
|
// https://github.com/python/cpython/blob/4e581d64b8aff3e2eda99b12f080c877bb78dfca/Lib/stat.py#L36
|
|
8
11
|
export enum LinuxFileType {
|
|
@@ -24,27 +27,27 @@ export interface AdbSyncStat {
|
|
|
24
27
|
ctime?: bigint;
|
|
25
28
|
}
|
|
26
29
|
|
|
27
|
-
export const AdbSyncLstatResponse =
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
)
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
});
|
|
30
|
+
export const AdbSyncLstatResponse = new Struct({ littleEndian: true })
|
|
31
|
+
.int32("mode")
|
|
32
|
+
.int32("size")
|
|
33
|
+
.int32("mtime")
|
|
34
|
+
.extra({
|
|
35
|
+
id: AdbSyncResponseId.Lstat as const,
|
|
36
|
+
get type() {
|
|
37
|
+
return (this.mode >> 12) as LinuxFileType;
|
|
38
|
+
},
|
|
39
|
+
get permission() {
|
|
40
|
+
return this.mode & 0b00001111_11111111;
|
|
41
|
+
},
|
|
42
|
+
})
|
|
43
|
+
.postDeserialize((object) => {
|
|
44
|
+
if (object.mode === 0 && object.size === 0 && object.mtime === 0) {
|
|
45
|
+
throw new Error("lstat error");
|
|
46
|
+
}
|
|
47
|
+
});
|
|
46
48
|
|
|
47
|
-
export type AdbSyncLstatResponse =
|
|
49
|
+
export type AdbSyncLstatResponse =
|
|
50
|
+
typeof AdbSyncLstatResponse["TDeserializeResult"];
|
|
48
51
|
|
|
49
52
|
export enum AdbSyncStatErrorCode {
|
|
50
53
|
SUCCESS = 0,
|
|
@@ -70,51 +73,67 @@ export enum AdbSyncStatErrorCode {
|
|
|
70
73
|
ETXTBSY = 26,
|
|
71
74
|
}
|
|
72
75
|
|
|
73
|
-
export const AdbSyncStatResponse =
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
.
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
76
|
+
export const AdbSyncStatResponse = new Struct({ littleEndian: true })
|
|
77
|
+
.uint32("error", placeholder<AdbSyncStatErrorCode>())
|
|
78
|
+
.uint64("dev")
|
|
79
|
+
.uint64("ino")
|
|
80
|
+
.uint32("mode")
|
|
81
|
+
.uint32("nlink")
|
|
82
|
+
.uint32("uid")
|
|
83
|
+
.uint32("gid")
|
|
84
|
+
.uint64("size")
|
|
85
|
+
.uint64("atime")
|
|
86
|
+
.uint64("mtime")
|
|
87
|
+
.uint64("ctime")
|
|
88
|
+
.extra({
|
|
89
|
+
id: AdbSyncResponseId.Stat as const,
|
|
90
|
+
get type() {
|
|
91
|
+
return (this.mode >> 12) as LinuxFileType;
|
|
92
|
+
},
|
|
93
|
+
get permission() {
|
|
94
|
+
return this.mode & 0b00001111_11111111;
|
|
95
|
+
},
|
|
96
|
+
})
|
|
97
|
+
.postDeserialize((object) => {
|
|
98
|
+
if (object.error) {
|
|
99
|
+
throw new Error(AdbSyncStatErrorCode[object.error]);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
96
102
|
|
|
97
|
-
export type AdbSyncStatResponse =
|
|
103
|
+
export type AdbSyncStatResponse =
|
|
104
|
+
typeof AdbSyncStatResponse["TDeserializeResult"];
|
|
98
105
|
|
|
99
106
|
export async function adbSyncLstat(
|
|
100
107
|
stream: BufferedReadableStream,
|
|
101
108
|
writer: WritableStreamDefaultWriter<Uint8Array>,
|
|
102
109
|
path: string,
|
|
103
|
-
v2: boolean
|
|
110
|
+
v2: boolean
|
|
104
111
|
): Promise<AdbSyncStat> {
|
|
105
112
|
if (v2) {
|
|
106
113
|
await adbSyncWriteRequest(writer, AdbSyncRequestId.Lstat2, path);
|
|
107
|
-
return await adbSyncReadResponse(
|
|
114
|
+
return await adbSyncReadResponse(
|
|
115
|
+
stream,
|
|
116
|
+
AdbSyncResponseId.Lstat2,
|
|
117
|
+
AdbSyncStatResponse
|
|
118
|
+
);
|
|
108
119
|
} else {
|
|
109
120
|
await adbSyncWriteRequest(writer, AdbSyncRequestId.Lstat, path);
|
|
110
|
-
const response = await adbSyncReadResponse(
|
|
121
|
+
const response = await adbSyncReadResponse(
|
|
122
|
+
stream,
|
|
123
|
+
AdbSyncResponseId.Lstat,
|
|
124
|
+
AdbSyncLstatResponse
|
|
125
|
+
);
|
|
111
126
|
return {
|
|
112
127
|
mode: response.mode,
|
|
113
128
|
// Convert to `BigInt` to make it compatible with `AdbSyncStatResponse`
|
|
114
129
|
size: BigInt(response.size),
|
|
115
130
|
mtime: BigInt(response.mtime),
|
|
116
|
-
get type() {
|
|
117
|
-
|
|
131
|
+
get type() {
|
|
132
|
+
return response.type;
|
|
133
|
+
},
|
|
134
|
+
get permission() {
|
|
135
|
+
return response.permission;
|
|
136
|
+
},
|
|
118
137
|
};
|
|
119
138
|
}
|
|
120
139
|
}
|
|
@@ -122,8 +141,12 @@ export async function adbSyncLstat(
|
|
|
122
141
|
export async function adbSyncStat(
|
|
123
142
|
stream: BufferedReadableStream,
|
|
124
143
|
writer: WritableStreamDefaultWriter<Uint8Array>,
|
|
125
|
-
path: string
|
|
144
|
+
path: string
|
|
126
145
|
): Promise<AdbSyncStatResponse> {
|
|
127
146
|
await adbSyncWriteRequest(writer, AdbSyncRequestId.Stat, path);
|
|
128
|
-
return await adbSyncReadResponse(
|
|
147
|
+
return await adbSyncReadResponse(
|
|
148
|
+
stream,
|
|
149
|
+
AdbSyncResponseId.Stat,
|
|
150
|
+
AdbSyncStatResponse
|
|
151
|
+
);
|
|
129
152
|
}
|
|
@@ -1,15 +1,23 @@
|
|
|
1
|
-
import { AutoDisposable } from
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
1
|
+
import { AutoDisposable } from "@yume-chan/event";
|
|
2
|
+
import {
|
|
3
|
+
BufferedReadableStream,
|
|
4
|
+
WrapReadableStream,
|
|
5
|
+
WrapWritableStream,
|
|
6
|
+
type ReadableStream,
|
|
7
|
+
type WritableStream,
|
|
8
|
+
type WritableStreamDefaultWriter,
|
|
9
|
+
} from "@yume-chan/stream-extra";
|
|
10
|
+
|
|
11
|
+
import { type Adb } from "../../adb.js";
|
|
12
|
+
import { AdbFeatures } from "../../features.js";
|
|
13
|
+
import { type AdbSocket } from "../../socket/index.js";
|
|
14
|
+
import { AutoResetEvent } from "../../utils/index.js";
|
|
15
|
+
import { escapeArg } from "../subprocess/index.js";
|
|
16
|
+
|
|
17
|
+
import { adbSyncOpenDir, type AdbSyncEntry } from "./list.js";
|
|
18
|
+
import { adbSyncPull } from "./pull.js";
|
|
19
|
+
import { adbSyncPush } from "./push.js";
|
|
20
|
+
import { adbSyncLstat, adbSyncStat } from "./stat.js";
|
|
13
21
|
|
|
14
22
|
/**
|
|
15
23
|
* A simplified `dirname` function that only handles absolute unix paths.
|
|
@@ -17,12 +25,12 @@ import { adbSyncLstat, adbSyncStat } from './stat.js';
|
|
|
17
25
|
* @returns the directory name of the input path
|
|
18
26
|
*/
|
|
19
27
|
export function dirname(path: string): string {
|
|
20
|
-
const end = path.lastIndexOf(
|
|
28
|
+
const end = path.lastIndexOf("/");
|
|
21
29
|
if (end === -1) {
|
|
22
30
|
throw new Error(`Invalid path`);
|
|
23
31
|
}
|
|
24
32
|
if (end === 0) {
|
|
25
|
-
return
|
|
33
|
+
return "/";
|
|
26
34
|
}
|
|
27
35
|
return path.substring(0, end);
|
|
28
36
|
}
|
|
@@ -51,7 +59,10 @@ export class AdbSync extends AutoDisposable {
|
|
|
51
59
|
|
|
52
60
|
public get needPushMkdirWorkaround(): boolean {
|
|
53
61
|
// https://android.googlesource.com/platform/packages/modules/adb/+/91768a57b7138166e0a3d11f79cd55909dda7014/client/file_sync_client.cpp#1361
|
|
54
|
-
return
|
|
62
|
+
return (
|
|
63
|
+
this.adb.features.includes(AdbFeatures.ShellV2) &&
|
|
64
|
+
!this.fixedPushMkdir
|
|
65
|
+
);
|
|
55
66
|
}
|
|
56
67
|
|
|
57
68
|
public constructor(adb: Adb, socket: AdbSocket) {
|
|
@@ -66,7 +77,12 @@ export class AdbSync extends AutoDisposable {
|
|
|
66
77
|
await this.sendLock.wait();
|
|
67
78
|
|
|
68
79
|
try {
|
|
69
|
-
return adbSyncLstat(
|
|
80
|
+
return adbSyncLstat(
|
|
81
|
+
this.stream,
|
|
82
|
+
this.writer,
|
|
83
|
+
path,
|
|
84
|
+
this.supportsStat
|
|
85
|
+
);
|
|
70
86
|
} finally {
|
|
71
87
|
this.sendLock.notify();
|
|
72
88
|
}
|
|
@@ -74,7 +90,7 @@ export class AdbSync extends AutoDisposable {
|
|
|
74
90
|
|
|
75
91
|
public async stat(path: string) {
|
|
76
92
|
if (!this.supportsStat) {
|
|
77
|
-
throw new Error(
|
|
93
|
+
throw new Error("Not supported");
|
|
78
94
|
}
|
|
79
95
|
|
|
80
96
|
await this.sendLock.wait();
|
|
@@ -88,7 +104,7 @@ export class AdbSync extends AutoDisposable {
|
|
|
88
104
|
|
|
89
105
|
public async isDirectory(path: string): Promise<boolean> {
|
|
90
106
|
try {
|
|
91
|
-
await this.lstat(path +
|
|
107
|
+
await this.lstat(path + "/");
|
|
92
108
|
return true;
|
|
93
109
|
} catch (e) {
|
|
94
110
|
return false;
|
|
@@ -101,7 +117,12 @@ export class AdbSync extends AutoDisposable {
|
|
|
101
117
|
await this.sendLock.wait();
|
|
102
118
|
|
|
103
119
|
try {
|
|
104
|
-
yield* adbSyncOpenDir(
|
|
120
|
+
yield* adbSyncOpenDir(
|
|
121
|
+
this.stream,
|
|
122
|
+
this.writer,
|
|
123
|
+
path,
|
|
124
|
+
this.supportsList2
|
|
125
|
+
);
|
|
105
126
|
} finally {
|
|
106
127
|
this.sendLock.notify();
|
|
107
128
|
}
|
|
@@ -127,7 +148,7 @@ export class AdbSync extends AutoDisposable {
|
|
|
127
148
|
await this.sendLock.wait();
|
|
128
149
|
return adbSyncPull(this.stream, this.writer, filename);
|
|
129
150
|
},
|
|
130
|
-
close:
|
|
151
|
+
close: () => {
|
|
131
152
|
this.sendLock.notify();
|
|
132
153
|
},
|
|
133
154
|
});
|
|
@@ -144,7 +165,7 @@ export class AdbSync extends AutoDisposable {
|
|
|
144
165
|
public write(
|
|
145
166
|
filename: string,
|
|
146
167
|
mode?: number,
|
|
147
|
-
mtime?: number
|
|
168
|
+
mtime?: number
|
|
148
169
|
): WritableStream<Uint8Array> {
|
|
149
170
|
return new WrapWritableStream({
|
|
150
171
|
start: async () => {
|
|
@@ -155,8 +176,8 @@ export class AdbSync extends AutoDisposable {
|
|
|
155
176
|
// Ignore the result.
|
|
156
177
|
// TODO: sync: test push mkdir workaround (need an Android 8 device)
|
|
157
178
|
await this.adb.subprocess.spawnAndWait([
|
|
158
|
-
|
|
159
|
-
|
|
179
|
+
"mkdir",
|
|
180
|
+
"-p",
|
|
160
181
|
escapeArg(dirname(filename)),
|
|
161
182
|
]);
|
|
162
183
|
}
|
|
@@ -166,12 +187,12 @@ export class AdbSync extends AutoDisposable {
|
|
|
166
187
|
this.writer,
|
|
167
188
|
filename,
|
|
168
189
|
mode,
|
|
169
|
-
mtime
|
|
190
|
+
mtime
|
|
170
191
|
);
|
|
171
192
|
},
|
|
172
|
-
close:
|
|
193
|
+
close: () => {
|
|
173
194
|
this.sendLock.notify();
|
|
174
|
-
}
|
|
195
|
+
},
|
|
175
196
|
});
|
|
176
197
|
}
|
|
177
198
|
|
package/src/crypto.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
getBigUint64,
|
|
3
|
+
setBigUint64,
|
|
4
|
+
} from "@yume-chan/dataview-bigint-polyfill/esm/fallback.js";
|
|
2
5
|
|
|
3
6
|
const BigInt0 = BigInt(0);
|
|
4
7
|
const BigInt1 = BigInt(1);
|
|
@@ -12,7 +15,11 @@ const BigInt64 = BigInt(64);
|
|
|
12
15
|
* Only supports Big-Endian, because that's what ADB uses.
|
|
13
16
|
* @param byteOffset The place in the buffer at which the value should be retrieved.
|
|
14
17
|
*/
|
|
15
|
-
export function getBigUint(
|
|
18
|
+
export function getBigUint(
|
|
19
|
+
dataView: DataView,
|
|
20
|
+
byteOffset: number,
|
|
21
|
+
length: number
|
|
22
|
+
): bigint {
|
|
16
23
|
let result = BigInt0;
|
|
17
24
|
|
|
18
25
|
// Currently `length` must be a multiplication of 8
|
|
@@ -34,7 +41,12 @@ export function getBigUint(dataView: DataView, byteOffset: number, length: numbe
|
|
|
34
41
|
* @param littleEndian If `false` or `undefined`, a big-endian value should be written,
|
|
35
42
|
* otherwise a little-endian value should be written.
|
|
36
43
|
*/
|
|
37
|
-
export function setBigUint(
|
|
44
|
+
export function setBigUint(
|
|
45
|
+
dataView: DataView,
|
|
46
|
+
byteOffset: number,
|
|
47
|
+
value: bigint,
|
|
48
|
+
littleEndian?: boolean
|
|
49
|
+
) {
|
|
38
50
|
const start = byteOffset;
|
|
39
51
|
|
|
40
52
|
if (littleEndian) {
|
|
@@ -98,7 +110,7 @@ export function parsePrivateKey(key: Uint8Array): [n: bigint, d: bigint] {
|
|
|
98
110
|
// I can't understand, but it does work
|
|
99
111
|
// Only used with numbers smaller than 2^32 so doesn't need BigInt
|
|
100
112
|
export function modInverse(a: number, m: number) {
|
|
101
|
-
a = (a % m + m) % m;
|
|
113
|
+
a = ((a % m) + m) % m;
|
|
102
114
|
if (!a || m < 2) {
|
|
103
115
|
return NaN; // invalid input
|
|
104
116
|
}
|
|
@@ -115,26 +127,24 @@ export function modInverse(a: number, m: number) {
|
|
|
115
127
|
// find the inverse
|
|
116
128
|
let x = 1;
|
|
117
129
|
let y = 0;
|
|
118
|
-
for (let i = s.length - 2; i >= 0;
|
|
130
|
+
for (let i = s.length - 2; i >= 0; i -= 1) {
|
|
119
131
|
[x, y] = [y, x - y * Math.floor(s[i]!.a / s[i]!.b)];
|
|
120
132
|
}
|
|
121
|
-
return (y % m + m) % m;
|
|
133
|
+
return ((y % m) + m) % m;
|
|
122
134
|
}
|
|
123
135
|
|
|
124
136
|
export function calculatePublicKeyLength() {
|
|
125
137
|
return 4 + 4 + 2048 / 8 + 2048 / 8 + 4;
|
|
126
138
|
}
|
|
127
139
|
|
|
128
|
-
export function calculatePublicKey(
|
|
129
|
-
privateKey: Uint8Array
|
|
130
|
-
): Uint8Array;
|
|
140
|
+
export function calculatePublicKey(privateKey: Uint8Array): Uint8Array;
|
|
131
141
|
export function calculatePublicKey(
|
|
132
142
|
privateKey: Uint8Array,
|
|
133
|
-
output: Uint8Array
|
|
143
|
+
output: Uint8Array
|
|
134
144
|
): number;
|
|
135
145
|
export function calculatePublicKey(
|
|
136
146
|
privateKey: Uint8Array,
|
|
137
|
-
output?: Uint8Array
|
|
147
|
+
output?: Uint8Array
|
|
138
148
|
): Uint8Array | number {
|
|
139
149
|
// Android has its own public key generation algorithm
|
|
140
150
|
// See https://android.googlesource.com/platform/system/core.git/+/91784040db2b9273687f88d8b95f729d4a61ecc2/libcrypto_utils/android_pubkey.cpp#111
|
|
@@ -156,20 +166,24 @@ export function calculatePublicKey(
|
|
|
156
166
|
// extract `n` from private key
|
|
157
167
|
const [n] = parsePrivateKey(privateKey);
|
|
158
168
|
|
|
159
|
-
let outputType:
|
|
169
|
+
let outputType: "Uint8Array" | "number";
|
|
160
170
|
const outputLength = calculatePublicKeyLength();
|
|
161
171
|
if (!output) {
|
|
162
172
|
output = new Uint8Array(outputLength);
|
|
163
|
-
outputType =
|
|
173
|
+
outputType = "Uint8Array";
|
|
164
174
|
} else {
|
|
165
175
|
if (output.byteLength < outputLength) {
|
|
166
|
-
throw new Error(
|
|
176
|
+
throw new Error("output buffer is too small");
|
|
167
177
|
}
|
|
168
178
|
|
|
169
|
-
outputType =
|
|
179
|
+
outputType = "number";
|
|
170
180
|
}
|
|
171
181
|
|
|
172
|
-
const outputView = new DataView(
|
|
182
|
+
const outputView = new DataView(
|
|
183
|
+
output.buffer,
|
|
184
|
+
output.byteOffset,
|
|
185
|
+
output.byteLength
|
|
186
|
+
);
|
|
173
187
|
let outputOffset = 0;
|
|
174
188
|
|
|
175
189
|
// modulusLengthInWords
|
|
@@ -179,7 +193,10 @@ export function calculatePublicKey(
|
|
|
179
193
|
// Calculate `n0inv`
|
|
180
194
|
// Don't know why need to multiple -1
|
|
181
195
|
// Didn't exist in Android codebase
|
|
182
|
-
const n0inv = modInverse(
|
|
196
|
+
const n0inv = modInverse(
|
|
197
|
+
Number(BigInt.asUintN(32, n) * BigInt(-1)),
|
|
198
|
+
2 ** 32
|
|
199
|
+
);
|
|
183
200
|
outputView.setUint32(outputOffset, n0inv, true);
|
|
184
201
|
outputOffset += 4;
|
|
185
202
|
|
|
@@ -188,14 +205,14 @@ export function calculatePublicKey(
|
|
|
188
205
|
outputOffset += 256;
|
|
189
206
|
|
|
190
207
|
// Calculate rr = (2^(rsa_size)) ^ 2 mod n
|
|
191
|
-
|
|
208
|
+
const rr = BigInt(2) ** BigInt(4096) % n;
|
|
192
209
|
outputOffset += setBigUint(outputView, outputOffset, rr, true);
|
|
193
210
|
|
|
194
211
|
// exponent
|
|
195
212
|
outputView.setUint32(outputOffset, 65537, true);
|
|
196
213
|
outputOffset += 4;
|
|
197
214
|
|
|
198
|
-
if (outputType ===
|
|
215
|
+
if (outputType === "Uint8Array") {
|
|
199
216
|
return output;
|
|
200
217
|
} else {
|
|
201
218
|
return outputLength;
|
|
@@ -209,7 +226,11 @@ export function calculatePublicKey(
|
|
|
209
226
|
*
|
|
210
227
|
* See https://en.wikipedia.org/wiki/Modular_exponentiation#Implementation_in_Lua
|
|
211
228
|
*/
|
|
212
|
-
export function powMod(
|
|
229
|
+
export function powMod(
|
|
230
|
+
base: bigint,
|
|
231
|
+
exponent: bigint,
|
|
232
|
+
modulus: bigint
|
|
233
|
+
): bigint {
|
|
213
234
|
if (modulus === BigInt1) {
|
|
214
235
|
return BigInt0;
|
|
215
236
|
}
|
|
@@ -219,7 +240,7 @@ export function powMod(base: bigint, exponent: bigint, modulus: bigint): bigint
|
|
|
219
240
|
|
|
220
241
|
while (exponent > BigInt0) {
|
|
221
242
|
if (BigInt.asUintN(1, exponent) === BigInt1) {
|
|
222
|
-
r = r * base % modulus;
|
|
243
|
+
r = (r * base) % modulus;
|
|
223
244
|
}
|
|
224
245
|
|
|
225
246
|
exponent >>= BigInt1;
|
|
@@ -238,12 +259,22 @@ export const ASN1_OID = 0x06;
|
|
|
238
259
|
|
|
239
260
|
// PKCS#1 SHA-1 hash digest info
|
|
240
261
|
export const SHA1_DIGEST_INFO = new Uint8Array([
|
|
241
|
-
ASN1_SEQUENCE,
|
|
242
|
-
|
|
262
|
+
ASN1_SEQUENCE,
|
|
263
|
+
0x0d + SHA1_DIGEST_LENGTH,
|
|
264
|
+
ASN1_SEQUENCE,
|
|
265
|
+
0x09,
|
|
243
266
|
// SHA-1 (1 3 14 3 2 26)
|
|
244
|
-
ASN1_OID,
|
|
245
|
-
|
|
246
|
-
|
|
267
|
+
ASN1_OID,
|
|
268
|
+
0x05,
|
|
269
|
+
1 * 40 + 3,
|
|
270
|
+
14,
|
|
271
|
+
3,
|
|
272
|
+
2,
|
|
273
|
+
26,
|
|
274
|
+
ASN1_NULL,
|
|
275
|
+
0x00,
|
|
276
|
+
ASN1_OCTET_STRING,
|
|
277
|
+
SHA1_DIGEST_LENGTH,
|
|
247
278
|
]);
|
|
248
279
|
|
|
249
280
|
// SubtleCrypto.sign() will hash the given data and sign the hash
|
|
@@ -265,7 +296,8 @@ export function sign(privateKey: Uint8Array, data: Uint8Array): Uint8Array {
|
|
|
265
296
|
padded[index] = 1;
|
|
266
297
|
index += 1;
|
|
267
298
|
|
|
268
|
-
const fillLength =
|
|
299
|
+
const fillLength =
|
|
300
|
+
padded.length - SHA1_DIGEST_INFO.length - data.length - 1;
|
|
269
301
|
while (index < fillLength) {
|
|
270
302
|
padded[index] = 0xff;
|
|
271
303
|
index += 1;
|
|
@@ -282,11 +314,7 @@ export function sign(privateKey: Uint8Array, data: Uint8Array): Uint8Array {
|
|
|
282
314
|
// Encryption
|
|
283
315
|
// signature = padded ** d % n
|
|
284
316
|
const view = new DataView(padded.buffer);
|
|
285
|
-
const signature = powMod(
|
|
286
|
-
getBigUint(view, 0, view.byteLength),
|
|
287
|
-
d,
|
|
288
|
-
n
|
|
289
|
-
);
|
|
317
|
+
const signature = powMod(getBigUint(view, 0, view.byteLength), d, n);
|
|
290
318
|
|
|
291
319
|
// `padded` is not used anymore,
|
|
292
320
|
// re-use the buffer to store the result
|
package/src/index.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
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 './utils/index.js';
|
|
1
|
+
export * from "./adb.js";
|
|
2
|
+
export * from "./auth.js";
|
|
3
|
+
export * from "./backend.js";
|
|
4
|
+
export * from "./commands/index.js";
|
|
5
|
+
export * from "./crypto.js";
|
|
6
|
+
export * from "./features.js";
|
|
7
|
+
export * from "./packet.js";
|
|
8
|
+
export * from "./socket/index.js";
|
|
9
|
+
export * from "./utils/index.js";
|
package/src/packet.ts
CHANGED
|
@@ -1,34 +1,32 @@
|
|
|
1
|
-
import { TransformStream } from
|
|
2
|
-
import Struct from
|
|
1
|
+
import { TransformStream } from "@yume-chan/stream-extra";
|
|
2
|
+
import Struct from "@yume-chan/struct";
|
|
3
3
|
|
|
4
4
|
export enum AdbCommand {
|
|
5
|
-
Auth = 0x48545541,
|
|
6
|
-
Close = 0x45534c43,
|
|
5
|
+
Auth = 0x48545541, // 'AUTH'
|
|
6
|
+
Close = 0x45534c43, // 'CLSE'
|
|
7
7
|
Connect = 0x4e584e43, // 'CNXN'
|
|
8
|
-
OK = 0x59414b4f,
|
|
9
|
-
Open = 0x4e45504f,
|
|
10
|
-
Write = 0x45545257,
|
|
8
|
+
OK = 0x59414b4f, // 'OKAY'
|
|
9
|
+
Open = 0x4e45504f, // 'OPEN'
|
|
10
|
+
Write = 0x45545257, // 'WRTE'
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
export const AdbPacketHeader =
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
.int32('magic');
|
|
13
|
+
export const AdbPacketHeader = new Struct({ littleEndian: true })
|
|
14
|
+
.uint32("command")
|
|
15
|
+
.uint32("arg0")
|
|
16
|
+
.uint32("arg1")
|
|
17
|
+
.uint32("payloadLength")
|
|
18
|
+
.uint32("checksum")
|
|
19
|
+
.int32("magic");
|
|
21
20
|
|
|
22
|
-
export type AdbPacketHeader = typeof AdbPacketHeader[
|
|
21
|
+
export type AdbPacketHeader = typeof AdbPacketHeader["TDeserializeResult"];
|
|
23
22
|
|
|
24
|
-
type AdbPacketHeaderInit = typeof AdbPacketHeader[
|
|
23
|
+
type AdbPacketHeaderInit = typeof AdbPacketHeader["TInit"];
|
|
25
24
|
|
|
26
|
-
export const AdbPacket =
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
.uint8Array('payload', { lengthField: 'payloadLength' });
|
|
25
|
+
export const AdbPacket = new Struct({ littleEndian: true })
|
|
26
|
+
.fields(AdbPacketHeader)
|
|
27
|
+
.uint8Array("payload", { lengthField: "payloadLength" });
|
|
30
28
|
|
|
31
|
-
export type AdbPacket = typeof AdbPacket[
|
|
29
|
+
export type AdbPacket = typeof AdbPacket["TDeserializeResult"];
|
|
32
30
|
|
|
33
31
|
/**
|
|
34
32
|
* `AdbPacketData` contains all the useful fields of `AdbPacket`.
|
|
@@ -40,29 +38,41 @@ export type AdbPacket = typeof AdbPacket['TDeserializeResult'];
|
|
|
40
38
|
* however, `AdbPacketDispatcher` will transform `AdbPacketData` to `AdbPacketInit` for you,
|
|
41
39
|
* so `AdbSocket#writable#write` only needs `AdbPacketData`.
|
|
42
40
|
*/
|
|
43
|
-
export type AdbPacketData = Omit<
|
|
41
|
+
export type AdbPacketData = Omit<
|
|
42
|
+
typeof AdbPacket["TInit"],
|
|
43
|
+
"checksum" | "magic"
|
|
44
|
+
>;
|
|
44
45
|
|
|
45
46
|
// All fields except `magic`, which can be calculated in `AdbPacketSerializeStream`
|
|
46
|
-
export type AdbPacketInit = Omit<typeof AdbPacket[
|
|
47
|
+
export type AdbPacketInit = Omit<typeof AdbPacket["TInit"], "magic">;
|
|
47
48
|
|
|
48
49
|
export function calculateChecksum(payload: Uint8Array): number;
|
|
49
50
|
export function calculateChecksum(init: AdbPacketData): AdbPacketInit;
|
|
50
|
-
export function calculateChecksum(
|
|
51
|
+
export function calculateChecksum(
|
|
52
|
+
payload: Uint8Array | AdbPacketData
|
|
53
|
+
): number | AdbPacketInit {
|
|
51
54
|
if (payload instanceof Uint8Array) {
|
|
52
55
|
return payload.reduce((result, item) => result + item, 0);
|
|
53
56
|
} else {
|
|
54
|
-
(payload as AdbPacketInit).checksum = calculateChecksum(
|
|
57
|
+
(payload as AdbPacketInit).checksum = calculateChecksum(
|
|
58
|
+
payload.payload
|
|
59
|
+
);
|
|
55
60
|
return payload as AdbPacketInit;
|
|
56
61
|
}
|
|
57
62
|
}
|
|
58
63
|
|
|
59
|
-
export class AdbPacketSerializeStream extends TransformStream<
|
|
64
|
+
export class AdbPacketSerializeStream extends TransformStream<
|
|
65
|
+
AdbPacketInit,
|
|
66
|
+
Uint8Array
|
|
67
|
+
> {
|
|
60
68
|
public constructor() {
|
|
61
69
|
super({
|
|
62
|
-
transform:
|
|
70
|
+
transform: (init, controller) => {
|
|
63
71
|
// This syntax is ugly, but I don't want to create a new object.
|
|
64
|
-
(init as unknown as AdbPacketHeaderInit).magic =
|
|
65
|
-
|
|
72
|
+
(init as unknown as AdbPacketHeaderInit).magic =
|
|
73
|
+
init.command ^ 0xffffffff;
|
|
74
|
+
(init as unknown as AdbPacketHeaderInit).payloadLength =
|
|
75
|
+
init.payload.byteLength;
|
|
66
76
|
|
|
67
77
|
controller.enqueue(
|
|
68
78
|
AdbPacketHeader.serialize(
|