Files
2026-08-25 10:14:32 +08:00

43 lines
1.3 KiB
Go

// Package internal 提供 controller 层的共享工具函数
package check
import (
"context"
"strings"
"gitea.redpowerfuture.com/red-future/common/beans"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
// WithAdminUser 在 context 中注入 admin 用户信息
func WithAdminUser(ctx context.Context) context.Context {
return context.WithValue(ctx, "user", &beans.User{UserName: "admin", TenantId: 1})
}
// WithCallbackUser 在 context 中注入 yidun_callback 用户信息
func WithCallbackUser(ctx context.Context) context.Context {
return context.WithValue(ctx, "user", &beans.User{UserName: "yidun_callback", TenantId: 1})
}
// CheckCallbackIP 校验回调请求 IP 是否在白名单内
// 读取配置 check.callback_allowed_ips,若未配置则跳过校验
func CheckCallbackIP(ctx context.Context) bool {
r := ghttp.RequestFromCtx(ctx)
if r == nil {
return true
}
allowedIPs := g.Cfg().MustGet(ctx, "check.callback_allowed_ips", "").String()
if allowedIPs == "" {
return true
}
clientIP := r.GetClientIp()
for _, ip := range strings.Split(allowedIPs, ",") {
if strings.TrimSpace(ip) == clientIP {
return true
}
}
g.Log().Warningf(ctx, "回调IP不在白名单中, clientIP=%s, allowedIPs=%s", clientIP, allowedIPs)
return false
}