Files
nodeagent/scripts/run-temp.cmd
T
jkooe 4cb8ad566b feat: run-temp.cmd —— 免安装、免管理员的临时启动方案
问题:Windows 端 agent 离线时,control.cmd 依赖已安装的计划任务
(Start-ScheduledTask),未安装就完全起不来;而 install.cmd 需要管理员
权限并写防火墙规则,临时试用太重。

run-temp.cmd 直接 node.exe agent.mjs —— agent 本身零配置,
首次运行会自动生成 %USERPROFILE%\.nodeagent\agent.json(随机 PSK +
自签证书)并开始监听,故无需任何前置步骤。

用法:
  run-temp.cmd            前台启动(Ctrl+C 停)
  run-temp.cmd background 最小化后台启动
  run-temp.cmd status     查看进程 / 监听 / 配置与密钥
  run-temp.cmd stop       结束

实现要点:
- agent **不解析命令行参数**(argv 出现 0 次),端口只来自 agent.json
  → 脚本不再传 --port(传了也会被忽略,属误导),脚本内 PORT 仅用于状态检查
- CMD 批处理规避:start "title" 后必须套 cmd /c ""..."" 才不吞标题;
  子程序用 goto 而非 call(call :LABEL 会在末尾 exit /b 时连带退出)
- 进程检测用 Win32_Process 查 CommandLine,而非 Get-Process.Path
  (后者在非管理员下拿不到 Path 属性)

pack.mjs 同步:
- 把 run-temp.cmd 纳入文件清单
- 新增 CRLF 守卫(此前只查非 ASCII)→ LF-only 的 .cmd 在部分
  Windows 环境会解析异常
- 两个守卫均已注入故障实测会红(LF-only 94 处 / 非 ASCII 偏移 1677)
- 包内 README 增补临时试用说明
2026-10-09 23:12:14 +08:00

95 lines
4.3 KiB
Batchfile

@echo off
REM ============================================================================
REM nodeagent - TEMPORARY launcher (no install, no admin, no scheduled task)
REM
REM Purpose: bring the agent up for a quick test WITHOUT running install.cmd.
REM The agent is zero-config: on first run it creates %USERPROFILE%\.nodeagent\
REM (agent.json with a random PSK + self-signed cert) and starts listening.
REM
REM Usage:
REM run-temp.cmd start in foreground (Ctrl+C to stop)
REM run-temp.cmd start same as above
REM run-temp.cmd background start hidden, then print status
REM run-temp.cmd stop stop the background instance
REM run-temp.cmd status show whether it is running
REM
REM IMPORTANT: the agent takes NO command-line arguments (it does not parse
REM argv at all). The listen port comes from %USERPROFILE%\.nodeagent\
REM agent.json -- the "<port>" below is used ONLY by this script's own status
REM check. To change the port, edit agent.json, not the command line.
REM
REM Notes:
REM * Pure ASCII on purpose (see install.cmd) so it survives the CMD code page.
REM * No admin needed. Nothing is written outside this folder and %USERPROFILE%.
REM * "background" still dies with the parent CMD window; use it for a test
REM session, not as a permanent daemon. For that, run install.cmd once.
REM ============================================================================
setlocal EnableExtensions
set "HERE=%~dp0"
set "NODE=%HERE%node.exe"
set "AGENT=%HERE%agent.mjs"
set "PORT=%~2"
if not defined PORT set "PORT=8765"
if not exist "%NODE%" (
echo [x] node.exe not found in "%HERE%"
exit /b 1
)
if not exist "%AGENT%" (
echo [x] agent.mjs not found in "%HERE%"
exit /b 1
)
if /i "%~1"=="stop" goto :STOP
if /i "%~1"=="status" goto :STATUS
if /i "%~1"=="background" goto :BACKGROUND
goto :FOREGROUND
REM ---------------------------------------------------------------------------
:FOREGROUND
echo ==========================================================
echo nodeagent (temporary, foreground)
echo ==========================================================
echo folder : "%HERE%"
echo port : %PORT% (for this status check only; agent reads agent.json)
echo stop : Ctrl+C
echo.
"%NODE%" "%AGENT%"
echo.
echo [i] agent exited (code %errorlevel%)
pause
exit /b 0
REM ---------------------------------------------------------------------------
:BACKGROUND
echo [*] starting agent in background...
start "nodeagent-temp" /min cmd /c ""%NODE%" "%AGENT%""
rem give it a moment to bind the port and generate cert/config
timeout /t 3 /nobreak >nul
goto :STATUS
REM ---------------------------------------------------------------------------
:STATUS
echo [*] checking status...
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
"$ErrorActionPreference='SilentlyContinue';" ^
"$p = Get-CimInstance Win32_Process -Filter \"Name='node.exe'\" -ErrorAction SilentlyContinue | Where-Object { $_.CommandLine -like '*agent.mjs*' };" ^
"if ($p) { Write-Host (' running : PID ' + (($p | ForEach-Object { $_.ProcessId }) -join ', ')) } else { Write-Host ' running : no' };" ^
"$l = Get-NetTCPConnection -LocalPort %PORT% -State Listen -ErrorAction SilentlyContinue;" ^
"if ($l) { Write-Host (' listening: port %PORT%') } else { Write-Host ' listening: no' };" ^
"$cfg = Join-Path $env:USERPROFILE '.nodeagent\agent.json';" ^
"if (Test-Path $cfg) { Write-Host (' config : ' + $cfg); try { (Get-Content $cfg -Raw | ConvertFrom-Json) | ForEach-Object { Write-Host (' node_id : ' + $_.node_id); Write-Host (' key : ' + $_.key) } } catch {} } else { Write-Host ' config : (not yet created)' };"
echo.
echo If the connect line above is empty, run: run-temp.cmd status
pause
exit /b 0
REM ---------------------------------------------------------------------------
:STOP
echo [*] stopping temporary agent...
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
"$ErrorActionPreference='SilentlyContinue';" ^
"Get-CimInstance Win32_Process -Filter \"Name='node.exe'\" | Where-Object { $_.CommandLine -like '*agent.mjs*' } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force; Write-Host (' killed PID ' + $_.ProcessId) };"
echo [i] done
exit /b 0