Feature: edit device remarks from the web console

The Go web console now stores a per-device remark and renders it as
"hostname (remark)" on the card, the label displayName already
supported. The card's "..." menu gets an Edit remark entry directly
below Terminal; it opens a small dialog, and an empty value clears the
label. Remarks live in device_notes.json next to users.json, independent
of the device history and ACL, so offline records carry them too.

The WebSocket command set_device_remark is admin-only and honors the
device ACL (a private device of another owner is refused). Updates are
audited as admin.device.remark and broadcast as devices_changed so every
open console refreshes. Deleting a device drops its remark. The
device_list reply reports remarkEdit only when a notes table is wired,
so the shared page hides the entry on the MFC server, which does not
implement it.

A malformed or newer-version device_notes.json disables writes instead
of letting the first save overwrite labels it could not parse; loaded
values are re-sanitized and capped. Verified with go test ./..., vet,
gofmt, and a node --check pass on the inline script.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-27 13:22:21 +01:00
1 parent 1bd7fa5b2d
commit 95d2bbc1b6
9 files changed
+741 -7

No files matched your search

+98
View File
@@ -8,6 +8,7 @@ import (
"strconv"
"strings"
"time"
"unicode/utf8"
"github.com/yuanyuanxiang/SimpleRemoter/server/go/hub"
"github.com/yuanyuanxiang/SimpleRemoter/server/go/protocol"
@@ -103,6 +104,8 @@ func (h *wsHub) dispatch(c *wsClient, cmd string, raw []byte) {
h.handleBindDevice(c, raw)
case "unbind_device":
h.handleUnbindDevice(c, raw)
case "set_device_remark":
h.handleSetDeviceRemark(c, raw)
case "get_groups":
h.handleGetGroups(c, raw)
case "query_audit":
@@ -800,6 +803,10 @@ func (h *wsHub) handleGetDevices(c *wsClient, raw []byte) {
"target": h.transferTarget,
"days": h.transferDays,
},
// Same convention for "edit remark": only a backend that wires a
// remark table reports it, so the shared page hides the menu entry
// on the MFC server.
"remarkEdit": h.notes != nil,
}))
}
@@ -809,6 +816,9 @@ func (h *wsHub) handleGetDevices(c *wsClient, raw []byte) {
type deviceListItem struct {
hub.DeviceInfo
Owner string `json:"owner,omitempty"`
// Remark is the operator's label for this host, rendered as
// "hostname (remark)" on the card. Set by the Go web console only.
Remark string `json:"remark,omitempty"`
// TransferTo / TransferUntil carry the "handed over to another master"
// marker so the card can say where the host went and how long it stays
// there. Absent for a host that was never transferred.
@@ -821,6 +831,13 @@ type deviceListItem struct {
// is either public or owned by the caller, so the field adds nothing.
func (h *wsHub) listVisibleDevices(c *wsClient) []deviceListItem {
all := h.listDevicesWithOffline()
// One snapshot instead of a read lock per device: Set holds the write
// lock across the temp-file fsync + rename, and a slow disk must not
// block every console's device list.
var remarks map[string]DeviceNoteEntry
if h.notes != nil {
remarks = h.notes.Snapshot()
}
out := make([]deviceListItem, 0, len(all))
for _, d := range all {
if !h.canAccessDevice(c, d.ID) {
@@ -831,6 +848,9 @@ func (h *wsHub) listVisibleDevices(c *wsClient) []deviceListItem {
owner = h.acl.Owner(d.ID)
}
item := deviceListItem{DeviceInfo: d, Owner: owner}
if remarks != nil {
item.Remark = remarks[d.ID].Note
}
if h.history != nil {
if target, until, ok := h.history.Transfer(d.ID); ok {
item.TransferTo, item.TransferUntil = target, until
@@ -1281,6 +1301,7 @@ func (h *wsHub) handleDeleteDevice(c *wsClient, raw []byte) {
// The device is gone; its binding must not survive a later
// client that happens to reuse the same ID.
h.dropDeviceBinding(in.ID)
h.dropDeviceNote(in.ID)
h.audit(c, false, auditDeviceAdmin, map[string]any{
"device_id": in.ID,
"mode": "uninstall",
@@ -1322,6 +1343,7 @@ func (h *wsHub) handleDeleteDevice(c *wsClient, raw []byte) {
return
}
h.dropDeviceBinding(in.ID)
h.dropDeviceNote(in.ID)
h.audit(c, false, auditDeviceAdmin, map[string]any{
"device_id": in.ID,
"mode": "removed",
@@ -1716,6 +1738,71 @@ func (h *wsHub) handleUnbindDevice(c *wsClient, raw []byte) {
c.queue(mustJSON(map[string]any{"cmd": replyCmd, "ok": true, "count": len(ids)}))
}
// handleSetDeviceRemark stores the free-text label shown on the device card
// ("hostname (remark)"). Admin role only, and only for a device the caller can
// access: the ACL keeps a private device out of other admins' cards, and the
// server enforces the same rule so a hand-crafted request cannot label it
// either. An empty remark clears the label.
func (h *wsHub) handleSetDeviceRemark(c *wsClient, raw []byte) {
const replyCmd = "set_device_remark_result"
if !h.requireAdmin(c, raw, replyCmd) {
return
}
var in struct {
ID string `json:"id"`
Remark string `json:"remark"`
}
id := ""
fail := func(msg, reason string) {
h.audit(c, true, auditDeviceRemark, map[string]any{
"device_id": id,
"ok": false,
"reason": reason,
"error": msg,
})
c.queue(mustJSON(map[string]any{"cmd": replyCmd, "ok": false, "msg": msg}))
}
// An unparsable payload (e.g. a non-string remark) must be rejected, not
// treated as an empty remark: empty clears the label.
if err := json.Unmarshal(raw, &in); err != nil {
fail("Invalid request", "invalid_request")
return
}
id = strings.TrimSpace(in.ID)
if id == "" {
fail("Invalid device id", "invalid_id")
return
}
if h.notes == nil {
fail("Device remarks are not enabled on this server", "disabled")
return
}
if h.denyIfNoDeviceAccess(c, id, replyCmd) {
return
}
// Sanitize before the length check and the reply so the echoed value is
// exactly what the store keeps (control characters folded to spaces).
remark := sanitizeRemark(in.Remark)
if utf8.RuneCountInString(remark) > MaxRemarkRunes {
fail("Remark is too long (max "+strconv.Itoa(MaxRemarkRunes)+" characters)", "too_long")
return
}
if err := h.notes.Set(id, remark, c.user); err != nil {
fail("Failed to save remark: "+err.Error(), "save_failed")
return
}
h.audit(c, false, auditDeviceRemark, map[string]any{
"device_id": id,
"ok": true,
"cleared": remark == "",
})
// Every open console re-reads the list so the card shows the new label.
h.broadcastAuthenticated(`{"cmd":"devices_changed"}`)
c.queue(mustJSON(map[string]any{
"cmd": replyCmd, "ok": true, "id": id, "remark": remark,
}))
}
// normalizeDeviceIDs merges the batch and single-device forms, trims blanks
// and dedupes while preserving order.
func normalizeDeviceIDs(ids []string, single string) []string {
@@ -1814,6 +1901,17 @@ func (h *wsHub) dropDeviceBinding(deviceID string) {
}
}
// dropDeviceNote removes the remark together with the device record, so a
// future client that reuses the same ID starts with a clean card.
func (h *wsHub) dropDeviceNote(deviceID string) {
if h.notes == nil {
return
}
if err := h.notes.Remove(deviceID); err != nil {
h.log.Warn("device remark remove failed for %s: %v", deviceID, err)
}
}
// handleTermOpen kicks off a web terminal session. On success the wsClient
// records `termWatching = deviceID` so subsequent term_input / term_resize
// have a target, and the hub sends COMMAND_SHELL to the device. The