2026-08-09 10:43:45 +08:00
|
|
|
|
package log
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
"log"
|
2026-09-22 21:15:34 +08:00
|
|
|
|
"net"
|
2026-08-09 10:43:45 +08:00
|
|
|
|
|
2026-08-09 12:41:08 +08:00
|
|
|
|
"bsm/full/module/base/logs/internal/impl"
|
|
|
|
|
|
"bsm/full/module/base/logs/internal/models"
|
2026-08-09 10:43:45 +08:00
|
|
|
|
"git.apinb.com/bsm-sdk/core/infra"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func Create(c *gin.Context) {
|
|
|
|
|
|
var (
|
|
|
|
|
|
request = make([]*models.LogData, 0)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-09-22 21:15:34 +08:00
|
|
|
|
// 验证请求IP:仅允许内网/本机地址提交,禁止公网网段提交
|
|
|
|
|
|
if !isPrivateIP(c.ClientIP()) {
|
2026-08-09 10:43:45 +08:00
|
|
|
|
log.Printf("request ip is not allowed")
|
|
|
|
|
|
err := errors.New("request ip is not allowed")
|
|
|
|
|
|
infra.Response.Error(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err := c.BindJSON(&request)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Printf("parse json error: %v", err)
|
|
|
|
|
|
infra.Response.Error(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(request) == 0 {
|
|
|
|
|
|
log.Printf("request data is empty")
|
|
|
|
|
|
infra.Response.Error(c, errors.New("request data is empty"))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := impl.DBService.Model(&models.LogData{}).Create(&request).Error; err != nil {
|
|
|
|
|
|
log.Printf("save log data error: %v", err)
|
|
|
|
|
|
infra.Response.Error(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
infra.Response.Success(c, "")
|
|
|
|
|
|
}
|
2026-09-22 21:15:34 +08:00
|
|
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
|
}
|