From 69ad01687ee79d0fe2afc3dfee1b011c6ab54e7c Mon Sep 17 00:00:00 2001 From: zhengkunwang <31820853+zhengkunwang223@users.noreply.github.com> Date: Fri, 20 Dec 2024 12:16:25 +0800 Subject: [PATCH] feat: Optimize Location Information Display in System Login Logs (#7459) --- backend/app/api/v1/auth.go | 7 ------- backend/app/api/v1/logs.go | 2 +- backend/app/service/logs.go | 8 ++++++-- backend/utils/geo/geo.go | 6 +++--- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/backend/app/api/v1/auth.go b/backend/app/api/v1/auth.go index 559e2e449..5907c2c9c 100644 --- a/backend/app/api/v1/auth.go +++ b/backend/app/api/v1/auth.go @@ -8,8 +8,6 @@ import ( "github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/global" "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" ) @@ -157,11 +155,6 @@ func saveLoginLogs(c *gin.Context, err error) { logs.Status = constant.StatusSuccess } 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.Address = address _ = logService.CreateLoginLog(logs) } diff --git a/backend/app/api/v1/logs.go b/backend/app/api/v1/logs.go index 0e2cfb281..cc33c6f95 100644 --- a/backend/app/api/v1/logs.go +++ b/backend/app/api/v1/logs.go @@ -21,7 +21,7 @@ func (b *BaseApi) GetLoginLogs(c *gin.Context) { return } - total, list, err := logService.PageLoginLog(req) + total, list, err := logService.PageLoginLog(c, req) if err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return diff --git a/backend/app/service/logs.go b/backend/app/service/logs.go index 41c2b7ffe..6256f7d7d 100644 --- a/backend/app/service/logs.go +++ b/backend/app/service/logs.go @@ -2,6 +2,9 @@ package service import ( "fmt" + "github.com/1Panel-dev/1Panel/backend/utils/common" + "github.com/1Panel-dev/1Panel/backend/utils/geo" + "github.com/gin-gonic/gin" "os" "path" "path/filepath" @@ -26,7 +29,7 @@ const logs = "https://resource.fit2cloud.com/installation-log.sh" type ILogService interface { ListSystemLogFile() ([]string, 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 PageOperationLog(search dto.SearchOpLogWithPage) (int64, interface{}, error) @@ -77,7 +80,7 @@ func (u *LogService) ListSystemLogFile() ([]string, error) { 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( req.Page, req.PageSize, @@ -91,6 +94,7 @@ func (u *LogService) PageLoginLog(req dto.SearchLgLogWithPage) (int64, interface if err := copier.Copy(&item, &op); err != nil { return 0, nil, errors.WithMessage(constant.ErrStructTransform, err.Error()) } + item.Address, _ = geo.GetIPLocation(item.IP, common.GetLang(c)) dtoOps = append(dtoOps, item) } return total, dtoOps, err diff --git a/backend/utils/geo/geo.go b/backend/utils/geo/geo.go index 5e15937e0..6ef24fe52 100644 --- a/backend/utils/geo/geo.go +++ b/backend/utils/geo/geo.go @@ -32,8 +32,8 @@ func GetIPLocation(ip, lang string) (string, error) { if err != nil { return "", err } - if lang == "en" { - return geoLocation.Country.En + " " + geoLocation.Province.En, nil + if lang == "zh" { + return geoLocation.Country.Zh + " " + geoLocation.Province.Zh, nil } - return geoLocation.Country.Zh + " " + geoLocation.Province.Zh, nil + return geoLocation.Country.En + " " + geoLocation.Province.En, nil }