mirror of
https://github.com/komari-monitor/komari.git
synced 2026-11-21 07:10:47 +00:00
将混乱的「Web 接口 → 业务逻辑」收敛为统一管道: 接口 → 生成 RPC 请求 → Dispatch(权限+命名空间) → RPC handler(业务), 为后续 JS 插件接入打地基(插件本身未实现)。 核心: - pkg/rpc/permission.go:声明式 ACL + 通配符权限,按特异性裁决 (精确 > 字面前缀更长的通配 > 全局 *)。Allow/RegisterNamespace 供插件扩展。 - pkg/rpc:新增 Unregister(动态注销,供插件卸载);ContextMeta 增 SessionToken/TempShareValid。 - web/rpc/jsonrpc 分层:dispatch(统一 Dispatch+OnInternalRequest)、 transport(HTTP/WS+CallFromGin)、register、bridge(声明式路由桥)。 WS 改同步写,删除旧 JsonRpc.go 的死代码 registry。 全量迁移: - 所有 JSON 接口迁为 RPC2 方法(admin/public/client 三命名空间), router 经 bridge.Bind 直接绑定,删除整个 per-resource gin handler 层。 - 渲染器覆盖全部响应形状:standard/flat/raw/message。 - agent v1/v2 上报逻辑统一到 web/api/client/ingest.go。 文件收敛: - web/api/admin 从 ~20 文件+5 子目录降到 6 个 REST-binary 文件 (2fa/theme/download/upload/update/oauth),update 子包合并。 - 二进制/流/重定向/特殊鉴权类保留为 REST。 开发者文档见 web/rpc/jsonrpc/README.md。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
105 lines
2.2 KiB
Go
105 lines
2.2 KiB
Go
package client
|
|
|
|
import (
|
|
"net"
|
|
|
|
"github.com/komari-monitor/komari/database/clients"
|
|
"github.com/komari-monitor/komari/pkg/config"
|
|
"github.com/komari-monitor/komari/utils/geoip"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func getClientIPType(ip net.IP) int {
|
|
// 0:ipv4 1:ipv6 -1:错误的输入
|
|
if ip == nil {
|
|
return -1
|
|
}
|
|
if ip.To4() == nil {
|
|
return 1
|
|
} else {
|
|
return 0
|
|
}
|
|
}
|
|
|
|
func saveClientBasicInfo(info map[string]interface{}, uuid string, fallbackIP string) error {
|
|
info["uuid"] = uuid
|
|
applyFallbackClientIP(info, fallbackIP)
|
|
appendClientRegionFromGeoIP(info)
|
|
return clients.SaveClientInfo(info)
|
|
}
|
|
|
|
func applyFallbackClientIP(info map[string]interface{}, fallbackIP string) {
|
|
if hasClientIP(info) {
|
|
return
|
|
}
|
|
ip := net.ParseIP(fallbackIP)
|
|
|
|
switch getClientIPType(ip) {
|
|
case 0:
|
|
info["ipv4"] = fallbackIP
|
|
case 1:
|
|
info["ipv6"] = fallbackIP
|
|
}
|
|
}
|
|
|
|
func hasClientIP(info map[string]interface{}) bool {
|
|
if ipv4, ok := info["ipv4"].(string); ok && ipv4 != "" {
|
|
return true
|
|
}
|
|
if ipv6, ok := info["ipv6"].(string); ok && ipv6 != "" {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func appendClientRegionFromGeoIP(info map[string]interface{}) {
|
|
cfg, err := config.GetAs[bool](config.GeoIpEnabledKey)
|
|
if err != nil || !cfg {
|
|
return
|
|
}
|
|
|
|
for _, key := range []string{"ipv4", "ipv6"} {
|
|
ipStr, ok := info[key].(string)
|
|
if !ok || ipStr == "" {
|
|
continue
|
|
}
|
|
ip := net.ParseIP(ipStr)
|
|
if ip == nil {
|
|
continue
|
|
}
|
|
record, _ := geoip.GetGeoInfo(ip)
|
|
if record == nil {
|
|
continue
|
|
}
|
|
region := geoip.GetRegionUnicodeEmoji(record.ISOCode)
|
|
if region == "" {
|
|
continue
|
|
}
|
|
info["region"] = region
|
|
return
|
|
}
|
|
}
|
|
|
|
func UploadBasicInfo(c *gin.Context) {
|
|
var cbi = map[string]interface{}{}
|
|
if err := c.ShouldBindJSON(&cbi); err != nil {
|
|
c.JSON(400, gin.H{"status": "error", "error": "Invalid or missing data"})
|
|
return
|
|
}
|
|
|
|
token := c.Query("token")
|
|
uuid, err := clients.GetClientUUIDByToken(token)
|
|
if uuid == "" || err != nil {
|
|
c.JSON(400, gin.H{"status": "error", "error": "Invalid token"})
|
|
return
|
|
}
|
|
|
|
if err := ingestBasicInfo(uuid, cbi, c.ClientIP()); err != nil {
|
|
c.JSON(500, gin.H{"status": "error", "error": err})
|
|
return
|
|
}
|
|
|
|
c.JSON(200, gin.H{"status": "success"})
|
|
}
|