fix: Fix the exception of refreshing the token of Ali Cloud Disk (#8288)

This commit is contained in:
ssongliu 2025-04-01 17:25:52 +08:00 committed by GitHub
parent 578fa45bb3
commit eaefac4be4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 98 additions and 203 deletions

View file

@ -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"
)
@ -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)
}

View file

@ -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
}
@ -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
}
@ -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)
@ -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

View file

@ -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 {

View file

@ -6,7 +6,6 @@ import (
"os"
"os/exec"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
@ -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
fileItems, err := os.ReadDir(pathItem)
if err != nil {
return logPaths
}
for _, item := range fileItems {
if !item.IsDir() {
logPaths = append(logPaths, item.Name())
}
if info.IsDir() || info.Name() == name {
return nil
}
logPaths = append(logPaths, info.Name())
return nil
})
}
return logPaths
}
func loadResultFromLog(pathItem string) dto.ClamLog {

View file

@ -1,14 +1,11 @@
package service
import (
"fmt"
"os"
"path"
"sort"
"strings"
"time"
"github.com/1Panel-dev/1Panel/agent/buserr"
"github.com/1Panel-dev/1Panel/agent/global"
)
@ -16,7 +13,6 @@ type LogService struct{}
type ILogService interface {
ListSystemLogFile() ([]string, error)
LoadSystemLog(name string) (string, error)
}
func NewILogService() ILogService {
@ -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 {
continue
}
listMap[itemFileName] = struct{}{}
listFile = append(listFile, itemFileName)
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[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
@ -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
}

View file

@ -5,7 +5,6 @@ import (
"os"
"os/user"
"path"
"path/filepath"
"sort"
"strings"
"time"
@ -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)

View file

@ -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)
}

View file

@ -4,7 +4,6 @@ import (
"fmt"
"os"
"path"
"path/filepath"
"github.com/1Panel-dev/1Panel/agent/utils/files"
)
@ -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

View file

