1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-31 14:08:06 +08:00

feat: Optimize Location Information Display in System Login Logs (#7459)

This commit is contained in:
zhengkunwang 2024-12-20 12:16:25 +08:00 committed by GitHub
parent e83d07e88e
commit 69ad01687e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 10 additions and 13 deletions

View File

@ -8,8 +8,6 @@ import (
"github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/captcha" "github.com/1Panel-dev/1Panel/backend/utils/captcha"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/1Panel-dev/1Panel/backend/utils/geo"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -157,11 +155,6 @@ func saveLoginLogs(c *gin.Context, err error) {
logs.Status = constant.StatusSuccess logs.Status = constant.StatusSuccess
} }
logs.IP = c.ClientIP() logs.IP = c.ClientIP()
address, err := geo.GetIPLocation(logs.IP, common.GetLang(c))
if err != nil {
global.LOG.Errorf("get ip location failed: %s", err)
}
logs.Agent = c.GetHeader("User-Agent") logs.Agent = c.GetHeader("User-Agent")
logs.Address = address
_ = logService.CreateLoginLog(logs) _ = logService.CreateLoginLog(logs)
} }

View File

@ -21,7 +21,7 @@ func (b *BaseApi) GetLoginLogs(c *gin.Context) {
return return
} }
total, list, err := logService.PageLoginLog(req) total, list, err := logService.PageLoginLog(c, req)
if err != nil { if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return return

View File

@ -2,6 +2,9 @@ package service
import ( import (
"fmt" "fmt"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/1Panel-dev/1Panel/backend/utils/geo"
"github.com/gin-gonic/gin"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -26,7 +29,7 @@ const logs = "https://resource.fit2cloud.com/installation-log.sh"
type ILogService interface { type ILogService interface {
ListSystemLogFile() ([]string, error) ListSystemLogFile() ([]string, error)
CreateLoginLog(operation model.LoginLog) error CreateLoginLog(operation model.LoginLog) error
PageLoginLog(search dto.SearchLgLogWithPage) (int64, interface{}, error) PageLoginLog(c *gin.Context, search dto.SearchLgLogWithPage) (int64, interface{}, error)
CreateOperationLog(operation model.OperationLog) error CreateOperationLog(operation model.OperationLog) error
PageOperationLog(search dto.SearchOpLogWithPage) (int64, interface{}, error) PageOperationLog(search dto.SearchOpLogWithPage) (int64, interface{}, error)
@ -77,7 +80,7 @@ func (u *LogService) ListSystemLogFile() ([]string, error) {
return files, nil return files, nil
} }
func (u *LogService) PageLoginLog(req dto.SearchLgLogWithPage) (int64, interface{}, error) { func (u *LogService) PageLoginLog(c *gin.Context, req dto.SearchLgLogWithPage) (int64, interface{}, error) {
total, ops, err := logRepo.PageLoginLog( total, ops, err := logRepo.PageLoginLog(
req.Page, req.Page,
req.PageSize, req.PageSize,
@ -91,6 +94,7 @@ func (u *LogService) PageLoginLog(req dto.SearchLgLogWithPage) (int64, interface
if err := copier.Copy(&item, &op); err != nil { if err := copier.Copy(&item, &op); err != nil {
return 0, nil, errors.WithMessage(constant.ErrStructTransform, err.Error()) return 0, nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
} }
item.Address, _ = geo.GetIPLocation(item.IP, common.GetLang(c))
dtoOps = append(dtoOps, item) dtoOps = append(dtoOps, item)
} }
return total, dtoOps, err return total, dtoOps, err

View File

@ -32,8 +32,8 @@ func GetIPLocation(ip, lang string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
if lang == "en" { if lang == "zh" {
return geoLocation.Country.En + " " + geoLocation.Province.En, nil return geoLocation.Country.Zh + " " + geoLocation.Province.Zh, nil
} }
return geoLocation.Country.Zh + " " + geoLocation.Province.Zh, nil return geoLocation.Country.En + " " + geoLocation.Province.En, nil
} }