dsh-apk-vm 0.8.0 → 0.8.1
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 +18 -0
- package/README.md +2 -1
- package/client.js +163 -1
- package/index.js +69 -0
- package/lib/remote.js +168 -0
- package/package.json +4 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.8.1
|
|
4
|
+
|
|
5
|
+
Remote VM over SSH.
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- **SSH adb tunnel** for a remote Android VM: forward a local loopback port to the remote
|
|
10
|
+
emulator's adb port with `ssh -N -L`, then attach the normal adb device. The key path is
|
|
11
|
+
supplied per connection and never stored.
|
|
12
|
+
- Remote SSH card with host/user/key/remote-port fields, connect/disconnect state, and automatic
|
|
13
|
+
tunnel cleanup when the plugin unloads.
|
|
14
|
+
|
|
15
|
+
### Notes
|
|
16
|
+
|
|
17
|
+
- SSH forwarding requires an SSH server and an adb port reachable from that server
|
|
18
|
+
(for example an emulator started with its console port forwarded).
|
|
19
|
+
- The remote host/user/key are not persisted.
|
|
20
|
+
|
|
3
21
|
## 0.8.0
|
|
4
22
|
|
|
5
23
|
Sidebar workspace, APK debugging, network and regression tools.
|
package/README.md
CHANGED
|
@@ -45,7 +45,7 @@ same-origin, loopback-only debug HTTP API and an `apk_vm` Agent tool.
|
|
|
45
45
|
> 当前 **Windows x64 与 Linux x86_64(headless)为端到端已验证平台**;macOS 仅做探测/CI
|
|
46
46
|
> 覆盖,详见 `docs/LINUX-E2E.md`。
|
|
47
47
|
|
|
48
|
-
**Status:** `0.8.
|
|
48
|
+
**Status:** `0.8.1` · Apache-2.0 · no npm runtime dependencies · no build step.
|
|
49
49
|
|
|
50
50
|
This is an open-source DSH plugin published on npm as **`dsh-apk-vm`**. Repository:
|
|
51
51
|
[github.com/yuxiaole-bili/dsh-apk-vm](https://github.com/yuxiaole-bili/dsh-apk-vm).
|
|
@@ -600,6 +600,7 @@ JSON bodies are bounded to 64 KiB; the raw APK upload is bounded to 500 MiB.
|
|
|
600
600
|
| GET/HEAD | `/apk-vm/apk-icon?path=…` | Extract the raster launcher icon from the APK ZIP (PNG/WebP/JPEG). |
|
|
601
601
|
| GET | `/apk-vm/devices` | List adb devices (`serial`, `state`, `kind`, `model`, `product`, transport) plus current/managed-device metadata. |
|
|
602
602
|
| POST | `/apk-vm/device` | `{"serial":"<adb-serial>"}` attach an online physical/network device; `{"serial":""}` detach and restore the managed AVD. |
|
|
603
|
+
| GET/POST | `/apk-vm/remote` | SSH adb tunnel: `POST {"action":"connect","host","user","keyPath"?,"remotePort":5558}` or `{"action":"disconnect"}`; `GET` returns tunnel status. |
|
|
603
604
|
| POST | `/apk-vm/vm` | `{"action":"start"\|"stop"\|"restart"\|"snapshot"}`. |
|
|
604
605
|
| POST | `/apk-vm/install` | `{"path":"C:/path/app.apk"}` → install + launch. |
|
|
605
606
|
| POST | `/apk-vm/upload` | Raw APK/APKS/XAPK/AAB bytes (≤ 500 MiB) with the file name in the `x-apk-name` header → validate ZIP/APK magic, write to the OS temp dir, install + launch. |
|
package/client.js
CHANGED
|
@@ -30,7 +30,7 @@ window.__ModuleLoader__.load({
|
|
|
30
30
|
var SWIPE_THRESHOLD_PX = 24;
|
|
31
31
|
var FRAME_INTERVAL_MS = 500;
|
|
32
32
|
var STATE_INTERVAL_MS = 2000;
|
|
33
|
-
var CLIENT_VERSION = '0.8.
|
|
33
|
+
var CLIENT_VERSION = '0.8.1';
|
|
34
34
|
|
|
35
35
|
/* ------------------------------------------------------------------ *
|
|
36
36
|
* Optional dsh-better-sidebar integration.
|
|
@@ -323,6 +323,15 @@ window.__ModuleLoader__.load({
|
|
|
323
323
|
'devices.select': 'Select device…',
|
|
324
324
|
'devices.attach': 'Attach',
|
|
325
325
|
'devices.remotePlaceholder': 'host:port',
|
|
326
|
+
'remote.title': 'Remote VM (SSH)',
|
|
327
|
+
'remote.host': 'SSH host',
|
|
328
|
+
'remote.user': 'SSH user',
|
|
329
|
+
'remote.key': 'SSH key path (optional)',
|
|
330
|
+
'remote.remotePort': 'Remote adb port',
|
|
331
|
+
'remote.connect': 'Connect tunnel',
|
|
332
|
+
'remote.disconnect': 'Disconnect',
|
|
333
|
+
'remote.connected': 'SSH tunnel connected',
|
|
334
|
+
'remote.disconnected': 'SSH tunnel closed',
|
|
326
335
|
'devices.detach': 'Detach',
|
|
327
336
|
'devices.attached': 'Attached {serial}',
|
|
328
337
|
'devices.detached': 'Detached external device',
|
|
@@ -603,6 +612,15 @@ window.__ModuleLoader__.load({
|
|
|
603
612
|
'devices.select': '选择设备…',
|
|
604
613
|
'devices.attach': '连接',
|
|
605
614
|
'devices.remotePlaceholder': '主机:端口',
|
|
615
|
+
'remote.title': '远程 VM(SSH)',
|
|
616
|
+
'remote.host': 'SSH 主机',
|
|
617
|
+
'remote.user': 'SSH 用户',
|
|
618
|
+
'remote.key': 'SSH 私钥路径(可选)',
|
|
619
|
+
'remote.remotePort': '远端 adb 端口',
|
|
620
|
+
'remote.connect': '连接隧道',
|
|
621
|
+
'remote.disconnect': '断开',
|
|
622
|
+
'remote.connected': 'SSH 隧道已连接',
|
|
623
|
+
'remote.disconnected': 'SSH 隧道已关闭',
|
|
606
624
|
'devices.detach': '断开',
|
|
607
625
|
'devices.attached': '已连接 {serial}',
|
|
608
626
|
'devices.detached': '已断开外部设备',
|
|
@@ -1369,6 +1387,24 @@ window.__ModuleLoader__.load({
|
|
|
1369
1387
|
var remoteSerialState = React.useState('');
|
|
1370
1388
|
var remoteSerial = remoteSerialState[0];
|
|
1371
1389
|
var setRemoteSerial = remoteSerialState[1];
|
|
1390
|
+
var remoteHostState = React.useState('');
|
|
1391
|
+
var remoteHost = remoteHostState[0];
|
|
1392
|
+
var setRemoteHost = remoteHostState[1];
|
|
1393
|
+
var remoteUserState = React.useState('');
|
|
1394
|
+
var remoteUser = remoteUserState[0];
|
|
1395
|
+
var setRemoteUser = remoteUserState[1];
|
|
1396
|
+
var remoteKeyState = React.useState('');
|
|
1397
|
+
var remoteKey = remoteKeyState[0];
|
|
1398
|
+
var setRemoteKey = remoteKeyState[1];
|
|
1399
|
+
var remotePortState = React.useState('5558');
|
|
1400
|
+
var remotePort = remotePortState[0];
|
|
1401
|
+
var setRemotePort = remotePortState[1];
|
|
1402
|
+
var remoteSshBusyState = React.useState(false);
|
|
1403
|
+
var remoteSshBusy = remoteSshBusyState[0];
|
|
1404
|
+
var setRemoteSshBusy = remoteSshBusyState[1];
|
|
1405
|
+
var remoteSshState = React.useState(null);
|
|
1406
|
+
var remoteSsh = remoteSshState[0];
|
|
1407
|
+
var setRemoteSsh = remoteSshState[1];
|
|
1372
1408
|
var deviceBusyState = React.useState(false);
|
|
1373
1409
|
var deviceBusy = deviceBusyState[0];
|
|
1374
1410
|
var setDeviceBusy = deviceBusyState[1];
|
|
@@ -2977,6 +3013,49 @@ window.__ModuleLoader__.load({
|
|
|
2977
3013
|
});
|
|
2978
3014
|
}
|
|
2979
3015
|
|
|
3016
|
+
function connectRemoteSsh() {
|
|
3017
|
+
if (remoteSshBusy || !remoteHost.trim() || !remoteUser.trim()) return;
|
|
3018
|
+
setRemoteSshBusy(true);
|
|
3019
|
+
requestJson('/apk-vm/remote', {
|
|
3020
|
+
method: 'POST',
|
|
3021
|
+
body: JSON.stringify({
|
|
3022
|
+
action: 'connect',
|
|
3023
|
+
host: remoteHost.trim(),
|
|
3024
|
+
user: remoteUser.trim(),
|
|
3025
|
+
keyPath: remoteKey.trim() || undefined,
|
|
3026
|
+
remotePort: Number(remotePort) || 5558,
|
|
3027
|
+
}),
|
|
3028
|
+
}, T).then(function (result) {
|
|
3029
|
+
if (!mountedRef.current) return;
|
|
3030
|
+
setRemoteSshBusy(false);
|
|
3031
|
+
if (!result.ok) {
|
|
3032
|
+
pushToast('error', (result.error || T('err.action')).slice(0, 500));
|
|
3033
|
+
return;
|
|
3034
|
+
}
|
|
3035
|
+
setRemoteSsh(result.data && result.data.tunnel ? result.data.tunnel : null);
|
|
3036
|
+
pushToast('success', T('remote.connected'));
|
|
3037
|
+
loadState();
|
|
3038
|
+
loadDevices();
|
|
3039
|
+
});
|
|
3040
|
+
}
|
|
3041
|
+
|
|
3042
|
+
function disconnectRemoteSsh() {
|
|
3043
|
+
if (remoteSshBusy) return;
|
|
3044
|
+
setRemoteSshBusy(true);
|
|
3045
|
+
requestJson('/apk-vm/remote', { method: 'POST', body: JSON.stringify({ action: 'disconnect' }) }, T).then(function (result) {
|
|
3046
|
+
if (!mountedRef.current) return;
|
|
3047
|
+
setRemoteSshBusy(false);
|
|
3048
|
+
if (!result.ok) {
|
|
3049
|
+
pushToast('error', (result.error || T('err.action')).slice(0, 500));
|
|
3050
|
+
return;
|
|
3051
|
+
}
|
|
3052
|
+
setRemoteSsh(null);
|
|
3053
|
+
pushToast('success', T('remote.disconnected'));
|
|
3054
|
+
loadState();
|
|
3055
|
+
loadDevices();
|
|
3056
|
+
});
|
|
3057
|
+
}
|
|
3058
|
+
|
|
2980
3059
|
function detachDevice() {
|
|
2981
3060
|
if (deviceBusy) return;
|
|
2982
3061
|
setDeviceBusy(true);
|
|
@@ -5148,6 +5227,88 @@ window.__ModuleLoader__.load({
|
|
|
5148
5227
|
)
|
|
5149
5228
|
);
|
|
5150
5229
|
|
|
5230
|
+
var remoteCard = h(
|
|
5231
|
+
'details',
|
|
5232
|
+
{ className: 'apkvm-card apkvm-debug' },
|
|
5233
|
+
h(
|
|
5234
|
+
'summary',
|
|
5235
|
+
{ className: 'apkvm-debug-summary' },
|
|
5236
|
+
h('span', { className: 'apkvm-card-title' }, T('remote.title')),
|
|
5237
|
+
h('span', { className: 'apkvm-dim', style: { marginLeft: 'auto' } }, remoteSsh ? remoteSsh.state || '' : ''),
|
|
5238
|
+
h('span', { className: 'apkvm-chevron', 'aria-hidden': 'true' }, '▸')
|
|
5239
|
+
),
|
|
5240
|
+
h(
|
|
5241
|
+
'div',
|
|
5242
|
+
{ className: 'apkvm-debug-body' },
|
|
5243
|
+
h('input', {
|
|
5244
|
+
className: 'apkvm-input',
|
|
5245
|
+
type: 'text',
|
|
5246
|
+
value: remoteHost,
|
|
5247
|
+
placeholder: T('remote.host'),
|
|
5248
|
+
spellCheck: false,
|
|
5249
|
+
disabled: remoteSshBusy,
|
|
5250
|
+
onChange: function (event) {
|
|
5251
|
+
setRemoteHost(event && event.target ? String(event.target.value) : '');
|
|
5252
|
+
},
|
|
5253
|
+
}),
|
|
5254
|
+
h('input', {
|
|
5255
|
+
className: 'apkvm-input',
|
|
5256
|
+
type: 'text',
|
|
5257
|
+
value: remoteUser,
|
|
5258
|
+
placeholder: T('remote.user'),
|
|
5259
|
+
spellCheck: false,
|
|
5260
|
+
disabled: remoteSshBusy,
|
|
5261
|
+
onChange: function (event) {
|
|
5262
|
+
setRemoteUser(event && event.target ? String(event.target.value) : '');
|
|
5263
|
+
},
|
|
5264
|
+
}),
|
|
5265
|
+
h('input', {
|
|
5266
|
+
className: 'apkvm-input',
|
|
5267
|
+
type: 'text',
|
|
5268
|
+
value: remoteKey,
|
|
5269
|
+
placeholder: T('remote.key'),
|
|
5270
|
+
spellCheck: false,
|
|
5271
|
+
disabled: remoteSshBusy,
|
|
5272
|
+
onChange: function (event) {
|
|
5273
|
+
setRemoteKey(event && event.target ? String(event.target.value) : '');
|
|
5274
|
+
},
|
|
5275
|
+
}),
|
|
5276
|
+
h('input', {
|
|
5277
|
+
className: 'apkvm-input',
|
|
5278
|
+
type: 'text',
|
|
5279
|
+
value: remotePort,
|
|
5280
|
+
placeholder: T('remote.remotePort'),
|
|
5281
|
+
spellCheck: false,
|
|
5282
|
+
disabled: remoteSshBusy,
|
|
5283
|
+
onChange: function (event) {
|
|
5284
|
+
setRemotePort(event && event.target ? String(event.target.value) : '');
|
|
5285
|
+
},
|
|
5286
|
+
}),
|
|
5287
|
+
h(
|
|
5288
|
+
'div',
|
|
5289
|
+
{ style: { display: 'flex', alignItems: 'center', gap: '5px', flexWrap: 'wrap' } },
|
|
5290
|
+
actionBtn(remoteSshBusy ? '…' : T('remote.connect'), {
|
|
5291
|
+
key: 'remote-connect',
|
|
5292
|
+
small: true,
|
|
5293
|
+
disabled: remoteSshBusy || !remoteHost.trim() || !remoteUser.trim(),
|
|
5294
|
+
onClick: connectRemoteSsh,
|
|
5295
|
+
}),
|
|
5296
|
+
remoteSsh
|
|
5297
|
+
? actionBtn(T('remote.disconnect'), {
|
|
5298
|
+
key: 'remote-disconnect',
|
|
5299
|
+
small: true,
|
|
5300
|
+
variant: 'danger',
|
|
5301
|
+
disabled: remoteSshBusy,
|
|
5302
|
+
onClick: disconnectRemoteSsh,
|
|
5303
|
+
})
|
|
5304
|
+
: null
|
|
5305
|
+
),
|
|
5306
|
+
remoteSsh && remoteSsh.serial
|
|
5307
|
+
? h('div', { className: 'apkvm-dim' }, 'adb ' + remoteSsh.serial)
|
|
5308
|
+
: null
|
|
5309
|
+
)
|
|
5310
|
+
);
|
|
5311
|
+
|
|
5151
5312
|
var primaryAction =
|
|
5152
5313
|
ready || busyState
|
|
5153
5314
|
? actionBtn(vmBusy ? T('action.stopping') : T('action.stop'), {
|
|
@@ -5585,6 +5746,7 @@ window.__ModuleLoader__.load({
|
|
|
5585
5746
|
networkCard,
|
|
5586
5747
|
storageCard,
|
|
5587
5748
|
snapshotCard,
|
|
5749
|
+
remoteCard,
|
|
5588
5750
|
appsCard,
|
|
5589
5751
|
diagCard,
|
|
5590
5752
|
h('div', { className: 'apkvm-footnote', title: T('hint.boot') }, T('hint.short')),
|
package/index.js
CHANGED
|
@@ -32,6 +32,7 @@ import { collectStorage, cleanTempFiles, cleanSnapshots, cleanOrphanAvds, format
|
|
|
32
32
|
import { buildApksFromAab, aabInfo, detectBundletool } from './lib/aab.js';
|
|
33
33
|
import { readHarFile, summarizeHar, filterHar, toCurl } from './lib/har.js';
|
|
34
34
|
import { runAssertions, compareScreenshot, createTestReport, reportMarkdown } from './lib/ui-tests.js';
|
|
35
|
+
import { SshAdbTunnel } from './lib/remote.js';
|
|
35
36
|
|
|
36
37
|
const PLUGIN_VERSION = (() => {
|
|
37
38
|
try {
|
|
@@ -296,6 +297,7 @@ export function apply(ctx, config = {}) {
|
|
|
296
297
|
};
|
|
297
298
|
}
|
|
298
299
|
let installInFlight = null;
|
|
300
|
+
let remoteTunnel = null;
|
|
299
301
|
let startKick = null;
|
|
300
302
|
let autoStartKicked = false;
|
|
301
303
|
const frame = { buffer: null, at: 0, inFlight: null, receivedAt: [] };
|
|
@@ -1787,6 +1789,65 @@ export function apply(ctx, config = {}) {
|
|
|
1787
1789
|
sendJson(res, 200, { ok: true, attached: serial, vm: await vm.status(), devices: await vm.listDevices() });
|
|
1788
1790
|
});
|
|
1789
1791
|
|
|
1792
|
+
register('/apk-vm/remote', async (req, res) => {
|
|
1793
|
+
if (req.method === 'GET') {
|
|
1794
|
+
sendJson(res, 200, { ok: true, tunnel: remoteTunnel ? remoteTunnel.status() : null });
|
|
1795
|
+
return;
|
|
1796
|
+
}
|
|
1797
|
+
if (req.method !== 'POST') {
|
|
1798
|
+
sendError(res, 405, 'GET or POST only');
|
|
1799
|
+
return;
|
|
1800
|
+
}
|
|
1801
|
+
const body = await readJsonBody(req);
|
|
1802
|
+
const action = String(body.action || 'connect').toLowerCase();
|
|
1803
|
+
try {
|
|
1804
|
+
if (action === 'disconnect') {
|
|
1805
|
+
if (remoteTunnel) {
|
|
1806
|
+
await remoteTunnel.stop();
|
|
1807
|
+
remoteTunnel = null;
|
|
1808
|
+
}
|
|
1809
|
+
await stopScrcpy();
|
|
1810
|
+
await vm.detachDevice();
|
|
1811
|
+
frame.buffer = null;
|
|
1812
|
+
frame.at = 0;
|
|
1813
|
+
sendJson(res, 200, { ok: true, detached: true, tunnel: null, vm: await vm.status() });
|
|
1814
|
+
return;
|
|
1815
|
+
}
|
|
1816
|
+
if (action !== 'connect') throw new Error(`unsupported remote action: ${action}`);
|
|
1817
|
+
if (remoteTunnel) {
|
|
1818
|
+
await remoteTunnel.stop();
|
|
1819
|
+
remoteTunnel = null;
|
|
1820
|
+
}
|
|
1821
|
+
const tunnel = new SshAdbTunnel({
|
|
1822
|
+
host: body.host,
|
|
1823
|
+
user: body.user,
|
|
1824
|
+
port: body.sshPort,
|
|
1825
|
+
keyPath: body.keyPath,
|
|
1826
|
+
remotePort: body.remotePort,
|
|
1827
|
+
localPort: body.localPort,
|
|
1828
|
+
sshPath: body.sshPath,
|
|
1829
|
+
});
|
|
1830
|
+
const started = await tunnel.start().catch(async (error) => {
|
|
1831
|
+
await tunnel.stop();
|
|
1832
|
+
throw error;
|
|
1833
|
+
});
|
|
1834
|
+
remoteTunnel = tunnel;
|
|
1835
|
+
try {
|
|
1836
|
+
await stopScrcpy();
|
|
1837
|
+
await vm.attachDevice(started.serial);
|
|
1838
|
+
} catch (error) {
|
|
1839
|
+
await tunnel.stop();
|
|
1840
|
+
remoteTunnel = null;
|
|
1841
|
+
throw error;
|
|
1842
|
+
}
|
|
1843
|
+
frame.buffer = null;
|
|
1844
|
+
frame.at = 0;
|
|
1845
|
+
sendJson(res, 200, { ok: true, tunnel: tunnel.status(), vm: await vm.status() });
|
|
1846
|
+
} catch (error) {
|
|
1847
|
+
sendError(res, 400, error.message);
|
|
1848
|
+
}
|
|
1849
|
+
});
|
|
1850
|
+
|
|
1790
1851
|
register('/apk-vm/vm', async (req, res) => {
|
|
1791
1852
|
if (req.method !== 'POST') {
|
|
1792
1853
|
sendError(res, 405, 'POST only');
|
|
@@ -2522,6 +2583,14 @@ export function apply(ctx, config = {}) {
|
|
|
2522
2583
|
host.effect(() => mountRoutes(host.webServer), 'dsh-apk-vm: http routes');
|
|
2523
2584
|
});
|
|
2524
2585
|
|
|
2586
|
+
ctx.effect(() => {
|
|
2587
|
+
return () => {
|
|
2588
|
+
const tunnel = remoteTunnel;
|
|
2589
|
+
remoteTunnel = null;
|
|
2590
|
+
if (tunnel) void tunnel.stop().catch(() => {});
|
|
2591
|
+
};
|
|
2592
|
+
}, 'dsh-apk-vm: ssh tunnel');
|
|
2593
|
+
|
|
2525
2594
|
// Optional warm start: boot the VM right after the plugin activates so the
|
|
2526
2595
|
// first panel/tab open lands on a running (or nearly booted) device. This is
|
|
2527
2596
|
// what makes the visible "open the VM" step effectively instant; opt in with
|
package/lib/remote.js
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SSH adb tunnel for running the Android VM on a remote Linux host.
|
|
3
|
+
*
|
|
4
|
+
* The tunnel is deliberately generic: it forwards a local loopback port to the
|
|
5
|
+
* remote emulator's adb port (normally 5558) with `ssh -N -L`, then the normal
|
|
6
|
+
* AndroidVm attach path runs `adb connect 127.0.0.1:<localPort>`. Nothing is
|
|
7
|
+
* stored, logged or sent to a third party. The caller owns the key path.
|
|
8
|
+
*/
|
|
9
|
+
import { spawn } from 'node:child_process';
|
|
10
|
+
import { existsSync } from 'node:fs';
|
|
11
|
+
import net from 'node:net';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
|
|
14
|
+
function asPort(value, fallback) {
|
|
15
|
+
const n = Number(value);
|
|
16
|
+
if (!Number.isInteger(n) || n < 1 || n > 65535) return fallback;
|
|
17
|
+
return n;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function safeHost(value) {
|
|
21
|
+
const host = String(value || '').trim();
|
|
22
|
+
if (!/^[A-Za-z0-9._:-]{1,253}$/.test(host)) throw new Error('invalid SSH host');
|
|
23
|
+
return host;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function safeUser(value) {
|
|
27
|
+
const user = String(value || '').trim() || 'root';
|
|
28
|
+
if (!/^[A-Za-z0-9._-]{1,64}$/.test(user)) throw new Error('invalid SSH user');
|
|
29
|
+
return user;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function findFreePort() {
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
const server = net.createServer();
|
|
35
|
+
server.once('error', reject);
|
|
36
|
+
server.listen(0, '127.0.0.1', () => {
|
|
37
|
+
const address = server.address();
|
|
38
|
+
const port = address && typeof address === 'object' ? address.port : 0;
|
|
39
|
+
server.close(() => resolve(port));
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function waitForPort(port, timeoutMs = 20000) {
|
|
45
|
+
const deadline = Date.now() + timeoutMs;
|
|
46
|
+
return new Promise((resolve, reject) => {
|
|
47
|
+
const attempt = () => {
|
|
48
|
+
const socket = net.connect({ host: '127.0.0.1', port });
|
|
49
|
+
socket.once('connect', () => {
|
|
50
|
+
socket.destroy();
|
|
51
|
+
resolve();
|
|
52
|
+
});
|
|
53
|
+
socket.once('error', () => {
|
|
54
|
+
socket.destroy();
|
|
55
|
+
if (Date.now() >= deadline) reject(new Error(`SSH tunnel did not listen on port ${port}`));
|
|
56
|
+
else setTimeout(attempt, 250);
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
attempt();
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export class SshAdbTunnel {
|
|
64
|
+
constructor(options = {}) {
|
|
65
|
+
this.sshHost = safeHost(options.host);
|
|
66
|
+
this.sshUser = safeUser(options.user);
|
|
67
|
+
this.sshPort = asPort(options.sshPort, 22);
|
|
68
|
+
this.remotePort = asPort(options.remotePort, 5558);
|
|
69
|
+
this.localPort = asPort(options.localPort, 0);
|
|
70
|
+
this.keyPath = String(options.keyPath || '').trim() ? path.resolve(String(options.keyPath)) : '';
|
|
71
|
+
if (this.keyPath && !existsSync(this.keyPath)) throw new Error(`SSH key not found: ${this.keyPath}`);
|
|
72
|
+
this.sshPath = String(options.sshPath || 'ssh');
|
|
73
|
+
this.child = null;
|
|
74
|
+
this.state = 'stopped';
|
|
75
|
+
this.lastError = null;
|
|
76
|
+
this.startedAt = 0;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
buildArgs() {
|
|
80
|
+
const args = [
|
|
81
|
+
'-N',
|
|
82
|
+
'-o',
|
|
83
|
+
'BatchMode=yes',
|
|
84
|
+
'-o',
|
|
85
|
+
'ExitOnForwardFailure=yes',
|
|
86
|
+
'-o',
|
|
87
|
+
'ServerAliveInterval=15',
|
|
88
|
+
'-p',
|
|
89
|
+
String(this.sshPort),
|
|
90
|
+
];
|
|
91
|
+
if (this.keyPath) args.push('-i', this.keyPath);
|
|
92
|
+
args.push(
|
|
93
|
+
'-L',
|
|
94
|
+
`127.0.0.1:${this.localPort}:127.0.0.1:${this.remotePort}`,
|
|
95
|
+
`${this.sshUser}@${this.sshHost}`,
|
|
96
|
+
);
|
|
97
|
+
return args;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async start() {
|
|
101
|
+
if (this.state === 'running') return { ok: true, ...this.status() };
|
|
102
|
+
if (this.state === 'starting') throw new Error('SSH tunnel is already starting');
|
|
103
|
+
if (!this.localPort) this.localPort = await findFreePort();
|
|
104
|
+
this.state = 'starting';
|
|
105
|
+
const args = this.buildArgs();
|
|
106
|
+
const child = spawn(this.sshPath, args, { windowsHide: true, stdio: ['ignore', 'ignore', 'pipe'] });
|
|
107
|
+
this.child = child;
|
|
108
|
+
let stderr = '';
|
|
109
|
+
child.stderr.on('data', (chunk) => {
|
|
110
|
+
stderr += chunk.toString('utf8').slice(0, 4000);
|
|
111
|
+
});
|
|
112
|
+
child.once('error', (error) => {
|
|
113
|
+
this.state = 'error';
|
|
114
|
+
this.lastError = error.message;
|
|
115
|
+
});
|
|
116
|
+
child.once('exit', (code) => {
|
|
117
|
+
if (this.state === 'running' || this.state === 'starting') {
|
|
118
|
+
this.state = 'error';
|
|
119
|
+
this.lastError = `ssh exited (${code}): ${stderr.trim().slice(0, 500)}`;
|
|
120
|
+
}
|
|
121
|
+
this.child = null;
|
|
122
|
+
});
|
|
123
|
+
await waitForPort(this.localPort, 20000);
|
|
124
|
+
this.state = 'running';
|
|
125
|
+
this.startedAt = Date.now();
|
|
126
|
+
return { ok: true, ...this.status() };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async stop() {
|
|
130
|
+
const child = this.child;
|
|
131
|
+
this.child = null;
|
|
132
|
+
this.state = 'stopped';
|
|
133
|
+
this.lastError = null;
|
|
134
|
+
if (!child) return { ok: true, ...this.status() };
|
|
135
|
+
try {
|
|
136
|
+
child.kill();
|
|
137
|
+
} catch {
|
|
138
|
+
/* ignore */
|
|
139
|
+
}
|
|
140
|
+
await new Promise((resolve) => {
|
|
141
|
+
const timer = setTimeout(resolve, 1500);
|
|
142
|
+
if (typeof timer.unref === 'function') timer.unref();
|
|
143
|
+
child.once('exit', () => {
|
|
144
|
+
clearTimeout(timer);
|
|
145
|
+
resolve();
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
return { ok: true, ...this.status() };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
status() {
|
|
152
|
+
return {
|
|
153
|
+
sshHost: this.sshHost,
|
|
154
|
+
sshUser: this.sshUser,
|
|
155
|
+
remotePort: this.remotePort,
|
|
156
|
+
localPort: this.localPort,
|
|
157
|
+
serial: this.localPort ? `127.0.0.1:${this.localPort}` : null,
|
|
158
|
+
state: this.state,
|
|
159
|
+
lastError: this.lastError,
|
|
160
|
+
startedAt: this.startedAt,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function buildSshTunnelArgs(options) {
|
|
166
|
+
const tunnel = new SshAdbTunnel({ ...options, localPort: options.localPort || 15037 });
|
|
167
|
+
return tunnel.buildArgs();
|
|
168
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-apk-vm",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Local AOSP Android VM for DeepSeek Harness: preview rooted APKs at up to 60 fps, automate UIAutomator flows, and give each Agent an isolated VM.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -25,9 +25,9 @@
|
|
|
25
25
|
"registry": "https://registry.npmjs.org/"
|
|
26
26
|
},
|
|
27
27
|
"scripts": {
|
|
28
|
-
"check": "node --check index.js && node --check client.js && node --check lib/android.js && node --check lib/apk-bundle.js && node --check lib/flows.js && node --check lib/storage.js && node --check lib/ui-inspector.js && node --check lib/x509.js && node --check lib/scrcpy.js && node --check lib/ws.js && node --check lib/setup.js && node --check lib/instances.js && node --check lib/video-stream.js && node --check lib/aab.js && node --check lib/har.js && node --check lib/ui-tests.js",
|
|
28
|
+
"check": "node --check index.js && node --check client.js && node --check lib/android.js && node --check lib/apk-bundle.js && node --check lib/flows.js && node --check lib/storage.js && node --check lib/ui-inspector.js && node --check lib/x509.js && node --check lib/scrcpy.js && node --check lib/ws.js && node --check lib/setup.js && node --check lib/instances.js && node --check lib/video-stream.js && node --check lib/aab.js && node --check lib/har.js && node --check lib/ui-tests.js && node --check lib/remote.js",
|
|
29
29
|
"privacy": "node scripts/privacy-scan.mjs",
|
|
30
|
-
"test": "node spike/ui-smoke.cjs && node spike/ui-better-sidebar-smoke.cjs && node spike/setup-smoke.mjs && node spike/android/mock-device.mjs && node spike/android/launch-plan.mjs && node spike/instances-smoke.mjs && node spike/features.mjs && node spike/aab-test.mjs && node spike/har-test.mjs && node spike/ui-tests-test.mjs"
|
|
30
|
+
"test": "node spike/ui-smoke.cjs && node spike/ui-better-sidebar-smoke.cjs && node spike/setup-smoke.mjs && node spike/android/mock-device.mjs && node spike/android/launch-plan.mjs && node spike/instances-smoke.mjs && node spike/features.mjs && node spike/aab-test.mjs && node spike/har-test.mjs && node spike/ui-tests-test.mjs && node spike/remote-test.mjs"
|
|
31
31
|
},
|
|
32
32
|
"exports": {
|
|
33
33
|
".": "./index.js",
|
|
@@ -54,6 +54,7 @@
|
|
|
54
54
|
"lib/aab.js",
|
|
55
55
|
"lib/har.js",
|
|
56
56
|
"lib/ui-tests.js",
|
|
57
|
+
"lib/remote.js",
|
|
57
58
|
"lib/vendor/scrcpy-server-v5.0.1.jar",
|
|
58
59
|
"LICENSE",
|
|
59
60
|
"NOTICE",
|