Skip to content

Commit 5449883

Browse files
committed
feat(hnp): 2in1 本地终端 busybox 打包(v5.5.26)
- module.json5 声明 hnpPackages(private electerm-shell.hnp,仅 2in1 安装) - local-terminal.js 多 shell 候选探测,单 shell 在 exec 阶段失败不连累整体 - session-local.js PATH 注入 /system/bin 与 /data/service/hnp/bin - build-web-app.sh 修复 HNP 注入(先解包 UNSIGNED_APP 再注入 hnp/<abi>/*.hnp), 增加 release 下载兜底与 assemble 前 staging - .gitignore 忽略 hnp/、entry/src/main/hnp/、hnp-build/(二进制改由 release 分发) - 版本号 5.5.25 → 5.5.26
1 parent 753bbcf commit 5449883

7 files changed

Lines changed: 198 additions & 30 deletions

File tree

‎.gitignore‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ entry/oh_modules/
6767
local.properties
6868
build/.verify-tmp
6969
/src
70+
# Prebuilt HNP (busybox) for the 2in1 local terminal. Published as a GitHub
71+
# release asset; build-web-app.sh downloads it on demand. Never commit the
72+
# ~4 MB binary. (Keep hnp-build/ scratch for local rebuilds if desired.)
73+
/hnp/
74+
# hvigor staging copy of the .hnp (generated by build-web-app.sh before assemble)
75+
/entry/src/main/hnp/
76+
# local HNP build scratch (rebuilt from HarmonyBrew busybox on demand)
77+
/hnp-build/
7078
.codeartsdoer
7179
.kilo
7280
.workbuddy-ai

‎build-src/replace/app/server/local-terminal.js‎

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,34 @@ function dataDir () {
253253
* fallbacks — and report the path chosen, so a report from a device we cannot
254254
* log into says which binary it settled on.
255255
*/
256-
function resolveShell () {
256+
/**
257+
* Which shells could a session fork, in the order it should try them?
258+
*
259+
* Not a fixed path. A local-terminal bookmark carries the shell as a *bare name*
260+
* — the default one on OHOS is 'bash' — and the addon resolves it through the
261+
* app's PATH the way execvp does. So probe by starting something a session
262+
* could really start — the session's own PATH first (the app's, plus
263+
* /system/bin and the HNP bin dir, which session-local.js appends), then the
264+
* absolute fallbacks — and collect every runnable match, in order.
265+
*
266+
* Unlike the old single-shot resolver this returns the *whole* ordered list, not
267+
* just the first hit: a binary may stat as executable yet fail at spawn time
268+
* (our bundled busybox needs the system musl loader at /lib/ld-musl-aarch64.so.1;
269+
* if that is missing on a given ROM the exec dies with ENOENT even though
270+
* access(X_OK) passed). The probe walks the list and picks the first shell that
271+
* actually forkpty-spawns `exit 0`, so a broken first candidate degrades to the
272+
* next one instead of taking the whole local terminal down.
273+
*/
274+
function resolveShellList () {
257275
const tried = []
276+
const found = []
258277
const dirs = (process.env.PATH || '').split(':').filter(Boolean)
259278
if (!dirs.includes('/system/bin')) {
260279
dirs.push('/system/bin')
261280
}
281+
if (!dirs.includes('/data/service/hnp/bin')) {
282+
dirs.push('/data/service/hnp/bin')
283+
}
262284
const fromPath = ['bash', 'sh'].flatMap(name => dirs.map(dir => join(dir, name)))
263285
const candidates = [
264286
process.env.SHELL,
@@ -275,10 +297,10 @@ function resolveShell () {
275297
tried.push(p)
276298
try {
277299
fs.accessSync(p, fs.constants.X_OK)
278-
return { shell: p, tried }
300+
found.push(p)
279301
} catch (_) {}
280302
}
281-
return { shell: '', tried }
303+
return { shells: found, tried }
282304
}
283305

284306
/**
@@ -346,30 +368,38 @@ async function probe () {
346368
}
347369
// Then the real thing: fork a shell into a pty exactly the way a session does.
348370
// `sh -c 'exit 0'` exits on its own, so nothing depends on kill() working.
371+
// Walk the candidate shells in order and keep the first one that actually
372+
// forkpty-spawns — see resolveShellList(): a binary can pass access(X_OK) yet
373+
// die at exec (e.g. our bundled busybox missing the musl loader), and we must
374+
// not let that single broken candidate disable the local terminal.
349375
const cwd = dataDir()
350-
const found = resolveShell()
351-
if (!found.shell) {
352-
return fail(pty, 'no-shell', `no runnable shell (tried ${found.tried.join(' ')})`)
376+
const { shells, tried } = resolveShellList()
377+
if (shells.length === 0) {
378+
return fail(pty, 'no-shell', `no runnable shell (tried ${tried.join(' ')})`)
353379
}
354-
let term = null
355-
try {
356-
term = pty.spawn(found.shell, ['-c', 'exit 0'], {
357-
name: 'xterm-256color',
358-
cols: 80,
359-
rows: 24,
360-
cwd,
361-
env: { ...process.env, HOME: cwd }
362-
})
363-
} catch (err) {
364-
return fail(pty, 'forkpty', `${describe(err)} (shell=${found.shell})`)
365-
} finally {
366-
if (term) {
367-
try {
368-
term.kill()
369-
} catch (_) {}
380+
let lastErr = null
381+
for (const shell of shells) {
382+
let term = null
383+
try {
384+
term = pty.spawn(shell, ['-c', 'exit 0'], {
385+
name: 'xterm-256color',
386+
cols: 80,
387+
rows: 24,
388+
cwd,
389+
env: { ...process.env, HOME: cwd }
390+
})
391+
return { available: true, reason: 'ok', detail: `openpty + forkpty ok (shell=${shell})` }
392+
} catch (err) {
393+
lastErr = err
394+
} finally {
395+
if (term) {
396+
try {
397+
term.kill()
398+
} catch (_) {}
399+
}
370400
}
371401
}
372-
return { available: true, reason: 'ok', detail: `openpty + forkpty ok (shell=${found.shell})` }
402+
return fail(pty, 'forkpty', `${describe(lastErr)} (tried ${shells.join(' ')})`)
373403
}
374404

375405
/**

‎build-src/replace/app/server/session-local.js‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,23 @@ class TerminalLocal extends TerminalBase {
110110
// Appended (not prepended) so a public HNP's richer binaries in
111111
// `/data/service/hnp/bin` keep priority; ELECTERM_PTY_ADDON is our OHOS
112112
// marker (set in entry/src/main/cpp/node_ctl.c) and the existsSync guard
113-
// keeps this a no-op on every other platform.
114-
if (process.env.ELECTERM_PTY_ADDON && fs.existsSync('/system/bin') &&
115-
!(env.PATH || '').split(':').includes('/system/bin')) {
116-
env.PATH = `${env.PATH || ''}:/system/bin`
113+
// keeps this a no-op on every other platform. `/data/service/hnp/bin` (and a
114+
// private/bundled HNP's `/data/app/bin`) is exactly where the HNP bash lives,
115+
// so it is added explicitly as well — the device may not have resolved the
116+
// HNP symlinks into the inherited PATH by the time the shell starts.
117+
if (process.env.ELECTERM_PTY_ADDON) {
118+
const ohosExtraPath = ['/system/bin', '/data/service/hnp/bin']
119+
const parts = (env.PATH || '').split(':').filter(Boolean)
120+
let changed = false
121+
for (const p of ohosExtraPath) {
122+
if (!parts.includes(p) && fs.existsSync(p)) {
123+
parts.push(p)
124+
changed = true
125+
}
126+
}
127+
if (changed) {
128+
env.PATH = parts.join(':')
129+
}
117130
}
118131
const spawnOptions = {
119132
name: term,

‎entry/src/main/module.json5‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@
1010
"tablet",
1111
"2in1"
1212
],
13+
// ── HNP (HarmonyOS Native Package) ───────────────────────────────────────
14+
// "走 HNP 路线": the local terminal on 2in1 gets a real, self-contained
15+
// busybox (shell + coreutils) instead of relying on whatever the device's
16+
// public HNP happens to provide. The .hnp lives at hnp/arm64-v8a/ and is
17+
// injected into the HAP by scripts/build-web-app.sh (hvigor/DevEco 26 does
18+
// not package HNP, so we do it with app_packing_tool.jar / manual injection).
19+
// HNP is a 2in1-only capability: on phone/tablet the HNP manager is absent
20+
// and the package is simply not installed — the app still installs fine, and
21+
// the local terminal stays hidden there (no PTY anyway). "private" = only
22+
// this app can exec the bundled binaries.
23+
"hnpPackages": [
24+
{
25+
"package": "electerm-shell.hnp",
26+
"type": "private"
27+
}
28+
],
1329
"deliveryWithInstall": true,
1430
"installationFree": false,
1531
"pages": "$profile:main_pages",

‎package-lock.json‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "electerm-harmony",
3-
"version": "5.5.25",
3+
"version": "5.5.26",
44
"description": "Free and open-sourced ssh/sftp/telnet/RDP/VNC/Spice/ftp client for HarmonyOS (ArkWeb + on-device Node.js)",
55
"main": "src/app/app.js",
66
"type": "module",

‎scripts/build-web-app.sh‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ CERT_FILE="${CERT_FILE:-electerm_publish.cer}"
5252
PROFILE_FILE="${PROFILE_FILE:-electermRelease.p7b}"
5353
KEY_ALIAS="${KEY_ALIAS:-electerm_key}"
5454

55+
# Prebuilt HNP (busybox) for the 2in1 local terminal. The ~4 MB binary is
56+
# published as a GitHub release asset rather than committed to the repo, so the
57+
# build fetches it on demand. A local copy under hnp/<abi>/ takes precedence
58+
# (lets you build offline or test a custom build). Override the URL if needed.
59+
HNP_RELEASE_URL="${HNP_RELEASE_URL:-https://github.com/electerm/electerm-harmony/releases/download/hnp-electerm-shell-1.37.0/electerm-shell.hnp}"
60+
HNP_PKG_NAME="${HNP_PKG_NAME:-electerm-shell.hnp}"
61+
5562
# compatibleSdkVersion is deliberately separable from the compile SDK: the two
5663
# answer different questions. compileSdkVersion is "which SDK built this"
5764
# (whatever the local DevEco ships); compatibleSdkVersion is "what is allowed to
@@ -379,6 +386,23 @@ cd "${PROJECT_ROOT}"
379386

380387
echo "==> Building unsigned APP (${BUILD_MODE}) ..."
381388

389+
# --- Stage HNP into the module dir (pre-assemble) ---------------------------
390+
# module.json5 declares hnpPackages, and hvigor may require the .hnp to exist
391+
# under the module's hnp/<abi>/ at assembleApp time. Copy it there now (the file
392+
# is gitignored). If hvigor packages it, the later injection is a harmless
393+
# duplicate; if it ignores HNP, the injection block still adds it. Either way
394+
# the build won't fail on a "missing HNP reference".
395+
MODULE_HNP_DIR="${PROJECT_ROOT}/entry/src/main/hnp/arm64-v8a"
396+
if compgen -G "${HNP_SRC_ROOT}/*/*.hnp" >/dev/null; then
397+
mkdir -p "${MODULE_HNP_DIR}"
398+
for hp in "${HNP_SRC_ROOT}"/*/*.hnp; do
399+
cp "${hp}" "${MODULE_HNP_DIR}/"
400+
done
401+
echo " ✓ Staged HNP into ${MODULE_HNP_DIR} for assembleApp"
402+
else
403+
echo " • No local/downloaded HNP; hvigor HNP reference may be skipped"
404+
fi
405+
382406
if [ "${BUILD_MODE}" = "debug" ]; then
383407
"${HVIGORW}" assembleApp -p product=default \
384408
-p buildMode=debug -p enableSignTask=false --no-daemon
@@ -427,6 +451,72 @@ fi
427451

428452
echo " ✓ Unsigned APP: ${UNSIGNED_APP} ($(du -h "${UNSIGNED_APP}" | cut -f1))"
429453

454+
# --- Inject HNP packages into the unsigned APP -------------------------------
455+
# hvigor / DevEco 26 does not package HNP declared in module.json5, so the .hnp
456+
# files under hnp/<abi>/ are injected manually: for each HAP inside the unsigned
457+
# .app we drop hnp/<abi>/<pkg>.hnp into it (the path the HNP installer expects),
458+
# then re-zip. app_packing_tool.jar --mode hap is the official alternative but it
459+
# demands re-supplying every HAP path (json/resources/ets/index/pack-info/…);
460+
# manual injection is equivalent and far less fragile.
461+
HNP_SRC_ROOT="${PROJECT_ROOT}/hnp"
462+
463+
# Download fallback: the ~4 MB .hnp is gitignored (published as a release asset
464+
# rather than committed), so CI / offline builders may not have it locally. If
465+
# no local .hnp is present, fetch the prebuilt release asset so the injection
466+
# below still runs. Non-fatal: a failed download just skips HNP and the 2in1
467+
# local terminal degrades to system HNP / toybox.
468+
if ! compgen -G "${HNP_SRC_ROOT}/*/*.hnp" >/dev/null; then
469+
echo "==> No local HNP packages; attempting download from release ..."
470+
if command -v curl >/dev/null 2>&1; then
471+
DL_DIR="${HNP_SRC_ROOT}/arm64-v8a"
472+
mkdir -p "${DL_DIR}"
473+
if curl -fsSL "${HNP_RELEASE_URL}" -o "${DL_DIR}/${HNP_PKG_NAME}"; then
474+
echo " ✓ Downloaded ${HNP_PKG_NAME} ($(du -h "${DL_DIR}/${HNP_PKG_NAME}" | cut -f1))"
475+
else
476+
echo " • Download failed (offline?); HNP injection will be skipped"
477+
rm -f "${DL_DIR}/${HNP_PKG_NAME}"
478+
fi
479+
else
480+
echo " • curl unavailable; HNP injection will be skipped"
481+
fi
482+
fi
483+
484+
if [ -d "${HNP_SRC_ROOT}" ] && compgen -G "${HNP_SRC_ROOT}/*/*.hnp" >/dev/null; then
485+
echo "==> Injecting HNP packages into unsigned APP ..."
486+
HNP_TMP=$(mktemp -d)
487+
hnp_injected=0
488+
echo " • Expanding ${UNSIGNED_APP} for HNP injection ..."
489+
unzip -q "${UNSIGNED_APP}" -d "${HNP_TMP}"
490+
while IFS= read -r hap; do
491+
hd=$(dirname "${hap}")
492+
bn=$(basename "${hap}" .hap)
493+
wext="${hd}/${bn}"
494+
rm -rf "${wext}"
495+
unzip -q "${hap}" -d "${wext}"
496+
rm -f "${hap}"
497+
for abi_dir in "${HNP_SRC_ROOT}"/*; do
498+
[ -d "${abi_dir}" ] || continue
499+
abi=$(basename "${abi_dir}")
500+
for hp in "${abi_dir}"/*.hnp; do
501+
[ -f "${hp}" ] || continue
502+
mkdir -p "${wext}/hnp/${abi}"
503+
cp "${hp}" "${wext}/hnp/${abi}/"
504+
hnp_injected=$((hnp_injected + 1))
505+
done
506+
done
507+
( cd "${wext}" && zip -r -q "../${bn}.hap" . )
508+
rm -rf "${wext}"
509+
done < <(find "${HNP_TMP}" -name '*.hap' -type f)
510+
# Re-zip the .app from the patched tree.
511+
mv "${UNSIGNED_APP}" "${UNSIGNED_APP}.prehnp"
512+
( cd "${HNP_TMP}" && zip -r -q "${UNSIGNED_APP}" . )
513+
rm -f "${UNSIGNED_APP}.prehnp"
514+
rm -rf "${HNP_TMP}"
515+
echo " ✓ Injected ${hnp_injected:-0} HNP package(s) into ${UNSIGNED_APP}"
516+
else
517+
echo " • No HNP packages found under ${HNP_SRC_ROOT}, skipping injection"
518+
fi
519+
430520
# --- Sign the APP with hap-sign-tool.jar ------------------------------------
431521

432522
echo "==> Signing APP with hap-sign-tool.jar ..."
@@ -570,6 +660,17 @@ if [ "${JS_COUNT}" = "0" ]; then
570660
ERRORS="${ERRORS}\n ✗ No frontend JS in resfile"
571661
fi
572662

663+
# The bundled HNP (electerm-shell.hnp) powers the 2in1 local terminal. Its
664+
# presence is informational here (private HNP only installs on 2in1), not a hard
665+
# gate — a missing .hnp simply means the local terminal falls back to the system
666+
# HNP / toybox on devices that have one.
667+
HNPS=$(find "${HAP_DIR}" -name '*.hnp' -type f 2>/dev/null)
668+
if [ -n "${HNPS}" ]; then
669+
echo " ✓ HNP packages in HAP: $(printf '%s\n' "${HNPS}" | wc -l | tr -d ' ') file(s)"
670+
else
671+
echo " • (no .hnp in HAP — local terminal will rely on system HNP/toybox if present)"
672+
fi
673+
573674
if [ -n "${ERRORS}" ]; then
574675
echo -e "::error::APP content verification failed:${ERRORS}"
575676
exit 1

0 commit comments

Comments
 (0)