@yume-chan/adb 0.0.18 → 0.0.20

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 (241) hide show
  1. package/CHANGELOG.json +54 -0
  2. package/CHANGELOG.md +25 -1
  3. package/README.md +220 -103
  4. package/esm/adb.d.ts +43 -60
  5. package/esm/adb.d.ts.map +1 -1
  6. package/esm/adb.js +68 -249
  7. package/esm/adb.js.map +1 -1
  8. package/esm/banner.d.ts +17 -0
  9. package/esm/banner.d.ts.map +1 -0
  10. package/esm/banner.js +67 -0
  11. package/esm/banner.js.map +1 -0
  12. package/esm/commands/base.d.ts +6 -6
  13. package/esm/commands/base.d.ts.map +1 -1
  14. package/esm/commands/base.js +8 -8
  15. package/esm/commands/framebuffer.d.ts +53 -53
  16. package/esm/commands/framebuffer.d.ts.map +1 -1
  17. package/esm/commands/framebuffer.js +46 -46
  18. package/esm/commands/framebuffer.js.map +1 -1
  19. package/esm/commands/index.d.ts +7 -8
  20. package/esm/commands/index.d.ts.map +1 -1
  21. package/esm/commands/index.js +7 -8
  22. package/esm/commands/index.js.map +1 -1
  23. package/esm/commands/power.d.ts +22 -22
  24. package/esm/commands/power.js +48 -48
  25. package/esm/commands/reverse.d.ts +40 -28
  26. package/esm/commands/reverse.d.ts.map +1 -1
  27. package/esm/commands/reverse.js +130 -100
  28. package/esm/commands/reverse.js.map +1 -1
  29. package/esm/commands/subprocess/command.d.ts +57 -51
  30. package/esm/commands/subprocess/command.d.ts.map +1 -1
  31. package/esm/commands/subprocess/command.js +86 -80
  32. package/esm/commands/subprocess/command.js.map +1 -1
  33. package/esm/commands/subprocess/index.d.ts +3 -3
  34. package/esm/commands/subprocess/index.js +3 -3
  35. package/esm/commands/subprocess/protocols/index.d.ts +3 -3
  36. package/esm/commands/subprocess/protocols/index.js +3 -3
  37. package/esm/commands/subprocess/protocols/none.d.ts +30 -35
  38. package/esm/commands/subprocess/protocols/none.d.ts.map +1 -1
  39. package/esm/commands/subprocess/protocols/none.js +65 -55
  40. package/esm/commands/subprocess/protocols/none.js.map +1 -1
  41. package/esm/commands/subprocess/protocols/shell.d.ts +33 -38
  42. package/esm/commands/subprocess/protocols/shell.d.ts.map +1 -1
  43. package/esm/commands/subprocess/protocols/shell.js +172 -162
  44. package/esm/commands/subprocess/protocols/shell.js.map +1 -1
  45. package/esm/commands/subprocess/protocols/types.d.ts +48 -49
  46. package/esm/commands/subprocess/protocols/types.d.ts.map +1 -1
  47. package/esm/commands/subprocess/protocols/types.js +1 -1
  48. package/esm/commands/subprocess/utils.d.ts +1 -1
  49. package/esm/commands/subprocess/utils.js +18 -18
  50. package/esm/commands/sync/index.d.ts +8 -7
  51. package/esm/commands/sync/index.d.ts.map +1 -1
  52. package/esm/commands/sync/index.js +8 -7
  53. package/esm/commands/sync/index.js.map +1 -1
  54. package/esm/commands/sync/list.d.ts +42 -40
  55. package/esm/commands/sync/list.d.ts.map +1 -1
  56. package/esm/commands/sync/list.js +67 -47
  57. package/esm/commands/sync/list.js.map +1 -1
  58. package/esm/commands/sync/pull.d.ts +13 -11
  59. package/esm/commands/sync/pull.d.ts.map +1 -1
  60. package/esm/commands/sync/pull.js +35 -37
  61. package/esm/commands/sync/pull.js.map +1 -1
  62. package/esm/commands/sync/push.d.ts +57 -7
  63. package/esm/commands/sync/push.d.ts.map +1 -1
  64. package/esm/commands/sync/push.js +89 -22
  65. package/esm/commands/sync/push.js.map +1 -1
  66. package/esm/commands/sync/request.d.ts +26 -23
  67. package/esm/commands/sync/request.d.ts.map +1 -1
  68. package/esm/commands/sync/request.js +40 -42
  69. package/esm/commands/sync/request.js.map +1 -1
  70. package/esm/commands/sync/response.d.ts +19 -19
  71. package/esm/commands/sync/response.d.ts.map +1 -1
  72. package/esm/commands/sync/response.js +54 -54
  73. package/esm/commands/sync/response.js.map +1 -1
  74. package/esm/commands/sync/socket.d.ts +22 -0
  75. package/esm/commands/sync/socket.d.ts.map +1 -0
  76. package/esm/commands/sync/socket.js +69 -0
  77. package/esm/commands/sync/socket.js.map +1 -0
  78. package/esm/commands/sync/stat.d.ts +72 -72
  79. package/esm/commands/sync/stat.d.ts.map +1 -1
  80. package/esm/commands/sync/stat.js +116 -104
  81. package/esm/commands/sync/stat.js.map +1 -1
  82. package/esm/commands/sync/sync.d.ts +65 -60
  83. package/esm/commands/sync/sync.d.ts.map +1 -1
  84. package/esm/commands/sync/sync.js +123 -149
  85. package/esm/commands/sync/sync.js.map +1 -1
  86. package/esm/commands/tcpip.d.ts +5 -5
  87. package/esm/commands/tcpip.d.ts.map +1 -1
  88. package/esm/commands/tcpip.js +20 -18
  89. package/esm/commands/tcpip.js.map +1 -1
  90. package/esm/{auth.d.ts → daemon/auth.d.ts} +50 -51
  91. package/esm/daemon/auth.d.ts.map +1 -0
  92. package/esm/{auth.js → daemon/auth.js} +95 -96
  93. package/esm/daemon/auth.js.map +1 -0
  94. package/esm/daemon/connection.d.ts +11 -0
  95. package/esm/daemon/connection.d.ts.map +1 -0
  96. package/esm/daemon/connection.js +2 -0
  97. package/esm/daemon/connection.js.map +1 -0
  98. package/esm/{crypto.d.ts → daemon/crypto.d.ts} +36 -36
  99. package/esm/daemon/crypto.d.ts.map +1 -0
  100. package/esm/{crypto.js → daemon/crypto.js} +244 -248
  101. package/esm/daemon/crypto.js.map +1 -0
  102. package/esm/daemon/device.d.ts +9 -0
  103. package/esm/daemon/device.d.ts.map +1 -0
  104. package/esm/daemon/device.js +2 -0
  105. package/esm/daemon/device.js.map +1 -0
  106. package/esm/daemon/dispatcher.d.ts +42 -0
  107. package/esm/daemon/dispatcher.d.ts.map +1 -0
  108. package/esm/{socket → daemon}/dispatcher.js +225 -234
  109. package/esm/daemon/dispatcher.js.map +1 -0
  110. package/esm/daemon/index.d.ts +8 -0
  111. package/esm/daemon/index.d.ts.map +1 -0
  112. package/esm/daemon/index.js +8 -0
  113. package/esm/daemon/index.js.map +1 -0
  114. package/esm/{packet.d.ts → daemon/packet.d.ts} +45 -46
  115. package/esm/daemon/packet.d.ts.map +1 -0
  116. package/esm/{packet.js → daemon/packet.js} +43 -48
  117. package/esm/daemon/packet.js.map +1 -0
  118. package/esm/daemon/socket.d.ts +58 -0
  119. package/esm/daemon/socket.d.ts.map +1 -0
  120. package/esm/{socket → daemon}/socket.js +121 -118
  121. package/esm/daemon/socket.js.map +1 -0
  122. package/esm/daemon/transport.d.ts +45 -0
  123. package/esm/daemon/transport.d.ts.map +1 -0
  124. package/esm/daemon/transport.js +184 -0
  125. package/esm/daemon/transport.js.map +1 -0
  126. package/esm/features.d.ts +10 -7
  127. package/esm/features.d.ts.map +1 -1
  128. package/esm/features.js +13 -10
  129. package/esm/features.js.map +1 -1
  130. package/esm/index.d.ts +7 -9
  131. package/esm/index.d.ts.map +1 -1
  132. package/esm/index.js +7 -9
  133. package/esm/index.js.map +1 -1
  134. package/esm/server/client.d.ts +78 -0
  135. package/esm/server/client.d.ts.map +1 -0
  136. package/esm/server/client.js +329 -0
  137. package/esm/server/client.js.map +1 -0
  138. package/esm/server/index.d.ts +3 -0
  139. package/esm/server/index.d.ts.map +1 -0
  140. package/esm/server/index.js +3 -0
  141. package/esm/server/index.js.map +1 -0
  142. package/esm/server/transport.d.ts +19 -0
  143. package/esm/server/transport.d.ts.map +1 -0
  144. package/esm/server/transport.js +44 -0
  145. package/esm/server/transport.js.map +1 -0
  146. package/esm/utils/auto-reset-event.d.ts +8 -9
  147. package/esm/utils/auto-reset-event.d.ts.map +1 -1
  148. package/esm/utils/auto-reset-event.js +33 -33
  149. package/esm/utils/auto-reset-event.js.map +1 -1
  150. package/esm/utils/base64.d.ts +27 -27
  151. package/esm/utils/base64.js +246 -246
  152. package/esm/utils/base64.js.map +1 -1
  153. package/esm/utils/conditional-variable.d.ts +8 -0
  154. package/esm/utils/conditional-variable.d.ts.map +1 -0
  155. package/esm/utils/conditional-variable.js +34 -0
  156. package/esm/utils/conditional-variable.js.map +1 -0
  157. package/esm/utils/hex.d.ts +3 -0
  158. package/esm/utils/hex.d.ts.map +1 -0
  159. package/esm/utils/hex.js +55 -0
  160. package/esm/utils/hex.js.map +1 -0
  161. package/esm/utils/index.d.ts +6 -3
  162. package/esm/utils/index.d.ts.map +1 -1
  163. package/esm/utils/index.js +6 -3
  164. package/esm/utils/index.js.map +1 -1
  165. package/esm/utils/no-op.d.ts +7 -0
  166. package/esm/utils/no-op.d.ts.map +1 -0
  167. package/esm/utils/no-op.js +12 -0
  168. package/esm/utils/no-op.js.map +1 -0
  169. package/package.json +14 -12
  170. package/src/adb.ts +47 -248
  171. package/src/banner.ts +82 -0
  172. package/src/commands/base.ts +1 -1
  173. package/src/commands/framebuffer.ts +4 -4
  174. package/src/commands/index.ts +7 -8
  175. package/src/commands/power.ts +2 -2
  176. package/src/commands/reverse.ts +90 -70
  177. package/src/commands/subprocess/command.ts +12 -4
  178. package/src/commands/subprocess/index.ts +3 -3
  179. package/src/commands/subprocess/protocols/index.ts +3 -3
  180. package/src/commands/subprocess/protocols/none.ts +36 -23
  181. package/src/commands/subprocess/protocols/shell.ts +94 -74
  182. package/src/commands/subprocess/protocols/types.ts +7 -7
  183. package/src/commands/subprocess/utils.ts +1 -1
  184. package/src/commands/sync/index.ts +8 -7
  185. package/src/commands/sync/list.ts +44 -25
  186. package/src/commands/sync/pull.ts +40 -45
  187. package/src/commands/sync/push.ts +158 -39
  188. package/src/commands/sync/request.ts +21 -16
  189. package/src/commands/sync/response.ts +10 -9
  190. package/src/commands/sync/socket.ts +99 -0
  191. package/src/commands/sync/stat.ts +47 -42
  192. package/src/commands/sync/sync.ts +73 -116
  193. package/src/commands/tcpip.ts +9 -7
  194. package/src/{auth.ts → daemon/auth.ts} +24 -23
  195. package/src/{crypto.ts → daemon/crypto.ts} +25 -33
  196. package/src/daemon/device.ts +14 -0
  197. package/src/{socket → daemon}/dispatcher.ts +95 -117
  198. package/src/daemon/index.ts +7 -0
  199. package/src/daemon/packet.ts +75 -0
  200. package/src/{socket → daemon}/socket.ts +71 -56
  201. package/src/daemon/transport.ts +269 -0
  202. package/src/features.ts +9 -6
  203. package/src/index.ts +3 -5
  204. package/src/server/client.ts +476 -0
  205. package/src/server/index.ts +2 -0
  206. package/src/server/transport.ts +77 -0
  207. package/src/utils/auto-reset-event.ts +14 -15
  208. package/src/utils/conditional-variable.ts +45 -0
  209. package/src/utils/hex.ts +58 -0
  210. package/src/utils/index.ts +6 -3
  211. package/src/utils/no-op.ts +12 -0
  212. package/tsconfig.build.json +12 -1
  213. package/tsconfig.build.tsbuildinfo +1 -1
  214. package/esm/auth.d.ts.map +0 -1
  215. package/esm/auth.js.map +0 -1
  216. package/esm/backend.d.ts +0 -9
  217. package/esm/backend.d.ts.map +0 -1
  218. package/esm/backend.js +0 -2
  219. package/esm/backend.js.map +0 -1
  220. package/esm/commands/install.d.ts +0 -4
  221. package/esm/commands/install.d.ts.map +0 -1
  222. package/esm/commands/install.js +0 -29
  223. package/esm/commands/install.js.map +0 -1
  224. package/esm/crypto.d.ts.map +0 -1
  225. package/esm/crypto.js.map +0 -1
  226. package/esm/packet.d.ts.map +0 -1
  227. package/esm/packet.js.map +0 -1
  228. package/esm/socket/dispatcher.d.ts +0 -60
  229. package/esm/socket/dispatcher.d.ts.map +0 -1
  230. package/esm/socket/dispatcher.js.map +0 -1
  231. package/esm/socket/index.d.ts +0 -3
  232. package/esm/socket/index.d.ts.map +0 -1
  233. package/esm/socket/index.js +0 -3
  234. package/esm/socket/index.js.map +0 -1
  235. package/esm/socket/socket.d.ts +0 -60
  236. package/esm/socket/socket.d.ts.map +0 -1
  237. package/esm/socket/socket.js.map +0 -1
  238. package/src/backend.ts +0 -14
  239. package/src/commands/install.ts +0 -40
  240. package/src/packet.ts +0 -90
  241. package/src/socket/index.ts +0 -2
