fix: 已信任设备可以拉取本机命令记录

This commit is contained in:
Nixevol
2026-10-12 07:12:29 +08:00
parent 33d8ec7861
commit fc27c58083
4 changed files with 65 additions and 0 deletions
@@ -105,6 +105,7 @@ class AgentRuntime(
val password = prefs.nixPassword val password = prefs.nixPassword
io.execute { runCatching { api.putLogs(endpoint, password, latest) } } io.execute { runCatching { api.putLogs(endpoint, password, latest) } }
}, },
localLogs = { db.logs() },
) )
fun start() { fun start() {
@@ -15,6 +15,7 @@ class Engine(
private val onText: (from: String, text: String) -> Unit, private val onText: (from: String, text: String) -> Unit,
private val onShortcuts: (from: String, items: List<Shortcut>) -> Unit, private val onShortcuts: (from: String, items: List<Shortcut>) -> Unit,
private val onLog: (LogEntry) -> Unit, private val onLog: (LogEntry) -> Unit,
private val localLogs: () -> List<LogEntry>,
) { ) {
/** 需要回复时返回正文,否则返回 null。 */ /** 需要回复时返回正文,否则返回 null。 */
fun handle(from: String, raw: String): String? { fun handle(from: String, raw: String): String? {
@@ -29,6 +30,7 @@ class Engine(
null null
} }
"shortcuts" -> onShortcutMessage(from, msg) "shortcuts" -> onShortcutMessage(from, msg)
"logs" -> onLogsMessage(from, msg)
else -> null else -> null
} }
} }
@@ -104,4 +106,15 @@ class Engine(
} }
return Protocol.shortcuts(allPublished()) return Protocol.shortcuts(allPublished())
} }
private fun onLogsMessage(from: String, msg: Message): String? {
val items = msg.logItems
if (items != null) return null
val id = msg.id
if (!Protocol.validId(id)) return null
if (!trusted(from)) {
return Protocol.result(id!!, false, "not_trusted", duplicate = false)
}
return Protocol.logs(localLogs())
}
} }
@@ -18,6 +18,7 @@ data class Message(
val shortcutId: String?, val shortcutId: String?,
val text: String?, val text: String?,
val items: List<Shortcut>?, val items: List<Shortcut>?,
val logItems: List<LogEntry>?,
val ok: Boolean?, val ok: Boolean?,
val error: String?, val error: String?,
val duplicate: Boolean, val duplicate: Boolean,
@@ -53,6 +54,21 @@ object Protocol {
} else { } else {
null null
} }
val logItems = if (o.string("type") == "logs" && o.has("items") && o.get("items").isJsonArray) {
o.getAsJsonArray("items").mapNotNull { el ->
if (!el.isJsonObject) return@mapNotNull null
val it = el.asJsonObject
val summary = it.string("summary") ?: return@mapNotNull null
LogEntry(
atMs = it.long("at_ms").takeIf { n -> n > 0 } ?: it.string("at")?.toLongOrNull() ?: 0L,
summary = summary,
ok = it.bool("ok"),
detail = it.string("detail") ?: it.string("result").orEmpty(),
)
}
} else {
null
}
return Message( return Message(
v = o.int("v"), v = o.int("v"),
type = o.string("type").orEmpty(), type = o.string("type").orEmpty(),
@@ -61,6 +77,7 @@ object Protocol {
shortcutId = o.string("shortcut_id"), shortcutId = o.string("shortcut_id"),
text = o.string("text"), text = o.string("text"),
items = items, items = items,
logItems = logItems,
ok = if (o.has("ok") && !o.get("ok").isJsonNull) o.get("ok").asBoolean else null, ok = if (o.has("ok") && !o.get("ok").isJsonNull) o.get("ok").asBoolean else null,
error = o.string("error"), error = o.string("error"),
duplicate = o.bool("duplicate"), duplicate = o.bool("duplicate"),
@@ -105,6 +122,23 @@ object Protocol {
} }
} }
fun logs(items: List<LogEntry>): String {
val arr = JsonArray()
for (item in items) {
val it = JsonObject()
it.addProperty("at", item.atMs.toString())
it.addProperty("summary", item.summary)
it.addProperty("ok", item.ok)
it.addProperty("detail", item.detail)
arr.add(it)
}
return obj {
addProperty("v", 1)
addProperty("type", "logs")
add("items", arr)
}
}
fun shortcutsQuery(id: String): String = obj { fun shortcutsQuery(id: String): String = obj {
addProperty("v", 1) addProperty("v", 1)
addProperty("type", "shortcuts") addProperty("type", "shortcuts")
@@ -143,4 +177,10 @@ object Protocol {
val el = get(key) val el = get(key)
return if (el.asJsonPrimitive.isNumber) el.asInt else 0 return if (el.asJsonPrimitive.isNumber) el.asInt else 0
} }
private fun JsonObject.long(key: String): Long {
if (!has(key) || get(key).isJsonNull || !get(key).isJsonPrimitive) return 0L
val prim = get(key).asJsonPrimitive
return if (prim.isNumber) prim.asLong else prim.asString.toLongOrNull() ?: 0L
}
} }
@@ -58,9 +58,20 @@ class CoreTest {
onText = { from, text -> texts.add(from to text) }, onText = { from, text -> texts.add(from to text) },
onShortcuts = { from, items -> synced.add(from to items) }, onShortcuts = { from, items -> synced.add(from to items) },
onLog = { logs.add(it) }, onLog = { logs.add(it) },
localLogs = { logs.toList() },
) )
} }
@Test
fun trustedPeerCanReadLocalLogs() {
val h = Harness(setOf("pc"))
h.engine.handle("pc", Protocol.shell(Protocol.newId(), "echo hi"))
val raw = """{"v":1,"type":"logs","id":"${Protocol.newId()}"}"""
val reply = Protocol.parse(h.engine.handle("pc", raw)!!)
assertEquals("logs", reply?.type)
assertEquals(1, reply?.logItems?.size)
}
@Test @Test
fun untrustedShellDoesNotRun() { fun untrustedShellDoesNotRun() {
val h = Harness() val h = Harness()