@ -1,13 +1,6 @@
package service
import (
"os"
"path"
"path/filepath"
"sort"
"strings"
"time"
"github.com/1Panel-dev/1Panel/core/buserr"
"github.com/1Panel-dev/1Panel/core/utils/common"
geo2 "github.com/1Panel-dev/1Panel/core/utils/geo"
@ -26,7 +19,6 @@ type LogService struct{}
const logs = "https://resource.fit2cloud.com/installation-log.sh"
type ILogService interface {
ListSystemLogFile() ([]string, error)
CreateLoginLog(operation model.LoginLog) error
PageLoginLog(ctx *gin.Context, search dto.SearchLgLogWithPage) (int64, interface{}, error)
@ -44,39 +36,6 @@ func (u *LogService) CreateLoginLog(operation model.LoginLog) error {
return logRepo.CreateLoginLog(&operation)
}
func (u *LogService) ListSystemLogFile() ([]string, error) {
logDir := path.Join(global.CONF.Base.InstallDir, "1panel/log")
var files []string
if err := filepath.Walk(logDir, func(pathItem string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && strings.HasPrefix(info.Name(), "1Panel") {
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)
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
}
func (u *LogService) PageLoginLog(ctx *gin.Context, req dto.SearchLgLogWithPage) (int64, interface{}, error) {
options := []global.DBOption{
repo.WithOrderBy("created_at desc"),

View file

@ -13,9 +13,6 @@ export const getLoginLogs = (info: Log.SearchLgLog) => {
export const getSystemFiles = () => {
return http.get<Array<string>>(`/logs/system/files`);
};
export const getSystemLogs = (name: string) => {
return http.post<string>(`/logs/system`, { name: name });
};
export const cleanLogs = (param: Log.CleanLog) => {
return http.post(`/core/logs/clean`, param);

View file

@ -100,9 +100,7 @@
<div v-if="row.accountExpand || (!row.accountExpand && index < 3)">
<div v-if="row.expand || (!row.expand && index < 3)">
<span type="info">
<span>
{{ loadName(item) }}
</span>
{{ item === 'localhost' ? $t('setting.LOCAL') : item }}
<el-icon
v-if="item === row.downloadAccount"
size="12"
@ -354,11 +352,6 @@ const loadDetail = (row: any) => {
dialogRecordRef.value!.acceptParams(params);
};
const loadName = (from: any) => {
let items = from.split(' - ');
return i18n.global.t('setting.' + items[0]) + ' ' + items[1];
};
const buttons = [
{
label: i18n.global.t('commons.button.handle'),

View file

@ -420,11 +420,10 @@
@change="changeAccount"
>
<div v-for="item in backupOptions" :key="item.id">
<el-option
v-if="item.type !== $t('setting.LOCAL')"
:value="item.id"
:label="item.type + ' - ' + item.name"
/>
<el-option v-if="item.type !== $t('setting.LOCAL')" :value="item.id" :label="item.name">
{{ item.name }}
<el-tag class="tagClass" type="primary">{{ item.type }}</el-tag>
</el-option>
<el-option v-else :value="item.id" :label="item.type" />
</div>
</el-select>
@ -450,11 +449,10 @@
<el-form-item :label="$t('cronjob.default_download_path')" prop="downloadAccountID">
<el-select class="selectClass" v-model="dialogData.rowData!.downloadAccountID">
<div v-for="item in accountOptions" :key="item.id">
<el-option
v-if="item.type !== $t('setting.LOCAL')"
:value="item.id"
:label="item.type + ' - ' + item.name"
/>
<el-option v-if="item.type !== $t('setting.LOCAL')" :value="item.id" :label="item.name">
{{ item.name }}
<el-tag class="tagClass" type="primary">{{ item.type }}</el-tag>
</el-option>
<el-option v-else :value="item.id" :label="item.type" />
</div>
</el-select>

View file

@ -55,7 +55,7 @@
<el-table-column :label="$t('commons.table.type')" :min-width="80" prop="type">
<template #default="{ row }">
<el-tag>{{ $t('setting.' + row.type) }}</el-tag>
<el-tooltip v-if="row.type === 'OneDrive'">
<el-tooltip v-if="hasTokenRefresh(row)">
<template #content>
{{ $t('setting.clickToRefresh') }}
<br />
@ -177,6 +177,10 @@ const loadEndpoint = (row: any) => {
return '';
};
const hasTokenRefresh = (row: any) => {
return row.type === 'OneDrive' || row.type === 'ALIYUN' || row.type === 'GoogleDrive';
};
const onDelete = async (row: Backup.BackupInfo) => {
opRef.value.acceptParams({
title: i18n.global.t('commons.button.delete'),

View file

@ -23,11 +23,10 @@
<el-form-item :label="$t('setting.backupAccount')" prop="fromAccounts">
<el-select multiple @change="changeAccount" v-model="form.fromAccounts" clearable>
<div v-for="item in backupOptions" :key="item.id">
<el-option
v-if="item.type !== $t('setting.LOCAL')"
:value="item.id"
:label="item.type + ' - ' + item.name"
/>
<el-option v-if="item.type !== $t('setting.LOCAL')" :value="item.id" :label="item.name">
{{ item.name }}
<el-tag class="tagClass" type="primary">{{ item.type }}</el-tag>
</el-option>
<el-option v-else :value="item.id" :label="item.type" />
</div>
</el-select>
@ -35,11 +34,10 @@
<el-form-item :label="$t('cronjob.default_download_path')" prop="downloadAccountID">
<el-select v-model="form.downloadAccountID" clearable>
<div v-for="item in accountOptions" :key="item.id">
<el-option
v-if="item.type !== $t('setting.LOCAL')"
:value="item.id"
:label="item.type + ' - ' + item.name"
/>
<el-option v-if="item.type !== $t('setting.LOCAL')" :value="item.id" :label="item.name">
{{ item.name }}
<el-tag class="tagClass" type="primary">{{ item.type }}</el-tag>
</el-option>
<el-option v-else :value="item.id" :label="item.type" />
</div>
</el-select>
@ -558,4 +556,10 @@ defineExpose({
}
}
}
.tagClass {
float: right;
margin-right: 10px;
font-size: 12px;
margin-top: 5px;
}
</style>

View file

@ -46,7 +46,7 @@
<div v-if="row.expand || (!row.expand && index < 3)">
<span type="info">
<span>
{{ loadName(item) }}
{{ item === 'localhost' ? $t('setting.LOCAL') : item }}
</span>
<el-icon
v-if="item === row.downloadAccount"
@ -481,11 +481,6 @@ const loadSize = async (params: any) => {
});
};
const loadName = (from: any) => {
let items = from.split(' - ');
return i18n.global.t('setting.' + items[0]) + ' ' + items[1];
};
onMounted(() => {
search();
});