feat(routers): integrate Banana Pi Client API and onboard LibreSpeed measurements
Discover Ethernet routers under the board, map physical SIM slots, expose vendor controls and real telemetry, and provide packaged VPS speed tests with a live monochrome gauge. Enforce compact typography across Core and Node UI.
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
package routers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"slices"
|
||||
"time"
|
||||
)
|
||||
|
||||
const evidenceChunkSize = 32768
|
||||
const evidenceMaxSize = 2 * 1024 * 1024
|
||||
const evidenceRetention = 20 * time.Minute
|
||||
|
||||
type EvidenceQuery struct {
|
||||
Resource string `json:"resource"`
|
||||
ID string `json:"id"`
|
||||
After string `json:"after"`
|
||||
Transfer string `json:"transfer"`
|
||||
Offset int `json:"offset"`
|
||||
}
|
||||
type EvidenceChunk struct {
|
||||
OperationID string `json:"operation_id"`
|
||||
DeviceID string `json:"device_id"`
|
||||
BootID string `json:"boot_id"`
|
||||
Resource string `json:"resource"`
|
||||
ID string `json:"id"`
|
||||
Transfer string `json:"transfer"`
|
||||
Offset int `json:"offset"`
|
||||
Total int `json:"total"`
|
||||
At int64 `json:"at"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
type evidenceCache struct {
|
||||
DeviceID, BootID, Resource, ID, After, Transfer string
|
||||
Data []byte
|
||||
At time.Time
|
||||
}
|
||||
|
||||
func validEvidence(cmd Command) bool {
|
||||
q := cmd.Query
|
||||
if q == nil || cmd.Action != "inspect" || cmd.Settings != nil || cmd.ClientAPI != nil || cmd.Username != "" || cmd.Password != "" || cmd.ModelID != "" || cmd.Policy != nil || cmd.Recovery != "" || cmd.SIM != "" || cmd.Mode != "" || len(cmd.Slots) != 0 || cmd.ExpectedRevision != "" || cmd.ExpectedBootID == "" || len(cmd.ExpectedBootID) > 128 {
|
||||
return false
|
||||
}
|
||||
if !slices.Contains([]string{"diagnostics_latest", "diagnostic", "bundle", "events", "recovery_history", "recordings", "recording", "targets"}, q.Resource) {
|
||||
return false
|
||||
}
|
||||
limit := 0
|
||||
if q.Resource == "events" || q.Resource == "recovery_history" {
|
||||
limit = 50
|
||||
}
|
||||
if _, err := clientAPIReadPath(q.Resource, q.ID, q.After, limit); err != nil {
|
||||
return false
|
||||
}
|
||||
if q.Transfer == "" {
|
||||
return q.Offset == 0
|
||||
}
|
||||
if len(q.Transfer) != 64 || q.Offset < 0 || q.Offset >= evidenceMaxSize || q.Offset%evidenceChunkSize != 0 {
|
||||
return false
|
||||
}
|
||||
_, err := hex.DecodeString(q.Transfer)
|
||||
return err == nil
|
||||
}
|
||||
func (c *Collector) readEvidence(ctx context.Context, cmd Command) {
|
||||
q := cmd.Query
|
||||
reason := "router_unavailable"
|
||||
defer func() {
|
||||
if reason != "" {
|
||||
c.setControlResult(cmd, "error", reason)
|
||||
}
|
||||
}()
|
||||
r, ok := c.state.Records[cmd.DeviceID]
|
||||
if !ok || r.Access == nil || r.Access.ClientAPI == nil || r.Device.State != "ready" || r.Device.ClientAPI == nil {
|
||||
return
|
||||
}
|
||||
if cmd.ExpectedBootID != r.Device.ClientAPI.BootID {
|
||||
reason = "router_conflict"
|
||||
return
|
||||
}
|
||||
right := "read"
|
||||
if q.Resource == "bundle" {
|
||||
right = "bundle"
|
||||
}
|
||||
if !slices.Contains(r.Device.ClientAPI.Rights, right) {
|
||||
reason = "access_rejected"
|
||||
return
|
||||
}
|
||||
cache := c.evidenceCache
|
||||
if q.Transfer != "" {
|
||||
if cache == nil || cache.Transfer != q.Transfer || cache.DeviceID != cmd.DeviceID || cache.BootID != cmd.ExpectedBootID || cache.Resource != q.Resource || cache.ID != q.ID || cache.After != q.After || time.Since(cache.At) > evidenceRetention {
|
||||
reason = "result_expired"
|
||||
return
|
||||
}
|
||||
} else {
|
||||
found := false
|
||||
for _, candidate := range c.candidates(ctx) {
|
||||
if candidate.Address != r.Device.Address || candidate.Interface != r.Device.Interface {
|
||||
continue
|
||||
}
|
||||
a, err := c.apiClient(candidate, *r.Access.ClientAPI)
|
||||
if err != nil || a.identity != cmd.DeviceID {
|
||||
return
|
||||
}
|
||||
raw, _, err := a.readResource(ctx, "status", "", "", 0)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var status clientObservation
|
||||
if json.Unmarshal(raw, &status) != nil {
|
||||
reason = "unsupported_response"
|
||||
return
|
||||
}
|
||||
if status.BootID != cmd.ExpectedBootID {
|
||||
reason = "router_conflict"
|
||||
return
|
||||
}
|
||||
limit := 0
|
||||
if q.Resource == "events" || q.Resource == "recovery_history" {
|
||||
limit = 50
|
||||
}
|
||||
raw, _, err = a.readResource(ctx, q.Resource, q.ID, q.After, limit)
|
||||
if err != nil {
|
||||
var e *clientAPIError
|
||||
if errors.As(err, &e) {
|
||||
reason = e.Code
|
||||
}
|
||||
return
|
||||
}
|
||||
data, err := projectEvidence(q.Resource, raw, r.Access.ClientAPI.Token)
|
||||
if err != nil {
|
||||
reason = "unsupported_response"
|
||||
return
|
||||
}
|
||||
sum := sha256.Sum256(data)
|
||||
cache = &evidenceCache{DeviceID: cmd.DeviceID, BootID: cmd.ExpectedBootID, Resource: q.Resource, ID: q.ID, After: q.After, Transfer: hex.EncodeToString(sum[:]), Data: data, At: time.Now()}
|
||||
c.evidenceCache = cache
|
||||
found = true
|
||||
break
|
||||
}
|
||||
if !found {
|
||||
return
|
||||
}
|
||||
}
|
||||
if ctx.Err() != nil || time.Now().Unix() >= cmd.Deadline {
|
||||
reason = "expired"
|
||||
return
|
||||
}
|
||||
if q.Offset >= len(cache.Data) {
|
||||
reason = "result_expired"
|
||||
return
|
||||
}
|
||||
end := min(q.Offset+evidenceChunkSize, len(cache.Data))
|
||||
c.evidence = &EvidenceChunk{OperationID: cmd.ID, DeviceID: cmd.DeviceID, BootID: cmd.ExpectedBootID, Resource: q.Resource, ID: q.ID, Transfer: cache.Transfer, Offset: q.Offset, Total: len(cache.Data), At: cache.At.Unix(), Data: base64.StdEncoding.EncodeToString(cache.Data[q.Offset:end])}
|
||||
c.setControlResult(cmd, "complete", "")
|
||||
reason = ""
|
||||
}
|
||||
Reference in New Issue
Block a user