feat(remote): 增加远端控制台与日志汇总导出

This commit is contained in:
yml
2026-11-21 20:07:40 +08:00
parent 79b503d1a6
commit d176c421a8
27 changed files with 3486 additions and 19 deletions
+458
View File
@@ -0,0 +1,458 @@
package remote
import (
"crypto/subtle"
"embed"
"encoding/json"
"errors"
"fmt"
"io/fs"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/gorilla/websocket"
)
//go:embed web/*
var webAssets embed.FS
type ServerOptions struct {
Addr string
DataDir string
AgentToken string
OperatorToken string
EnrollToken string
EnrollWindow time.Duration
}
type Server struct {
opts ServerOptions
hub *Hub
tokens *tokenStore
upgrader websocket.Upgrader
enrollUntil time.Time
sessionsMu sync.RWMutex
sessions map[string]*agentConn
}
type agentConn struct {
conn *websocket.Conn
writeMu sync.Mutex
machineID string
machineName string
instanceID string
instanceName string
remoteAddr string
}
func NewServer(opts ServerOptions) (*Server, error) {
opts.Addr = strings.TrimSpace(opts.Addr)
if opts.Addr == "" {
opts.Addr = ":18080"
}
opts.AgentToken = strings.TrimSpace(opts.AgentToken)
opts.OperatorToken = strings.TrimSpace(opts.OperatorToken)
opts.EnrollToken = strings.TrimSpace(opts.EnrollToken)
if opts.OperatorToken == "" {
return nil, errors.New("OperatorToken 不能为空")
}
if opts.EnrollWindow <= 0 {
opts.EnrollWindow = 10 * time.Minute
}
hub, err := NewHub(opts.DataDir)
if err != nil {
return nil, err
}
tokens, err := newTokenStore(opts.DataDir)
if err != nil {
_ = hub.Close()
return nil, err
}
return &Server{
opts: opts,
hub: hub,
tokens: tokens,
enrollUntil: time.Now().Add(opts.EnrollWindow),
sessions: make(map[string]*agentConn),
upgrader: websocket.Upgrader{
HandshakeTimeout: 8 * time.Second,
CheckOrigin: func(r *http.Request) bool {
origin := strings.TrimSpace(r.Header.Get("Origin"))
return origin == "" || sameOrigin(origin, r.Host)
},
},
}, nil
}
func (s *Server) Close() error {
s.sessionsMu.Lock()
for _, session := range s.sessions {
_ = session.conn.Close()
}
s.sessions = make(map[string]*agentConn)
s.sessionsMu.Unlock()
return s.hub.Close()
}
func (s *Server) Handler() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/api/health", s.handleHealth)
mux.HandleFunc("/api/dashboard", s.handleDashboard)
mux.HandleFunc("/api/events", s.handleEvents)
mux.HandleFunc("/api/export", s.handleExport)
mux.HandleFunc("/api/command", s.handleCommand)
mux.HandleFunc("/api/enroll", s.handleEnroll)
mux.HandleFunc("/ws/agent", s.handleAgent)
assets, _ := fs.Sub(webAssets, "web")
mux.Handle("/", http.FileServer(http.FS(assets)))
return mux
}
func (s *Server) handleExport(w http.ResponseWriter, r *http.Request) {
if !s.authorize(w, r, s.opts.OperatorToken) {
return
}
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
format := strings.ToLower(strings.TrimSpace(r.URL.Query().Get("format")))
if format == "" {
format = "csv"
}
if format != "csv" && format != "json" {
writeJSON(w, http.StatusBadRequest, map[string]any{"error": "format 仅支持 csv 或 json"})
return
}
filter := EventExportFilter{
MachineID: strings.TrimSpace(r.URL.Query().Get("machineId")),
InstanceID: strings.TrimSpace(r.URL.Query().Get("instanceId")),
RunID: strings.TrimSpace(r.URL.Query().Get("runId")),
Query: strings.TrimSpace(r.URL.Query().Get("q")),
}
if raw := strings.TrimSpace(r.URL.Query().Get("from")); raw != "" {
value, err := time.Parse(time.RFC3339, raw)
if err != nil {
writeJSON(w, http.StatusBadRequest, map[string]any{"error": "from 时间格式应为 RFC3339"})
return
}
filter.From = value
}
if raw := strings.TrimSpace(r.URL.Query().Get("to")); raw != "" {
value, err := time.Parse(time.RFC3339, raw)
if err != nil {
writeJSON(w, http.StatusBadRequest, map[string]any{"error": "to 时间格式应为 RFC3339"})
return
}
filter.To = value
}
extension := format
if format == "json" {
extension = "json"
}
w.Header().Set("Content-Type", exportContentType(format))
w.Header().Set("Content-Disposition", fmt.Sprintf(
`attachment; filename="douyu-logs-%s.%s"`,
time.Now().Format("20060102-150405"),
extension,
))
if _, err := s.hub.ExportEvents(w, format, filter); err != nil {
// Headers may already be committed. The client receives a truncated file
// rather than blocking the control plane.
return
}
}
func (s *Server) handleEnroll(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
if s.opts.EnrollToken != "" {
provided := strings.TrimSpace(r.Header.Get("X-Enroll-Token"))
if subtle.ConstantTimeCompare([]byte(provided), []byte(s.opts.EnrollToken)) != 1 {
writeJSON(w, http.StatusUnauthorized, map[string]any{"error": "注册令牌无效"})
return
}
} else if time.Now().After(s.enrollUntil) {
writeJSON(w, http.StatusForbidden, map[string]any{"error": "实例自动注册窗口已关闭"})
return
}
var request struct {
MachineID string `json:"machineId"`
MachineName string `json:"machineName"`
InstanceID string `json:"instanceId"`
InstanceName string `json:"instanceName"`
}
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 64<<10)).Decode(&request); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]any{"error": "请求格式错误"})
return
}
request.MachineID = strings.TrimSpace(request.MachineID)
request.InstanceID = strings.TrimSpace(request.InstanceID)
if request.MachineID == "" || request.InstanceID == "" {
writeJSON(w, http.StatusBadRequest, map[string]any{"error": "缺少机器或实例标识"})
return
}
token, err := s.tokens.Issue(request.MachineID, request.MachineName, request.InstanceID, request.InstanceName)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]any{"error": "生成实例令牌失败"})
return
}
writeJSON(w, http.StatusOK, map[string]any{"token": token})
}
func (s *Server) ListenAndServe() error {
server := &http.Server{
Addr: s.opts.Addr,
Handler: s.Handler(),
ReadHeaderTimeout: 10 * time.Second,
IdleTimeout: 90 * time.Second,
}
return server.ListenAndServe()
}
func (s *Server) handleHealth(w http.ResponseWriter, _ *http.Request) {
writeJSON(w, http.StatusOK, map[string]any{
"success": true,
"serverTime": time.Now(),
})
}
func (s *Server) handleDashboard(w http.ResponseWriter, r *http.Request) {
if !s.authorize(w, r, s.opts.OperatorToken) {
return
}
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
writeJSON(w, http.StatusOK, s.hub.Dashboard())
}
func (s *Server) handleEvents(w http.ResponseWriter, r *http.Request) {
if !s.authorize(w, r, s.opts.OperatorToken) {
return
}
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
since, _ := strconv.ParseUint(r.URL.Query().Get("since"), 10, 64)
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
writeJSON(w, http.StatusOK, map[string]any{
"events": s.hub.EventsSinceFiltered(
since,
limit,
strings.TrimSpace(r.URL.Query().Get("machineId")),
strings.TrimSpace(r.URL.Query().Get("instanceId")),
strings.TrimSpace(r.URL.Query().Get("runId")),
),
})
}
func (s *Server) handleCommand(w http.ResponseWriter, r *http.Request) {
if !s.authorize(w, r, s.opts.OperatorToken) {
return
}
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
var request struct {
MachineID string `json:"machineId"`
InstanceID string `json:"instanceId"`
Type string `json:"type"`
}
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 64<<10)).Decode(&request); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]any{"error": "请求格式错误"})
return
}
request.Type = strings.TrimSpace(request.Type)
if !allowedCommand(request.Type) {
writeJSON(w, http.StatusBadRequest, map[string]any{"error": "不支持的命令"})
return
}
id, err := randomID()
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]any{"error": err.Error()})
return
}
key := instanceKey(request.MachineID, request.InstanceID)
s.sessionsMu.RLock()
session := s.sessions[key]
s.sessionsMu.RUnlock()
if session == nil {
writeJSON(w, http.StatusConflict, map[string]any{"error": "实例当前离线"})
return
}
now := time.Now()
command := Command{
ID: id,
Type: request.Type,
CreatedAt: now,
ExpiresAt: now.Add(30 * time.Second),
}
s.hub.AddCommand(CommandAudit{
ID: id,
MachineID: request.MachineID,
MachineName: session.machineName,
InstanceID: request.InstanceID,
InstanceName: session.instanceName,
Type: request.Type,
State: "queued",
CreatedAt: now,
})
if err := session.write(Envelope{Type: MessageCommand, Command: &command}); err != nil {
writeJSON(w, http.StatusBadGateway, map[string]any{"error": "命令发送失败: " + err.Error()})
return
}
s.hub.MarkCommandDelivered(id)
writeJSON(w, http.StatusOK, map[string]any{"success": true, "commandId": id})
}
func (s *Server) handleAgent(w http.ResponseWriter, r *http.Request) {
token := requestToken(r)
legacy := s.opts.AgentToken != "" && subtle.ConstantTimeCompare([]byte(token), []byte(s.opts.AgentToken)) == 1
if !legacy && !s.tokens.Valid(token) {
writeJSON(w, http.StatusUnauthorized, map[string]any{"error": "未授权"})
return
}
conn, err := s.upgrader.Upgrade(w, r, nil)
if err != nil {
return
}
session := &agentConn{conn: conn, remoteAddr: r.RemoteAddr}
defer conn.Close()
var key string
defer func() {
if key != "" {
s.sessionsMu.Lock()
if s.sessions[key] == session {
delete(s.sessions, key)
s.hub.MarkOffline(session.machineID, session.instanceID)
}
s.sessionsMu.Unlock()
}
}()
conn.SetReadLimit(2 << 20)
for {
var envelope Envelope
if err := conn.ReadJSON(&envelope); err != nil {
return
}
switch envelope.Type {
case MessageHello, MessageHeartbeat, MessageSnapshot:
snapshot := envelope.Hello
if envelope.Type != MessageHello {
snapshot = envelope.Snapshot
}
if snapshot == nil || snapshot.MachineID == "" || snapshot.InstanceID == "" {
continue
}
if !legacy && !s.tokens.Matches(token, snapshot.MachineID, snapshot.InstanceID) {
_ = conn.WriteControl(
websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.ClosePolicyViolation, "instance token identity mismatch"),
time.Now().Add(2*time.Second),
)
return
}
session.machineID = snapshot.MachineID
session.machineName = snapshot.MachineName
session.instanceID = snapshot.InstanceID
session.instanceName = snapshot.InstanceName
newKey := instanceKey(snapshot.MachineID, snapshot.InstanceID)
s.sessionsMu.Lock()
if key != "" && key != newKey {
delete(s.sessions, key)
}
s.sessions[newKey] = session
key = newKey
s.sessionsMu.Unlock()
s.hub.ApplySnapshot(*snapshot, r.RemoteAddr)
case MessageEvents:
s.hub.AppendEvents(envelope.Events)
case MessageCommandResult:
if envelope.Result != nil {
s.hub.ApplyCommandResult(*envelope.Result)
}
}
}
}
func (c *agentConn) write(envelope Envelope) error {
c.writeMu.Lock()
defer c.writeMu.Unlock()
_ = c.conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
return c.conn.WriteJSON(envelope)
}
func (s *Server) authorize(w http.ResponseWriter, r *http.Request, expected string) bool {
provided := requestToken(r)
if subtle.ConstantTimeCompare([]byte(provided), []byte(expected)) == 1 {
return true
}
writeJSON(w, http.StatusUnauthorized, map[string]any{"error": "未授权"})
return false
}
func requestToken(r *http.Request) string {
provided := strings.TrimSpace(r.Header.Get("X-Control-Token"))
if provided == "" {
auth := strings.TrimSpace(r.Header.Get("Authorization"))
if strings.HasPrefix(strings.ToLower(auth), "bearer ") {
provided = strings.TrimSpace(auth[7:])
}
}
if provided == "" {
provided = strings.TrimSpace(r.URL.Query().Get("token"))
}
return provided
}
func allowedCommand(command string) bool {
switch command {
case "ping", "status", "stop", "cancel_schedule":
return true
default:
return false
}
}
func writeJSON(w http.ResponseWriter, status int, value any) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(value)
}
func exportContentType(format string) string {
if format == "json" {
return "application/json; charset=utf-8"
}
return "text/csv; charset=utf-8"
}
func sameOrigin(origin, host string) bool {
origin = strings.TrimSpace(origin)
if origin == "" {
return true
}
origin = strings.TrimPrefix(strings.TrimPrefix(origin, "https://"), "http://")
return strings.EqualFold(origin, host)
}
func RandomToken() (string, error) {
id, err := randomID()
if err != nil {
return "", err
}
return fmt.Sprintf("%s-%s", id[:16], id[16:]), nil
}