Skip to content

Commit eaefac4

Browse files
authored
fix: Fix the exception of refreshing the token of Ali Cloud Disk (#8288)
1 parent 578fa45 commit eaefac4

15 files changed

Lines changed: 96 additions & 201 deletions

File tree

agent/app/api/v2/logs.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package v2
22

33
import (
44
"github.com/1Panel-dev/1Panel/agent/app/api/v2/helper"
5-
"github.com/1Panel-dev/1Panel/agent/app/dto"
65
"github.com/gin-gonic/gin"
76
)
87

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

2221
helper.SuccessWithData(c, data)
2322
}
24-
25-
// @Tags Logs
26-
// @Summary Load system logs
27-
// @Success 200 {string} data
28-
// @Security ApiKeyAuth
29-
// @Security Timestamp
30-
// @Router /logs/system [post]
31-
func (b *BaseApi) GetSystemLogs(c *gin.Context) {
32-
var req dto.OperationWithName
33-
if err := helper.CheckBindAndValidate(&req, c); err != nil {
34-
return
35-
}
36-
37-
data, err := logService.LoadSystemLog(req.Name)
38-
if err != nil {
39-
helper.InternalServer(c, err)
40-
return
41-
}
42-
43-
helper.SuccessWithData(c, data)
44-
}

agent/app/service/backup.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (u *BackupService) Create(req dto.BackupOperate) error {
147147
}
148148
backup.Credential = string(itemCredential)
149149

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

