update. 优化服务端以及 HTML 显示效果

This commit is contained in:
ngfchl
2026-06-20 00:13:13 +08:00
parent 65310c07e9
commit 7217974395
3 changed files with 478 additions and 1662 deletions
+38 -31
View File
@@ -71,26 +71,22 @@ func resolvePath(baseDir, p string) string {
// 配置结构 // 配置结构
// ═══════════════════════════════════════════════ // ═══════════════════════════════════════════════
type AuthConfig struct {
MinerID string `yaml:"miner_id"`
Password string `yaml:"password"`
}
type ServerConfig struct { type ServerConfig struct {
Listen string `yaml:"listen"` Listen string `yaml:"listen"`
DevicesFile string `yaml:"devices_file"` DevicesFile string `yaml:"devices_file"`
Auth AuthConfig `yaml:"auth"` MinerUID string `yaml:"miner_uid"`
MinerUID string `yaml:"miner_uid"` Password string `yaml:"password"`
MinerAPIBase string `yaml:"miner_api_base"` MinerAPIBase string `yaml:"miner_api_base"`
FetchInterval int `yaml:"fetch_interval"` FetchInterval int `yaml:"fetch_interval"`
HistoryMax int `yaml:"history_max"` HistoryMax int `yaml:"history_max"`
} }
func defaultConfig() ServerConfig { func defaultConfig() ServerConfig {
return ServerConfig{ return ServerConfig{
Listen: ":8080", Listen: ":8080",
DevicesFile: "data/devices.yaml", DevicesFile: "data/devices.yaml",
Auth: AuthConfig{MinerID: "admin", Password: "123456"}, MinerUID: "",
Password: "123456",
MinerAPIBase: "https://pool.kryptex.com/prl/api/v3/miner/workers", MinerAPIBase: "https://pool.kryptex.com/prl/api/v3/miner/workers",
FetchInterval: 30, FetchInterval: 30,
HistoryMax: 200, HistoryMax: 200,
@@ -195,11 +191,8 @@ func fillDefaults(cfg *ServerConfig) {
if cfg.DevicesFile == "" { if cfg.DevicesFile == "" {
cfg.DevicesFile = "data/devices.yaml" cfg.DevicesFile = "data/devices.yaml"
} }
if cfg.Auth.MinerID == "" { if cfg.Password == "" {
cfg.Auth.MinerID = "admin" cfg.Password = "123456"
}
if cfg.Auth.Password == "" {
cfg.Auth.Password = "123456"
} }
if cfg.MinerAPIBase == "" { if cfg.MinerAPIBase == "" {
cfg.MinerAPIBase = "https://pool.kryptex.com/prl/api/v3/miner/workers" cfg.MinerAPIBase = "https://pool.kryptex.com/prl/api/v3/miner/workers"
@@ -318,15 +311,16 @@ type BrowserCmd struct {
// ═══════════════════════════════════════════════ // ═══════════════════════════════════════════════
type Hub struct { type Hub struct {
mu sync.RWMutex mu sync.RWMutex
fileMu sync.Mutex fileMu sync.Mutex
records map[string]*DeviceRecord records map[string]*DeviceRecord
live map[string]json.RawMessage live map[string]json.RawMessage
monitors map[*websocket.Conn]struct{} monitors map[*websocket.Conn]struct{}
miningData map[string]*MiningData miningData map[string]*MiningData
hashHist map[string][]HashratePoint hashHist map[string][]HashratePoint
devicesAbs string devicesAbs string
config ServerConfig config ServerConfig
lastWorkerCount int
} }
func NewHub(cfg ServerConfig, devicesAbs string) *Hub { func NewHub(cfg ServerConfig, devicesAbs string) *Hub {
@@ -623,7 +617,13 @@ func (h *Hub) fetchMinerData(url string) {
h.mu.RUnlock() h.mu.RUnlock()
h.broadcast(BrowserMsg{Type: "mining", Machine: w.Worker, Mining: md, History: hist}) h.broadcast(BrowserMsg{Type: "mining", Machine: w.Worker, Mining: md, History: hist})
} }
logInfo("miner", "更新 %d 个 worker", len(apiResp.Results)) h.mu.Lock()
cnt := len(apiResp.Results)
if cnt != h.lastWorkerCount {
logInfo("miner", "worker 数量变更: %d → %d", h.lastWorkerCount, cnt)
h.lastWorkerCount = cnt
}
h.mu.Unlock()
} }
// ═══════════════════════════════════════════════ // ═══════════════════════════════════════════════
@@ -639,6 +639,13 @@ var upgrader = websocket.Upgrader{
func handleAgent(conn *websocket.Conn, hub *Hub) { func handleAgent(conn *websocket.Conn, hub *Hub) {
defer conn.Close() defer conn.Close()
addr := conn.RemoteAddr().String() addr := conn.RemoteAddr().String()
hub.mu.RLock()
monCnt := len(hub.monitors)
hub.mu.RUnlock()
if monCnt == 0 {
logInfo("agent", "拒绝 %s (无浏览器连接)", addr)
return
}
logInfo("agent+", "%s", addr) logInfo("agent+", "%s", addr)
defer logInfo("agent-", "%s", addr) defer logInfo("agent-", "%s", addr)
for { for {
@@ -677,7 +684,7 @@ func handleMonitor(conn *websocket.Conn, hub *Hub) {
} }
// 校验 // 校验
if cmd.MinerID == hub.config.Auth.MinerID && cmd.Password == hub.config.Auth.Password { if cmd.MinerID == hub.config.MinerUID && cmd.Password == hub.config.Password {
authOK = true authOK = true
break break
} }
@@ -784,9 +791,9 @@ func main() {
if len(uid) > 22 { if len(uid) > 22 {
uid = uid[:22] + "..." uid = uid[:22] + "..."
} }
fmt.Printf(" ║ Miner : %-29s║\n", uid) fmt.Printf(" ║ Miner UID : %-29s║\n", uid)
} else { } else {
fmt.Println(" ║ Miner : 未配置 ║") fmt.Println(" ║ Miner UID : 未配置 ║")
} }
fmt.Println(" ╚═══════════════════════════════════════════╝") fmt.Println(" ╚═══════════════════════════════════════════╝")
fmt.Println() fmt.Println()
File diff suppressed because it is too large Load Diff
+1 -3
View File
@@ -4,9 +4,7 @@ listen: ":8080"
# 设备注册文件 # 设备注册文件
devices_file: "data/devices.yaml" devices_file: "data/devices.yaml"
# ─── 登录认证 ─── # ─── 登录认证 ───
auth: password: "adminadmin"
miner_id: "admin"
password: "123456"
# ─── 矿池配置 ─── # ─── 矿池配置 ───
# Kryptex 矿池 UID (钱包地址) # Kryptex 矿池 UID (钱包地址)
miner_uid: "prl1prkg7des097jx9lw22kusw75s0uy0xfmxgmr7l7r67csmpxzad4lqsaajgt" miner_uid: "prl1prkg7des097jx9lw22kusw75s0uy0xfmxgmr7l7r67csmpxzad4lqsaajgt"