@@ -1,249 +1,245 @@
1
- import { getBigUint64, setBigUint64, } from "@yume-chan/dataview-bigint-polyfill/esm/fallback.js";
2
- const BigInt0 = BigInt(0);
3
- const BigInt1 = BigInt(1);
4
- const BigInt2 = BigInt(2);
5
- const BigInt64 = BigInt(64);
6
- /**
7
- * Gets the `BigInt` value at the specified byte offset and length from the start of the view. There is
8
- * no alignment constraint; multi-byte values may be fetched from any offset.
9
- *
10
- * Only supports Big-Endian, because that's what ADB uses.
11
- * @param byteOffset The place in the buffer at which the value should be retrieved.
12
- */
13
- export function getBigUint(dataView, byteOffset, length) {
14
- let result = BigInt0;
15
- // Currently `length` must be a multiplication of 8
16
- // Support for arbitrary length can be easily added
17
- for (let i = byteOffset; i < byteOffset + length; i += 8) {
18
- result <<= BigInt64;
19
- const value = getBigUint64(dataView, i, false);
20
- result += value;
21
- }
22
- return result;
23
- }
24
- /**
25
- * Stores an arbitrary-precision positive `BigInt` value at the specified byte offset from the start of the view.
26
- * @param byteOffset The place in the buffer at which the value should be set.
27
- * @param value The value to set.
28
- * @param littleEndian If `false` or `undefined`, a big-endian value should be written,
29
- * otherwise a little-endian value should be written.
30
- */
31
- export function setBigUint(dataView, byteOffset, value, littleEndian) {
32
- const start = byteOffset;
33
- if (littleEndian) {
34
- while (value > BigInt0) {
35
- setBigUint64(dataView, byteOffset, value, true);
36
- byteOffset += 8;
37
- value >>= BigInt64;
38
- }
39
- }
40
- else {
41
- // Because we don't know how long (in bits) the `value` is,
42
- // Convert it to an array of `uint64` first.
43
- const uint64Array = [];
44
- while (value > BigInt0) {
45
- uint64Array.push(BigInt.asUintN(64, value));
46
- value >>= BigInt64;
47
- }
48
- for (let i = uint64Array.length - 1; i >= 0; i -= 1) {
49
- setBigUint64(dataView, byteOffset, uint64Array[i], false);
50
- byteOffset += 8;
51
- }
52
- }
53
- return byteOffset - start;
54
- }
55
- // These values are correct only if
56
- // modulus length is 2048 and
57
- // public exponent (e) is 65537
58
- // Anyway, that's how this library generates keys
59
- // To support other parameters,
60
- // a proper ASN.1 parser can be used
61
- // References:
62
- //
63
- // https://tools.ietf.org/html/rfc8017#appendix-A.1.2
64
- // PKCS #1: RSA Cryptography Specifications Version 2.2
65
- // A.1.2. RSA Private Key Syntax
66
- //
67
- // https://lapo.it/asn1js/
68
- // https://github.com/lapo-luchini/asn1js
69
- // ASN.1 JavaScript decoder
70
- //
71
- // https://www.itu.int/rec/T-REC-X.690-201508-I/en
72
- // X.690: Specification of Distinguished Encoding Rules (DER)
73
- const RsaPrivateKeyNOffset = 38;
74
- const RsaPrivateKeyNLength = 2048 / 8;
75
- const RsaPrivateKeyDOffset = 303;
76
- const RsaPrivateKeyDLength = 2048 / 8;
77
- export function parsePrivateKey(key) {
78
- const view = new DataView(key.buffer, key.byteOffset, key.byteLength);
79
- const n = getBigUint(view, RsaPrivateKeyNOffset, RsaPrivateKeyNLength);
80
- const d = getBigUint(view, RsaPrivateKeyDOffset, RsaPrivateKeyDLength);
81
- return [n, d];
82
- }
83
- // Taken from https://stackoverflow.com/a/51562038
84
- // I can't understand, but it does work
85
- // Only used with numbers smaller than 2^32 so doesn't need BigInt
86
- export function modInverse(a, m) {
87
- a = ((a % m) + m) % m;
88
- if (!a || m < 2) {
89
- return NaN; // invalid input
90
- }
91
- // find the gcd
92
- const s = [];
93
- let b = m;
94
- while (b) {
95
- [a, b] = [b, a % b];
96
- s.push({ a, b });
97
- }
98
- if (a !== 1) {
99
- return NaN; // inverse does not exists
100
- }
101
- // find the inverse
102
- let x = 1;
103
- let y = 0;
104
- for (let i = s.length - 2; i >= 0; i -= 1) {
105
- [x, y] = [y, x - y * Math.floor(s[i].a / s[i].b)];
106
- }
107
- return ((y % m) + m) % m;
108
- }
109
- export function calculatePublicKeyLength() {
110
- return 4 + 4 + 2048 / 8 + 2048 / 8 + 4;
111
- }
112
- export function calculatePublicKey(privateKey, output) {
113
- // Android has its own public key generation algorithm
114
- // See https://android.googlesource.com/platform/system/core.git/+/91784040db2b9273687f88d8b95f729d4a61ecc2/libcrypto_utils/android_pubkey.cpp#111
115
- // The public key is an array of
116
- //
117
- // [
118
- // modulusLengthInWords, // 32-bit integer, a "word" is 32-bit so it must be 2048 / 8 / 4
119
- // // Actually the comment in Android source code was wrong
120
- // n0inv, // 32-bit integer, the modular inverse of (low 32 bits of n)
121
- // modulus, // n
122
- // rr, // Montgomery parameter R^2
123
- // exponent, // 32-bit integer, must be 65537
124
- // ]
125
- //
126
- // (All in little endian)
127
- // See https://android.googlesource.com/platform/system/core.git/+/91784040db2b9273687f88d8b95f729d4a61ecc2/libcrypto_utils/android_pubkey.cpp#38
128
- // extract `n` from private key
129
- const [n] = parsePrivateKey(privateKey);
130
- let outputType;
131
- const outputLength = calculatePublicKeyLength();
132
- if (!output) {
133
- output = new Uint8Array(outputLength);
134
- outputType = "Uint8Array";
135
- }
136
- else {
137
- if (output.byteLength < outputLength) {
138
- throw new Error("output buffer is too small");
139
- }
140
- outputType = "number";
141
- }
142
- const outputView = new DataView(output.buffer, output.byteOffset, output.byteLength);
143
- let outputOffset = 0;
144
- // modulusLengthInWords
145
- outputView.setUint32(outputOffset, 2048 / 8 / 4, true);
146
- outputOffset += 4;
147
- // Calculate `n0inv`
148
- // Don't know why need to multiple -1
149
- // Didn't exist in Android codebase
150
- const n0inv = modInverse(Number(BigInt.asUintN(32, n) * BigInt(-1)), 2 ** 32);
151
- outputView.setUint32(outputOffset, n0inv, true);
152
- outputOffset += 4;
153
- // Write n
154
- setBigUint(outputView, outputOffset, n, true);
155
- outputOffset += 256;
156
- // Calculate rr = (2^(rsa_size)) ^ 2 mod n
157
- const rr = BigInt(2) ** BigInt(4096) % n;
158
- outputOffset += setBigUint(outputView, outputOffset, rr, true);
159
- // exponent
160
- outputView.setUint32(outputOffset, 65537, true);
161
- outputOffset += 4;
162
- if (outputType === "Uint8Array") {
163
- return output;
164
- }
165
- else {
166
- return outputLength;
167
- }
168
- }
169
- /**
170
- * Modular exponentiation.
171
- *
172
- * Calculate `(base ** exponent) % modulus` without actually calculating `(base ** exponent)`.
173
- *
174
- * See https://en.wikipedia.org/wiki/Modular_exponentiation#Implementation_in_Lua
175
- */
176
- export function powMod(base, exponent, modulus) {
177
- if (modulus === BigInt1) {
178
- return BigInt0;
179
- }
180
- let r = BigInt1;
181
- base = base % modulus;
182
- while (exponent > BigInt0) {
183
- if (BigInt.asUintN(1, exponent) === BigInt1) {
184
- r = (r * base) % modulus;
185
- }
186
- exponent >>= BigInt1;
187
- base = base ** BigInt2 % modulus;
188
- }
189
- return r;
190
- }
191
- export const SHA1_DIGEST_LENGTH = 20;
192
- export const ASN1_SEQUENCE = 0x30;
193
- export const ASN1_OCTET_STRING = 0x04;
194
- export const ASN1_NULL = 0x05;
195
- export const ASN1_OID = 0x06;
196
- // PKCS#1 SHA-1 hash digest info
197
- export const SHA1_DIGEST_INFO = new Uint8Array([
198
- ASN1_SEQUENCE,
199
- 0x0d + SHA1_DIGEST_LENGTH,
200
- ASN1_SEQUENCE,
201
- 0x09,
202
- // SHA-1 (1 3 14 3 2 26)
203
- ASN1_OID,
204
- 0x05,
205
- 1 * 40 + 3,
206
- 14,
207
- 3,
208
- 2,
209
- 26,
210
- ASN1_NULL,
211
- 0x00,
212
- ASN1_OCTET_STRING,
213
- SHA1_DIGEST_LENGTH,
214
- ]);
215
- // SubtleCrypto.sign() will hash the given data and sign the hash
216
- // But we don't need the hashing step
217
- // (In another word, ADB just requires the client to
218
- // encrypt the given data with its private key)
219
- // However SubtileCrypto.encrypt() doesn't accept 'RSASSA-PKCS1-v1_5' algorithm
220
- // So we need to implement the encryption by ourself
221
- export function sign(privateKey, data) {
222
- const [n, d] = parsePrivateKey(privateKey);
223
- // PKCS#1 padding
224
- const padded = new Uint8Array(256);
225
- let index = 0;
226
- padded[index] = 0;
227
- index += 1;
228
- padded[index] = 1;
229
- index += 1;
230
- const fillLength = padded.length - SHA1_DIGEST_INFO.length - data.length - 1;
231
- while (index < fillLength) {
232
- padded[index] = 0xff;
233
- index += 1;
234
- }
235
- padded[index] = 0;
236
- index += 1;
237
- padded.set(SHA1_DIGEST_INFO, index);
238
- index += SHA1_DIGEST_INFO.length;
239
- padded.set(data, index);
240
- // Encryption
241
- // signature = padded ** d % n
242
- const view = new DataView(padded.buffer);
243
- const signature = powMod(getBigUint(view, 0, view.byteLength), d, n);
244
- // `padded` is not used anymore,
245
- // re-use the buffer to store the result
246
- setBigUint(view, 0, signature, false);
247
- return padded;
248
- }
1
+ import { getBigUint64, setBigUint64, } from "@yume-chan/dataview-bigint-polyfill/esm/fallback.js";
2
+ /**
3
+ * Gets the `BigInt` value at the specified byte offset and length from the start of the view. There is
4
+ * no alignment constraint; multi-byte values may be fetched from any offset.
5
+ *
6
+ * Only supports Big-Endian, because that's what ADB uses.
7
+ * @param byteOffset The place in the buffer at which the value should be retrieved.
8
+ */
9
+ export function getBigUint(dataView, byteOffset, length) {
10
+ let result = 0n;
11
+ // Currently `length` must be a multiplication of 8
12
+ // Support for arbitrary length can be easily added
13
+ for (let i = byteOffset; i < byteOffset + length; i += 8) {
14
+ result <<= 64n;
15
+ const value = getBigUint64(dataView, i, false);
16
+ result += value;
17
+ }
18
+ return result;
19
+ }
20
+ /**
21
+ * Stores an arbitrary-precision positive `BigInt` value at the specified byte offset from the start of the view.
22
+ * @param byteOffset The place in the buffer at which the value should be set.
23
+ * @param value The value to set.
24
+ * @param littleEndian If `false` or `undefined`, a big-endian value should be written,
25
+ * otherwise a little-endian value should be written.
26
+ */
27
+ export function setBigUint(dataView, byteOffset, value, littleEndian) {
28
+ const start = byteOffset;
29
+ if (littleEndian) {
30
+ while (value > 0n) {
31
+ setBigUint64(dataView, byteOffset, value, true);
32
+ byteOffset += 8;
33
+ value >>= 64n;
34
+ }
35
+ }
36
+ else {
37
+ // Because we don't know how long (in bits) the `value` is,
38
+ // Convert it to an array of `uint64` first.
39
+ const uint64Array = [];
40
+ while (value > 0n) {
41
+ uint64Array.push(BigInt.asUintN(64, value));
42
+ value >>= 64n;
43
+ }
44
+ for (let i = uint64Array.length - 1; i >= 0; i -= 1) {
45
+ setBigUint64(dataView, byteOffset, uint64Array[i], false);
46
+ byteOffset += 8;
47
+ }
48
+ }
49
+ return byteOffset - start;
50
+ }
51
+ // These values are correct only if
52
+ // modulus length is 2048 and
53
+ // public exponent (e) is 65537
54
+ // Anyway, that's how this library generates keys
55
+ // To support other parameters,
56
+ // a proper ASN.1 parser can be used
57
+ // References:
58
+ //
59
+ // https://tools.ietf.org/html/rfc8017#appendix-A.1.2
60
+ // PKCS #1: RSA Cryptography Specifications Version 2.2
61
+ // A.1.2. RSA Private Key Syntax
62
+ //
63
+ // https://lapo.it/asn1js/
64
+ // https://github.com/lapo-luchini/asn1js
65
+ // ASN.1 JavaScript decoder
66
+ //
67
+ // https://www.itu.int/rec/T-REC-X.690-201508-I/en
68
+ // X.690: Specification of Distinguished Encoding Rules (DER)
69
+ const RsaPrivateKeyNOffset = 38;
70
+ const RsaPrivateKeyNLength = 2048 / 8;
71
+ const RsaPrivateKeyDOffset = 303;
72
+ const RsaPrivateKeyDLength = 2048 / 8;
73
+ export function rsaParsePrivateKey(key) {
74
+ const view = new DataView(key.buffer, key.byteOffset, key.byteLength);
75
+ const n = getBigUint(view, RsaPrivateKeyNOffset, RsaPrivateKeyNLength);
76
+ const d = getBigUint(view, RsaPrivateKeyDOffset, RsaPrivateKeyDLength);
77
+ return [n, d];
78
+ }
79
+ // Taken from https://stackoverflow.com/a/51562038
80
+ // I can't understand, but it does work
81
+ // Only used with numbers smaller than 2^32 so doesn't need BigInt
82
+ export function modInverse(a, m) {
83
+ a = ((a % m) + m) % m;
84
+ if (!a || m < 2) {
85
+ return NaN; // invalid input
86
+ }
87
+ // find the gcd
88
+ const s = [];
89
+ let b = m;
90
+ while (b) {
91
+ [a, b] = [b, a % b];
92
+ s.push({ a, b });
93
+ }
94
+ if (a !== 1) {
95
+ return NaN; // inverse does not exists
96
+ }
97
+ // find the inverse
98
+ let x = 1;
99
+ let y = 0;
100
+ for (let i = s.length - 2; i >= 0; i -= 1) {
101
+ [x, y] = [y, x - y * Math.floor(s[i].a / s[i].b)];
102
+ }
103
+ return ((y % m) + m) % m;
104
+ }
105
+ export function adbGetPublicKeySize() {
106
+ return 4 + 4 + 2048 / 8 + 2048 / 8 + 4;
107
+ }
108
+ export function adbGeneratePublicKey(privateKey, output) {
109
+ // Android has its own public key generation algorithm
110
+ // See https://android.googlesource.com/platform/system/core.git/+/91784040db2b9273687f88d8b95f729d4a61ecc2/libcrypto_utils/android_pubkey.cpp#111
111
+ // The public key is an array of
112
+ //
113
+ // [
114
+ // modulusLengthInWords, // 32-bit integer, a "word" is 32-bit so it must be 2048 / 8 / 4
115
+ // // Actually the comment in Android source code was wrong
116
+ // n0inv, // 32-bit integer, the modular inverse of (low 32 bits of n)
117
+ // modulus, // n
118
+ // rr, // Montgomery parameter R^2
119
+ // exponent, // 32-bit integer, must be 65537
120
+ // ]
121
+ //
122
+ // (All in little endian)
123
+ // See https://android.googlesource.com/platform/system/core.git/+/91784040db2b9273687f88d8b95f729d4a61ecc2/libcrypto_utils/android_pubkey.cpp#38
124
+ // extract `n` from private key
125
+ const [n] = rsaParsePrivateKey(privateKey);
126
+ let outputType;
127
+ const outputLength = adbGetPublicKeySize();
128
+ if (!output) {
129
+ output = new Uint8Array(outputLength);
130
+ outputType = "Uint8Array";
131
+ }
132
+ else {
133
+ if (output.byteLength < outputLength) {
134
+ throw new Error("output buffer is too small");
135
+ }
136
+ outputType = "number";
137
+ }
138
+ const outputView = new DataView(output.buffer, output.byteOffset, output.byteLength);
139
+ let outputOffset = 0;
140
+ // modulusLengthInWords
141
+ outputView.setUint32(outputOffset, 2048 / 8 / 4, true);
142
+ outputOffset += 4;
143
+ // Calculate `n0inv`
144
+ // Don't know why need to multiply by -1
145
+ // Didn't exist in Android codebase
146
+ const n0inv = modInverse(-Number(BigInt.asUintN(32, n)), 2 ** 32);
147
+ outputView.setUint32(outputOffset, n0inv, true);
148
+ outputOffset += 4;
149
+ // Write n
150
+ setBigUint(outputView, outputOffset, n, true);
151
+ outputOffset += 256;
152
+ // Calculate rr = (2^(rsa_size)) ^ 2 mod n
153
+ const rr = 2n ** 4096n % n;
154
+ outputOffset += setBigUint(outputView, outputOffset, rr, true);
155
+ // exponent
156
+ outputView.setUint32(outputOffset, 65537, true);
157
+ outputOffset += 4;
158
+ if (outputType === "Uint8Array") {
159
+ return output;
160
+ }
161
+ else {
162
+ return outputLength;
163
+ }
164
+ }
165
+ /**
166
+ * Modular exponentiation.
167
+ *
168
+ * Calculate `(base ** exponent) % modulus` without actually calculating `(base ** exponent)`.
169
+ *
170
+ * See https://en.wikipedia.org/wiki/Modular_exponentiation#Implementation_in_Lua
171
+ */
172
+ export function powMod(base, exponent, modulus) {
173
+ if (modulus === 1n) {
174
+ return 0n;
175
+ }
176
+ let r = 1n;
177
+ base = base % modulus;
178
+ while (exponent > 0n) {
179
+ if (BigInt.asUintN(1, exponent) === 1n) {
180
+ r = (r * base) % modulus;
181
+ }
182
+ base = (base * base) % modulus;
183
+ exponent >>= 1n;
184
+ }
185
+ return r;
186
+ }
187
+ export const SHA1_DIGEST_LENGTH = 20;
188
+ export const ASN1_SEQUENCE = 0x30;
189
+ export const ASN1_OCTET_STRING = 0x04;
190
+ export const ASN1_NULL = 0x05;
191
+ export const ASN1_OID = 0x06;
192
+ // PKCS#1 SHA-1 hash digest info
193
+ export const SHA1_DIGEST_INFO = new Uint8Array([
194
+ ASN1_SEQUENCE,
195
+ 0x0d + SHA1_DIGEST_LENGTH,
196
+ ASN1_SEQUENCE,
197
+ 0x09,
198
+ // SHA-1 (1 3 14 3 2 26)
199
+ ASN1_OID,
200
+ 0x05,
201
+ 1 * 40 + 3,
202
+ 14,
203
+ 3,
204
+ 2,
205
+ 26,
206
+ ASN1_NULL,
207
+ 0x00,
208
+ ASN1_OCTET_STRING,
209
+ SHA1_DIGEST_LENGTH,
210
+ ]);
211
+ // SubtleCrypto.sign() will hash the given data and sign the hash
212
+ // But we don't need the hashing step
213
+ // (In another word, ADB just requires the client to
214
+ // encrypt the given data with its private key)
215
+ // However SubtileCrypto.encrypt() doesn't accept 'RSASSA-PKCS1-v1_5' algorithm
216
+ // So we need to implement the encryption by ourself
217
+ export function rsaSign(privateKey, data) {
218
+ const [n, d] = rsaParsePrivateKey(privateKey);
219
+ // PKCS#1 padding
220
+ const padded = new Uint8Array(256);
221
+ let index = 0;
222
+ padded[index] = 0;
223
+ index += 1;
224
+ padded[index] = 1;
225
+ index += 1;
226
+ const fillLength = padded.length - SHA1_DIGEST_INFO.length - data.length - 1;
227
+ while (index < fillLength) {
228
+ padded[index] = 0xff;
229
+ index += 1;
230
+ }
231
+ padded[index] = 0;
232
+ index += 1;
233
+ padded.set(SHA1_DIGEST_INFO, index);
234
+ index += SHA1_DIGEST_INFO.length;
235
+ padded.set(data, index);
236
+ // Encryption
237
+ // signature = padded ** d % n
238
+ const view = new DataView(padded.buffer);
239
+ const signature = powMod(getBigUint(view, 0, view.byteLength), d, n);
240
+ // `padded` is not used anymore,
241
+ // re-use the buffer to store the result
242
+ setBigUint(view, 0, signature, false);
243
+ return padded;
244
+ }
249
245
  //# sourceMappingURL=crypto.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crypto.js","sourceRoot":"","sources":["../../src/daemon/crypto.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,YAAY,EACZ,YAAY,GACf,MAAM,qDAAqD,CAAC;AAE7D;;;;;;GAMG;AACH,MAAM,UAAU,UAAU,CACtB,QAAkB,EAClB,UAAkB,EAClB,MAAc;IAEd,IAAI,MAAM,GAAG,EAAE,CAAC;IAEhB,mDAAmD;IACnD,mDAAmD;IAEnD,KAAK,IAAI,CAAC,GAAG,UAAU,EAAE,CAAC,GAAG,UAAU,GAAG,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE;QACtD,MAAM,KAAK,GAAG,CAAC;QACf,MAAM,KAAK,GAAG,YAAY,CAAC,QAAQ,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC;QAC/C,MAAM,IAAI,KAAK,CAAC;KACnB;IAED,OAAO,MAAM,CAAC;AAClB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,UAAU,CACtB,QAAkB,EAClB,UAAkB,EAClB,KAAa,EACb,YAAsB;IAEtB,MAAM,KAAK,GAAG,U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KAAK,CAAC,GAAG,CAAC,CAAC;IAClB,KAAK,IAAI,CAAC,CAAC;IAEX,MAAM,UAAU,GACZ,MAAM,CAAC,MAAM,GAAG,gBAAgB,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;IAC9D,OAAO,KAAK,GAAG,UAAU,EAAE;QACvB,MAAM,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;QACrB,KAAK,IAAI,CAAC,CAAC;KACd;IAED,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAClB,KAAK,IAAI,CAAC,CAAC;IAEX,MAAM,CAAC,GAAG,CAAC,gBAAgB,EAAE,KAAK,CAAC,CAAC;IACpC,KAAK,IAAI,gBAAgB,CAAC,MAAM,CAAC;IAEjC,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAExB,aAAa;IACb,8BAA8B;IAC9B,MAAM,IAAI,GAAG,IAAI,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACzC,MAAM,SAAS,GAAG,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC,EAAE,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IAErE,gCAAgC;IAChC,wCAAwC;IACxC,UAAU,CAAC,IAAI,EAAE,CAAC,EAAE,SAAS,EAAE,KAAK,CAAC,CAAC;IAEtC,OAAO,MAAM,CAAC;AAClB,CAAC"}
