fix(webui): run SSH probes on Windows hosts with python
This commit is contained in:
1 parent
307b3bce82
commit
b18ca98aff
14 files changed
+110
-54
No files matched your search
+46
-31
@@ -12,7 +12,6 @@ import contextlib
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import shlex
|
||||
import shutil
|
||||
import socket
|
||||
from pathlib import Path
|
||||
@@ -63,14 +62,14 @@ def transport_arguments(profile: RemoteProfile, remote_port: int, known_hosts: P
|
||||
# Frame the result so login banners don't get mistaken for invalid config JSON.
|
||||
_PROBE_PREFIX = b"\x1eNANOBOT_REMOTE:"
|
||||
|
||||
# Fixed program, quoted as one shell argument. No user-supplied shell commands.
|
||||
# Fixed program sent over SSH stdin, not interpreted by the remote shell.
|
||||
_PROBE = r'''
|
||||
import json, os, pathlib, socket, sys
|
||||
import json, pathlib, socket, sys
|
||||
def emit(value):
|
||||
print("\n\x1eNANOBOT_REMOTE:" + json.dumps(value))
|
||||
try:
|
||||
path = pathlib.Path(sys.argv[1]).expanduser()
|
||||
data = json.loads(path.read_text())
|
||||
data = json.loads(path.read_text(encoding="utf-8"))
|
||||
ws = data.get("channels", {}).get("websocket", {})
|
||||
secret = ws.get("tokenIssueSecret") or ws.get("token_issue_secret") or ws.get("token")
|
||||
if not ws.get("enabled"):
|
||||
@@ -141,36 +140,52 @@ async def run_check(
|
||||
profile: RemoteProfile, program: str, known_hosts: Path | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Run a fixed read-only program with SSH's existing identity and trust policy."""
|
||||
remote = ["python3", "-c", program, profile.config_path]
|
||||
if profile.runtime_user:
|
||||
remote = ["sudo", "-n", "-H", "-u", profile.runtime_user, "--", *remote]
|
||||
process = await asyncio.create_subprocess_exec(
|
||||
*ssh_arguments(profile, known_hosts), profile.host, shlex.join(remote),
|
||||
stdin=asyncio.subprocess.DEVNULL, stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
args = ssh_arguments(profile, known_hosts)
|
||||
sudo = f"sudo -n -H -u {profile.runtime_user} -- " if profile.runtime_user else ""
|
||||
# A Python string literal keeps spaces, Unicode and shell metacharacters
|
||||
# in the path out of the command line on both cmd.exe and Unix shells.
|
||||
script = f"import sys; sys.argv.append({profile.config_path!a})\n{program}".encode("utf-8")
|
||||
try:
|
||||
stdout, stderr = await asyncio.wait_for(process.communicate(), 20)
|
||||
if process.returncode:
|
||||
raise RemoteError(ssh_error(stderr))
|
||||
try:
|
||||
records = [line[len(_PROBE_PREFIX):] for line in stdout.split(b"\n")
|
||||
if line.startswith(_PROBE_PREFIX)]
|
||||
if len(records) != 1:
|
||||
raise ValueError
|
||||
raw = json.loads(records[0])
|
||||
if not isinstance(raw, dict):
|
||||
raise ValueError
|
||||
data = cast(dict[str, Any], raw)
|
||||
if data.get("error"):
|
||||
raise RemoteError(str(data["error"]))
|
||||
return data
|
||||
except (ValueError, TypeError):
|
||||
raise RemoteError("probe_failed") from None
|
||||
async with asyncio.timeout(20):
|
||||
for executable in ("python3", "python"):
|
||||
process = await asyncio.create_subprocess_exec(
|
||||
*args, profile.host, f"{sudo}{executable} -",
|
||||
stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
try:
|
||||
stdout, stderr = await process.communicate(script)
|
||||
finally:
|
||||
await stop_process(process)
|
||||
if process.returncode == 0:
|
||||
break
|
||||
# Missing command: POSIX 127, cmd.exe 9009 (truncated to 49
|
||||
# by POSIX SSH clients), or PowerShell's exception marker.
|
||||
if not sudo and (process.returncode in {49, 127, 9009} or (
|
||||
process.returncode == 1 and b"CommandNotFoundException" in stderr
|
||||
)):
|
||||
continue
|
||||
error = ssh_error(stderr)
|
||||
raise RemoteError("probe_failed" if error == "ssh_unreachable" and process.returncode != 255 else error)
|
||||
else:
|
||||
raise RemoteError("python_unavailable")
|
||||
except TimeoutError:
|
||||
raise RemoteError("ssh_unreachable") from None
|
||||
finally:
|
||||
await stop_process(process)
|
||||
|
||||
try:
|
||||
records = [line[len(_PROBE_PREFIX):] for line in stdout.split(b"\n")
|
||||
if line.startswith(_PROBE_PREFIX)]
|
||||
if len(records) != 1:
|
||||
raise ValueError
|
||||
raw = json.loads(records[0])
|
||||
if not isinstance(raw, dict):
|
||||
raise ValueError
|
||||
data = cast(dict[str, Any], raw)
|
||||
if data.get("error"):
|
||||
raise RemoteError(str(data["error"]))
|
||||
return data
|
||||
except (ValueError, TypeError):
|
||||
raise RemoteError("probe_failed") from None
|
||||
|
||||
|
||||
async def probe(profile: RemoteProfile, known_hosts: Path | None = None) -> dict[str, Any]:
|
||||
|
||||
Reference in new issue
Block a user