feat(remote): 显示控制中心和实例版本

This commit is contained in:
yml
2026-11-22 15:26:41 +08:00
parent 0940dd8f85
commit 9ed41d6551
6 changed files with 86 additions and 6 deletions
+6 -4
View File
@@ -112,10 +112,12 @@ type ServerEvent struct {
// DashboardState is the polling payload consumed by the remote web console.
type DashboardState struct {
ServerTime time.Time `json:"serverTime"`
Instances []InstanceState `json:"instances"`
Runs []RunRecord `json:"runs"`
Commands []CommandAudit `json:"commands"`
ServerTime time.Time `json:"serverTime"`
ControlVersion string `json:"controlVersion,omitempty"`
ControlBuildTime string `json:"controlBuildTime,omitempty"`
Instances []InstanceState `json:"instances"`
Runs []RunRecord `json:"runs"`
Commands []CommandAudit `json:"commands"`
}
// CommandAudit records one command and its delivery/execution state.
+46
View File
@@ -191,6 +191,52 @@ func TestDashboardRequiresOperatorToken(t *testing.T) {
}
}
func TestHealthAndDashboardExposeControlVersion(t *testing.T) {
server, err := NewServer(ServerOptions{
DataDir: t.TempDir(),
OperatorToken: "operator",
Version: "0.6.4",
BuildTime: "2026-11-22 16:00:00",
})
if err != nil {
t.Fatal(err)
}
defer server.Close()
httpServer := httptest.NewServer(server.Handler())
defer httpServer.Close()
healthResponse, err := http.Get(httpServer.URL + "/api/health")
if err != nil {
t.Fatal(err)
}
defer healthResponse.Body.Close()
var health map[string]any
if err := json.NewDecoder(healthResponse.Body).Decode(&health); err != nil {
t.Fatal(err)
}
if health["version"] != "0.6.4" || health["buildTime"] != "2026-11-22 16:00:00" {
t.Fatalf("unexpected health version payload: %#v", health)
}
request, err := http.NewRequest(http.MethodGet, httpServer.URL+"/api/dashboard", nil)
if err != nil {
t.Fatal(err)
}
request.Header.Set("X-Control-Token", "operator")
dashboardResponse, err := http.DefaultClient.Do(request)
if err != nil {
t.Fatal(err)
}
defer dashboardResponse.Body.Close()
var dashboard DashboardState
if err := json.NewDecoder(dashboardResponse.Body).Decode(&dashboard); err != nil {
t.Fatal(err)
}
if dashboard.ControlVersion != "0.6.4" || dashboard.ControlBuildTime != "2026-11-22 16:00:00" {
t.Fatalf("unexpected dashboard version payload: %+v", dashboard)
}
}
func TestAgentAutoEnrollsWithoutManualToken(t *testing.T) {
server, err := NewServer(ServerOptions{
DataDir: t.TempDir(),
+10 -1
View File
@@ -26,6 +26,8 @@ type ServerOptions struct {
OperatorToken string
EnrollToken string
EnrollWindow time.Duration
Version string
BuildTime string
}
type Server struct {
@@ -57,6 +59,8 @@ func NewServer(opts ServerOptions) (*Server, error) {
opts.AgentToken = strings.TrimSpace(opts.AgentToken)
opts.OperatorToken = strings.TrimSpace(opts.OperatorToken)
opts.EnrollToken = strings.TrimSpace(opts.EnrollToken)
opts.Version = strings.TrimSpace(opts.Version)
opts.BuildTime = strings.TrimSpace(opts.BuildTime)
if opts.OperatorToken == "" {
return nil, errors.New("OperatorToken 不能为空")
}
@@ -221,6 +225,8 @@ func (s *Server) handleHealth(w http.ResponseWriter, _ *http.Request) {
writeJSON(w, http.StatusOK, map[string]any{
"success": true,
"serverTime": time.Now(),
"version": s.opts.Version,
"buildTime": s.opts.BuildTime,
})
}
@@ -232,7 +238,10 @@ func (s *Server) handleDashboard(w http.ResponseWriter, r *http.Request) {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
writeJSON(w, http.StatusOK, s.hub.Dashboard())
dashboard := s.hub.Dashboard()
dashboard.ControlVersion = s.opts.Version
dashboard.ControlBuildTime = s.opts.BuildTime
writeJSON(w, http.StatusOK, dashboard)
}
func (s *Server) handleEvents(w http.ResponseWriter, r *http.Request) {
+15 -1
View File
@@ -21,6 +21,10 @@
}
header h1 { font-size: 16px; margin: 0; white-space: nowrap; }
.muted { color: #8f9baa; }
.version-pill {
padding: 4px 8px; border: 1px solid #35404c; border-radius: 999px;
color: #9dabba; font-size: 11px; white-space: nowrap;
}
.grow { flex: 1; }
.token-box { display: flex; gap: 8px; align-items: center; }
input {
@@ -132,6 +136,7 @@
<div class="shell">
<header>
<h1>斗鱼抢购远端控制台</h1>
<span id="control-version" class="version-pill">控制中心版本加载中</span>
<span id="connection" class="muted">未连接</span>
<div class="grow"></div>
<div class="token-box">
@@ -170,6 +175,7 @@
const tokenInput = document.getElementById('token');
const connectButton = document.getElementById('connect');
const connection = document.getElementById('connection');
const controlVersion = document.getElementById('control-version');
const machinesRoot = document.getElementById('machines');
const runsRoot = document.getElementById('runs');
const summary = document.getElementById('summary');
@@ -301,6 +307,11 @@
}
function renderDashboard(data) {
const controlVersionText = String(data.controlVersion || '').split(' ')[0].replace(/^v/, '');
controlVersion.textContent = controlVersionText
? `控制中心 v${controlVersionText}`
: '控制中心版本未知';
controlVersion.title = data.controlBuildTime ? `构建时间: ${data.controlBuildTime}` : '';
const groups = new Map();
for (const item of data.instances || []) {
const key = item.machineId || item.machineName;
@@ -359,7 +370,10 @@
? `代理 ${proxy.activeCount || 0} · 备用 ${proxy.standbyCount || 0}/${proxy.standbyTarget || 0} · 直连 ${proxy.directCount || 0}`
: '本地直连';
const proxyWarning = ['direct_fallback', 'failed', 'degraded'].includes(proxy?.state);
const meta = textNode('div', `${item.accountCount || 0} 账号 · ${proxyText}`, proxyWarning ? 'bad' : 'muted');
const instanceVersion = String(item.version || '').split(' ')[0].replace(/^v/, '');
const versionText = instanceVersion ? ` · v${instanceVersion}` : '';
const meta = textNode('div', `${item.accountCount || 0} 账号 · ${proxyText}${versionText}`, proxyWarning ? 'bad' : 'muted');
meta.title = item.version ? `实例版本: ${item.version}` : '';
const actions = element('div', 'actions');
actions.append(