@@ -0,0 +1,9 @@
1
+ import type { Consumable, ReadableWritablePair } from "@yume-chan/stream-extra";
2
+ import type { ValueOrPromise } from "@yume-chan/struct";
3
+ import type { AdbPacketData, AdbPacketInit } from "./packet.js";
4
+ export interface AdbDaemonDevice {
5
+ readonly serial: string;
6
+ readonly name: string | undefined;
7
+ connect(): ValueOrPromise<ReadableWritablePair<AdbPacketData, Consumable<AdbPacketInit>>>;
8
+ }
9
+ //# sourceMappingURL=device.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"device.d.ts","sourceRoot":"","sources":["../../src/daemon/device.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAChF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAExD,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAEhE,MAAM,WAAW,eAAe;IAC5B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAExB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC;IAElC,OAAO,IAAI,cAAc,CACrB,oBAAoB,CAAC,aAAa,EAAE,UAAU,CAAC,aAAa,CAAC,CAAC,CACjE,CAAC;CACL"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=device.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"device.js","sourceRoot":"","sources":["../../src/daemon/device.ts"],"names":[],"mappings":""}
@@ -0,0 +1,42 @@
1
+ import type { Consumable, ReadableWritablePair } from "@yume-chan/stream-extra";
2
+ import type { AdbIncomingSocketHandler, AdbSocket, Closeable } from "../adb.js";
3
+ import type { AdbPacketData, AdbPacketInit } from "./packet.js";
4
+ import { AdbCommand } from "./packet.js";
5
+ export interface AdbPacketDispatcherOptions {
6
+ calculateChecksum: boolean;
7
+ /**
8
+ * Before Android 9.0, ADB uses `char*` to parse service string,
9
+ * thus requires a null character to terminate.
10
+ *
11
+ * Usually it should have the same value as `calculateChecksum`.
12
+ */
13
+ appendNullToServiceString: boolean;
14
+ maxPayloadSize: number;
15
+ }
16
+ /**
17
+ * The dispatcher is the "dumb" part of the connection handling logic.
18
+ *
19
+ * Except some options to change some minor behaviors,
20
+ * its only job is forwarding packets between authenticated underlying streams
21
+ * and abstracted socket objects.
22
+ *
23
+ * The `Adb` class is responsible for doing the authentication,
24
+ * negotiating the options, and has shortcuts to high-level services.
25
+ */
26
+ export declare class AdbPacketDispatcher implements Closeable {
27
+ #private;
28
+ readonly options: AdbPacketDispatcherOptions;
29
+ get disconnected(): Promise<void>;
30
+ constructor(connection: ReadableWritablePair<AdbPacketData, Consumable<AdbPacketInit>>, options: AdbPacketDispatcherOptions);
31
+ private handleOk;
32
+ private handleClose;
33
+ addReverseTunnel(service: string, handler: AdbIncomingSocketHandler): void;
34
+ removeReverseTunnel(address: string): void;
35
+ clearReverseTunnels(): void;
36
+ private handleOpen;
37
+ createSocket(service: string): Promise<AdbSocket>;
38
+ sendPacket(command: AdbCommand, arg0: number, arg1: number, payload?: string | Uint8Array): Promise<void>;
39
+ close(): Promise<void>;
40
+ private dispose;
41
+ }
42
+ //# sourceMappingURL=dispatcher.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dispatcher.d.ts","sourceRoot":"","sources":["../../src/daemon/dispatcher.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACR,UAAU,EACV,oBAAoB,EAEvB,MAAM,yBAAyB,CAAC;AAQjC,OAAO,KAAK,EAAE,wBAAwB,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAGhF,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAChE,OAAO,EAAE,UAAU,EAAqB,MAAM,aAAa,CAAC;AAG5D,MAAM,WAAW,0BAA0B;IACvC,iBAAiB,EAAE,OAAO,CAAC;IAC3B;;;;;OAKG;IACH,yBAAyB,EAAE,OAAO,CAAC;IACnC,cAAc,EAAE,MAAM,CAAC;CAC1B;AAED;;;;;;;;;GASG;AACH,qBAAa,mBAAoB,YAAW,SAAS;;IAWjD,SAAgB,OAAO,EAAE,0BAA0B,CAAC;IAIpD,IAAW,YAAY,kBAEtB;gBAMG,UAAU,EAAE,oBAAoB,CAC5B,aAAa,EACb,UAAU,CAAC,aAAa,CAAC,CAC5B,EACD,OAAO,EAAE,0BAA0B;IAuEvC,OAAO,CAAC,QAAQ;YAkBF,WAAW;IA6ClB,gBAAgB,CACnB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,wBAAwB;IAK9B,mBAAmB,CAAC,OAAO,EAAE,MAAM;IAInC,mBAAmB;YAIZ,UAAU;IAmCX,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC;IAsBjD,UAAU,CACnB,OAAO,EAAE,UAAU,EACnB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,MAAM,GAAG,UAA8B,GACjD,OAAO,CAAC,IAAI,CAAC;IAqBH,KAAK;IAgBlB,OAAO,CAAC,OAAO;CAOlB"}