新增 admin「渠道监控」模块(参考 BingZi-233/check-cx),独立于现有 Channel 体系。
admin 配置 + 后台定时调用上游 LLM chat completions 健康检查 + 所有登录用户只读可见。
后端:
- ent: channel_monitor + channel_monitor_history(AES-256-GCM 加密 api_key)
- service 按职责拆分:service/aggregator/validate/checker/runner/ssrf
- provider strategy map 替代 switch(openai/anthropic/gemini)
- repository batch 聚合(ListLatestForMonitorIDs + ComputeAvailabilityForMonitors)消除 N+1
- runner: ticker(5s) + pond worker pool(5) + inFlight 防并发 + TrySubmit 防雪崩
+ 凌晨 3 点 cron 清理 30 天历史
- SSRF 防护:强制 https + 私网/loopback/云元数据 IP 拒绝(127/8、10/8、172.16/12、
192.168/16、169.254/16、100.64/10、::1、fc00::/7、fe80::/10)+ DialContext
在 socket 层防 DNS rebinding
- API key sanitize:擦除 url.Error 与上游响应 body 中的 sk-/sk-ant-/AIza/JWT 模式
- APIKeyDecryptFailed 标志位 + 单 monitor 路径检测,避免空 key 调用上游
handler:
- admin: CRUD + 手动触发 + 历史接口(api_key 脱敏)
- user: 只读列表 + 状态详情(去除 api_key/endpoint)
- ParseChannelMonitorID 共用 + dto.ChannelMonitorExtraModelStatus 共用
前端:
- 路由 /admin/channels/{pricing,monitor} + /monitor(用户只读)
- AppSidebar 父项 expandOnly 支持
- ChannelMonitorView 拆为 8 个子组件 + ChannelStatusView 拆出 detail dialog
- composables/useChannelMonitorFormat + constants/channelMonitor 共享
- i18n monitorCommon namespace 消除 admin/user 两 view 重复
合规:所有文件符合 CLAUDE.md(Go ≤ 500 行 / Vue ≤ 300 行 / 函数 ≤ 30 行)
CI: go build / gofmt / golangci-lint(0 issues) / make test-unit / pnpm build 全绿
100 lines
2.8 KiB
Go
100 lines
2.8 KiB
Go
package service
|
||
|
||
import (
|
||
"context"
|
||
"net/url"
|
||
"strings"
|
||
)
|
||
|
||
// 渠道监控参数校验与归一化辅助函数。
|
||
// 校验失败一律返回 channel_monitor_const.go 中预定义的 Err* 错误,错误信息不含具体 IP/hostname,避免泄露内网拓扑。
|
||
|
||
// validateProvider 校验 provider 字符串。
|
||
// 唯一来源于 providerAdapters:新增 provider 只需要在 channel_monitor_checker.go 注册 adapter。
|
||
func validateProvider(p string) error {
|
||
if !isSupportedProvider(p) {
|
||
return ErrChannelMonitorInvalidProvider
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// validateInterval 校验 interval_seconds 范围。
|
||
func validateInterval(sec int) error {
|
||
if sec < monitorMinIntervalSeconds || sec > monitorMaxIntervalSeconds {
|
||
return ErrChannelMonitorInvalidInterval
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// validateEndpoint 校验 endpoint:
|
||
// - scheme 强制 https(拒绝 http,避免明文凭证 + 部分 SSRF 利用面)
|
||
// - 必须为 origin(无 path/query/fragment),防止用户填 https://api.openai.com/v1
|
||
// 导致 joinURL 拼出 /v1/v1/chat/completions
|
||
// - hostname 不能是 localhost/metadata 等已知元数据 hostname
|
||
// - 解析所有 IP,任一落在 loopback/RFC1918/link-local/ULA 段即拒绝(防 SSRF)
|
||
//
|
||
// 错误信息不暴露具体 IP / hostname,避免泄露内网拓扑。
|
||
func validateEndpoint(ep string) error {
|
||
ep = strings.TrimSpace(ep)
|
||
if ep == "" {
|
||
return ErrChannelMonitorInvalidEndpoint
|
||
}
|
||
u, err := url.Parse(ep)
|
||
if err != nil {
|
||
return ErrChannelMonitorInvalidEndpoint
|
||
}
|
||
if u.Scheme != "https" {
|
||
return ErrChannelMonitorEndpointScheme
|
||
}
|
||
if u.Host == "" {
|
||
return ErrChannelMonitorInvalidEndpoint
|
||
}
|
||
if u.Path != "" && u.Path != "/" {
|
||
return ErrChannelMonitorEndpointPath
|
||
}
|
||
if u.RawQuery != "" || u.Fragment != "" {
|
||
return ErrChannelMonitorEndpointPath
|
||
}
|
||
|
||
hostname := u.Hostname()
|
||
ctx, cancel := context.WithTimeout(context.Background(), monitorEndpointResolveTimeout)
|
||
defer cancel()
|
||
blocked, err := isPrivateOrLoopbackHost(ctx, hostname)
|
||
if err != nil {
|
||
return ErrChannelMonitorEndpointUnreachable
|
||
}
|
||
if blocked {
|
||
return ErrChannelMonitorEndpointPrivate
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// normalizeEndpoint 去除前后空白与末尾 `/`,保证存储统一为 origin。
|
||
// validateEndpoint 已确保格式合法(仅 origin),这里只做最终归一化。
|
||
func normalizeEndpoint(ep string) string {
|
||
ep = strings.TrimSpace(ep)
|
||
ep = strings.TrimRight(ep, "/")
|
||
return ep
|
||
}
|
||
|
||
// normalizeModels 去除空白、重复模型名。保留输入顺序(map 的迭代顺序无关)。
|
||
func normalizeModels(in []string) []string {
|
||
if len(in) == 0 {
|
||
return []string{}
|
||
}
|
||
seen := make(map[string]struct{}, len(in))
|
||
out := make([]string, 0, len(in))
|
||
for _, m := range in {
|
||
m = strings.TrimSpace(m)
|
||
if m == "" {
|
||
continue
|
||
}
|
||
if _, ok := seen[m]; ok {
|
||
continue
|
||
}
|
||
seen[m] = struct{}{}
|
||
out = append(out, m)
|
||
}
|
||
return out
|
||
}
|