Files
Havline/internal/frp/diagnostics.go
T
mrleeandDevin 9d75fb1d3e
CI / verify (push) Canceled after 0s
fix: 收敛正确性缺口并补齐质量门禁
备份、隧道生命周期、日志、会话与设置项存在一批「静默失效」问题,本次一并收敛。

正确性:
- 备份归档补上 session_secret 与 frp 配置。缺密钥时换机恢复后库里所有凭据都解不开,
  界面表现为「配置都在、一执行就报解密失败」;同时支持口令加密(scrypt + AES-256-GCM)。
- 修复「立即备份」必失败:tar 头用的是遍历时的文件尺寸,而 logs、frpc 配置在打包期间
  还在增长,读出的字节多于声明值导致 archive/tar 报错、整份备份作废。改为打开后重新
  取尺寸并用 LimitReader 兜住,被截短时补零保持条目结构完整。
- 数据库改用 VACUUM INTO 生成一致性快照。直接打包正在写入的 SQLite 文件可能得到
  结构完整但内容自相矛盾的副本,恢复后才暴露。
- Cloudflare 启动改用 StartAuto,Shutdown 显式 StopAll,子进程绑定应用 ctx。此前
  重启会把用户手动停掉的隧道重新拉起,退出也可能留下孤儿进程。
- Nginx 访问/错误日志加轮转(200MB 阈值、gzip 归档、USR1 重开),读取改为从文件尾
  分块倒读,并跨当日归档统计。此前日志只增不减,读接口每次全量扫描。
- 过期会话改由日任务清理;删除反代规则时同步清掉流量累计,避免同名规则重建继承旧读数。
- 启动加就绪门:Nginx bootstrap 完成后再放行 FRP 对账、Agent 隧道与 Cloudflare 自启。

设置项:
- 新增设置项注册表(internal/settings/registry.go),白名单与默认值从它派生,
  并记录每个键的消费者,防止再出现「界面能改、后端没人读」的黑洞设置。
- 接上三个此前不生效的设置项:timezone(启动与保存时应用到 time.Local)、
  theme(后端为权威源,挂载时覆盖本地偏好、顶栏切换回写)、log_retention_days
  (控制日志归档保留天数)。

工程质量:
- gofmt 全量格式化 23 个历史文件,CI 增加格式化门禁。
- 前端引入 ESLint(flat config)与 lint 脚本,CI 增加 lint 步骤;顺带修掉
  logDisplay 的 let→const 与 LogsView 模板内直接改 prop 两处问题。
- 删除无引用的 pnpm-lock.yaml,prototype/ 加入忽略列表。
- 打包版本对齐:Docker manifest 同步到 2.0.0,CI 校验 VERSION 与 package.json 一致。

验证:gofmt 0 文件、go vet、go test ./...、npm run lint/typecheck/test/build 全绿。

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-24 16:30:00 +08:00

