2022-11-15 17:20:57 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2023-09-20 16:24:26 +08:00
|
|
|
"fmt"
|
2023-08-02 16:47:30 +08:00
|
|
|
"os"
|
|
|
|
"path"
|
2023-09-20 16:24:26 +08:00
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2023-10-07 16:58:46 +08:00
|
|
|
"time"
|
2023-08-02 16:47:30 +08:00
|
|
|
|
2022-11-15 17:20:57 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2023-08-02 16:47:30 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
2022-11-15 17:20:57 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2023-08-02 16:47:30 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2023-03-15 15:58:26 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
2022-11-15 17:20:57 +08:00
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LogService struct{}
|
|
|
|
|
2023-03-15 15:58:26 +08:00
|
|
|
const logs = "https://resource.fit2cloud.com/installation-log.sh"
|
|
|
|
|
2022-11-15 17:20:57 +08:00
|
|
|
type ILogService interface {
|
2023-09-20 16:24:26 +08:00
|
|
|
ListSystemLogFile() ([]string, error)
|
2022-11-15 17:20:57 +08:00
|
|
|
CreateLoginLog(operation model.LoginLog) error
|
2023-02-07 18:48:32 +08:00
|
|
|
PageLoginLog(search dto.SearchLgLogWithPage) (int64, interface{}, error)
|
2022-11-15 17:20:57 +08:00
|
|
|
|
|
|
|
CreateOperationLog(operation model.OperationLog) error
|
2023-02-07 18:48:32 +08:00
|
|
|
PageOperationLog(search dto.SearchOpLogWithPage) (int64, interface{}, error)
|
2022-11-15 18:44:50 +08:00
|
|
|
|
2023-09-20 16:24:26 +08:00
|
|
|
LoadSystemLog(name string) (string, error)
|
2023-08-02 16:47:30 +08:00
|
|
|
|
2022-11-15 18:44:50 +08:00
|
|
|
CleanLogs(logtype string) error
|
2022-11-15 17:20:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewILogService() ILogService {
|
|
|
|
return &LogService{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *LogService) CreateLoginLog(operation model.LoginLog) error {
|
|
|
|
return logRepo.CreateLoginLog(&operation)
|
|
|
|
}
|
|
|
|
|
2023-09-20 16:24:26 +08:00
|
|
|
func (u *LogService) ListSystemLogFile() ([]string, error) {
|
|
|
|
logDir := path.Join(global.CONF.System.BaseDir, "1panel/log")
|
|
|
|
var files []string
|
|
|
|
if err := filepath.Walk(logDir, func(pathItem string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-12-07 19:02:07 +08:00
|
|
|
if !info.IsDir() && strings.HasPrefix(info.Name(), "1Panel") {
|
2023-10-07 16:58:46 +08:00
|
|
|
if info.Name() == "1Panel.log" {
|
|
|
|
files = append(files, time.Now().Format("2006-01-02"))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
itemFileName := strings.TrimPrefix(info.Name(), "1Panel-")
|
|
|
|
itemFileName = strings.TrimSuffix(itemFileName, ".gz")
|
|
|
|
itemFileName = strings.TrimSuffix(itemFileName, ".log")
|
|
|
|
files = append(files, itemFileName)
|
2023-09-20 16:24:26 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(files) < 2 {
|
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
sort.Slice(files, func(i, j int) bool {
|
|
|
|
return files[i] > files[j]
|
|
|
|
})
|
|
|
|
|
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
2023-02-07 18:48:32 +08:00
|
|
|
func (u *LogService) PageLoginLog(req dto.SearchLgLogWithPage) (int64, interface{}, error) {
|
|
|
|
total, ops, err := logRepo.PageLoginLog(
|
|
|
|
req.Page,
|
|
|
|
req.PageSize,
|
|
|
|
logRepo.WithByIP(req.IP),
|
|
|
|
logRepo.WithByStatus(req.Status),
|
|
|
|
commonRepo.WithOrderBy("created_at desc"),
|
|
|
|
)
|
2022-11-15 17:20:57 +08:00
|
|
|
var dtoOps []dto.LoginLog
|
|
|
|
for _, op := range ops {
|
|
|
|
var item dto.LoginLog
|
|
|
|
if err := copier.Copy(&item, &op); err != nil {
|
|
|
|
return 0, nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
|
|
|
dtoOps = append(dtoOps, item)
|
|
|
|
}
|
|
|
|
return total, dtoOps, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *LogService) CreateOperationLog(operation model.OperationLog) error {
|
|
|
|
return logRepo.CreateOperationLog(&operation)
|
|
|
|
}
|
|
|
|
|
2023-02-07 18:48:32 +08:00
|
|
|
func (u *LogService) PageOperationLog(req dto.SearchOpLogWithPage) (int64, interface{}, error) {
|
|
|
|
total, ops, err := logRepo.PageOperationLog(
|
|
|
|
req.Page,
|
|
|
|
req.PageSize,
|
|
|
|
logRepo.WithByGroup(req.Source),
|
|
|
|
logRepo.WithByLikeOperation(req.Operation),
|
|
|
|
logRepo.WithByStatus(req.Status),
|
|
|
|
commonRepo.WithOrderBy("created_at desc"),
|
|
|
|
)
|
2022-11-15 17:20:57 +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, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
|
|
|
dtoOps = append(dtoOps, item)
|
|
|
|
}
|
|
|
|
return total, dtoOps, err
|
|
|
|
}
|
|
|
|
|
2023-09-20 16:24:26 +08:00
|
|
|
func (u *LogService) LoadSystemLog(name string) (string, error) {
|
2023-10-07 16:58:46 +08:00
|
|
|
if name == time.Now().Format("2006-01-02") {
|
|
|
|
name = "1Panel.log"
|
|
|
|
} else {
|
|
|
|
name = "1Panel-" + name + ".log"
|
|
|
|
}
|
2023-09-20 16:24:26 +08:00
|
|
|
filePath := path.Join(global.CONF.System.DataDir, "log", name)
|
2023-08-02 16:47:30 +08:00
|
|
|
if _, err := os.Stat(filePath); err != nil {
|
2023-09-20 16:24:26 +08:00
|
|
|
fileGzPath := path.Join(global.CONF.System.DataDir, "log", name+".gz")
|
|
|
|
if _, err := os.Stat(fileGzPath); err != nil {
|
|
|
|
return "", buserr.New("ErrHttpReqNotFound")
|
|
|
|
}
|
|
|
|
if err := handleGunzip(fileGzPath); err != nil {
|
2023-10-07 16:58:46 +08:00
|
|
|
return "", fmt.Errorf("handle ungzip file %s failed, err: %v", fileGzPath, err)
|
2023-09-20 16:24:26 +08:00
|
|
|
}
|
2023-08-02 16:47:30 +08:00
|
|
|
}
|
|
|
|
content, err := os.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(content), nil
|
|
|
|
}
|
|
|
|
|
2022-11-15 18:44:50 +08:00
|
|
|
func (u *LogService) CleanLogs(logtype string) error {
|
|
|
|
if logtype == "operation" {
|
|
|
|
return logRepo.CleanOperation()
|
|
|
|
}
|
|
|
|
return logRepo.CleanLogin()
|
|
|
|
}
|
2023-03-15 15:58:26 +08:00
|
|
|
|
|
|
|
func writeLogs(version string) {
|
|
|
|
_, _ = cmd.Execf("curl -sfL %s | sh -s 1p upgrade %s", logs, version)
|
|
|
|
}
|