|
|
@@ -1,4 +1,5 @@
|
|
|
借助MQTT实现远程控制Linux设备的SSH服务端和客户端
|
|
|
+作者:niujiuru 日期:2026-01-20
|
|
|
|
|
|
1, 整体使用JSONRPC2.0 OVER MQTT的技术方案来架构
|
|
|
|
|
|
@@ -17,3 +18,130 @@
|
|
|
5,客户端原理:
|
|
|
- 客户端启动时,会连接到MQTT Broker,发布主题:/yfkj/device/rpc/imei/cmd,向服务端发送指令请求
|
|
|
- 客户端启动时,会连接到MQTT Broker,订阅主题:/yfkj/device/rpc/imei/ack,接收服务端的指令结果
|
|
|
+
|
|
|
+6,总体架构图:
|
|
|
+
|
|
|
+┌──────────────────────────────────────────┐
|
|
|
+│ Remote Client │
|
|
|
+│ (Web / App / CLI 运维平台) │
|
|
|
+└───────────────┬──────────────────────────┘
|
|
|
+ │ JSON-RPC 2.0
|
|
|
+ │ - shell.execute
|
|
|
+ │ - shell.interrupt
|
|
|
+ │ - ping
|
|
|
+ v
|
|
|
+┌──────────────────────────────────────────┐
|
|
|
+│ MQTT Broker │
|
|
|
+│ (QoS1 / KeepAlive / Reconnect) │
|
|
|
+└───────────────┬──────────────────────────┘
|
|
|
+ │ MQTT Message
|
|
|
+ │ Topic: /yfkj/device/rpc/{imei}/cmd
|
|
|
+ v
|
|
|
+┌──────────────────────────────────────────┐
|
|
|
+│ sshd / MQTTCoupler │
|
|
|
+│ │
|
|
|
+│ Transport & RPC Adapter Layer │
|
|
|
+│ --------------------------------------- │
|
|
|
+│ - MQTT 连接管理 │
|
|
|
+│ - JSON-RPC 解析 / 校验 │
|
|
|
+│ - 方法分发 (Method Dispatch) │
|
|
|
+│ │
|
|
|
+│ RPC → 本地语义映射 │
|
|
|
+│ --------------------------------------- │
|
|
|
+│ shell.execute → Executor.Exec() │
|
|
|
+│ shell.interrupt → Executor.Interrupt() │
|
|
|
+│ │
|
|
|
+└───────────────┬──────────────────────────┘
|
|
|
+ │ 串行 / 单 Session
|
|
|
+ v
|
|
|
+┌──────────────────────────────────────────┐
|
|
|
+│ shell.Executor │
|
|
|
+│ │
|
|
|
+│ Session State Layer (≈ SSH Session) │
|
|
|
+│ --------------------------------------- │
|
|
|
+│ State: │
|
|
|
+│ - cwd : 当前工作目录 │
|
|
|
+│ - pg : 当前前台进程组 │
|
|
|
+│ │
|
|
|
+│ Built-in Commands │
|
|
|
+│ --------------------------------------- │
|
|
|
+│ - cd │
|
|
|
+│ - pwd │
|
|
|
+│ │
|
|
|
+│ Control Interface │
|
|
|
+│ --------------------------------------- │
|
|
|
+│ Exec() : 启动命令 │
|
|
|
+│ Interrupt() : Ctrl+C (SIGINT) │
|
|
|
+│ │
|
|
|
+└───────────────┬──────────────────────────┘
|
|
|
+ │ 单次命令执行
|
|
|
+ v
|
|
|
+┌──────────────────────────────────────────┐
|
|
|
+│ executeInternal │
|
|
|
+│ │
|
|
|
+│ Process Lifecycle Layer │
|
|
|
+│ --------------------------------------- │
|
|
|
+│ - fork/exec │
|
|
|
+│ - 新进程组 (setpgid) │
|
|
|
+│ - stdout / stderr 限流 │
|
|
|
+│ - 超时控制 (context timeout) │
|
|
|
+│ - SIGTERM → SIGKILL │
|
|
|
+│ │
|
|
|
+└───────────────┬──────────────────────────┘
|
|
|
+ │
|
|
|
+ v
|
|
|
+┌──────────────────────────────────────────┐
|
|
|
+│ Linux Kernel │
|
|
|
+│ │
|
|
|
+│ - Process Group │
|
|
|
+│ - Signal Delivery │
|
|
|
+│ - Exit Status │
|
|
|
+└──────────────────────────────────────────┘
|
|
|
+
|
|
|
+7, 取消设计图:Ctrl+C
|
|
|
+
|
|
|
+Remote Client
|
|
|
+ │
|
|
|
+ │ shell.execute
|
|
|
+ v
|
|
|
+┌───────────────┐
|
|
|
+│ sshd │
|
|
|
+└──────┬────────┘
|
|
|
+ │
|
|
|
+ v
|
|
|
+┌───────────────────────────┐
|
|
|
+│ shell.Executor │
|
|
|
+│ │
|
|
|
+│ Exec(): │
|
|
|
+│ - 设置 p.Dir = cwd │
|
|
|
+│ - 注册前台进程组 pg │
|
|
|
+│ │
|
|
|
+└──────┬────────────────────┘
|
|
|
+ │
|
|
|
+ v
|
|
|
+┌───────────────────────────┐
|
|
|
+│ executeInternal │
|
|
|
+│ │
|
|
|
+│ - Start cmd │
|
|
|
+│ - setpgid │
|
|
|
+│ - onStart(pg) │◄──────┐
|
|
|
+│ │ │
|
|
|
+│ - wait / timeout │ │
|
|
|
+└──────┬────────────────────┘ │
|
|
|
+ │ │
|
|
|
+ v │
|
|
|
+ Command Running │
|
|
|
+ │
|
|
|
+Remote Client Ctrl+C │
|
|
|
+ │ │
|
|
|
+ │ shell.interrupt │
|
|
|
+ v │
|
|
|
+┌───────────────┐ │
|
|
|
+│ sshd │ │
|
|
|
+└──────┬────────┘ │
|
|
|
+ │ │
|
|
|
+ v │
|
|
|
+┌───────────────────────────┐ │
|
|
|
+│ Executor.Interrupt() │ │
|
|
|
+│ → SIGINT to -pgid │───────┘
|
|
|
+└───────────────────────────┘
|