Feature: transfer a Go-managed host to the MFC master from the web console
A Go-managed host cannot receive the client upgrade that only the MFC master can push. Transfer moves its main link there and brings it back after N days; MFC has no reason to return it. Transfer is not share: ASSIGN writes the target and drops the current connection, so the old master sees the host go offline and the new master gets a normal main link it can remote control and upgrade. While a transfer is live the client refuses a second transfer and any share, so no host is handed to a third master that expiry would orphan. Server: admin-only `transfer_device` WS command, COMMAND_ASSIGN_MASTER + SHARE_TYPE_YAMA_FOREVER as `host:port:days::transport\0`, byte-compatible with the MFC dialog. The transport segment is always present (YAMA_TRANSFER_TRANSPORT, default 0/TCP) because clients keep their current link when it is missing: a WSS-managed host would otherwise retry WSS against the MFC master's raw TCP port and never connect. The empty shine segment keeps the host's header encryption. The button only renders when `device_list.transfer` is present, which MFC never sends, so no MFC change is needed. devicehistory keeps transfer_to/transfer_until for the card badge and drops the marker on expiry or on a fresh login here. Client: KernelManager.cpp writes the registry and disconnects; ClientDll.cpp re-reads the registry at the top of every reconnect, so a mid-session transfer lands on the next round. Expiry is checked before every connect attempt as well as inside the heartbeat loop, so an unreachable target cannot pin the host to an expired address until a restart; restoring rebuilds the link captured on entry (address, header encryption, transport). linux/main.cpp applies the transfer on the main loop and restores the captured endpoint at expiry; Android does the same; macOS has neither share nor transfer.
This commit is contained in:
1 parent
b466f568ab
commit
63c7cdf588
18 files changed
+1024
-177
No files matched your search
@@ -32,12 +32,13 @@ const (
|
||||
auditClipGet = "remote.clipboard.get"
|
||||
auditRdpReset = "remote.rdp_reset"
|
||||
|
||||
auditUserCreate = "admin.user.create"
|
||||
auditUserDelete = "admin.user.delete"
|
||||
auditDeviceAdmin = "admin.device.delete"
|
||||
auditDeviceBind = "admin.device.bind"
|
||||
auditDeviceUnbind = "admin.device.unbind"
|
||||
auditQuery = "admin.audit.query"
|
||||
auditUserCreate = "admin.user.create"
|
||||
auditUserDelete = "admin.user.delete"
|
||||
auditDeviceAdmin = "admin.device.delete"
|
||||
auditDeviceTransfer = "admin.device.transfer"
|
||||
auditDeviceBind = "admin.device.bind"
|
||||
auditDeviceUnbind = "admin.device.unbind"
|
||||
auditQuery = "admin.audit.query"
|
||||
)
|
||||
|
||||
// audit emits one audit record for a WebSocket client. Identity fields are
|
||||
|
||||
+22
-1
@@ -40,6 +40,9 @@ type Server struct {
|
||||
trustForwardedFor bool // honor X-Forwarded-For (behind trusted proxy only)
|
||||
basePath string // web UI mount prefix (e.g. "/remoter"); "" = domain root
|
||||
brandName string // white-label title; "" = "SimpleRemoter" (official branding)
|
||||
transferTarget string // pre-filled "transfer host" target, "host:port"
|
||||
transferDays float64 // pre-filled "transfer host" days
|
||||
transferTransport int // link to the target: protocol.TransferTransport*
|
||||
sso SSODetector // nil = SSO disabled
|
||||
mcp mcpEndpoint // nil = no /mcp route
|
||||
sweepDone chan struct{} // closes to stop the login-limiter sweeper
|
||||
@@ -93,6 +96,20 @@ type Config struct {
|
||||
// behaviour: every authenticated session sees every device). The shipped
|
||||
// server always wires one; tests may omit it.
|
||||
DeviceACL *DeviceACL
|
||||
// TransferTarget ("host:port") and TransferDays pre-fill the web UI's
|
||||
// "transfer host" dialog. Reporting them is also what tells the shared
|
||||
// page that this backend implements COMMAND_ASSIGN_MASTER — the MFC server
|
||||
// serves the same page but never reports it, so no transfer button appears
|
||||
// where the command would be dropped. Empty target = the operator types
|
||||
// the address; days <= 0 falls back to 1.
|
||||
//
|
||||
// TransferTransport is the link the transferred host must use to reach the
|
||||
// target (protocol.TransferTransportTCP/WS/WSS; zero value = TCP, what an
|
||||
// MFC master listens on). It is always sent to the client: leaving it out
|
||||
// would make a host that currently talks WSS keep WSS against the target.
|
||||
TransferTarget string
|
||||
TransferDays float64
|
||||
TransferTransport int
|
||||
}
|
||||
|
||||
// New creates an HTTP server bound to the given port. port=0 disables the server.
|
||||
@@ -121,6 +138,9 @@ func (s *Server) WithConfig(cfg Config) *Server {
|
||||
s.brandName = resolveBrandName(cfg.BrandName, s.basePath)
|
||||
s.sso = cfg.SSO
|
||||
s.deviceACL = cfg.DeviceACL
|
||||
s.transferTarget = cfg.TransferTarget
|
||||
s.transferDays = cfg.TransferDays
|
||||
s.transferTransport = cfg.TransferTransport
|
||||
return s
|
||||
}
|
||||
|
||||
@@ -145,7 +165,8 @@ func (s *Server) Start() error {
|
||||
withOriginAllowlist(s.allowedOrigins).
|
||||
withLoginRateLimiters(s.loginIPLimit, s.loginUserLimit).
|
||||
withTrustForwardedFor(s.trustForwardedFor).
|
||||
withDeviceACL(s.deviceACL)
|
||||
withDeviceACL(s.deviceACL).
|
||||
withTransferDefaults(s.transferTarget, s.transferDays, s.transferTransport)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/", s.handleIndex)
|
||||
|
||||
@@ -0,0 +1,278 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/yuanyuanxiang/SimpleRemoter/server/go/connection"
|
||||
"github.com/yuanyuanxiang/SimpleRemoter/server/go/hub"
|
||||
"github.com/yuanyuanxiang/SimpleRemoter/server/go/protocol"
|
||||
)
|
||||
|
||||
// transferReply drains queued messages until the transfer result arrives. The
|
||||
// handler also broadcasts devices_changed to every authenticated client, so
|
||||
// the reply is not necessarily the first message on the channel.
|
||||
func transferReply(t *testing.T, c *wsClient) map[string]any {
|
||||
t.Helper()
|
||||
for i := 0; i < 8; i++ {
|
||||
select {
|
||||
case m := <-c.send:
|
||||
var out map[string]any
|
||||
if err := json.Unmarshal(m.data, &out); err != nil {
|
||||
t.Fatalf("decode reply: %v", err)
|
||||
}
|
||||
if out["cmd"] == "transfer_device_result" {
|
||||
return out
|
||||
}
|
||||
default:
|
||||
t.Fatal("no transfer_device_result reply")
|
||||
}
|
||||
}
|
||||
t.Fatal("transfer_device_result not queued")
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestTransferDeviceSendsAssignMaster(t *testing.T) {
|
||||
wh, h, hist := deleteTestHub(t)
|
||||
var sent []byte
|
||||
h.SetSender(func(_ *connection.Context, data []byte) error {
|
||||
sent = append([]byte(nil), data...)
|
||||
return nil
|
||||
})
|
||||
h.Register(&hub.Device{ID: "live", Name: "Live"},
|
||||
&connection.Context{RemoteAddr: "10.0.0.9:1000"})
|
||||
|
||||
c, tok := deleteTestClient(wh, "admin")
|
||||
raw, _ := json.Marshal(map[string]any{
|
||||
"token": tok, "id": "live", "host": "mfc.example", "port": 6544, "days": 2,
|
||||
})
|
||||
wh.handleTransferDevice(c, raw)
|
||||
|
||||
rep := transferReply(t, c)
|
||||
if rep["ok"] != true {
|
||||
t.Fatalf("reply = %+v, want ok", rep)
|
||||
}
|
||||
if rep["target"] != "mfc.example:6544" {
|
||||
t.Fatalf("target = %v, want mfc.example:6544", rep["target"])
|
||||
}
|
||||
|
||||
// Wire format must match the MFC master byte for byte: command, transfer
|
||||
// type, then host:port:days::transport as a NUL-terminated string. The
|
||||
// empty shine segment keeps the host's header-encryption variant; the
|
||||
// transport segment must always be present (TCP unless configured), or a
|
||||
// host that currently talks WSS would keep WSS against the target.
|
||||
want := string([]byte{protocol.CommandAssignMaster, protocol.ShareTypeYamaForever}) +
|
||||
"mfc.example:6544:2::0\x00"
|
||||
if string(sent) != want {
|
||||
t.Fatalf("sent = %q, want %q", sent, want)
|
||||
}
|
||||
|
||||
// The marker is what the card renders as "transferred → …".
|
||||
target, until, ok := hist.Transfer("live")
|
||||
if !ok || target != "mfc.example:6544" || until <= 0 {
|
||||
t.Fatalf("history marker = %q/%d/%v, want mfc.example:6544 with a deadline", target, until, ok)
|
||||
}
|
||||
}
|
||||
|
||||
// The configured transport must reach the device: the clients keep their
|
||||
// current link when the segment is missing, so a WSS-managed host handed to a
|
||||
// plain-TCP master would retry WSS forever.
|
||||
func TestTransferDeviceSendsConfiguredTransport(t *testing.T) {
|
||||
wh, h, _ := deleteTestHub(t)
|
||||
wh.withTransferDefaults("mfc.example:6544", 2, protocol.TransferTransportWSS)
|
||||
var sent []byte
|
||||
h.SetSender(func(_ *connection.Context, data []byte) error {
|
||||
sent = append([]byte(nil), data...)
|
||||
return nil
|
||||
})
|
||||
h.Register(&hub.Device{ID: "live", Name: "Live"},
|
||||
&connection.Context{RemoteAddr: "10.0.0.14:1000"})
|
||||
|
||||
c, tok := deleteTestClient(wh, "admin")
|
||||
raw, _ := json.Marshal(map[string]any{
|
||||
"token": tok, "id": "live", "host": "mfc.example", "port": 6544, "days": 1,
|
||||
})
|
||||
wh.handleTransferDevice(c, raw)
|
||||
|
||||
if rep := transferReply(t, c); rep["ok"] != true {
|
||||
t.Fatalf("reply = %+v, want ok", rep)
|
||||
}
|
||||
want := string([]byte{protocol.CommandAssignMaster, protocol.ShareTypeYamaForever}) +
|
||||
"mfc.example:6544:1::6\x00"
|
||||
if string(sent) != want {
|
||||
t.Fatalf("sent = %q, want %q", sent, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransferDeviceRequiresAdmin(t *testing.T) {
|
||||
wh, h, hist := deleteTestHub(t)
|
||||
sent := false
|
||||
h.SetSender(func(_ *connection.Context, _ []byte) error {
|
||||
sent = true
|
||||
return nil
|
||||
})
|
||||
h.Register(&hub.Device{ID: "live", Name: "Live"},
|
||||
&connection.Context{RemoteAddr: "10.0.0.10:1000"})
|
||||
|
||||
c, tok := deleteTestClient(wh, "viewer")
|
||||
raw, _ := json.Marshal(map[string]any{
|
||||
"token": tok, "id": "live", "host": "mfc.example", "port": 6544, "days": 1,
|
||||
})
|
||||
wh.handleTransferDevice(c, raw)
|
||||
|
||||
if rep := transferReply(t, c); rep["ok"] != false {
|
||||
t.Fatalf("reply = %+v, want ok:false", rep)
|
||||
}
|
||||
if sent {
|
||||
t.Fatal("viewer's transfer command reached the device")
|
||||
}
|
||||
if _, _, ok := hist.Transfer("live"); ok {
|
||||
t.Fatal("viewer's transfer left a marker")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransferDeviceOffline(t *testing.T) {
|
||||
wh, _, _ := deleteTestHub(t)
|
||||
|
||||
c, tok := deleteTestClient(wh, "admin")
|
||||
raw, _ := json.Marshal(map[string]any{
|
||||
"token": tok, "id": "never-seen", "host": "mfc.example", "port": 6544, "days": 1,
|
||||
})
|
||||
wh.handleTransferDevice(c, raw)
|
||||
|
||||
if rep := transferReply(t, c); rep["ok"] != false || rep["msg"] != "Host is offline" {
|
||||
t.Fatalf("reply = %+v, want offline failure", rep)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransferDeviceRejectsBadTarget(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
in map[string]any
|
||||
}{
|
||||
{"empty host", map[string]any{"host": "", "port": 6544, "days": 1}},
|
||||
{"colon in host", map[string]any{"host": "mfc:6544", "port": 6544, "days": 1}},
|
||||
{"port zero", map[string]any{"host": "mfc.example", "port": 0, "days": 1}},
|
||||
{"port too big", map[string]any{"host": "mfc.example", "port": 70000, "days": 1}},
|
||||
{"zero days", map[string]any{"host": "mfc.example", "port": 6544, "days": 0}},
|
||||
{"too many days", map[string]any{"host": "mfc.example", "port": 6544, "days": 400}},
|
||||
{"no id", map[string]any{"id": "", "host": "mfc.example", "port": 6544, "days": 1}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
wh, h, _ := deleteTestHub(t)
|
||||
sent := false
|
||||
h.SetSender(func(_ *connection.Context, _ []byte) error {
|
||||
sent = true
|
||||
return nil
|
||||
})
|
||||
h.Register(&hub.Device{ID: "live", Name: "Live"},
|
||||
&connection.Context{RemoteAddr: "10.0.0.11:1000"})
|
||||
|
||||
c, tok := deleteTestClient(wh, "admin")
|
||||
args := map[string]any{"token": tok, "id": "live"}
|
||||
for k, v := range tc.in {
|
||||
args[k] = v
|
||||
}
|
||||
raw, _ := json.Marshal(args)
|
||||
wh.handleTransferDevice(c, raw)
|
||||
|
||||
if rep := transferReply(t, c); rep["ok"] != false {
|
||||
t.Fatalf("reply = %+v, want ok:false", rep)
|
||||
}
|
||||
if sent {
|
||||
t.Fatal("invalid target still reached the device")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A second assignment while the first one is live would be ignored by the
|
||||
// Windows client ("Avoid assign again"), so the server must not report success
|
||||
// for it.
|
||||
func TestTransferDeviceRefusesSecondTransfer(t *testing.T) {
|
||||
wh, h, hist := deleteTestHub(t)
|
||||
sends := 0
|
||||
h.SetSender(func(_ *connection.Context, _ []byte) error {
|
||||
sends++
|
||||
return nil
|
||||
})
|
||||
h.Register(&hub.Device{ID: "live", Name: "Live"},
|
||||
&connection.Context{RemoteAddr: "10.0.0.12:1000"})
|
||||
if !hist.SetTransfer("live", "mfc.example:6544", 1<<62) {
|
||||
t.Fatal("SetTransfer on a live device failed")
|
||||
}
|
||||
|
||||
c, tok := deleteTestClient(wh, "admin")
|
||||
raw, _ := json.Marshal(map[string]any{
|
||||
"token": tok, "id": "live", "host": "other.example", "port": 6544, "days": 1,
|
||||
})
|
||||
wh.handleTransferDevice(c, raw)
|
||||
|
||||
rep := transferReply(t, c)
|
||||
if rep["ok"] != false {
|
||||
t.Fatalf("reply = %+v, want ok:false", rep)
|
||||
}
|
||||
if sends != 0 {
|
||||
t.Fatalf("device received %d commands, want 0", sends)
|
||||
}
|
||||
if target, _, _ := hist.Transfer("live"); target != "mfc.example:6544" {
|
||||
t.Fatalf("marker overwritten to %q", target)
|
||||
}
|
||||
}
|
||||
|
||||
// The shared page decides whether to show the transfer button by the presence
|
||||
// of the "transfer" object in device_list, and reads the card marker from
|
||||
// transferTo/transferUntil. Both are part of the contract with index.html.
|
||||
func TestDeviceListReportsTransferContract(t *testing.T) {
|
||||
wh, h, hist := deleteTestHub(t)
|
||||
wh.withTransferDefaults("mfc.example:6544", 2, protocol.TransferTransportTCP)
|
||||
|
||||
h.Register(&hub.Device{ID: "live", Name: "Live"},
|
||||
&connection.Context{RemoteAddr: "10.0.0.13:1000"})
|
||||
if !hist.SetTransfer("live", "mfc.example:6544", 4102444800) { // year 2100
|
||||
t.Fatal("SetTransfer failed")
|
||||
}
|
||||
|
||||
c, tok := deleteTestClient(wh, "admin")
|
||||
raw, _ := json.Marshal(map[string]any{"token": tok})
|
||||
wh.handleGetDevices(c, raw)
|
||||
|
||||
var out struct {
|
||||
Cmd string `json:"cmd"`
|
||||
Transfer *struct {
|
||||
Target string `json:"target"`
|
||||
Days float64 `json:"days"`
|
||||
} `json:"transfer"`
|
||||
Devices []struct {
|
||||
ID string `json:"id"`
|
||||
TransferTo string `json:"transferTo"`
|
||||
TransferUntil int64 `json:"transferUntil"`
|
||||
} `json:"devices"`
|
||||
}
|
||||
for i := 0; i < 8; i++ {
|
||||
select {
|
||||
case m := <-c.send:
|
||||
if err := json.Unmarshal(m.data, &out); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
default:
|
||||
t.Fatal("no device_list reply")
|
||||
}
|
||||
if out.Cmd == "device_list" {
|
||||
break
|
||||
}
|
||||
}
|
||||
if out.Transfer == nil {
|
||||
t.Fatal("device_list carries no transfer config — the page would hide the button")
|
||||
}
|
||||
if out.Transfer.Target != "mfc.example:6544" || out.Transfer.Days != 2 {
|
||||
t.Fatalf("transfer config = %+v", *out.Transfer)
|
||||
}
|
||||
if len(out.Devices) != 1 {
|
||||
t.Fatalf("devices = %d, want 1", len(out.Devices))
|
||||
}
|
||||
if d := out.Devices[0]; d.TransferTo != "mfc.example:6544" || d.TransferUntil == 0 {
|
||||
t.Fatalf("device marker = %+v, want mfc.example:6544 with a deadline", d)
|
||||
}
|
||||
}
|
||||
@@ -181,6 +181,16 @@ type wsHub struct {
|
||||
loginIPLimit *wsauth.RateLimiter
|
||||
loginUserLimit *wsauth.RateLimiter
|
||||
trustForwardedFor bool // honor X-Forwarded-For (only when behind a trusted proxy)
|
||||
|
||||
// transferTarget / transferDays pre-fill the "transfer host" dialog and,
|
||||
// by being reported to the browser, tell the shared page that this backend
|
||||
// implements COMMAND_ASSIGN_MASTER at all (see withTransferDefaults).
|
||||
// transferTransport is the link the transferred host uses to reach the
|
||||
// target (protocol.TransferTransport*); it is sent in the command because
|
||||
// the clients keep their current link when it is missing.
|
||||
transferTarget string
|
||||
transferDays float64
|
||||
transferTransport int
|
||||
}
|
||||
|
||||
func newWSHub(auth *wsauth.Authenticator, devices *hub.Hub, history *devicehistory.Store, log *logger.Logger) *wsHub {
|
||||
@@ -227,6 +237,22 @@ func (h *wsHub) withDeviceACL(acl *DeviceACL) *wsHub {
|
||||
return h
|
||||
}
|
||||
|
||||
// withTransferDefaults installs the pre-filled target/days and the link type
|
||||
// (protocol.TransferTransport*) for the web UI's "transfer host" dialog. An
|
||||
// empty target still enables the feature: the operator types the address in
|
||||
// the dialog. The front-end keys the button's visibility off this being
|
||||
// reported at all, so the shared page shows no transfer button on the MFC
|
||||
// server (which never reports it).
|
||||
func (h *wsHub) withTransferDefaults(target string, days float64, transport int) *wsHub {
|
||||
h.transferTarget = target
|
||||
if days <= 0 {
|
||||
days = 1 // one day is enough for an upgrade round-trip
|
||||
}
|
||||
h.transferDays = days
|
||||
h.transferTransport = transport
|
||||
return h
|
||||
}
|
||||
|
||||
// isSuperAdmin reports whether this session is the built-in admin account
|
||||
// (username "admin" + role admin). Other admin-role users — local accounts
|
||||
// created in the user panel or portal admins bridged by SSO — are device-
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"math"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -90,6 +91,8 @@ func (h *wsHub) dispatch(c *wsClient, cmd string, raw []byte) {
|
||||
// Admin operations (Phase 7).
|
||||
case "delete_device":
|
||||
h.handleDeleteDevice(c, raw)
|
||||
case "transfer_device":
|
||||
h.handleTransferDevice(c, raw)
|
||||
case "create_user":
|
||||
h.handleCreateUser(c, raw)
|
||||
case "delete_user":
|
||||
@@ -790,6 +793,13 @@ func (h *wsHub) handleGetDevices(c *wsClient, raw []byte) {
|
||||
"devices": h.listVisibleDevices(c),
|
||||
"username": c.user,
|
||||
"role": c.role,
|
||||
// Presence of this object is what tells the shared page that this
|
||||
// backend implements "transfer host" (the MFC server omits it), so the
|
||||
// button never appears where the command would go nowhere.
|
||||
"transfer": map[string]any{
|
||||
"target": h.transferTarget,
|
||||
"days": h.transferDays,
|
||||
},
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -799,6 +809,11 @@ func (h *wsHub) handleGetDevices(c *wsClient, raw []byte) {
|
||||
type deviceListItem struct {
|
||||
hub.DeviceInfo
|
||||
Owner string `json:"owner,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.
|
||||
TransferTo string `json:"transferTo,omitempty"`
|
||||
TransferUntil int64 `json:"transferUntil,omitempty"`
|
||||
}
|
||||
|
||||
// listVisibleDevices applies the device ACL to the online + offline snapshot.
|
||||
@@ -815,7 +830,13 @@ func (h *wsHub) listVisibleDevices(c *wsClient) []deviceListItem {
|
||||
if h.acl != nil && c.isSuperAdmin() {
|
||||
owner = h.acl.Owner(d.ID)
|
||||
}
|
||||
out = append(out, deviceListItem{DeviceInfo: d, Owner: owner})
|
||||
item := deviceListItem{DeviceInfo: d, Owner: owner}
|
||||
if h.history != nil {
|
||||
if target, until, ok := h.history.Transfer(d.ID); ok {
|
||||
item.TransferTo, item.TransferUntil = target, until
|
||||
}
|
||||
}
|
||||
out = append(out, item)
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -1314,6 +1335,124 @@ func (h *wsHub) handleDeleteDevice(c *wsClient, raw []byte) {
|
||||
}))
|
||||
}
|
||||
|
||||
// maxTransferDays bounds the dialog input. A handover that outlives a year is
|
||||
// indistinguishable from "lost the host": the client would sit on the other
|
||||
// master long after whatever upgrade it was sent there for.
|
||||
const maxTransferDays = 365
|
||||
|
||||
// handleTransferDevice hands one online host over to another master
|
||||
// (COMMAND_ASSIGN_MASTER + SHARE_TYPE_YAMA_FOREVER) — the path that lets a Go
|
||||
// server's host be upgraded by the MFC master: only a master the host is
|
||||
// actually online on can push a new client build to it.
|
||||
//
|
||||
// The deadline is the client's business. It stored the master it booted
|
||||
// against and reconnects there when the days run out, so nothing here
|
||||
// schedules a restore; the server only keeps a marker so the card can read
|
||||
// "transferred → host:port, N left" instead of silently going offline.
|
||||
//
|
||||
// Admin-only: this moves a host off this master.
|
||||
func (h *wsHub) handleTransferDevice(c *wsClient, raw []byte) {
|
||||
const replyCmd = "transfer_device_result"
|
||||
if !h.requireAdmin(c, raw, replyCmd) {
|
||||
return
|
||||
}
|
||||
var in struct {
|
||||
ID string `json:"id"`
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
Days float64 `json:"days"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &in); err != nil {
|
||||
h.audit(c, true, auditDeviceTransfer, map[string]any{
|
||||
"ok": false,
|
||||
"reason": "bad_payload",
|
||||
})
|
||||
c.queue(mustJSON(map[string]any{
|
||||
"cmd": replyCmd, "ok": false, "msg": "Invalid transfer payload",
|
||||
}))
|
||||
return
|
||||
}
|
||||
in.ID = strings.TrimSpace(in.ID)
|
||||
in.Host = strings.TrimSpace(in.Host)
|
||||
|
||||
// A ':' in the host would shift every following payload field, and the
|
||||
// whole packet has to stay inside the 260-byte buffer both clients parse
|
||||
// (MFC's assignFunction uses bToken[_MAX_PATH]).
|
||||
if in.ID == "" || in.Host == "" || strings.Contains(in.Host, ":") || len(in.Host) > 200 ||
|
||||
in.Port <= 0 || in.Port > 65535 || in.Days <= 0 || in.Days > maxTransferDays {
|
||||
h.audit(c, true, auditDeviceTransfer, map[string]any{
|
||||
"device_id": in.ID,
|
||||
"host": in.Host,
|
||||
"port": in.Port,
|
||||
"days": in.Days,
|
||||
"ok": false,
|
||||
"reason": "invalid_target",
|
||||
})
|
||||
c.queue(mustJSON(map[string]any{
|
||||
"cmd": replyCmd, "ok": false,
|
||||
"msg": "Invalid target address or days (1-365)",
|
||||
}))
|
||||
return
|
||||
}
|
||||
|
||||
// The Windows client ignores a second assignment while the first one is
|
||||
// still live ("Avoid assign again"), so accepting it here would report a
|
||||
// success the host never acted on.
|
||||
target := in.Host + ":" + strconv.Itoa(in.Port)
|
||||
if h.history != nil {
|
||||
if prev, until, active := h.history.Transfer(in.ID); active {
|
||||
h.audit(c, true, auditDeviceTransfer, map[string]any{
|
||||
"device_id": in.ID,
|
||||
"target": prev,
|
||||
"ok": false,
|
||||
"reason": "already_transferred",
|
||||
})
|
||||
c.queue(mustJSON(map[string]any{
|
||||
"cmd": replyCmd, "ok": false,
|
||||
"msg": "Host is already transferred to " + prev + " until it expires",
|
||||
"target": prev, "until": until,
|
||||
}))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err := h.devices.SendToDevice(in.ID, protocol.BuildAssignMasterPacket(in.Host, in.Port, in.Days, h.transferTransport)); err != nil {
|
||||
reason := "send_failed"
|
||||
if errors.Is(err, hub.ErrDeviceOffline) {
|
||||
reason = "offline"
|
||||
}
|
||||
h.audit(c, true, auditDeviceTransfer, map[string]any{
|
||||
"device_id": in.ID,
|
||||
"target": target,
|
||||
"ok": false,
|
||||
"reason": reason,
|
||||
"error": err.Error(),
|
||||
})
|
||||
c.queue(mustJSON(map[string]any{
|
||||
"cmd": replyCmd, "ok": false, "msg": "Host is offline",
|
||||
}))
|
||||
return
|
||||
}
|
||||
|
||||
until := time.Now().Add(time.Duration(in.Days * 24 * float64(time.Hour))).Unix()
|
||||
if h.history != nil {
|
||||
// Display-only: a device whose login event has not landed yet still got
|
||||
// the command, so a missing record must not turn this into a failure.
|
||||
h.history.SetTransfer(in.ID, target, until)
|
||||
}
|
||||
h.audit(c, false, auditDeviceTransfer, map[string]any{
|
||||
"device_id": in.ID,
|
||||
"target": target,
|
||||
"days": in.Days,
|
||||
"until": until,
|
||||
"ok": true,
|
||||
})
|
||||
h.broadcastAuthenticated(`{"cmd":"devices_changed"}`)
|
||||
c.queue(mustJSON(map[string]any{
|
||||
"cmd": replyCmd, "ok": true, "id": in.ID, "target": target, "until": until,
|
||||
}))
|
||||
}
|
||||
|
||||
// handleDeleteUser removes an account and revokes any of its live sessions.
|
||||
// Admin-only. The bootstrap "admin" account is rejected at the wsauth layer.
|
||||
func (h *wsHub) handleDeleteUser(c *wsClient, raw []byte) {
|
||||
|
||||
Reference in new issue
Block a user