59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package log
|
||
|
||
import (
|
||
"errors"
|
||
"log"
|
||
"net"
|
||
|
||
"bsm/full/module/base/logs/internal/impl"
|
||
"bsm/full/module/base/logs/internal/models"
|
||
"git.apinb.com/bsm-sdk/core/infra"
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
func Create(c *gin.Context) {
|
||
var (
|
||
request = make([]*models.LogData, 0)
|
||
)
|
||
|
||
// 验证请求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)
|
||
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, "")
|
||
}
|
||
|
||
// 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()
|
||
}
|