dsh-apk-vm 0.10.2 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/README.md +46 -3
- package/client.js +1 -1
- package/index.js +376 -8
- package/lib/android.js +258 -6
- package/lib/cdp.js +1166 -0
- package/lib/instances.js +10 -2
- package/lib/json-safe.js +39 -0
- package/lib/png-annotate.js +114 -0
- package/lib/video-stream.js +8 -0
- package/package.json +6 -3
package/lib/android.js
CHANGED
|
@@ -472,6 +472,8 @@ export class AndroidVm {
|
|
|
472
472
|
this.networkMode = opts.networkMode === 'shared' ? 'shared' : 'isolated';
|
|
473
473
|
this.tcpdumpPath = opts.tcpdumpPath ? path.resolve(String(opts.tcpdumpPath)) : null;
|
|
474
474
|
this._portForwards = new Map();
|
|
475
|
+
this._reverseForwards = new Map();
|
|
476
|
+
this._envRestoreTimer = null;
|
|
475
477
|
this._external = false;
|
|
476
478
|
this._externalSerial = null;
|
|
477
479
|
this._managedAvdName = this.avdName;
|
|
@@ -1400,29 +1402,69 @@ export class AndroidVm {
|
|
|
1400
1402
|
const serial = this._debugSerial();
|
|
1401
1403
|
const identity = this._identity || (await this._readIdentity());
|
|
1402
1404
|
let size = null;
|
|
1405
|
+
let physicalSize = null;
|
|
1403
1406
|
let density = null;
|
|
1404
1407
|
let uptime = null;
|
|
1408
|
+
let statusBarTop = null;
|
|
1409
|
+
let navigationBarBottom = null;
|
|
1410
|
+
let rotation = null;
|
|
1405
1411
|
try {
|
|
1406
1412
|
const wm = await this._adbText(['-s', serial, 'shell', 'wm', 'size'], { timeoutMs: 10 * 1000 });
|
|
1407
|
-
const
|
|
1408
|
-
|
|
1413
|
+
const overrideMatch = /Override size:\s*(\d+)\s*x\s*(\d+)/i.exec(wm);
|
|
1414
|
+
const physicalMatch = /Physical size:\s*(\d+)\s*x\s*(\d+)/i.exec(wm);
|
|
1415
|
+
if (physicalMatch) {
|
|
1416
|
+
physicalSize = { width: Number(physicalMatch[1]), height: Number(physicalMatch[2]) };
|
|
1417
|
+
}
|
|
1418
|
+
if (overrideMatch) {
|
|
1419
|
+
size = { width: Number(overrideMatch[1]), height: Number(overrideMatch[2]) };
|
|
1420
|
+
} else if (physicalSize) {
|
|
1421
|
+
size = { ...physicalSize };
|
|
1422
|
+
}
|
|
1409
1423
|
const wmDensity = await this._adbText(['-s', serial, 'shell', 'wm', 'density'], { timeoutMs: 10 * 1000 });
|
|
1410
1424
|
const densityMatch = /(\d+)/.exec(wmDensity);
|
|
1411
1425
|
if (densityMatch) density = Number(densityMatch[1]);
|
|
1412
1426
|
uptime = (await this._adbText(['-s', serial, 'shell', 'cat', '/proc/uptime'], { timeoutMs: 10 * 1000 })).trim();
|
|
1427
|
+
// Stable insets are in device pixels. They tell us where the WebView
|
|
1428
|
+
// viewport starts relative to the physical screen (status bar top).
|
|
1429
|
+
const windows = await this._adbText(
|
|
1430
|
+
['-s', serial, 'shell', 'dumpsys', 'window', 'displays'],
|
|
1431
|
+
{ timeoutMs: 15 * 1000 },
|
|
1432
|
+
);
|
|
1433
|
+
const stable = /mStableInsets=Rect\(\s*(-?\d+),\s*(-?\d+)\s*-\s*(-?\d+),\s*(-?\d+)\s*\)/.exec(windows);
|
|
1434
|
+
if (stable) {
|
|
1435
|
+
statusBarTop = Math.max(0, Number(stable[2]));
|
|
1436
|
+
navigationBarBottom = Math.max(0, Number(stable[4]));
|
|
1437
|
+
}
|
|
1438
|
+
const rotationMatch = /mRotation=(\d+)/.exec(windows);
|
|
1439
|
+
if (rotationMatch) rotation = Number(rotationMatch[1]);
|
|
1413
1440
|
} catch {
|
|
1414
1441
|
/* optional extras */
|
|
1415
1442
|
}
|
|
1443
|
+
const dpr = density ? Math.round((density / 160) * 1000) / 1000 : null;
|
|
1416
1444
|
return {
|
|
1417
1445
|
ok: true,
|
|
1418
1446
|
serial,
|
|
1419
1447
|
state: this._state,
|
|
1420
1448
|
root: this._root === true,
|
|
1449
|
+
adbPath: this.adbPath,
|
|
1450
|
+
external: this._external === true,
|
|
1421
1451
|
debugToolkit: this._debugToolkitPath,
|
|
1422
|
-
avatar: undefined,
|
|
1423
1452
|
identity,
|
|
1424
1453
|
size,
|
|
1454
|
+
physicalSize,
|
|
1425
1455
|
density,
|
|
1456
|
+
dpr,
|
|
1457
|
+
statusBarTop,
|
|
1458
|
+
navigationBarBottom,
|
|
1459
|
+
rotation,
|
|
1460
|
+
coordinateMap: {
|
|
1461
|
+
device: size,
|
|
1462
|
+
dpr,
|
|
1463
|
+
offsetX: 0,
|
|
1464
|
+
offsetY: statusBarTop || 0,
|
|
1465
|
+
formula: 'device = round(css * dpr) + offset',
|
|
1466
|
+
note: 'For a WebView selector, prefer tap_selector (CDP) instead of converting coordinates.',
|
|
1467
|
+
},
|
|
1426
1468
|
uptime,
|
|
1427
1469
|
};
|
|
1428
1470
|
}
|
|
@@ -1465,16 +1507,32 @@ export class AndroidVm {
|
|
|
1465
1507
|
return { ok: true, remotePath: remote, localPath: local, bytes: size };
|
|
1466
1508
|
}
|
|
1467
1509
|
|
|
1468
|
-
async reboot(mode = '') {
|
|
1510
|
+
async reboot(mode = '', options = {}) {
|
|
1469
1511
|
const serial = this._debugSerial();
|
|
1512
|
+
const restore = options && options.restoreEnv === false ? null : await this.captureEnv().catch(() => null);
|
|
1470
1513
|
const args = ['-s', serial, 'reboot'];
|
|
1471
1514
|
if (mode) args.push(String(mode));
|
|
1472
1515
|
const result = await this._adb(args, { timeoutMs: 20 * 1000, maxBytes: 64 * 1024 });
|
|
1473
1516
|
if (result.code !== 0 && !result.timedOut) {
|
|
1474
1517
|
throw new Error(`adb reboot failed: ${result.stderr.toString('utf8').slice(0, 300)}`);
|
|
1475
1518
|
}
|
|
1476
|
-
this.
|
|
1477
|
-
|
|
1519
|
+
const restoreScheduled = this._scheduleEnvRestore(restore);
|
|
1520
|
+
this._pushLog(`[vm] adb reboot${mode ? ` ${mode}` : ''} requested${restoreScheduled ? '; environment restore scheduled' : ''}`);
|
|
1521
|
+
return {
|
|
1522
|
+
ok: true,
|
|
1523
|
+
mode: mode || 'system',
|
|
1524
|
+
restoreScheduled,
|
|
1525
|
+
envSnapshot: restore
|
|
1526
|
+
? {
|
|
1527
|
+
networkMode: restore.networkMode,
|
|
1528
|
+
proxy: restore.proxy,
|
|
1529
|
+
dnsServers: restore.dnsServers.length,
|
|
1530
|
+
hostsEntries: restore.hostsEntries.length,
|
|
1531
|
+
forwards: restore.forwards.length,
|
|
1532
|
+
reverses: restore.reverses.length,
|
|
1533
|
+
}
|
|
1534
|
+
: null,
|
|
1535
|
+
};
|
|
1478
1536
|
}
|
|
1479
1537
|
|
|
1480
1538
|
async forceStop(packageName) {
|
|
@@ -1751,6 +1809,161 @@ export class AndroidVm {
|
|
|
1751
1809
|
}));
|
|
1752
1810
|
}
|
|
1753
1811
|
|
|
1812
|
+
async addReverse(localPort, hostPort) {
|
|
1813
|
+
const guest = Number(localPort);
|
|
1814
|
+
const host = Number(hostPort);
|
|
1815
|
+
if (!Number.isInteger(guest) || guest < 1 || guest > 65535) throw new Error('invalid guest localPort');
|
|
1816
|
+
if (!Number.isInteger(host) || host < 1 || host > 65535) throw new Error('invalid hostPort');
|
|
1817
|
+
const serial = await this._requireSerial();
|
|
1818
|
+
if (this._reverseForwards.has(String(guest))) throw new Error(`guest port ${guest} already has a reverse`);
|
|
1819
|
+
const result = await this._adb(
|
|
1820
|
+
['-s', serial, 'reverse', `tcp:${guest}`, `tcp:${host}`],
|
|
1821
|
+
{ timeoutMs: 15000, maxBytes: 64 * 1024 },
|
|
1822
|
+
);
|
|
1823
|
+
if (result.code !== 0 || result.timedOut) {
|
|
1824
|
+
throw new Error(`adb reverse failed: ${result.stderr.toString('utf8').slice(0, 300) || `exit ${result.code}`}`);
|
|
1825
|
+
}
|
|
1826
|
+
const rule = { localPort: guest, hostPort: host };
|
|
1827
|
+
this._reverseForwards.set(String(guest), rule);
|
|
1828
|
+
this._pushLog(`[net] reverse guest 127.0.0.1:${guest} -> host 127.0.0.1:${host}`);
|
|
1829
|
+
return { ok: true, ...rule, guestUrl: `http://127.0.0.1:${guest}`, note: 'adb reverse is per-device and may need re-adding after reboot' };
|
|
1830
|
+
}
|
|
1831
|
+
|
|
1832
|
+
async removeReverse(localPort) {
|
|
1833
|
+
const guest = Number(localPort);
|
|
1834
|
+
if (!Number.isInteger(guest) || guest < 1 || guest > 65535) throw new Error('invalid guest localPort');
|
|
1835
|
+
const serial = await this._requireSerial();
|
|
1836
|
+
await this._adb(['-s', serial, 'reverse', '--remove', `tcp:${guest}`], {
|
|
1837
|
+
timeoutMs: 10000,
|
|
1838
|
+
maxBytes: 64 * 1024,
|
|
1839
|
+
}).catch(() => {});
|
|
1840
|
+
this._reverseForwards.delete(String(guest));
|
|
1841
|
+
this._pushLog(`[net] removed reverse tcp:${guest}`);
|
|
1842
|
+
return { ok: true, localPort: guest };
|
|
1843
|
+
}
|
|
1844
|
+
|
|
1845
|
+
listReverseForwards() {
|
|
1846
|
+
return [...this._reverseForwards.values()].map((rule) => ({
|
|
1847
|
+
...rule,
|
|
1848
|
+
guestUrl: `http://127.0.0.1:${rule.localPort}`,
|
|
1849
|
+
}));
|
|
1850
|
+
}
|
|
1851
|
+
|
|
1852
|
+
/**
|
|
1853
|
+
* Snapshot the parts of the guest environment that adb reboot resets:
|
|
1854
|
+
* in-memory network mode, proxy, DNS intent, hosts entries and forwards.
|
|
1855
|
+
*/
|
|
1856
|
+
async captureEnv() {
|
|
1857
|
+
const proxy = await this.getProxy().catch(() => ({ proxy: null }));
|
|
1858
|
+
return {
|
|
1859
|
+
version: 1,
|
|
1860
|
+
capturedAt: new Date().toISOString(),
|
|
1861
|
+
networkMode: this.networkMode,
|
|
1862
|
+
proxy: proxy && proxy.proxy ? proxy.proxy : null,
|
|
1863
|
+
dnsServers: Array.isArray(this.dnsServers) ? [...this.dnsServers] : [],
|
|
1864
|
+
hostsEntries: Array.isArray(this.hostsEntries) ? [...this.hostsEntries] : [],
|
|
1865
|
+
forwards: this.listPortForwards().map(({ localPort, remotePort, shared }) => ({ localPort, remotePort, shared })),
|
|
1866
|
+
reverses: this.listReverseForwards().map(({ localPort, hostPort }) => ({ localPort, hostPort })),
|
|
1867
|
+
};
|
|
1868
|
+
}
|
|
1869
|
+
|
|
1870
|
+
/** Re-apply a captureEnv() snapshot after the device is back online. */
|
|
1871
|
+
async restoreEnv(snapshot) {
|
|
1872
|
+
if (!snapshot || typeof snapshot !== 'object') throw new Error('restoreEnv requires an environment snapshot');
|
|
1873
|
+
const applied = [];
|
|
1874
|
+
const errors = [];
|
|
1875
|
+
try {
|
|
1876
|
+
await this.ensureRoot().catch(() => {});
|
|
1877
|
+
} catch {
|
|
1878
|
+
/* optional */
|
|
1879
|
+
}
|
|
1880
|
+
if (snapshot.networkMode === 'isolated' || snapshot.networkMode === 'shared') {
|
|
1881
|
+
this.setNetworkMode(snapshot.networkMode);
|
|
1882
|
+
applied.push('networkMode');
|
|
1883
|
+
}
|
|
1884
|
+
if (Array.isArray(snapshot.dnsServers)) {
|
|
1885
|
+
try {
|
|
1886
|
+
await this.setDnsServers(snapshot.dnsServers);
|
|
1887
|
+
applied.push('dnsServers');
|
|
1888
|
+
} catch (error) {
|
|
1889
|
+
errors.push(`dnsServers: ${error.message}`);
|
|
1890
|
+
}
|
|
1891
|
+
}
|
|
1892
|
+
if (Array.isArray(snapshot.hostsEntries)) {
|
|
1893
|
+
try {
|
|
1894
|
+
await this.setHosts(snapshot.hostsEntries);
|
|
1895
|
+
applied.push('hostsEntries');
|
|
1896
|
+
} catch (error) {
|
|
1897
|
+
errors.push(`hostsEntries: ${error.message}`);
|
|
1898
|
+
}
|
|
1899
|
+
}
|
|
1900
|
+
if (snapshot.proxy) {
|
|
1901
|
+
const parsed = /^([^:]+):(\d+)$/.exec(String(snapshot.proxy));
|
|
1902
|
+
if (parsed) {
|
|
1903
|
+
try {
|
|
1904
|
+
await this.setProxy({ enabled: true, host: parsed[1], port: Number(parsed[2]) });
|
|
1905
|
+
applied.push('proxy');
|
|
1906
|
+
} catch (error) {
|
|
1907
|
+
errors.push(`proxy: ${error.message}`);
|
|
1908
|
+
}
|
|
1909
|
+
}
|
|
1910
|
+
}
|
|
1911
|
+
for (const rule of snapshot.forwards || []) {
|
|
1912
|
+
try {
|
|
1913
|
+
await this.addPortForward(rule.localPort, rule.remotePort, { shared: rule.shared === true });
|
|
1914
|
+
applied.push(`forward:${rule.localPort}`);
|
|
1915
|
+
} catch (error) {
|
|
1916
|
+
if (!/already forwarded/.test(error.message)) errors.push(`forward ${rule.localPort}: ${error.message}`);
|
|
1917
|
+
}
|
|
1918
|
+
}
|
|
1919
|
+
for (const rule of snapshot.reverses || []) {
|
|
1920
|
+
try {
|
|
1921
|
+
await this.addReverse(rule.localPort, rule.hostPort);
|
|
1922
|
+
applied.push(`reverse:${rule.localPort}`);
|
|
1923
|
+
} catch (error) {
|
|
1924
|
+
if (!/already has a reverse/.test(error.message)) errors.push(`reverse ${rule.localPort}: ${error.message}`);
|
|
1925
|
+
}
|
|
1926
|
+
}
|
|
1927
|
+
return { ok: errors.length === 0, applied, errors, snapshotAt: snapshot.capturedAt || null };
|
|
1928
|
+
}
|
|
1929
|
+
|
|
1930
|
+
async _waitForBoot(timeoutMs = 120000) {
|
|
1931
|
+
const serial = this._debugSerial();
|
|
1932
|
+
const deadline = Date.now() + Math.max(30000, timeoutMs);
|
|
1933
|
+
while (Date.now() < deadline) {
|
|
1934
|
+
try {
|
|
1935
|
+
const result = await this._adb(
|
|
1936
|
+
['-s', serial, 'shell', 'getprop', 'sys.boot_completed'],
|
|
1937
|
+
{ timeoutMs: 8000, maxBytes: 8 * 1024 },
|
|
1938
|
+
);
|
|
1939
|
+
if (result.code === 0 && String(result.stdout).trim() === '1') return true;
|
|
1940
|
+
} catch {
|
|
1941
|
+
/* booting */
|
|
1942
|
+
}
|
|
1943
|
+
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
1944
|
+
}
|
|
1945
|
+
return false;
|
|
1946
|
+
}
|
|
1947
|
+
|
|
1948
|
+
/** Schedule best-effort environment restoration for the next reboot. */
|
|
1949
|
+
_scheduleEnvRestore(snapshot) {
|
|
1950
|
+
if (!snapshot) return false;
|
|
1951
|
+
if (this._envRestoreTimer) clearTimeout(this._envRestoreTimer);
|
|
1952
|
+
this._envRestoreTimer = setTimeout(async () => {
|
|
1953
|
+
this._envRestoreTimer = null;
|
|
1954
|
+
try {
|
|
1955
|
+
const booted = await this._waitForBoot();
|
|
1956
|
+
if (!booted) throw new Error('device did not report boot_completed');
|
|
1957
|
+
await this.restoreEnv(snapshot);
|
|
1958
|
+
this._pushLog('[vm] environment restored after reboot');
|
|
1959
|
+
} catch (error) {
|
|
1960
|
+
this._pushLog(`[vm] environment restore failed: ${error.message}`);
|
|
1961
|
+
}
|
|
1962
|
+
}, 3000);
|
|
1963
|
+
if (this._envRestoreTimer.unref) this._envRestoreTimer.unref();
|
|
1964
|
+
return true;
|
|
1965
|
+
}
|
|
1966
|
+
|
|
1754
1967
|
async testNetwork(options = {}) {
|
|
1755
1968
|
const serial = await this._requireSerial();
|
|
1756
1969
|
const host = String(options.host || GUEST_HOST_ALIAS).trim();
|
|
@@ -1784,6 +1997,38 @@ export class AndroidVm {
|
|
|
1784
1997
|
return { ok: checks.every((item) => item.ok), checks, host, port: port || null };
|
|
1785
1998
|
}
|
|
1786
1999
|
|
|
2000
|
+
async networkValidation() {
|
|
2001
|
+
const result = {
|
|
2002
|
+
validated: false,
|
|
2003
|
+
internet: false,
|
|
2004
|
+
captivePortalMode: null,
|
|
2005
|
+
activeNetwork: null,
|
|
2006
|
+
hint: null,
|
|
2007
|
+
};
|
|
2008
|
+
try {
|
|
2009
|
+
const serial = this._debugSerial();
|
|
2010
|
+
const text = await this._adbText(
|
|
2011
|
+
['-s', serial, 'shell', 'dumpsys connectivity'],
|
|
2012
|
+
{ timeoutMs: 20000, maxBytes: 512 * 1024 },
|
|
2013
|
+
);
|
|
2014
|
+
result.validated = /\bVALIDATED\b/.test(text);
|
|
2015
|
+
result.internet = /\bINTERNET\b/.test(text);
|
|
2016
|
+
const active = /Active default network:\s*([^\n]+)/.exec(text);
|
|
2017
|
+
if (active) result.activeNetwork = active[1].trim();
|
|
2018
|
+
result.captivePortalMode = (await this._adbText(
|
|
2019
|
+
['-s', serial, 'shell', 'settings', 'get', 'global', 'captive_portal_mode'],
|
|
2020
|
+
{ timeoutMs: 8000, maxBytes: 8 * 1024 },
|
|
2021
|
+
)).trim();
|
|
2022
|
+
if (!result.validated) {
|
|
2023
|
+
result.hint =
|
|
2024
|
+
'Android has not marked the active network VALIDATED. For WebView, start a tiny HTTP 204 server on the host alias, or use networkAction=mode with a gateway that answers the captive-portal probe. adb reverse does not change validation.';
|
|
2025
|
+
}
|
|
2026
|
+
} catch (error) {
|
|
2027
|
+
result.error = error.message;
|
|
2028
|
+
}
|
|
2029
|
+
return result;
|
|
2030
|
+
}
|
|
2031
|
+
|
|
1787
2032
|
async networkInfo() {
|
|
1788
2033
|
const proxy = await this.getProxy().catch(() => ({ proxy: null }));
|
|
1789
2034
|
let guestAddresses = [];
|
|
@@ -1804,6 +2049,8 @@ export class AndroidVm {
|
|
|
1804
2049
|
dnsServers: this.dnsServers,
|
|
1805
2050
|
hostsEntries: this.hostsEntries.length,
|
|
1806
2051
|
forwards: this.listPortForwards(),
|
|
2052
|
+
reverses: this.listReverseForwards(),
|
|
2053
|
+
validation: await this.networkValidation(),
|
|
1807
2054
|
pcap: this.pcapStatus(),
|
|
1808
2055
|
};
|
|
1809
2056
|
}
|
|
@@ -2724,6 +2971,11 @@ export class AndroidVm {
|
|
|
2724
2971
|
return this._run(this.adbPath, args, options);
|
|
2725
2972
|
}
|
|
2726
2973
|
|
|
2974
|
+
/** Public adb passthrough for features (CDP) that need arbitrary adb args. */
|
|
2975
|
+
async adb(args, options = {}) {
|
|
2976
|
+
return this._adb(args, options);
|
|
2977
|
+
}
|
|
2978
|
+
|
|
2727
2979
|
async _adbText(args, options = {}) {
|
|
2728
2980
|
const result = await this._adb(args, options);
|
|
2729
2981
|
if (result.timedOut) {
|