fix version 1
This commit is contained in:
@@ -3,7 +3,7 @@ package log
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"strings"
|
||||
"net"
|
||||
|
||||
"bsm/full/module/base/logs/internal/impl"
|
||||
"bsm/full/module/base/logs/internal/models"
|
||||
@@ -16,9 +16,8 @@ func Create(c *gin.Context) {
|
||||
request = make([]*models.LogData, 0)
|
||||
)
|
||||
|
||||
// 验证请求IP,禁止公网网段提交
|
||||
ip := c.ClientIP()
|
||||
if ip == "127.0.0.1" || ip == "localhost" || strings.HasPrefix(ip, "10.") || strings.HasPrefix(ip, "172.") || strings.HasPrefix(ip, "192.") {
|
||||
// 验证请求IP:仅允许内网/本机地址提交,禁止公网网段提交
|
||||
if !isPrivateIP(c.ClientIP()) {
|
||||
log.Printf("request ip is not allowed")
|
||||
err := errors.New("request ip is not allowed")
|
||||
infra.Response.Error(c, err)
|
||||
@@ -45,3 +44,15 @@ func Create(c *gin.Context) {
|
||||
|
||||
infra.Response.Success(c, "")
|
||||
}
|
||||
|
||||
// isPrivateIP 判断是否为内网或本机地址
|
||||
func isPrivateIP(ip string) bool {
|
||||
if ip == "localhost" {
|
||||
return true
|
||||
}
|
||||
parsed := net.ParseIP(ip)
|
||||
if parsed == nil {
|
||||
return false
|
||||
}
|
||||
return parsed.IsLoopback() || parsed.IsPrivate()
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
var (
|
||||
DefaultPage = 1
|
||||
DefaultSize = 50
|
||||
MaxSize = 200
|
||||
)
|
||||
|
||||
func LogFetch(c *gin.Context) {
|
||||
@@ -30,26 +31,27 @@ func LogFetch(c *gin.Context) {
|
||||
tx = impl.DBService.Model(&models.LogData{})
|
||||
)
|
||||
|
||||
var size int
|
||||
var page int
|
||||
if request["op_name"] != "" {
|
||||
tx.Where("op_name like ?", "%"+request["op_name"].(string)+"%")
|
||||
}
|
||||
if request["service"] != "" {
|
||||
tx.Where("service like ?", "%"+request["service"].(string)+"%")
|
||||
}
|
||||
if request["op_ip"] != "" {
|
||||
tx.Where("op_ip = ?", "%"+request["ip"].(string)+"%")
|
||||
}
|
||||
if request["level"] != "" {
|
||||
tx.Where("level = ?", request["level"].(int))
|
||||
// 分页参数归一化:page 至少为 1,size 默认 50 且不超过上限
|
||||
page := normalizePage(getInt(request, "page"))
|
||||
sizeInput := getInt(request, "size")
|
||||
if sizeInput <= 0 {
|
||||
// 兼容 page_size 写法
|
||||
sizeInput = getInt(request, "page_size")
|
||||
}
|
||||
size := normalizeSize(sizeInput)
|
||||
|
||||
if page <= 0 {
|
||||
page = DefaultPage
|
||||
// 过滤条件统一用安全类型断言取值,键名与查询列保持一致
|
||||
if opName := getString(request, "op_name"); opName != "" {
|
||||
tx = tx.Where("op_name like ?", "%"+opName+"%")
|
||||
}
|
||||
if size <= 0 {
|
||||
size = DefaultSize
|
||||
if service := getString(request, "service"); service != "" {
|
||||
tx = tx.Where("service like ?", "%"+service+"%")
|
||||
}
|
||||
if opIP := getString(request, "op_ip"); opIP != "" {
|
||||
tx = tx.Where("op_ip = ?", opIP)
|
||||
}
|
||||
if level := getInt(request, "level"); level > 0 {
|
||||
tx = tx.Where("level = ?", level)
|
||||
}
|
||||
|
||||
if err := tx.Count(&count).Limit(size).Offset((page - 1) * size).Find(&data).Error; err != nil {
|
||||
@@ -63,3 +65,43 @@ func LogFetch(c *gin.Context) {
|
||||
"total": count,
|
||||
})
|
||||
}
|
||||
|
||||
// getString 安全读取字符串参数,缺失或类型不符时返回空串
|
||||
func getString(m map[string]any, key string) string {
|
||||
if v, ok := m[key].(string); ok {
|
||||
return v
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// getInt 安全读取整型参数,兼容 JSON 反序列化产生的 float64
|
||||
func getInt(m map[string]any, key string) int {
|
||||
switch v := m[key].(type) {
|
||||
case int:
|
||||
return v
|
||||
case int64:
|
||||
return int(v)
|
||||
case float64:
|
||||
return int(v)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// normalizePage 归一化页码,最小为 DefaultPage
|
||||
func normalizePage(page int) int {
|
||||
if page < DefaultPage {
|
||||
return DefaultPage
|
||||
}
|
||||
return page
|
||||
}
|
||||
|
||||
// normalizeSize 归一化每页条数:默认 DefaultSize,并限制不超过 MaxSize
|
||||
func normalizeSize(size int) int {
|
||||
if size <= 0 {
|
||||
return DefaultSize
|
||||
}
|
||||
if size > MaxSize {
|
||||
return MaxSize
|
||||
}
|
||||
return size
|
||||
}
|
||||
|
||||
@@ -6,25 +6,33 @@ import (
|
||||
"bsm/full/module/base/logs/internal/logic/hello"
|
||||
"bsm/full/module/base/logs/internal/logic/log"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/middleware"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Register(srvKey string, engine *gin.Engine) {
|
||||
v1_key := fmt.Sprintf("/rest/%s", srvKey)
|
||||
registerAnonymous(v1_key, engine)
|
||||
registerAuth(v1_key, engine)
|
||||
}
|
||||
|
||||
// registerAnonymous 不需要auth的接口
|
||||
func registerAnonymous(v1_key string, engine *gin.Engine) {
|
||||
// Anonymous router.
|
||||
fmt.Println(v1_key)
|
||||
anonymous := engine.Group(v1_key)
|
||||
{
|
||||
// anonymous.Use(middleware.JwtAuth(impl.RedisCache))
|
||||
anonymous.GET("/ping", hello.Ping)
|
||||
anonymous.POST("/create", log.Create)
|
||||
anonymous.POST("/fetch", log.LogFetch)
|
||||
anonymous.POST("/total", log.Total)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// registerAuth 需要JWT鉴权的业务接口
|
||||
func registerAuth(v1_key string, engine *gin.Engine) {
|
||||
// Authorized router.
|
||||
auth := engine.Group(v1_key)
|
||||
auth.Use(middleware.JwtAuth(true))
|
||||
{
|
||||
auth.POST("/create", log.Create)
|
||||
auth.POST("/fetch", log.LogFetch)
|
||||
auth.POST("/total", log.Total)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user