Files

139 lines
3.8 KiB
Go
Raw Permalink Normal View History

2024-07-19 19:04:11 +08:00
package service
import (
"bytes"
"fmt"
"net/http"
"os"
"github.com/1Panel-dev/1Panel/core/buserr"
"github.com/1Panel-dev/1Panel/core/constant"
"github.com/1Panel-dev/1Panel/core/utils/cmd"
"github.com/1Panel-dev/1Panel/core/utils/common"
geo2 "github.com/1Panel-dev/1Panel/core/utils/geo"
"github.com/1Panel-dev/1Panel/core/utils/req_helper"
"github.com/gin-gonic/gin"
2024-07-19 19:04:11 +08:00
"github.com/1Panel-dev/1Panel/core/app/dto"
"github.com/1Panel-dev/1Panel/core/app/model"
"github.com/1Panel-dev/1Panel/core/app/repo"
2024-07-19 19:04:11 +08:00
"github.com/1Panel-dev/1Panel/core/global"
"github.com/jinzhu/copier"
)
type LogService struct{}
const logs = "https://resource.fit2cloud.com/installation-log.sh"
type ILogService interface {
CreateLoginLog(operation model.LoginLog) error
2025-01-15 18:25:59 +08:00
PageLoginLog(ctx *gin.Context, search dto.SearchLgLogWithPage) (int64, interface{}, error)
2024-07-19 19:04:11 +08:00
2024-07-29 18:09:57 +08:00
CreateOperationLog(operation *model.OperationLog) error
2024-07-19 19:04:11 +08:00
PageOperationLog(search dto.SearchOpLogWithPage) (int64, interface{}, error)
CleanLogs(logtype string) error
}
func NewILogService() ILogService {
return &LogService{}
}
func (u *LogService) CreateLoginLog(operation model.LoginLog) error {
return logRepo.CreateLoginLog(&operation)
}
2025-01-15 18:25:59 +08:00
func (u *LogService) PageLoginLog(ctx *gin.Context, req dto.SearchLgLogWithPage) (int64, interface{}, error) {
2024-11-30 19:42:41 +08:00
options := []global.DBOption{
repo.WithOrderDesc("created_at"),
2024-11-30 19:42:41 +08:00
}
if len(req.Info) != 0 {
options = append(options, logRepo.WithByInfo(req.Info))
2024-11-30 19:42:41 +08:00
}
if len(req.Status) != 0 {
options = append(options, repo.WithByStatus(req.Status))
2024-11-30 19:42:41 +08:00
}
2026-05-27 14:09:50 +08:00
if !req.StartTime.IsZero() && !req.EndTime.IsZero() {
options = append(options, repo.WithByCreatedAt(req.StartTime, req.EndTime))
}
2024-07-19 19:04:11 +08:00
total, ops, err := logRepo.PageLoginLog(
req.Page,
req.PageSize,
2024-11-30 19:42:41 +08:00
options...,
2024-07-19 19:04:11 +08:00
)
var dtoOps []dto.LoginLog
2025-01-15 18:25:59 +08:00
geoDB, _ := geo2.NewGeo()
2024-07-19 19:04:11 +08:00
for _, op := range ops {
var item dto.LoginLog
if err := copier.Copy(&item, &op); err != nil {
return 0, nil, buserr.WithErr("ErrTransform", err)
2024-07-19 19:04:11 +08:00
}
2025-01-15 18:25:59 +08:00
if geoDB != nil {
item.Address, _ = geo2.GetIPLocation(geoDB, item.IP, common.GetLang(ctx))
}
2024-07-19 19:04:11 +08:00
dtoOps = append(dtoOps, item)
}
return total, dtoOps, err
}
2024-07-29 18:09:57 +08:00
func (u *LogService) CreateOperationLog(operation *model.OperationLog) error {
return logRepo.CreateOperationLog(operation)
2024-07-19 19:04:11 +08:00
}
func (u *LogService) PageOperationLog(req dto.SearchOpLogWithPage) (int64, interface{}, error) {
2024-11-30 19:42:41 +08:00
options := []global.DBOption{
repo.WithOrderDesc("created_at"),
2024-11-30 19:42:41 +08:00
logRepo.WithByLikeOperation(req.Operation),
}
if len(req.Source) != 0 {
options = append(options, logRepo.WithBySource(req.Source))
}
if len(req.Status) != 0 {
options = append(options, repo.WithByStatus(req.Status))
2024-11-30 19:42:41 +08:00
}
if len(req.Node) != 0 {
options = append(options, repo.WithByNode(req.Node))
}
2024-11-30 19:42:41 +08:00
2024-07-19 19:04:11 +08:00
total, ops, err := logRepo.PageOperationLog(
req.Page,
req.PageSize,
2024-11-30 19:42:41 +08:00
options...,
2024-07-19 19:04:11 +08:00
)
var dtoOps []dto.OperationLog
for _, op := range ops {
var item dto.OperationLog
if err := copier.Copy(&item, &op); err != nil {
return 0, nil, buserr.WithErr("ErrTransform", err)
2024-07-19 19:04:11 +08:00
}
dtoOps = append(dtoOps, item)
}
return total, dtoOps, err
}
func (u *LogService) CleanLogs(logtype string) error {
if logtype == "operation" {
return logRepo.CleanOperation()
}
return logRepo.CleanLogin()
}
func writeLogs(version string) {
_ = runRemoteShellScript(logs, "1p", "upgrade", version)
}
func runRemoteShellScript(url string, args ...string) error {
statusCode, script, err := req_helper.HandleRequestWithProxy(url, http.MethodGet, constant.TimeOut20s)
if err != nil {
return err
}
if statusCode < http.StatusOK || statusCode >= http.StatusMultipleChoices {
return fmt.Errorf("download script failed, status code: %d", statusCode)
}
_, err = cmd.NewCommandMgr().RunPipeToFile(os.DevNull, cmd.PipeCommand{
Name: "sh",
Args: append([]string{"-s"}, args...),
Stdin: bytes.NewReader(script),
})
return err
2024-07-19 19:04:11 +08:00
}