Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions agent/app/api/v2/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package v2

import (
"github.com/1Panel-dev/1Panel/agent/app/api/v2/helper"
"github.com/1Panel-dev/1Panel/agent/app/dto"
"github.com/gin-gonic/gin"
)

Expand All @@ -21,24 +20,3 @@ func (b *BaseApi) GetSystemFiles(c *gin.Context) {

helper.SuccessWithData(c, data)
}

// @Tags Logs
// @Summary Load system logs
// @Success 200 {string} data
// @Security ApiKeyAuth
// @Security Timestamp
// @Router /logs/system [post]
func (b *BaseApi) GetSystemLogs(c *gin.Context) {
var req dto.OperationWithName
if err := helper.CheckBindAndValidate(&req, c); err != nil {
return
}

data, err := logService.LoadSystemLog(req.Name)
if err != nil {
helper.InternalServer(c, err)
return
}

helper.SuccessWithData(c, data)
}
18 changes: 10 additions & 8 deletions agent/app/service/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (u *BackupService) Create(req dto.BackupOperate) error {
}
backup.Credential = string(itemCredential)

if req.Type == constant.OneDrive || req.Type == constant.GoogleDrive {
if req.Type == constant.OneDrive || req.Type == constant.GoogleDrive || req.Type == constant.ALIYUN {
if err := loadRefreshTokenByCode(&backup); err != nil {
return err
}
Expand Down Expand Up @@ -247,7 +247,7 @@ func (u *BackupService) Update(req dto.BackupOperate) error {
global.Dir.LocalBackupDir = newBackup.BackupPath
}

if newBackup.Type == constant.OneDrive || newBackup.Type == constant.GoogleDrive {
if newBackup.Type == constant.OneDrive || newBackup.Type == constant.GoogleDrive || newBackup.Type == constant.ALIYUN {
if err := loadRefreshTokenByCode(&newBackup); err != nil {
return err
}
Expand Down Expand Up @@ -501,16 +501,19 @@ func loadRefreshTokenByCode(backup *model.BackupAccount) error {
if err != nil {
return err
}
} else {
}
if backup.Type == constant.OneDrive {
refreshToken, err = client.RefreshToken("authorization_code", "refreshToken", varMap)
if err != nil {
return err
}
}
delete(varMap, "code")
if backup.Type != constant.ALIYUN {
delete(varMap, "code")
varMap["refresh_token"] = refreshToken
}
varMap["refresh_status"] = constant.StatusSuccess
varMap["refresh_time"] = time.Now().Format(constant.DateTimeLayout)
varMap["refresh_token"] = refreshToken
itemVars, err := json.Marshal(varMap)
if err != nil {
return fmt.Errorf("json marshal var map failed, err: %v", err)
Expand All @@ -535,10 +538,9 @@ func loadBackupNamesByID(accountIDs string, downloadID uint) ([]string, string,
var accounts []string
var downloadAccount string
for _, item := range list {
itemName := fmt.Sprintf("%s - %s", item.Type, item.Name)
accounts = append(accounts, itemName)
accounts = append(accounts, item.Name)
if item.ID == downloadID {
downloadAccount = itemName
downloadAccount = item.Name
}
}
return accounts, downloadAccount, nil
Expand Down
14 changes: 4 additions & 10 deletions agent/app/service/backup_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,26 +176,20 @@ func (u *BackupRecordService) ListAppRecords(name, detailName, fileName string)
}

func (u *BackupRecordService) ListFiles(req dto.OperateByID) []string {
var datas []string
backupItem, client, err := NewBackupClientWithID(req.ID)
if err != nil {
return datas
return []string{}
}
prefix := "system_snapshot"
if len(backupItem.BackupPath) != 0 {
if len(backupItem.BackupPath) != 0 {
prefix = path.Join(backupItem.BackupPath, prefix)
}
files, err := client.ListObjects(prefix)
if err != nil {
global.LOG.Debugf("load files failed, err: %v", err)
return datas
return []string{}
}
for _, file := range files {
if len(file) != 0 {
datas = append(datas, path.Base(file))
}
}
return datas
return files
}

type backupSizeHelper struct {
Expand Down
18 changes: 8 additions & 10 deletions agent/app/service/clam.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"os/exec"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -543,16 +542,15 @@ func StopAllCronJob(withCheck bool) bool {
func loadFileByName(name string) []string {
var logPaths []string
pathItem := path.Join(global.Dir.DataDir, resultDir, name)
_ = filepath.Walk(pathItem, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.IsDir() || info.Name() == name {
return nil
fileItems, err := os.ReadDir(pathItem)
if err != nil {
return logPaths
}
for _, item := range fileItems {
if !item.IsDir() {
logPaths = append(logPaths, item.Name())
}
logPaths = append(logPaths, info.Name())
return nil
})
}
return logPaths
}
func loadResultFromLog(pathItem string) dto.ClamLog {
Expand Down
68 changes: 21 additions & 47 deletions agent/app/service/logs.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
package service

import (
"fmt"
"os"
"path"
"sort"
"strings"
"time"

"github.com/1Panel-dev/1Panel/agent/buserr"
"github.com/1Panel-dev/1Panel/agent/global"
)

type LogService struct{}

type ILogService interface {
ListSystemLogFile() ([]string, error)
LoadSystemLog(name string) (string, error)
}

func NewILogService() ILogService {
Expand All @@ -31,29 +27,30 @@ func (u *LogService) ListSystemLogFile() ([]string, error) {
}
listMap := make(map[string]struct{})
for _, item := range files {
if !item.IsDir() && strings.HasPrefix(item.Name(), "1Panel") {
if item.Name() == "1Panel.log" || item.Name() == "1Panel-Core.log" {
itemName := time.Now().Format("2006-01-02")
if _, ok := listMap[itemName]; ok {
continue
}
listMap[itemName] = struct{}{}
listFile = append(listFile, itemName)
continue
}
itemFileName := strings.TrimPrefix(item.Name(), "1Panel-Core-")
itemFileName = strings.TrimPrefix(itemFileName, "1Panel-")
itemFileName = strings.TrimSuffix(itemFileName, ".gz")
itemFileName = strings.TrimSuffix(itemFileName, ".log")
if len(itemFileName) == 0 {
continue
}
if _, ok := listMap[itemFileName]; ok {
if item.IsDir() || !strings.HasPrefix(item.Name(), "1Panel") {
continue
}
if item.Name() == "1Panel.log" || item.Name() == "1Panel-Core.log" {
itemName := time.Now().Format("2006-01-02")
if _, ok := listMap[itemName]; ok {
continue
}
listMap[itemFileName] = struct{}{}
listFile = append(listFile, itemFileName)
listMap[itemName] = struct{}{}
listFile = append(listFile, itemName)
continue
}
itemFileName := strings.TrimPrefix(item.Name(), "1Panel-Core-")
itemFileName = strings.TrimPrefix(itemFileName, "1Panel-")
itemFileName = strings.TrimSuffix(itemFileName, ".gz")
itemFileName = strings.TrimSuffix(itemFileName, ".log")
if len(itemFileName) == 0 {
continue
}
if _, ok := listMap[itemFileName]; ok {
continue
}
listMap[itemFileName] = struct{}{}
listFile = append(listFile, itemFileName)
}
if len(listFile) < 2 {
return listFile, nil
Expand All @@ -64,26 +61,3 @@ func (u *LogService) ListSystemLogFile() ([]string, error) {

return listFile, nil
}

func (u *LogService) LoadSystemLog(name string) (string, error) {
if name == time.Now().Format("2006-01-02") {
name = "1Panel.log"
} else {
name = "1Panel-" + name + ".log"
}
filePath := path.Join(global.Dir.LogDir, name)
if _, err := os.Stat(filePath); err != nil {
fileGzPath := path.Join(global.Dir.LogDir, name+".gz")
if _, err := os.Stat(fileGzPath); err != nil {
return "", buserr.New("ErrHttpReqNotFound")
}
if err := handleGunzip(fileGzPath); err != nil {
return "", fmt.Errorf("handle ungzip file %s failed, err: %v", fileGzPath, err)
}
}
content, err := os.ReadFile(filePath)
if err != nil {
return "", err
}
return string(content), nil
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code differences: The method LoadSystemLog checks for existence of both .log and .gz files with the current date suffix but only returns the content of the .log file if it exists after uncompression. This might not handle cases where users log in with a previous date which does not have a corresponding .gz file.

Optimization suggestion: Instead of directly returning the contents of the .log file, consider handling both .log and .gz files within the same method to ensure consistency, especially for logging into any day prior to today's logging period.

Additional notes: Ensure that the global.Dir.LogDir is correctly set up and accessible. Also, make sure there are no typos or unnecessary lines left from earlier version changes.

34 changes: 17 additions & 17 deletions agent/app/service/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"os/user"
"path"
"path/filepath"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -288,25 +287,26 @@ func (u *SSHService) LoadLog(ctx *gin.Context, req dto.SearchSSHLog) (*dto.SSHLo
var fileList []sshFileItem
var data dto.SSHLog
baseDir := "/var/log"
if err := filepath.Walk(baseDir, func(pathItem string, info os.FileInfo, err error) error {
if err != nil {
return err
fileItems, err := os.ReadDir(baseDir)
if err != nil {
return &data, err
}
for _, item := range fileItems {
if item.IsDir() || (!strings.HasPrefix(item.Name(), "secure") && !strings.HasPrefix(item.Name(), "auth")) {
continue
}
if !info.IsDir() && (strings.HasPrefix(info.Name(), "secure") || strings.HasPrefix(info.Name(), "auth")) {
if !strings.HasSuffix(info.Name(), ".gz") {
fileList = append(fileList, sshFileItem{Name: pathItem, Year: info.ModTime().Year()})
return nil
}
itemFileName := strings.TrimSuffix(pathItem, ".gz")
if _, err := os.Stat(itemFileName); err != nil && os.IsNotExist(err) {
if err := handleGunzip(pathItem); err == nil {
fileList = append(fileList, sshFileItem{Name: itemFileName, Year: info.ModTime().Year()})
}
info, _ := item.Info()
itemPath := path.Join(baseDir, info.Name())
if !strings.HasSuffix(item.Name(), ".gz") {
fileList = append(fileList, sshFileItem{Name: itemPath, Year: info.ModTime().Year()})
continue
}
itemFileName := strings.TrimSuffix(itemPath, ".gz")
if _, err := os.Stat(itemFileName); err != nil && os.IsNotExist(err) {
if err := handleGunzip(itemPath); err == nil {
fileList = append(fileList, sshFileItem{Name: itemFileName, Year: info.ModTime().Year()})
}
}
return nil
}); err != nil {
return nil, err
}
fileList = sortFileList(fileList)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes:

  1. The use of filepath.Walk has been replaced with os.ReadDir, which simplifies directory traversal without handling errors directly.

  2. Removed unused imports: path/filepath.

  3. Simplified the condition for processing files from directories that do not start with "secure" or "auth".

  4. Updated the error handling to return early when an error occurs, improving performance.

  5. Added type assertions _, since Info() returns os.FileInfo but is only used on a boolean expression.

  6. Simplified the logic for reading non-gzipped files and unconditionally appending them to the list.

  7. Moved some initialization and variable declarations closer to where they are used, reducing scope and improving readability.

  8. Used continue for skipping unnecessary operations rather than returning inside the loop.

Expand Down
1 change: 0 additions & 1 deletion agent/router/ro_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ func (s *LogRouter) InitRouter(Router *gin.RouterGroup) {
baseApi := v2.ApiGroupApp.BaseApi
{
operationRouter.GET("/system/files", baseApi.GetSystemFiles)
operationRouter.POST("/system", baseApi.GetSystemLogs)
operationRouter.POST("/tasks/search", baseApi.PageTasks)
operationRouter.GET("/tasks/executing/count", baseApi.CountExecutingTasks)
}
Expand Down
14 changes: 7 additions & 7 deletions agent/utils/cloud_storage/client/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"
"path"
"path/filepath"

"github.com/1Panel-dev/1Panel/agent/utils/files"
)
Expand Down Expand Up @@ -80,13 +79,14 @@ func (c localClient) ListObjects(prefix string) ([]string, error) {
if _, err := os.Stat(prefix); err != nil {
return files, nil
}
if err := filepath.Walk(prefix, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, info.Name())
list, err := os.ReadDir(prefix)
if err != nil {
return files, err
}
for i := 0; i < len(list); i++ {
if !list[i].IsDir() {
files = append(files, list[i].Name())
}
return nil
}); err != nil {
return nil, err
}

return files, nil
Expand Down
Loading
Loading