252 lines
8.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package frp
import (
"context"
"fmt"
"net"
"os"
"strings"
"time"
)
// 诊断状态常量:ok 已确认可用;failed 已确认失败;unknown 无法确认(区别于失败)。
const (
diagStateOK = "ok"
diagStateFailed = "failed"
diagStateUnknown = "unknown"
diagTCPTimeout = 3 * time.Second
diagDNSTimeout = 2 * time.Second
diagnosticsCacheTTL = 15 * time.Second
diagnosticsMaxInFlight = 4
)
type diagnosticsCacheEntry struct {
diagnostics ServerDiagnostics
expiresAt time.Time
}
// ServerDiagnostics 返回服务端诊断结果。带短 TTL 缓存与并发上限:
// 列表页轮询不应同步等待定位等外部 API,也不能无限叠加探测请求。
func (s *Service) ServerDiagnostics(ctx context.Context, id int64) (ServerDiagnostics, error) {
server, err := s.store.GetServer(ctx, id)
if err != nil {
return ServerDiagnostics{}, err
}
return s.runDiagnostics(ctx, server), nil
}
func (s *Service) runDiagnostics(ctx context.Context, server Server) ServerDiagnostics {
s.diagMu.Lock()
if entry, ok := s.diagCache[server.ID]; ok && time.Now().Before(entry.expiresAt) {
cached := entry.diagnostics
s.diagMu.Unlock()
return cached
}
// 并发上限:超限时返回旧结果或 unknown,不排队等待
if s.diagInFlight >= diagnosticsMaxInFlight {
cached, ok := s.diagCache[server.ID]
s.diagMu.Unlock()
if ok {
return cached.diagnostics
}
return ServerDiagnostics{ServerID: server.ID, Checks: []DiagnosticCheck{{Name: "tcp_reachable", State: diagStateUnknown, Detail: "探测请求过多,请稍后重试"}}}
}
s.diagInFlight++
s.diagMu.Unlock()
defer func() {
s.diagMu.Lock()
s.diagInFlight--
s.diagMu.Unlock()
}()
result := s.computeDiagnostics(ctx, server)
s.diagMu.Lock()
s.diagCache[server.ID] = diagnosticsCacheEntry{diagnostics: result, expiresAt: time.Now().Add(diagnosticsCacheTTL)}
s.diagMu.Unlock()
return result
}
// computeDiagnostics 执行各诊断项。定位失败、日志缺失等一律记为 unknown 或非阻断展示,
// 绝不写入 Error(Error 仅表示控制端口探测失败)。
func (s *Service) computeDiagnostics(ctx context.Context, server Server) ServerDiagnostics {
dnsCheck := diagnoseDNS(ctx, server.ServerAddr)
tcpCheck := diagnoseTCP(ctx, server, dnsCheck)
diagnostics := ServerDiagnostics{
ServerID: server.ID,
Available: tcpCheck.State == diagStateOK,
LatencyMS: tcpCheck.LatencyMS,
Checks: []DiagnosticCheck{tcpCheck, s.diagnoseFRPCLogin(server.ID), dnsCheck},
}
if tcpCheck.State == diagStateFailed {
diagnostics.Error = tcpCheck.Detail
}
// location:非阻断展示项。缓存命中直接返回;未命中先展示 IP 占位并后台刷新,
// 不让列表页同步等待外部定位 API。
location, resolved := s.locationSnapshot(ctx, server.ServerAddr)
diagnostics.Location = location
locationCheck := DiagnosticCheck{Name: "location", State: diagStateOK, Detail: location}
if !resolved {
locationCheck.State = diagStateUnknown
locationCheck.Detail = "定位信息后台获取中"
go s.refreshLocation(server.ServerAddr)
}
diagnostics.Checks = append(diagnostics.Checks, locationCheck)
s.store.AddServerDiagnostic(ctx, diagnostics)
return diagnostics
}
// diagnoseTCP 控制端口 TCP 探测:只证明端口可达,不涉及认证与代理可用性。
func diagnoseTCP(ctx context.Context, server Server, dnsCheck DiagnosticCheck) DiagnosticCheck {
check := DiagnosticCheck{Name: "tcp_reachable"}
if dnsCheck.State == diagStateFailed {
check.State = diagStateFailed
check.Detail = "已跳过:" + dnsCheck.Detail
return check
}
dialer := net.Dialer{Timeout: diagTCPTimeout}
started := time.Now()
conn, err := dialer.DialContext(ctx, "tcp", net.JoinHostPort(server.ServerAddr, fmt.Sprint(server.ServerPort)))
check.LatencyMS = time.Since(started).Milliseconds()
if check.LatencyMS < 1 {
check.LatencyMS = 1
}
if err != nil {
check.State = diagStateFailed
check.Detail = diagnoseDialError(err)
return check
}
_ = conn.Close()
check.State = diagStateOK
check.Detail = fmt.Sprintf("控制端口 %d 可达", server.ServerPort)
return check
}
// diagnoseDNS 记录域名解析结果:A/AAAA 地址、解析耗时;TTL 由系统解析器决定,标准库无法读取,不展示。
func diagnoseDNS(ctx context.Context, host string) DiagnosticCheck {
check := DiagnosticCheck{Name: "dns_resolution"}
if ip := net.ParseIP(host); ip != nil {
check.State = diagStateOK
check.Detail = fmt.Sprintf("配置为 IP 地址 %s,未经过域名解析", ip)
return check
}
resolver := net.DefaultResolver
ctxTimeout, cancel := context.WithTimeout(ctx, diagDNSTimeout)
defer cancel()
started := time.Now()
addresses, err := resolver.LookupIPAddr(ctxTimeout, host)
elapsed := time.Since(started).Milliseconds()
if err != nil || len(addresses) == 0 {
check.State = diagStateFailed
check.Detail = "域名解析失败:" + diagnoseResolveError(err)
return check
}
var ipv4, ipv6 []string
for _, address := range addresses {
if address.IP.To4() != nil {
ipv4 = append(ipv4, address.IP.String())
} else {
ipv6 = append(ipv6, address.IP.String())
}
}
summary := "系统解析器;A=" + strings.Join(ipv4, ", ")
if len(ipv6) > 0 {
summary += ";AAAA=" + strings.Join(ipv6, ", ")
} else {
summary += ";AAAA=无"
}
check.LatencyMS = elapsed
check.Detail = fmt.Sprintf("%s(解析耗时 %d ms)", summary, elapsed)
return check
}
func diagnoseResolveError(err error) string {
if err == nil {
return "无解析结果"
}
if dnsErr, ok := err.(*net.DNSError); ok && dnsErr.IsNotFound {
return "域名不存在"
}
return err.Error()
}
// diagnoseFRPCLogin 从 frpc 运行日志推断登录状态:ok 已登录,failed 最近一次登录失败,unknown 无证据。
func (s *Service) diagnoseFRPCLogin(serverID int64) DiagnosticCheck {
check := DiagnosticCheck{Name: "frpc_login"}
data, err := os.ReadFile(s.serverLogPath(serverID))
if os.IsNotExist(err) {
check.State = diagStateUnknown
check.Detail = "frpc 尚未运行,无登录记录"
return check
}
if err != nil {
check.State = diagStateUnknown
check.Detail = "读取 frpc 日志失败"
return check
}
state, detail := summarizeFRPCLogin(string(data))
check.State = state
check.Detail = detail
return check
}
// summarizeFRPCLogin 从日志尾部向前找最近的登录结论;登录成功优先于更早的失败记录。
func summarizeFRPCLogin(log string) (string, string) {
lines := strings.Split(strings.TrimRight(log, "\n"), "\n")
const window = 200
if len(lines) > window {
lines = lines[len(lines)-window:]
}
failureDetail := ""
for index := len(lines) - 1; index >= 0; index-- {
line := lines[index]
if prefix := strings.Index(line, "] "); prefix >= 0 {
line = line[prefix+2:]
}
if strings.Contains(line, "login to server success") {
return diagStateOK, "frpc 已登录 frps"
}
if failureDetail == "" && isLoginFailureLine(line) {
failureDetail = line
}
}
if failureDetail != "" {
if len(failureDetail) > 160 {
failureDetail = failureDetail[:160] + "…"
}
return diagStateFailed, "frpc 登录失败:" + failureDetail
}
return diagStateUnknown, "日志中暂无登录结果记录"
}
func isLoginFailureLine(line string) bool {
if strings.Contains(line, "login to the server failed") || strings.Contains(line, "connect to server error") || strings.Contains(line, "authentication failed") {
return true
}
return strings.Contains(line, "token") && strings.Contains(line, "error")
}
// locationSnapshot 返回定位缓存值;未命中时返回 IP 占位(resolved=false),由调用方后台刷新。
func (s *Service) locationSnapshot(ctx context.Context, host string) (string, bool) {
ip := resolvePublicIP(ctx, host)
if ip == "" {
// 内网地址或解析失败:展示主机本身,不发起外部定位,也不视为错误
return strings.TrimSpace(host), true
}
s.locationMu.Lock()
cached, ok := s.locations[ip]
s.locationMu.Unlock()
if ok && time.Now().Before(cached.expiresAt) {
return cached.value, true
}
return ip, false
}
// refreshLocation 后台补全定位缓存;结果只影响展示,不参与可用性判定。
func (s *Service) refreshLocation(host string) {
ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
defer cancel()
_ = s.serverLocation(ctx, host)
}