250-
if newBackup.Type == constant.OneDrive || newBackup.Type == constant.GoogleDrive {
250+
if newBackup.Type == constant.OneDrive || newBackup.Type == constant.GoogleDrive || newBackup.Type == constant.ALIYUN {
251251
if err := loadRefreshTokenByCode(&newBackup); err != nil {
252252
return err
253253
}
@@ -501,16 +501,19 @@ func loadRefreshTokenByCode(backup *model.BackupAccount) error {
501501
if err != nil {
502502
return err
503503
}
504-
} else {
504+
}
505+
if backup.Type == constant.OneDrive {
505506
refreshToken, err = client.RefreshToken("authorization_code", "refreshToken", varMap)
506507
if err != nil {
507508
return err
508509
}
509510
}
510-
delete(varMap, "code")
511+
if backup.Type != constant.ALIYUN {
512+
delete(varMap, "code")
513+
varMap["refresh_token"] = refreshToken
514+
}
511515
varMap["refresh_status"] = constant.StatusSuccess
512516
varMap["refresh_time"] = time.Now().Format(constant.DateTimeLayout)
513-
varMap["refresh_token"] = refreshToken
514517
itemVars, err := json.Marshal(varMap)
515518
if err != nil {
516519
return fmt.Errorf("json marshal var map failed, err: %v", err)
@@ -535,10 +538,9 @@ func loadBackupNamesByID(accountIDs string, downloadID uint) ([]string, string,
535538
var accounts []string
536539
var downloadAccount string
537540
for _, item := range list {
538-
itemName := fmt.Sprintf("%s - %s", item.Type, item.Name)
539-
accounts = append(accounts, itemName)
541+
accounts = append(accounts, item.Name)
540542
if item.ID == downloadID {
541-
downloadAccount = itemName
543+
downloadAccount = item.Name
542544
}
543545
}
544546
return accounts, downloadAccount, nil

agent/app/service/backup_record.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,26 +176,20 @@ func (u *BackupRecordService) ListAppRecords(name, detailName, fileName string)
176176
}
177177

178178
func (u *BackupRecordService) ListFiles(req dto.OperateByID) []string {
179-
var datas []string
180179
backupItem, client, err := NewBackupClientWithID(req.ID)
181180
if err != nil {
182-
return datas
181+
return []string{}
183182
}
184183
prefix := "system_snapshot"
185-
if len(backupItem.BackupPath) != 0 {
184+
if len(backupItem.BackupPath) != 0 {
186185
prefix = path.Join(backupItem.BackupPath, prefix)
187186
}
188187
files, err := client.ListObjects(prefix)
189188
if err != nil {
190189
global.LOG.Debugf("load files failed, err: %v", err)
191-
return datas
190+
return []string{}
192191
}
193-
for _, file := range files {
194-
if len(file) != 0 {
195-
datas = append(datas, path.Base(file))
196-
}
197-
}
198-
return datas
192+
return files
199193
}
200194

201195
type backupSizeHelper struct {

agent/app/service/clam.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"os"
77
"os/exec"
88
"path"
9-
"path/filepath"
109
"sort"
1110
"strconv"
1211
"strings"
@@ -543,16 +542,15 @@ func StopAllCronJob(withCheck bool) bool {
543542
func loadFileByName(name string) []string {
544543
var logPaths []string
545544
pathItem := path.Join(global.Dir.DataDir, resultDir, name)
546-
_ = filepath.Walk(pathItem, func(path string, info os.FileInfo, err error) error {
547-
if err != nil {
548-
return nil
549-
}
550-
if info.IsDir() || info.Name() == name {
551-
return nil
545+
fileItems, err := os.ReadDir(pathItem)
546+
if err != nil {
547+
return logPaths
548+
}
549+
for _, item := range fileItems {
550+
if !item.IsDir() {
551+
logPaths = append(logPaths, item.Name())
552552
}
553-
logPaths = append(logPaths, info.Name())
554-
return nil
555-
})
553+
}
556554
return logPaths
557555
}
558556
func loadResultFromLog(pathItem string) dto.ClamLog {

agent/app/service/logs.go

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
package service
22

33
import (
4-
"fmt"
54
"os"
6-
"path"
75
"sort"
86
"strings"
97
"time"
108

11-
"github.com/1Panel-dev/1Panel/agent/buserr"
129
"github.com/1Panel-dev/1Panel/agent/global"
1310
)
1411

1512
type LogService struct{}
1613

1714
type ILogService interface {
1815
ListSystemLogFile() ([]string, error)
19-
LoadSystemLog(name string) (string, error)
2016
}
2117

2218
func NewILogService() ILogService {
@@ -31,29 +27,30 @@ func (u *LogService) ListSystemLogFile() ([]string, error) {
3127
}
3228
listMap := make(map[string]struct{})
3329
for _, item := range files {
34-
if !item.IsDir() && strings.HasPrefix(item.Name(), "1Panel") {
35-
if item.Name() == "1Panel.log" || item.Name() == "1Panel-Core.log" {
36-
itemName := time.Now().Format("2006-01-02")
37-
if _, ok := listMap[itemName]; ok {
38-
continue
39-
}
40-
listMap[itemName] = struct{}{}
41-
listFile = append(listFile, itemName)
42-
continue
43-
}
44-
itemFileName := strings.TrimPrefix(item.Name(), "1Panel-Core-")
45-
itemFileName = strings.TrimPrefix(itemFileName, "1Panel-")
46-
itemFileName = strings.TrimSuffix(itemFileName, ".gz")
47-
itemFileName = strings.TrimSuffix(itemFileName, ".log")
48-
if len(itemFileName) == 0 {
49-
continue
50-
}
51-
if _, ok := listMap[itemFileName]; ok {
30+
if item.IsDir() || !strings.HasPrefix(item.Name(), "1Panel") {
31+
continue
32+
}
33+
if item.Name() == "1Panel.log" || item.Name() == "1Panel-Core.log" {
34+
itemName := time.Now().Format("2006-01-02")
35+
if _, ok := listMap[itemName]; ok {
5236
continue
5337
}
54-
listMap[itemFileName] = struct{}{}
55-
listFile = append(listFile, itemFileName)
38+
listMap[itemName] = struct{}{}
39+
listFile = append(listFile, itemName)
40+
continue
41+
}
42+
itemFileName := strings.TrimPrefix(item.Name(), "1Panel-Core-")
43+
itemFileName = strings.TrimPrefix(itemFileName, "1Panel-")
44+
itemFileName = strings.TrimSuffix(itemFileName, ".gz")
45+
itemFileName = strings.TrimSuffix(itemFileName, ".log")
46+
if len(itemFileName) == 0 {
47+
continue
5648
}
49+
if _, ok := listMap[itemFileName]; ok {
50+
continue
51+
}
52+
listMap[itemFileName] = struct{}{}
53+
listFile = append(listFile, itemFileName)
5754
}
5855
if len(listFile) < 2 {
5956
return listFile, nil
@@ -64,26 +61,3 @@ func (u *LogService) ListSystemLogFile() ([]string, error) {
6461

6562
return listFile, nil
6663
}
67-
68-
func (u *LogService) LoadSystemLog(name string) (string, error) {
69-
if name == time.Now().Format("2006-01-02") {
70-
name = "1Panel.log"
71-
} else {
72-
name = "1Panel-" + name + ".log"
73-
}
74-
filePath := path.Join(global.Dir.LogDir, name)
75-
if _, err := os.Stat(filePath); err != nil {
76-
fileGzPath := path.Join(global.Dir.LogDir, name+".gz")
77-
if _, err := os.Stat(fileGzPath); err != nil {
78-
return "", buserr.New("ErrHttpReqNotFound")
79-
}
80-
if err := handleGunzip(fileGzPath); err != nil {
81-
return "", fmt.Errorf("handle ungzip file %s failed, err: %v", fileGzPath, err)
82-
}
83-
}
84-
content, err := os.ReadFile(filePath)
85-
if err != nil {
86-
return "", err
87-
}
88-
return string(content), nil
89-
}

agent/app/service/ssh.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"os"
66
"os/user"
77
"path"
8-
"path/filepath"
98
"sort"
109
"strings"
1110
"time"
@@ -288,25 +287,26 @@ func (u *SSHService) LoadLog(ctx *gin.Context, req dto.SearchSSHLog) (*dto.SSHLo
288287
var fileList []sshFileItem
289288
var data dto.SSHLog
290289
baseDir := "/var/log"
291-
if err := filepath.Walk(baseDir, func(pathItem string, info os.FileInfo, err error) error {
292-
if err != nil {
293-
return err
290+
fileItems, err := os.ReadDir(baseDir)
291+
if err != nil {
292+
return &data, err
293+
}
294+
for _, item := range fileItems {
295+
if item.IsDir() || (!strings.HasPrefix(item.Name(), "secure") && !strings.HasPrefix(item.Name(), "auth")) {
296+
continue
294297
}
295-
if !info.IsDir() && (strings.HasPrefix(info.Name(), "secure") || strings.HasPrefix(info.Name(), "auth")) {
296-
if !strings.HasSuffix(info.Name(), ".gz") {
297-
fileList = append(fileList, sshFileItem{Name: pathItem, Year: info.ModTime().Year()})
298-
return nil
299-
}
300-
itemFileName := strings.TrimSuffix(pathItem, ".gz")
301-
if _, err := os.Stat(itemFileName); err != nil && os.IsNotExist(err) {
302-
if err := handleGunzip(pathItem); err == nil {
303-
fileList = append(fileList, sshFileItem{Name: itemFileName, Year: info.ModTime().Year()})
304-
}
298+
info, _ := item.Info()
299+
itemPath := path.Join(baseDir, info.Name())
300+
if !strings.HasSuffix(item.Name(), ".gz") {
301+
fileList = append(fileList, sshFileItem{Name: itemPath, Year: info.ModTime().Year()})
302+
continue
303+
}
304+
itemFileName := strings.TrimSuffix(itemPath, ".gz")
305+
if _, err := os.Stat(itemFileName); err != nil && os.IsNotExist(err) {
306+
if err := handleGunzip(itemPath); err == nil {
307+
fileList = append(fileList, sshFileItem{Name: itemFileName, Year: info.ModTime().Year()})
305308
}
306309
}
307-
return nil
308-
}); err != nil {
309-
return nil, err
310310
}
311311
fileList = sortFileList(fileList)
312312

agent/router/ro_log.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ func (s *LogRouter) InitRouter(Router *gin.RouterGroup) {
1212
baseApi := v2.ApiGroupApp.BaseApi
1313
{
1414
operationRouter.GET("/system/files", baseApi.GetSystemFiles)
15-
operationRouter.POST("/system", baseApi.GetSystemLogs)
1615
operationRouter.POST("/tasks/search", baseApi.PageTasks)
1716
operationRouter.GET("/tasks/executing/count", baseApi.CountExecutingTasks)
1817
}

agent/utils/cloud_storage/client/local.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"os"
66
"path"
7-
"path/filepath"
87

98
"github.com/1Panel-dev/1Panel/agent/utils/files"
109
)
@@ -80,13 +79,14 @@ func (c localClient) ListObjects(prefix string) ([]string, error) {
8079
if _, err := os.Stat(prefix); err != nil {
8180
return files, nil
8281
}
83-
if err := filepath.Walk(prefix, func(path string, info os.FileInfo, err error) error {
84-
if !info.IsDir() {
85-
files = append(files, info.Name())
82+
list, err := os.ReadDir(prefix)
83+
if err != nil {
84+
return files, err
85+
}
86+
for i := 0; i < len(list); i++ {
87+
if !list[i].IsDir() {
88+
files = append(files, list[i].Name())
8689
}
87-
return nil
88-
}); err != nil {
89-
return nil, err
9090
}
9191

9292
return files, nil

0 commit comments

Comments
 (0)