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 {
Listen string `yaml:"listen"`
DevicesFile string `yaml:"devices_file"`
Auth AuthConfig `yaml:"auth"`
MinerUID string `yaml:"miner_uid"`
MinerAPIBase string `yaml:"miner_api_base"`
FetchInterval int `yaml:"fetch_interval"`
HistoryMax int `yaml:"history_max"`
Listen string `yaml:"listen"`
DevicesFile string `yaml:"devices_file"`
MinerUID string `yaml:"miner_uid"`
Password string `yaml:"password"`
MinerAPIBase string `yaml:"miner_api_base"`
FetchInterval int `yaml:"fetch_interval"`
HistoryMax int `yaml:"history_max"`
}
func defaultConfig() ServerConfig {
return ServerConfig{
Listen: ":8080",
DevicesFile: "data/devices.yaml",
Auth: AuthConfig{MinerID: "admin", Password: "123456"},
MinerUID: "",
Password: "123456",
MinerAPIBase: "https://pool.kryptex.com/prl/api/v3/miner/workers",
FetchInterval: 30,
HistoryMax: 200,
@@ -195,11 +191,8 @@ func fillDefaults(cfg *ServerConfig) {
if cfg.DevicesFile == "" {
cfg.DevicesFile = "data/devices.yaml"
}
if cfg.Auth.MinerID == "" {
cfg.Auth.MinerID = "admin"
}
if cfg.Auth.Password == "" {
cfg.Auth.Password = "123456"
if cfg.Password == "" {
cfg.Password = "123456"
}
if cfg.MinerAPIBase == "" {
cfg.MinerAPIBase = "https://pool.kryptex.com/prl/api/v3/miner/workers"
@@ -318,15 +311,16 @@ type BrowserCmd struct {
// ═══════════════════════════════════════════════
type Hub struct {
mu sync.RWMutex
fileMu sync.Mutex
records map[string]*DeviceRecord
live map[string]json.RawMessage
monitors map[*websocket.Conn]struct{}
miningData map[string]*MiningData
hashHist map[string][]HashratePoint
devicesAbs string
config ServerConfig
mu sync.RWMutex
fileMu sync.Mutex
records map[string]*DeviceRecord
live map[string]json.RawMessage
monitors map[*websocket.Conn]struct{}
miningData map[string]*MiningData
hashHist map[string][]HashratePoint
devicesAbs string
config ServerConfig
lastWorkerCount int
}
func NewHub(cfg ServerConfig, devicesAbs string) *Hub {
@@ -623,7 +617,13 @@ func (h *Hub) fetchMinerData(url string) {
h.mu.RUnlock()
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) {
defer conn.Close()
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)
defer logInfo("agent-", "%s", addr)
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
break
}
@@ -784,9 +791,9 @@ func main() {
if len(uid) > 22 {
uid = uid[:22] + "..."
}
fmt.Printf(" ║ Miner : %-29s║\n", uid)
fmt.Printf(" ║ Miner UID : %-29s║\n", uid)
} else {
fmt.Println(" ║ Miner : 未配置 ║")
fmt.Println(" ║ Miner UID : 未配置 ║")
}
fmt.Println(" ╚═══════════════════════════════════════════╝")
fmt.Println()
File diff suppressed because it is too large Load Diff